GNU Octave  4.4.1
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-2018 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QAbstractButton>
28 
29 #include "ButtonControl.h"
30 #include "ButtonGroup.h"
31 #include "Container.h"
32 #include "QtHandlesUtils.h"
33 
34 namespace QtHandles
35 {
36 
37  ButtonControl::ButtonControl (const graphics_object& go, QAbstractButton *btn)
38  : BaseControl (go, btn), m_blockCallback (false)
39  {
40  uicontrol::properties& up = properties<uicontrol> ();
41 
42  QString str = Utils::fromStdString (up.get_string_string ());
43  str.replace ("&", "&&");
44  btn->setText (str);
45  if (btn->isCheckable () || up.style_is ("togglebutton"))
46  {
47  btn->setCheckable (true);
48 
49  Matrix value = up.get_value ().matrix_value ();
50 
51  if (value.numel () > 0 && value(0) == up.get_max ())
52  btn->setChecked (true);
53  }
54 
55  connect (btn, SIGNAL (clicked (void)), SLOT (clicked (void)));
56  connect (btn, SIGNAL (toggled (bool)), SLOT (toggled (bool)));
57  }
58 
60  { }
61 
62  void
64  {
65  uicontrol::properties& up = properties<uicontrol> ();
66  QAbstractButton *btn = qWidget<QAbstractButton> ();
67 
68  switch (pId)
69  {
70  case uicontrol::properties::ID_STRING:
71  {
72  QString str = Utils::fromStdString (up.get_string_string ());
73  str.replace ("&", "&&");
74  btn->setText (str);
75  break;
76  }
77 
78  case uicontrol::properties::ID_VALUE:
79  m_blockCallback = true;
80  if (btn->isCheckable ())
81  {
82  Matrix value = up.get_value ().matrix_value ();
83 
84  if (value.numel () > 0)
85  {
86  double dValue = value(0);
87 
88  if (dValue != 0.0 && dValue != 1.0)
89  warning ("button value not within valid display range");
90  else if (dValue == up.get_min () && btn->isChecked ())
91  {
92  btn->setChecked (false);
93  if (up.style_is ("radiobutton") || up.style_is ("togglebutton"))
94  {
96  up.get___myhandle__ ()));
97  ButtonGroup *btnGroup = dynamic_cast<ButtonGroup *>(parent);
98  if (btnGroup)
99  btnGroup->selectNothing ();
100  }
101  }
102  else if (dValue == up.get_max () && ! btn->isChecked ())
103  btn->setChecked (true);
104  }
105  }
106  m_blockCallback = false;
107  break;
108 
109  default:
110  BaseControl::update (pId);
111  break;
112  }
113  }
114 
115  void
116  ButtonControl::toggled (bool checked)
117  {
118  QAbstractButton *btn = qWidget<QAbstractButton> ();
119 
120  if (! m_blockCallback && btn->isCheckable ())
121  {
123 
124  uicontrol::properties& up = properties<uicontrol> ();
125 
126  Matrix oldValue = up.get_value ().matrix_value ();
127  double newValue = (checked ? up.get_max () : up.get_min ());
128 
129  if (oldValue.numel () != 1 || (newValue != oldValue(0)))
130  gh_manager::post_set (m_handle, "value", newValue, false);
131  gh_manager::post_callback (m_handle, "callback");
132  }
133  }
134 
135  void
137  {
138  QAbstractButton *btn = qWidget<QAbstractButton> ();
139 
140  if (! btn->isCheckable ())
141  gh_manager::post_callback (m_handle, "callback");
142  }
143 
144 };
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.in.h:6214
QString fromStdString(const std::string &s)
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
std::string str
Definition: hash.cc:118
graphics_handle m_handle
Definition: Object.h:115
is false
Definition: cellfun.cc:400
Definition: dMatrix.h:36
void warning(const char *fmt,...)
Definition: error.cc:801
void update(int pId)
Definition: BaseControl.cc:124
static graphics_object get_object(double val)
Definition: graphics.in.h:6098
void toggled(bool checked)
ButtonControl(const graphics_object &go, QAbstractButton *btn)
nd group nd example For each display the value
Definition: sysdep.cc:866
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.in.h:6228