GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ButtonControl.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <QAbstractButton>
31 
32 #include "ButtonControl.h"
33 #include "ButtonGroup.h"
34 #include "Container.h"
35 #include "QtHandlesUtils.h"
36 
37 #include "graphics.h"
38 #include "interpreter.h"
39 
41 
43  octave::interpreter& interp,
44  const graphics_object& go,
45  QAbstractButton *btn)
46 : BaseControl (oct_qobj, interp, go, btn), m_blockCallback (false)
47 {
48  uicontrol::properties& up = properties<uicontrol> ();
49 
50  QString str = Utils::fromStdString (up.get_string_string ());
51  str.replace ("&", "&&");
52  btn->setText (str);
53  if (btn->isCheckable () || up.style_is ("togglebutton"))
54  {
55  btn->setCheckable (true);
56 
57  Matrix value = up.get_value ().matrix_value ();
58 
59  if (value.numel () > 0 && value(0) == up.get_max ())
60  btn->setChecked (true);
61  }
62 
63  connect (btn, &QAbstractButton::clicked, this, &ButtonControl::clicked);
64  connect (btn, &QAbstractButton::toggled, this, &ButtonControl::toggled);
65 }
66 
68 { }
69 
70 void
72 {
73  uicontrol::properties& up = properties<uicontrol> ();
74  QAbstractButton *btn = qWidget<QAbstractButton> ();
75 
76  switch (pId)
77  {
78  case uicontrol::properties::ID_STRING:
79  {
80  QString str = Utils::fromStdString (up.get_string_string ());
81  str.replace ("&", "&&");
82  btn->setText (str);
83  break;
84  }
85 
86  case uicontrol::properties::ID_VALUE:
87  m_blockCallback = true;
88  if (btn->isCheckable ())
89  {
90  Matrix value = up.get_value ().matrix_value ();
91 
92  if (value.numel () > 0)
93  {
94  double dValue = value(0);
95 
96  if (dValue != 0.0 && dValue != 1.0)
97  warning ("button value not within valid display range");
98  else if (dValue == up.get_min () && btn->isChecked ())
99  {
100  btn->setChecked (false);
101  if (up.style_is ("radiobutton") || up.style_is ("togglebutton"))
102  {
103  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
104 
105  Object *parent = Object::parentObject (m_interpreter, gh_mgr.get_object (up.get___myhandle__ ()));
106  ButtonGroup *btnGroup = dynamic_cast<ButtonGroup *>(parent);
107  if (btnGroup)
108  btnGroup->selectNothing ();
109  }
110  }
111  else if (dValue == up.get_max () && ! btn->isChecked ())
112  btn->setChecked (true);
113  }
114  }
115  m_blockCallback = false;
116  break;
117 
118  default:
119  BaseControl::update (pId);
120  break;
121  }
122 }
123 
124 void
126 {
127  QAbstractButton *btn = qWidget<QAbstractButton> ();
128 
129  if (! m_blockCallback && btn->isCheckable ())
130  {
131  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
132 
133  octave::autolock guard (gh_mgr.graphics_lock ());
134 
135  uicontrol::properties& up = properties<uicontrol> ();
136 
137  Matrix oldValue = up.get_value ().matrix_value ();
138  double newValue = (checked ? up.get_max () : up.get_min ());
139 
140  if (oldValue.numel () != 1 || (newValue != oldValue(0)))
141  emit gh_set_event (m_handle, "value", newValue, false);
142  emit gh_callback_event (m_handle, "callback");
143  }
144 }
145 
146 void
148 {
149  QAbstractButton *btn = qWidget<QAbstractButton> ();
150 
151  if (! btn->isCheckable ())
152  emit gh_callback_event (m_handle, "callback");
153 }
154 
OCTAVE_END_NAMESPACE(octave)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
void update(int pId)
Definition: BaseControl.cc:164
~ButtonControl(void)
bool m_blockCallback
Definition: ButtonControl.h:55
void clicked(void)
void toggled(bool checked)
void update(int pId)
void selectNothing(void)
Definition: ButtonGroup.cc:460
Definition: dMatrix.h:42
Definition: Object.h:47
void gh_callback_event(const graphics_handle &h, const std::string &name)
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:200
graphics_handle m_handle
Definition: Object.h:153
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
octave::interpreter & m_interpreter
Definition: Object.h:136
Base class for Octave interfaces that use Qt.
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void warning(const char *fmt,...)
Definition: error.cc:1054
QString fromStdString(const std::string &s)
T::properties & properties(graphics_object obj)