GNU Octave  6.2.0
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-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 <QScrollBar>
31 
32 #include "Container.h"
33 #include "SliderControl.h"
34 #include "QtHandlesUtils.h"
35 
36 #include "octave-qobject.h"
37 
38 #include "graphics.h"
39 #include "interpreter.h"
40 
41 #define RANGE_INT_MAX 1000000
42 
43 namespace QtHandles
44 {
45 
46  SliderControl*
48  octave::interpreter& interp,
49  const graphics_object& go)
50  {
51  Object *parent = parentObject (interp, go);
52 
53  if (parent)
54  {
55  Container *container = parent->innerContainer ();
56 
57  if (container)
58  return new SliderControl (oct_qobj, interp, go,
59  new QScrollBar (container));
60  }
61 
62  return nullptr;
63  }
64 
66  octave::interpreter& interp,
67  const graphics_object& go,
68  QAbstractSlider *slider)
69  : BaseControl (oct_qobj, interp, go, slider), m_blockUpdates (false)
70  {
71  uicontrol::properties& up = properties<uicontrol> ();
72 
73  slider->setTracking (false);
74  Matrix bb = up.get_boundingbox ();
75  bool vertical_slider = ( bb(2) < bb(3) );
76  slider->setOrientation (vertical_slider ? Qt::Vertical : Qt::Horizontal);
77  if (vertical_slider)
78  slider->setInvertedAppearance (true); // Matlab compatibility
79  Matrix steps = up.get_sliderstep ().matrix_value ();
80  slider->setMinimum (0);
81  slider->setMaximum (RANGE_INT_MAX);
82  slider->setSingleStep (octave::math::round (steps(0) * RANGE_INT_MAX));
83  slider->setPageStep (octave::math::round (steps(1) * RANGE_INT_MAX));
84  Matrix value = up.get_value ().matrix_value ();
85  if (value.numel () > 0)
86  {
87  double dmin = up.get_min (), dmax = up.get_max ();
88 
89  slider->setValue (octave::math::round (((value(0) - dmin) / (dmax - dmin))
90  * RANGE_INT_MAX));
91  }
92 
93  connect (slider, SIGNAL (valueChanged (int)), SLOT (valueChanged (int)));
94  }
95 
97  { }
98 
99  void
101  {
102  uicontrol::properties& up = properties<uicontrol> ();
103  QScrollBar *slider = qWidget<QScrollBar> ();
104 
105  switch (pId)
106  {
107  case uicontrol::properties::ID_SLIDERSTEP:
108  {
109  Matrix steps = up.get_sliderstep ().matrix_value ();
110 
111  slider->setSingleStep (octave::math::round (steps(0) * RANGE_INT_MAX));
112  slider->setPageStep (octave::math::round (steps(1) * RANGE_INT_MAX));
113  }
114  break;
115 
116  case uicontrol::properties::ID_VALUE:
117  {
118  Matrix value = up.get_value ().matrix_value ();
119  double dmax = up.get_max (), dmin = up.get_min ();
120 
121  if (value.numel () > 0)
122  {
123  int ival = octave::math::round (((value(0) - dmin) / (dmax - dmin))
124  * RANGE_INT_MAX);
125 
126  m_blockUpdates = true;
127  slider->setValue (ival);
128  m_blockUpdates = false;
129  }
130  }
131  break;
132 
133  default:
134  BaseControl::update (pId);
135  break;
136  }
137  }
138 
139  void
141  {
142  if (! m_blockUpdates)
143  {
145 
146  octave::autolock guard (gh_mgr.graphics_lock ());
147 
148  graphics_object go = object ();
149 
150  if (go.valid_object ())
151  {
152  uicontrol::properties& up = Utils::properties<uicontrol> (go);
153 
154  Matrix value = up.get_value ().matrix_value ();
155  double dmin = up.get_min (), dmax = up.get_max ();
156 
157  int ival_tmp = (value.numel () > 0 ?
158  octave::math::round (((value(0) - dmin) / (dmax - dmin))
159  * RANGE_INT_MAX) :
160  0);
161 
162  if (ival != ival_tmp || value.numel () > 0)
163  {
164  double dval = dmin + (ival * (dmax - dmin) / RANGE_INT_MAX);
165 
166  emit gh_set_event (m_handle, "value", octave_value (dval));
167  emit gh_callback_event (m_handle, "callback");
168  }
169  }
170  }
171  }
172 
173 }
#define RANGE_INT_MAX
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
graphics_object object(void) const
Definition: Object.cc:83
virtual Container * innerContainer(void)=0
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)
static SliderControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
void valueChanged(int ival)
SliderControl(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QAbstractSlider *slider)
octave::mutex graphics_lock(void)
Definition: graphics.in.h:6393
bool valid_object(void) const
Definition: graphics.in.h:2847
Base class for Octave interfaces that use Qt.
gh_manager & get_gh_manager(void)
Definition: interpreter.h:295
Matrix get_boundingbox(bool internal=false, const Matrix &parent_pix_size=Matrix()) const
Definition: graphics.cc:10933
double round(double x)
Definition: lo-mappers.h:136