GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
SliderControl.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 <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 nullptr;
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  bool vertical_slider = ( bb(2) < bb(3) );
63  slider->setOrientation (vertical_slider ? Qt::Vertical : Qt::Horizontal);
64  if (vertical_slider)
65  slider->setInvertedAppearance (true); // Matlab compatibility
66  Matrix steps = up.get_sliderstep ().matrix_value ();
67  slider->setMinimum (0);
68  slider->setMaximum (RANGE_INT_MAX);
69  slider->setSingleStep (octave::math::round (steps(0) * RANGE_INT_MAX));
70  slider->setPageStep (octave::math::round (steps(1) * RANGE_INT_MAX));
71  Matrix value = up.get_value ().matrix_value ();
72  if (value.numel () > 0)
73  {
74  double dmin = up.get_min (), dmax = up.get_max ();
75 
76  slider->setValue (octave::math::round (((value(0) - dmin) / (dmax - dmin))
77  * RANGE_INT_MAX));
78  }
79 
80  connect (slider, SIGNAL (valueChanged (int)), SLOT (valueChanged (int)));
81  }
82 
84  { }
85 
86  void
88  {
89  uicontrol::properties& up = properties<uicontrol> ();
90  QScrollBar *slider = qWidget<QScrollBar> ();
91 
92  switch (pId)
93  {
94  case uicontrol::properties::ID_SLIDERSTEP:
95  {
96  Matrix steps = up.get_sliderstep ().matrix_value ();
97 
98  slider->setSingleStep (octave::math::round (steps(0) * RANGE_INT_MAX));
99  slider->setPageStep (octave::math::round (steps(1) * RANGE_INT_MAX));
100  }
101  break;
102 
103  case uicontrol::properties::ID_VALUE:
104  {
105  Matrix value = up.get_value ().matrix_value ();
106  double dmax = up.get_max (), dmin = up.get_min ();
107 
108  if (value.numel () > 0)
109  {
110  int ival = octave::math::round (((value(0) - dmin) / (dmax - dmin))
111  * RANGE_INT_MAX);
112 
113  m_blockUpdates = true;
114  slider->setValue (ival);
115  m_blockUpdates = false;
116  }
117  }
118  break;
119 
120  default:
121  BaseControl::update (pId);
122  break;
123  }
124  }
125 
126  void
128  {
129  if (! m_blockUpdates)
130  {
132  graphics_object go = object ();
133 
134  if (go.valid_object ())
135  {
136  uicontrol::properties& up = Utils::properties<uicontrol> (go);
137 
138  Matrix value = up.get_value ().matrix_value ();
139  double dmin = up.get_min (), dmax = up.get_max ();
140 
141  int ival_tmp = (value.numel () > 0 ?
142  octave::math::round (((value(0) - dmin) / (dmax - dmin))
143  * RANGE_INT_MAX) :
144  0);
145 
146  if (ival != ival_tmp || value.numel () > 0)
147  {
148  double dval = dmin + (ival * (dmax - dmin) / RANGE_INT_MAX);
149 
150  gh_manager::post_set (m_handle, "value", octave_value (dval));
151  gh_manager::post_callback (m_handle, "callback");
152  }
153  }
154  }
155  }
156 
157 }
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.in.h:6214
SliderControl(const graphics_object &go, QAbstractSlider *slider)
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
virtual Container * innerContainer(void)=0
bool valid_object(void) const
Definition: graphics.in.h:2806
graphics_handle m_handle
Definition: Object.h:115
is false
Definition: cellfun.cc:400
Matrix get_boundingbox(bool internal=false, const Matrix &parent_pix_size=Matrix()) const
Definition: graphics.cc:9527
Definition: dMatrix.h:36
#define RANGE_INT_MAX
graphics_object object(void) const
Definition: Object.cc:72
void valueChanged(int ival)
void update(int pId)
Definition: BaseControl.cc:124
double round(double x)
Definition: lo-mappers.h:145
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