fw4spl
SSlider.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2015-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "guiQt/editor/SSlider.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 #include <fwCom/Slots.hxx>
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwGuiQt/container/QtContainer.hpp>
15 
16 #include <fwServices/macros.hpp>
17 
18 #include <QHBoxLayout>
19 #include <QIntValidator>
20 #include <QString>
21 #include <QTime>
22 
23 #include <chrono>
24 
25 namespace guiQt
26 {
27 namespace editor
28 {
29 
30 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::guiQt::editor::SSlider );
31 
32 const ::fwCom::Signals::SignalKeyType SSlider::s_VALUE_CHANGED_SIG = "valueChanged";
33 
34 const ::fwCom::Slots::SlotKeyType SSlider::s_SET_VALUE_SLIDER_SLOT = "setValue";
35 const ::fwCom::Slots::SlotKeyType SSlider::s_SET_MIN_VALUE_SLIDER_SLOT = "setMinValue";
36 const ::fwCom::Slots::SlotKeyType SSlider::s_SET_MAX_VALUE_SLIDER_SLOT = "setMaxValue";
37 
38 //------------------------------------------------------------------------------
39 
40 SSlider::SSlider() noexcept :
41  m_value(0),
42  m_minValue(0),
43  m_maxValue(100),
44  m_defaultValue(0),
45  m_text(""),
46  m_isUpdatedOnRelease(false),
47  m_hasResetButton(false),
48  m_hasEditBox(false),
49  m_sliderPressed(false)
50 {
51  newSlot(s_SET_VALUE_SLIDER_SLOT, &SSlider::setValue, this);
52  newSlot(s_SET_MIN_VALUE_SLIDER_SLOT, &SSlider::setMinValue, this);
53  newSlot(s_SET_MAX_VALUE_SLIDER_SLOT, &SSlider::setMaxValue, this);
54 
55  m_sigValueChanged = newSignal<ValueChangedSignalType>(s_VALUE_CHANGED_SIG);
56 }
57 
58 //------------------------------------------------------------------------------
59 
61 {
62 }
63 
64 //------------------------------------------------------------------------------
66 {
67  this->initialize();
68 
69  // VALUE
70  {
71  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("value");
72  if(config)
73  {
74  m_value = std::stoi(config->getValue());
75  }
76  }
77 
78  // DEFAULT VALUE
79  {
80  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("defaultValue");
81  if(config)
82  {
83  m_defaultValue = std::stoi(config->getValue());
84  }
85  }
86 
87  // RESET BUTTON
88  {
89  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("resetButton");
90  if(config)
91  {
92  m_hasResetButton = (config->getValue() == "true");
93  }
94  }
95 
96  // UPDATE ON RELEASE
97  {
98  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("updateOnRelease");
99  if(config)
100  {
101  m_isUpdatedOnRelease = (config->getValue() == "true");
102  }
103  }
104 
105  // EDIT BOX
106  {
107  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("editBox");
108  if(config)
109  {
110  m_hasEditBox = (config->getValue() == "true");
111  }
112  }
113 
114  // TEXT
115  {
116  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("text");
117  if(config)
118  {
119  m_text = QString(config->getValue().c_str());
120  }
121  }
122 
123  // RANGE
124  {
125  ::fwRuntime::ConfigurationElement::sptr config = m_configuration->findConfigurationElement("range");
126  if(config)
127  {
128  ::fwRuntime::ConfigurationElement::sptr minCfg = config->findConfigurationElement("min");
129  ::fwRuntime::ConfigurationElement::sptr maxCfg = config->findConfigurationElement("max");
130  SLM_ASSERT("Missing min and max configuration", minCfg && maxCfg);
131 
132  m_minValue = std::stoi(minCfg->getValue());
133  m_maxValue = std::stoi(maxCfg->getValue());
134  }
135  }
136 }
137 
138 //------------------------------------------------------------------------------
140 {
141  this->create();
142  ::fwGuiQt::container::QtContainer::sptr qtContainer =
143  ::fwGuiQt::container::QtContainer::dynamicCast(this->getContainer());
144 
145  QPointer<QHBoxLayout> layout = new QHBoxLayout();
146  m_valueSlider = new QSlider(Qt::Horizontal);
147  m_valueSlider->setRange(m_minValue, m_maxValue);
148  m_valueSlider->setValue(m_value);
149 
150  QMetaObject::Connection isConnected;
151  isConnected = QObject::connect(m_valueSlider, SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
152  SLM_ASSERT("sliderPressed Signal failed to connect to sliderPressed Slot.", isConnected);
153 
154  isConnected = QObject::connect(m_valueSlider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
155  SLM_ASSERT("valueChanged Signal failed to connect to setValue Slot.", isConnected);
156 
157  isConnected = QObject::connect(m_valueSlider, SIGNAL(sliderReleased()), this, SLOT(changeValue()));
158  SLM_ASSERT("sliderReleased Signal failed to connect to changeValue Slot.", isConnected);
159 
160  m_textLabel = new QLabel();
161  m_textLabel->setText(m_text);
162 
163  if( !m_hasEditBox )
164  {
165  m_valueLabel = new QLabel();
166  m_valueLabel->setText(QString::number(m_value));
167  }
168 
169  m_minValueLabel = new QLabel();
170  m_minValueLabel->setText(QString::number(m_minValue));
171 
172  m_maxValueLabel = new QLabel();
173  m_maxValueLabel->setText(QString::number(m_maxValue));
174 
175  layout->addWidget( m_textLabel );
176 
177  if( !m_hasEditBox )
178  {
179  layout->addWidget( m_valueLabel );
180  }
181  layout->addWidget( m_minValueLabel );
182  layout->addWidget( m_valueSlider );
183  layout->addWidget( m_maxValueLabel );
184 
185  if( m_hasEditBox )
186  {
187  m_valueEdit = new QLineEdit("");
188  m_valueEdit->setMaximumWidth( 70 );
189  m_valueEdit->setInputMask("#0000");
190 
191  isConnected = QObject::connect( m_valueEdit, SIGNAL(returnPressed()), this, SLOT(editValue()) );
192  SLM_ASSERT("editingFinished Signal failed to connect to onTextChanged Slot.", isConnected);
193 
194  layout->addWidget( m_valueEdit );
195  }
196 
197  if( m_hasResetButton )
198  {
199  m_resetButton = new QPushButton("R"); // "R" is codename for Reset !
200 
201  isConnected = QObject::connect(m_resetButton, SIGNAL(clicked()), this, SLOT(resetValue()));
202  SLM_ASSERT("clicked Signal failed to connect to resetValue Slot.", isConnected);
203 
204  layout->addWidget( m_resetButton );
205 
206  }
207 
208  qtContainer->setLayout(layout);
209 
210  this->resetValue();
211 }
212 
213 //------------------------------------------------------------------------------
214 
216 {
217  SLM_TRACE_FUNC();
218 
219  QObject::disconnect(m_valueSlider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
220 
221  this->destroy();
222 }
223 //------------------------------------------------------------------------------
224 
226 {
227 }
228 
229 //------------------------------------------------------------------------------
230 
232 {
233  this->updating();
234 }
235 
236 //------------------------------------------------------------------------------
238 {
239  m_sliderPressed = true;
240 }
241 
242 //------------------------------------------------------------------------------
244 {
245  setValue( m_defaultValue, true );
246 }
247 
248 //------------------------------------------------------------------------------
250 {
251  SLM_ASSERT( "m_valueSlider must not be null", nullptr != m_valueSlider );
252  SLM_ASSERT( "m_sigValueChanged must not be null", nullptr != m_sigValueChanged );
253 
254  int value = m_valueSlider->sliderPosition();
255  m_valueSlider->setSliderPosition(value);
256 
257  // we use either an edit box or a label to display the current value
258  if( !m_hasEditBox )
259  {
260  SLM_ASSERT("m_valueLabel must not be null", nullptr != m_valueLabel);
261 
262  m_valueLabel->setText(QString::number(value));
263  }
264  else
265  {
266  SLM_ASSERT("m_valueEdit must not be null", nullptr != m_valueEdit);
267 
268  m_valueEdit->setText(QString::number(value));
269  }
270 
271  // Notify the new position
272  m_sigValueChanged->asyncEmit(value);
273 
274  m_sliderPressed = false;
275 }
276 
277 //------------------------------------------------------------------------------
278 
280 {
281  SLM_ASSERT("m_valueEdit must not be null", false );// && nullptr != m_valueEdit);
282 
283  QString strValue = m_valueEdit->text();
284 
285  setValue( strValue.toInt(), true );
286 }
287 
288 //------------------------------------------------------------------------------
289 
290 void SSlider::setValue( int value, bool _bForced )
291 {
292  SLM_ASSERT("m_valueSlider must not be null", nullptr != m_valueSlider);
293 
294  // we use either an edit box or a label to display the current value
295  if( !m_hasEditBox )
296  {
297  SLM_ASSERT("m_valueLabel must not be null", nullptr != m_valueLabel);
298 
299  m_valueLabel->setText(QString::number(value));
300  }
301  else
302  {
303  SLM_ASSERT("m_valueEdit must not be null", nullptr != m_valueEdit);
304 
305  m_valueEdit->setText(QString::number(value));
306  }
307 
308  if( !m_sliderPressed || !m_isUpdatedOnRelease || _bForced )
309  {
310  m_value = value;
311  m_valueSlider->setValue(value);
312 
313  // Notify the new position
314  m_sigValueChanged->asyncEmit(value);
315  }
316 }
317 
318 //------------------------------------------------------------------------------
319 
320 void SSlider::setMinValue(int value)
321 {
322  SLM_ASSERT("m_valueSlider must not be null", nullptr != m_valueSlider);
323  SLM_ASSERT("m_valueSlider must not be null", nullptr != m_valueSlider);
324 
325  m_minValue = value;
326  m_valueSlider->setMinimum( value );
327  m_minValueLabel->setText( QString::number(value));
328 }
329 
330 //------------------------------------------------------------------------------
331 
332 void SSlider::setMaxValue(int value)
333 {
334  SLM_ASSERT("m_valueSlider must not be null", nullptr != m_valueSlider);
335  SLM_ASSERT("m_maxValueLabel must not be null", nullptr != m_maxValueLabel);
336 
337  m_maxValue = value;
338  m_valueSlider->setMaximum( value );
339  m_maxValueLabel->setText( QString::number(value));
340 }
341 
342 //------------------------------------------------------------------------------
343 
344 } //namespace editor
345 } //namespace guiQt
virtual void configuring() override
Configure the service.
Definition: SSlider.cpp:65
void setMaxValue(int value)
SLOT : Called to set the max range.
Definition: SSlider.cpp:332
void setValue(int value, bool _bForced=false)
SLOT : Called to set the value.
Definition: SSlider.cpp:290
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
virtual GUIQT_API ~SSlider() noexcept
Destructor. Do nothing.
Definition: SSlider.cpp:60
This editor allows to draw a slider with an integer data.
Definition: SSlider.hpp:61
void sliderPressed()
Internal slot. Called when the cursor starts to move.
Definition: SSlider.cpp:237
virtual void starting() override
Installs the layout.
Definition: SSlider.cpp:139
void resetValue()
Internal slot. Reset the value - and the slider position - to default.
Definition: SSlider.cpp:243
virtual void swapping() override
Does nothing.
Definition: SSlider.cpp:231
ValueChangedSignalType::sptr m_sigValueChanged
Signal when the position os the slider changed.
Definition: SSlider.hpp:116
GUIQT_API SSlider() noexcept
Constructor. Do nothing.
Definition: SSlider.cpp:40
void editValue()
Internal slot. Called when Return is pressed on the edit box.
Definition: SSlider.cpp:279
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
virtual void stopping() override
Destroys the layout.
Definition: SSlider.cpp:215
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
void changeValue()
Internal slot. Called when the cursor is moved.
Definition: SSlider.cpp:249
void setMinValue(int value)
SLOT : Called to set the min range.
Definition: SSlider.cpp:320
virtual void updating() override
Does nothing.
Definition: SSlider.cpp:225
The namespace guiQt contains the basic services to build the application IHM with Qt...
Definition: Code.hpp:21
FWGUI_API void initialize()
Initialize managers.