GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ButtonControl.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <QAbstractButton>
28 
29 #include "ButtonControl.h"
30 #include "Container.h"
31 #include "QtHandlesUtils.h"
32 
33 namespace QtHandles
34 {
35 
36 ButtonControl::ButtonControl (const graphics_object& go, QAbstractButton* btn)
37  : BaseControl (go, btn), m_blockCallback (false)
38 {
39  uicontrol::properties& up = properties<uicontrol> ();
40 
41  btn->setText (Utils::fromStdString (up.get_string_string ()));
42  if (btn->isCheckable () || up.style_is ("togglebutton"))
43  {
44  btn->setCheckable (true);
45 
46  Matrix value = up.get_value ().matrix_value ();
47 
48  if (value.numel () > 0 && value(0) == up.get_max ())
49  btn->setChecked (true);
50  }
51 
52  connect (btn, SIGNAL (clicked (void)), SLOT (clicked (void)));
53  connect (btn, SIGNAL (toggled (bool)), SLOT (toggled (bool)));
54 }
55 
57 {
58 }
59 
60 void
62 {
63  uicontrol::properties& up = properties<uicontrol> ();
64  QAbstractButton* btn = qWidget<QAbstractButton> ();
65 
66  switch (pId)
67  {
69  btn->setText (Utils::fromStdString (up.get_string_string ()));
70  break;
71 
73  m_blockCallback = true;
74  if (btn->isCheckable ())
75  {
76  Matrix value = up.get_value ().matrix_value ();
77 
78  if (value.numel () > 0)
79  {
80  double dValue = value(0);
81 
82  if (dValue != 0.0 && dValue != 1.0)
83  warning ("button value not within valid display range");
84  else if (dValue == up.get_min () && btn->isChecked ())
85  btn->setChecked (false);
86  else if (dValue == up.get_max () && ! btn->isChecked ())
87  btn->setChecked (true);
88  }
89  }
90  m_blockCallback = false;
91  break;
92 
93  default:
94  BaseControl::update (pId);
95  break;
96  }
97 }
98 
99 void
101 {
102  QAbstractButton* btn = qWidget<QAbstractButton> ();
103 
104  if (! m_blockCallback && btn->isCheckable ())
105  {
107 
108  uicontrol::properties& up = properties<uicontrol> ();
109 
110  Matrix oldValue = up.get_value ().matrix_value ();
111  double newValue = (checked ? up.get_max () : up.get_min ());
112 
113  if (oldValue.numel() != 1
114  || (newValue != oldValue(0)))
115  gh_manager::post_set (m_handle, "value", newValue, false);
116  gh_manager::post_callback (m_handle, "callback");
117  }
118 }
119 
120 void
122 {
123  QAbstractButton* btn = qWidget<QAbstractButton> ();
124 
125  if (! btn->isCheckable ())
126  gh_manager::post_callback (m_handle, "callback");
127 }
128 
129 };
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.h:13329
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
QString fromStdString(const std::string &s)
graphics_handle m_handle
Definition: Object.h:100
Definition: dMatrix.h:35
std::string get_string_string(void) const
Definition: graphics.h:11745
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:773
double get_min(void) const
Definition: graphics.h:11739
void warning(const char *fmt,...)
Definition: error.cc:681
void update(int pId)
Definition: BaseControl.cc:106
octave_value get_value(void) const
Definition: graphics.h:11757
void toggled(bool checked)
double get_max(void) const
Definition: graphics.h:11737
ButtonControl(const graphics_object &go, QAbstractButton *btn)
bool style_is(const std::string &v) const
Definition: graphics.h:11749
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.h:13343