GNU Octave  4.2.1
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
SliderControl.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2017 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 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QScrollBar>
28 
29 #include "Container.h"
30 #include "SliderControl.h"
31 #include "QtHandlesUtils.h"
32 
33 #define RANGE_INT_MAX 1000000
34 
35 namespace QtHandles
36 {
37 
38  SliderControl*
40  {
41  Object* parent = Object::parentObject (go);
42 
43  if (parent)
44  {
45  Container* container = parent->innerContainer ();
46 
47  if (container)
48  return new SliderControl (go, new QScrollBar (container));
49  }
50 
51  return 0;
52  }
53 
55  QAbstractSlider* slider)
56  : BaseControl (go, slider), m_blockUpdates (false)
57  {
58  uicontrol::properties& up = properties<uicontrol> ();
59 
60  slider->setTracking (false);
61  Matrix bb = up.get_boundingbox ();
62  slider->setOrientation (bb(2) > bb(3) ? Qt::Horizontal : Qt::Vertical);
63  Matrix steps = up.get_sliderstep ().matrix_value ();
64  slider->setMinimum (0);
65  slider->setMaximum (RANGE_INT_MAX);
66  slider->setSingleStep (octave::math::round (steps(0) * RANGE_INT_MAX));
67  slider->setPageStep (octave::math::round (steps(1) * RANGE_INT_MAX));
69  if (value.numel () > 0)
70  {
71  double dmin = up.get_min (), dmax = up.get_max ();
72 
73  slider->setValue (octave::math::round (((value(0) - dmin) / (dmax - dmin))
74  * RANGE_INT_MAX));
75  }
76 
77  connect (slider, SIGNAL (valueChanged (int)), SLOT (valueChanged (int)));
78  }
79 
81  { }
82 
83  void
85  {
86  uicontrol::properties& up = properties<uicontrol> ();
87  QScrollBar* slider = qWidget<QScrollBar> ();
88 
89  switch (pId)
90  {
92  {
93  Matrix steps = up.get_sliderstep ().matrix_value ();
94 
95  slider->setSingleStep (octave::math::round (steps(0) * RANGE_INT_MAX));
96  slider->setPageStep (octave::math::round (steps(1) * RANGE_INT_MAX));
97  }
98  break;
99 
101  {
102  Matrix value = up.get_value ().matrix_value ();
103  double dmax = up.get_max (), dmin = up.get_min ();
104 
105  if (value.numel () > 0)
106  {
107  int ival = octave::math::round (((value(0) - dmin) / (dmax - dmin))
108  * RANGE_INT_MAX);
109 
110  m_blockUpdates = true;
111  slider->setValue (ival);
112  m_blockUpdates = false;
113  }
114  }
115  break;
116 
117  default:
118  BaseControl::update (pId);
119  break;
120  }
121  }
122 
123  void
125  {
126  if (! m_blockUpdates)
127  {
129  graphics_object go = object ();
130 
131  if (go.valid_object ())
132  {
133  uicontrol::properties& up = Utils::properties<uicontrol> (go);
134 
135  Matrix value = up.get_value ().matrix_value ();
136  double dmin = up.get_min (), dmax = up.get_max ();
137 
138  int ival_tmp = (value.numel () > 0 ?
139  octave::math::round (((value(0) - dmin) / (dmax - dmin))
140  * RANGE_INT_MAX) :
141  0);
142 
143  if (ival != ival_tmp || value.numel () > 0)
144  {
145  double dval = dmin + (ival * (dmax - dmin) / RANGE_INT_MAX);
146 
147  gh_manager::post_set (m_handle, "value", octave_value (dval));
148  gh_manager::post_callback (m_handle, "callback");
149  }
150  }
151  }
152  }
153 
154 }
static SliderControl * create(const graphics_object &go)
static void post_callback(const graphics_handle &h, const std::string &name, const octave_value &data=Matrix())
Definition: graphics.h:13910
Matrix get_boundingbox(bool internal=false, const Matrix &parent_pix_size=Matrix()) const
Definition: graphics.cc:8831
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
SliderControl(const graphics_object &go, QAbstractSlider *slider)
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
virtual Container * innerContainer(void)=0
double round(double x)
Definition: lo-mappers.cc:333
graphics_handle m_handle
Definition: Object.h:100
is false
Definition: cellfun.cc:398
octave_value get_sliderstep(void) const
Definition: graphics.h:11975
Definition: dMatrix.h:37
#define RANGE_INT_MAX
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
void valueChanged(int ival)
double get_min(void) const
Definition: graphics.h:11971
graphics_object object(void) const
Definition: Object.cc:72
bool valid_object(void) const
Definition: graphics.h:3306
void update(int pId)
Definition: BaseControl.cc:124
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol zero divided by nd tex zero divided by nd ifnottex and any operation involving another NaN value(5+NaN).Note that NaN always compares not equal to NaN(NaN!
octave_value get_value(void) const
Definition: graphics.h:11989
double get_max(void) const
Definition: graphics.h:11969
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.h:13924