GNU Octave  4.0.0
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-2015 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 #ifdef 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 (xround (steps(0) * RANGE_INT_MAX));
67  slider->setPageStep (xround (steps(1) * RANGE_INT_MAX));
68  Matrix value = up.get_value ().matrix_value ();
69  if (value.numel () > 0)
70  {
71  double dmin = up.get_min (), dmax = up.get_max ();
72 
73  slider->setValue (xround (((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 
84 void
86 {
87  uicontrol::properties& up = properties<uicontrol> ();
88  QScrollBar* slider = qWidget<QScrollBar> ();
89 
90  switch (pId)
91  {
93  {
94  Matrix steps = up.get_sliderstep ().matrix_value ();
95 
96  slider->setSingleStep (xround (steps(0) * RANGE_INT_MAX));
97  slider->setPageStep (xround (steps(1) * RANGE_INT_MAX));
98  }
99  break;
100 
102  {
103  Matrix value = up.get_value ().matrix_value ();
104  double dmax = up.get_max (), dmin = up.get_min ();
105 
106  if (value.numel () > 0)
107  {
108  int ival = xround (((value(0) - dmin) / (dmax - dmin))
109  * RANGE_INT_MAX);
110 
111  m_blockUpdates = true;
112  slider->setValue (ival);
113  m_blockUpdates = false;
114  }
115  }
116  break;
117 
118  default:
119  BaseControl::update (pId);
120  break;
121  }
122 }
123 
124 void
126 {
127  if (! m_blockUpdates)
128  {
130  graphics_object go = object ();
131 
132  if (go.valid_object ())
133  {
134  uicontrol::properties& up = Utils::properties<uicontrol> (go);
135 
136  Matrix value = up.get_value ().matrix_value ();
137  double dmin = up.get_min (), dmax = up.get_max ();
138 
139  int ival_tmp = (value.numel () > 0 ?
140  xround (((value(0) - dmin) / (dmax - dmin))
141  * RANGE_INT_MAX) :
142  0);
143 
144  if (ival != ival_tmp || value.numel () > 0)
145  {
146  double dval = dmin + (ival * (dmax - dmin) / RANGE_INT_MAX);
147 
148  gh_manager::post_set (m_handle, "value", octave_value (dval));
149  gh_manager::post_callback (m_handle, "callback");
150  }
151  }
152  }
153 }
154 
155 }; // namespace QtHandles
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:13329
double xround(double x)
Definition: lo-mappers.cc:63
Matrix get_boundingbox(bool internal=false, const Matrix &parent_pix_size=Matrix()) const
Definition: graphics.cc:8961
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
SliderControl(const graphics_object &go, QAbstractSlider *slider)
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:170
virtual Container * innerContainer(void)=0
graphics_handle m_handle
Definition: Object.h:100
octave_value get_sliderstep(void) const
Definition: graphics.h:11743
Definition: dMatrix.h:35
#define RANGE_INT_MAX
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:773
void valueChanged(int ival)
double get_min(void) const
Definition: graphics.h:11739
graphics_object object(void) const
Definition: Object.cc:73
bool valid_object(void) const
Definition: graphics.h:3395
void update(int pId)
Definition: BaseControl.cc:106
octave_value get_value(void) const
Definition: graphics.h:11757
double get_max(void) const
Definition: graphics.h:11737
static void post_set(const graphics_handle &h, const std::string &name, const octave_value &value, bool notify_toolkit=true)
Definition: graphics.h:13343