GNU Octave  6.2.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-2021 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 
40 namespace QtHandles
41 {
42 
44  octave::interpreter& interp,
45  const graphics_object& go,
46  QAbstractButton *btn)
47  : BaseControl (oct_qobj, interp, go, btn), m_blockCallback (false)
48  {
49  uicontrol::properties& up = properties<uicontrol> ();
50 
51  QString str = Utils::fromStdString (up.get_string_string ());
52  str.replace ("&", "&&");
53  btn->setText (str);
54  if (btn->isCheckable () || up.style_is ("togglebutton"))
55  {
56  btn->setCheckable (true);
57 
58  Matrix value = up.get_value ().matrix_value ();
59 
60  if (value.numel () > 0 && value(0) == up.get_max ())
61  btn->setChecked (true);
62  }
63 
64  connect (btn, SIGNAL (clicked (void)), SLOT (clicked (void)));
65  connect (btn, SIGNAL (toggled (bool)), SLOT (toggled (bool)));
66  }
67 
69  { }
70 
71  void
73  {
74  uicontrol::properties& up = properties<uicontrol> ();
75  QAbstractButton *btn = qWidget<QAbstractButton> ();
76 
77  switch (pId)
78  {
79  case uicontrol::properties::ID_STRING:
80  {
81  QString str = Utils::fromStdString (up.get_string_string ());
82  str.replace ("&", "&&");
83  btn->setText (str);
84  break;
85  }
86 
87  case uicontrol::properties::ID_VALUE:
88  m_blockCallback = true;
89  if (btn->isCheckable ())
90  {
91  Matrix value = up.get_value ().matrix_value ();
92 
93  if (value.numel () > 0)
94  {
95  double dValue = value(0);
96 
97  if (dValue != 0.0 && dValue != 1.0)
98  warning ("button value not within valid display range");
99  else if (dValue == up.get_min () && btn->isChecked ())
100  {
101  btn->setChecked (false);
102  if (up.style_is ("radiobutton") || up.style_is ("togglebutton"))
103  {
105 
106  Object *parent = Object::parentObject (m_interpreter, gh_mgr.get_object (up.get___myhandle__ ()));
107  ButtonGroup *btnGroup = dynamic_cast<ButtonGroup *>(parent);
108  if (btnGroup)
109  btnGroup->selectNothing ();
110  }
111  }
112  else if (dValue == up.get_max () && ! btn->isChecked ())
113  btn->setChecked (true);
114  }
115  }
116  m_blockCallback = false;
117  break;
118 
119  default:
120  BaseControl::update (pId);
121  break;
122  }
123  }
124 
125  void
126  ButtonControl::toggled (bool checked)
127  {
128  QAbstractButton *btn = qWidget<QAbstractButton> ();
129 
130  if (! m_blockCallback && btn->isCheckable ())
131  {
133 
134  octave::autolock guard (gh_mgr.graphics_lock ());
135 
136  uicontrol::properties& up = properties<uicontrol> ();
137 
138  Matrix oldValue = up.get_value ().matrix_value ();
139  double newValue = (checked ? up.get_max () : up.get_min ());
140 
141  if (oldValue.numel () != 1 || (newValue != oldValue(0)))
142  emit gh_set_event (m_handle, "value", newValue, false);
143  emit gh_callback_event (m_handle, "callback");
144  }
145  }
146 
147  void
149  {
150  QAbstractButton *btn = qWidget<QAbstractButton> ();
151 
152  if (! btn->isCheckable ())
153  emit gh_callback_event (m_handle, "callback");
154  }
155 
156 };
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:377
Definition: dMatrix.h:42
void update(int pId)
Definition: BaseControl.cc:142
ButtonControl(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QAbstractButton *btn)
void toggled(bool checked)
octave::interpreter & m_interpreter
Definition: Object.h:140
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:201
void gh_callback_event(const graphics_handle &h, const std::string &name)
graphics_handle m_handle
Definition: Object.h:157
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
graphics_object get_object(double val) const
Definition: graphics.in.h:6260
octave::mutex graphics_lock(void)
Definition: graphics.in.h:6393
Base class for Octave interfaces that use Qt.
gh_manager & get_gh_manager(void)
Definition: interpreter.h:295
void warning(const char *fmt,...)
Definition: error.cc:1050
QString fromStdString(const std::string &s)