GNU Octave  8.1.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-2023 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 
44 
46 SliderControl::create (octave::base_qobject& oct_qobj,
47  octave::interpreter& interp,
48  const graphics_object& go)
49 {
50  Object *parent = parentObject (interp, go);
51 
52  if (parent)
53  {
54  Container *container = parent->innerContainer ();
55 
56  if (container)
57  return new SliderControl (oct_qobj, interp, go,
58  new QScrollBar (container));
59  }
60 
61  return nullptr;
62 }
63 
64 SliderControl::SliderControl (octave::base_qobject& oct_qobj,
65  octave::interpreter& interp,
66  const graphics_object& go,
67  QAbstractSlider *slider)
68  : BaseControl (oct_qobj, interp, go, slider), m_blockUpdates (false)
69 {
70  uicontrol::properties& up = properties<uicontrol> ();
71 
72  slider->setTracking (false);
73  Matrix bb = up.get_boundingbox ();
74  bool vertical_slider = ( bb(2) < bb(3) );
75  slider->setOrientation (vertical_slider ? Qt::Vertical : Qt::Horizontal);
76  if (vertical_slider)
77  slider->setInvertedAppearance (true); // Matlab compatibility
78  Matrix steps = up.get_sliderstep ().matrix_value ();
79  slider->setMinimum (0);
80  slider->setMaximum (RANGE_INT_MAX);
81  slider->setSingleStep (octave::math::round (steps(0) * RANGE_INT_MAX));
82  slider->setPageStep (octave::math::round (steps(1) * RANGE_INT_MAX));
83  Matrix value = up.get_value ().matrix_value ();
84  if (value.numel () > 0)
85  {
86  double dmin = up.get_min (), dmax = up.get_max ();
87 
88  slider->setValue (octave::math::round (((value(0) - dmin) / (dmax - dmin))
89  * RANGE_INT_MAX));
90  }
91 
92  connect (slider, &QAbstractSlider::valueChanged,
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  {
144  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
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 
OCTAVE_END_NAMESPACE(octave)
#define RANGE_INT_MAX
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
void update(int pId)
Definition: BaseControl.cc:164
Definition: dMatrix.h:42
Definition: Object.h:47
void gh_callback_event(const graphics_handle &h, const std::string &name)
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:200
graphics_handle m_handle
Definition: Object.h:153
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
graphics_object object(void) const
Definition: Object.cc:82
virtual Container * innerContainer(void)=0
octave::interpreter & m_interpreter
Definition: Object.h:136
void update(int pId)
~SliderControl(void)
void valueChanged(int ival)
SliderControl(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QAbstractSlider *slider)
static SliderControl * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
double round(double x)
Definition: lo-mappers.h:136
T::properties & properties(graphics_object obj)