GNU Octave 7.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-2022 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
43namespace octave
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, &QAbstractSlider::valueChanged,
95 }
96
98 { }
99
100 void
102 {
103 uicontrol::properties& up = properties<uicontrol> ();
104 QScrollBar *slider = qWidget<QScrollBar> ();
105
106 switch (pId)
107 {
108 case uicontrol::properties::ID_SLIDERSTEP:
109 {
110 Matrix steps = up.get_sliderstep ().matrix_value ();
111
112 slider->setSingleStep (octave::math::round (steps(0) * RANGE_INT_MAX));
113 slider->setPageStep (octave::math::round (steps(1) * RANGE_INT_MAX));
114 }
115 break;
116
117 case uicontrol::properties::ID_VALUE:
118 {
119 Matrix value = up.get_value ().matrix_value ();
120 double dmax = up.get_max (), dmin = up.get_min ();
121
122 if (value.numel () > 0)
123 {
124 int ival = octave::math::round (((value(0) - dmin) / (dmax - dmin))
125 * RANGE_INT_MAX);
126
127 m_blockUpdates = true;
128 slider->setValue (ival);
129 m_blockUpdates = false;
130 }
131 }
132 break;
133
134 default:
136 break;
137 }
138 }
139
140 void
142 {
143 if (! m_blockUpdates)
144 {
145 gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
146
147 octave::autolock guard (gh_mgr.graphics_lock ());
148
149 graphics_object go = object ();
150
151 if (go.valid_object ())
152 {
153 uicontrol::properties& up = Utils::properties<uicontrol> (go);
154
155 Matrix value = up.get_value ().matrix_value ();
156 double dmin = up.get_min (), dmax = up.get_max ();
157
158 int ival_tmp = (value.numel () > 0 ?
159 octave::math::round (((value(0) - dmin) / (dmax - dmin))
160 * RANGE_INT_MAX) :
161 0);
162
163 if (ival != ival_tmp || value.numel () > 0)
164 {
165 double dval = dmin + (ival * (dmax - dmin) / RANGE_INT_MAX);
166
167 emit gh_set_event (m_handle, "value", octave_value (dval));
168 emit gh_callback_event (m_handle, "callback");
169 }
170 }
171 }
172 }
173
174}
#define RANGE_INT_MAX
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
Definition: dMatrix.h:42
void update(int pId)
Definition: BaseControl.cc:165
virtual Container * innerContainer(void)=0
void gh_callback_event(const graphics_handle &h, const std::string &name)
octave::interpreter & m_interpreter
Definition: Object.h:140
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
graphics_handle m_handle
Definition: Object.h:157
graphics_object object(void) const
Definition: Object.cc:83
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:201
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)
void valueChanged(int ival)
Base class for Octave interfaces that use Qt.
T::properties & properties(graphics_object obj)
double round(double x)
Definition: lo-mappers.h:136