Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkSliderNavigatorWidget.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 QmitkSliderNavigatorWidget::QmitkSliderNavigatorWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f)
20 {
21  this->setupUi(this);
22 
23  m_Slider->setOrientation(Qt::Horizontal);
24  m_Slider->setMinimum(0);
25  m_Slider->setMaximum(0);
26  m_Slider->setValue(0);
27  m_Slider->setSingleStep(1);
28  m_Slider->setPageStep(10);
29 
30  m_SpinBox->setMinimum( 0 );
31  m_SpinBox->setMaximum(0);
32  m_SpinBox->setValue(0);
33  m_SpinBox->setDecimals(0);
34  m_SpinBox->setSingleStep(1);
35 
36  this->connect(m_Slider, SIGNAL(valueChanged(double)), SLOT(slider_valueChanged(double)));
37  this->connect(m_SpinBox, SIGNAL(valueChanged(double)), SLOT(spinBox_valueChanged(double)));
38 
39  // this avoids trying to use m_Stepper until it is set to something != NULL
40  // (additionally to the avoiding recursions during refetching)
41  m_InRefetch = true;
42 
43  // Hide slider labeling -- until it is explicitly activated
44  this->ShowLabels(false);
45 
46  // Set label values as invalid (N/A)
47  this->SetLabelValuesValid(false, false);
48 
49  m_HasLabels = false;
50  m_HasLabelUnit = true;
51  m_InverseDirection = false;
52  m_InvertedControls = false;
53 }
54 
56 {
57  if (!m_InRefetch)
58  {
59  m_InRefetch = true;
60 
61  if (m_Stepper->GetSteps() == 0)
62  {
63  m_Slider->setMaximum(0);
64  m_Slider->setValue(0);
65  m_SpinBox->setMaximum(0);
66  m_SpinBox->setValue(0);
67  }
68  else
69  {
70  m_Slider->setMaximum(m_Stepper->GetSteps() - 1);
72  {
73  m_Slider->setValue(m_Stepper->GetSteps() - 1 - m_Stepper->GetPos());
74  }
75  else
76  {
77  m_Slider->setValue(m_Stepper->GetPos());
78  }
79 
80  m_SpinBox->setMaximum(m_Stepper->GetSteps() - 1);
82  {
83  m_SpinBox->setValue(m_Stepper->GetSteps() - 1 - m_Stepper->GetPos());
84  }
85  else
86  {
87  m_SpinBox->setValue(m_Stepper->GetPos());
88  }
89  }
90 
91  if (m_Stepper->HasRange() && m_HasLabels)
92  {
93  // Show slider with labels according to below settings
94  m_SliderLabelLeft->setHidden(false);
95  m_SliderLabelRight->setHidden(false);
96 
97  if (m_Stepper->HasValidRange())
98  {
99  this->SetLabelValuesValid(true, true);
100  this->SetLabelValues(m_Stepper->GetRangeMin(), m_Stepper->GetRangeMax());
101  }
102  else
103  {
104  this->SetLabelValuesValid(false, false);
105  }
106 
107  if (m_Stepper->HasUnitName())
108  {
109  this->SetLabelUnit(m_Stepper->GetUnitName());
110  }
111  }
112  else
113  {
114  // Show slider without any labels
115  m_SliderLabelLeft->setHidden(true);
116  m_SliderLabelRight->setHidden(true);
117  }
118 
119  // Update GUI according to above settings
120  this->SetLabels();
121 
122  m_InRefetch = false;
123  }
124 }
125 
127 {
128  m_Stepper = stepper;
129 
130  // this avoids trying to use m_Stepper until it is set to something != NULL
131  // (additionally to the avoiding recursions during refetching)
132  m_InRefetch = (stepper == nullptr);
133 }
134 
135 
137 {
138  if (!m_InRefetch)
139  {
140  if (m_InverseDirection)
141  {
142  m_Stepper->SetPos(m_Stepper->GetSteps() - 1 - m_Slider->value());
143  }
144  else
145  {
146  m_Stepper->SetPos(m_Slider->value());
147  }
148  this->Refetch();
149  }
150 }
151 
153 {
154  m_HasLabels = show;
155 }
156 
158 {
159  m_HasLabelUnit = show;
160 }
161 
163 {
164  m_MinValue = min;
165  m_MaxValue = max;
166 }
167 
168 void QmitkSliderNavigatorWidget::SetLabelValuesValid(bool minValid, bool maxValid)
169 {
170  m_MinValueValid = minValid;
171  m_MaxValueValid = maxValid;
172 }
173 
175 {
176  m_LabelUnit = unit;
177 }
178 
180 {
181  return m_LabelUnit;
182 }
183 
185 {
186  if (value < -10000000.0)
187  {
188  return "-INF";
189  }
190  else if (value > 10000000.0)
191  {
192  return "+INF";
193  }
194  else
195  {
196  return QString::number(value, 'f', 2);
197  }
198 }
199 
201 {
202  if (m_MinValueValid)
203  {
204  return this->ClippedValueToString(m_MinValue);
205  }
206  else
207  {
208  return "N/A";
209  }
210 }
211 
213 {
214  if (m_MaxValueValid)
215  {
216  return this->ClippedValueToString(m_MaxValue);
217  }
218  else
219  {
220  return "N/A";
221  }
222 }
223 
225 {
226  QString minText = "<p align='center'><font size='2'>" + this->GetMinValueLabel();
227 
228  QString maxText = "<p align='center'><font size='2'>" + this->GetMaxValueLabel();
229 
230  if (m_HasLabelUnit)
231  {
232  minText += "&nbsp;" + this->GetLabelUnit();
233  maxText += "&nbsp;" + this->GetLabelUnit();
234  }
235 
236  if (m_MinValueValid)
237  {
238  minText += "<br>(pos&nbsp;0)";
239  }
240 
241  if (m_MaxValueValid)
242  {
243  maxText += "<br>(pos&nbsp;" + QString::number(m_Stepper->GetSteps() - 1) + ")";
244  }
245 
246  minText += "</font></p>";
247  maxText += "</font></p>";
248 
249  m_SliderLabelLeft->setText(minText);
250  m_SliderLabelRight->setText(maxText);
251 }
252 
254 {
255  if (!m_InRefetch)
256  {
257  if (m_InverseDirection)
258  {
259  m_Stepper->SetPos(m_Stepper->GetSteps() - 1 - m_SpinBox->value());
260  }
261  else
262  {
263  m_Stepper->SetPos(m_SpinBox->value());
264  }
265  this->Refetch();
266  }
267 }
268 
270 {
271  return m_Stepper->GetPos();
272 }
273 
275 {
276  if (!m_InRefetch)
277  {
278  m_Stepper->SetPos(val);
279  }
280 }
281 
283 {
284  return m_InverseDirection;
285 }
286 
288 {
289  if (inverseDirection != m_InverseDirection)
290  {
291  m_InverseDirection = inverseDirection;
292  this->Refetch();
293  }
294 }
295 
297 {
298  return m_InvertedControls;
299 }
300 
302 {
303  if (invertedControls != m_InvertedControls)
304  {
305  m_InvertedControls = invertedControls;
306  m_Slider->setInvertedAppearance(invertedControls);
307  m_Slider->setInvertedControls(invertedControls);
308  m_SpinBox->setInvertedControls(invertedControls);
309  }
310 }
311 
void SetInverseDirection(bool inverseDirection)
void SetStepper(mitk::Stepper *stepper)
void SetLabels()
Configure slider with labels according to range and unit settings.
void SetLabelValuesValid(bool minValid, bool maxValid)
void Refetch()
Updates the slider with the recent changes applied to the navigator.
QString ClippedValueToString(float value)
Converts the passed value to a QString representation.
QmitkSliderNavigatorWidget(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
static T max(T x, T y)
Definition: svm.cpp:70
QString GetMinValueLabel()
Returns range-minimum (displayed as label left of slider if enabled)
void SetLabelValues(float min, float max)
Set range minimum and maximum (displayed as labels left and right of slider if enabled) ...
void SetInvertedControls(bool invertedControls)
static T min(T x, T y)
Definition: svm.cpp:67
Helper class to step through a list.
Definition: mitkStepper.h:51
void SetLabelUnit(const char *unit)
Set range unit (e.g. mm or ms) which will be displayed below range labels if enabled.
void ShowLabelUnit(bool show)
En-/disables displaying of the unit label (range will be displayed without unit if enabled)...