Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkNumberPropertyEditor.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 ===================================================================*/
17 #include <QTextStream>
18 #include <mitkRenderingManager.h>
19 
20 #define DT_SHORT 1
21 #define DT_INT 2
22 #define DT_FLOAT 3
23 #define DT_DOUBLE 4
24 
25 #define ROUND(x) (((x) > 0) ? int((x) + 0.5) : int((x)-0.5))
26 #define ROUND_SHORT(x) (((x) > 0) ? short((x) + 0.5) : short((x)-0.5))
27 
29  : QSpinBox(parent), PropertyEditor(property), m_IntProperty(property), m_DataType(DT_INT)
30 {
31  initialize();
32 }
33 
35  : QSpinBox(parent), PropertyEditor(property), m_FloatProperty(property), m_DataType(DT_FLOAT)
36 {
37  initialize();
38 }
39 
41  : QSpinBox(parent), PropertyEditor(property), m_DoubleProperty(property), m_DataType(DT_DOUBLE)
42 {
43  initialize();
44 }
45 
47 {
48 }
49 
51 { // only to be called from constructors
52 
53  // spinbox settings
54  setSuffix("");
55 
56  // protected
57  m_DecimalPlaces = 0;
60  m_ShowPercents = false;
61 
62  // private
63  m_SelfChangeLock = false;
64 
65  connect(this, SIGNAL(valueChanged(int)), this, SLOT(onValueChanged(int)));
66 
67  // display current value of our property
68  DisplayNumber();
69 }
70 
71 void QmitkNumberPropertyEditor::adjustFactors(short newDecimalPlaces, bool newShowPercents)
72 {
73  int oldMax = maxValue();
74  int oldMin = minValue();
75 
76  m_DecimalPlaces = newDecimalPlaces;
77  m_ShowPercents = newShowPercents;
78 
81 
82  if (m_ShowPercents)
83  {
85  setSuffix("%");
86  }
87  else
88  {
89  setSuffix("");
90  }
91 
92  setMinValue(oldMin);
93  setMaxValue(oldMax);
94 }
95 
97 {
98  return m_DecimalPlaces;
99 }
100 
102 {
103  switch (m_DataType)
104  {
105  case DT_FLOAT:
106  case DT_DOUBLE:
107  {
108  adjustFactors(places, m_ShowPercents);
109  DisplayNumber();
110  break;
111  }
112  default:
113  break;
114  }
115 }
116 
118 {
119  return m_ShowPercents;
120 }
121 
123 {
124  if (showPercent == m_ShowPercents)
125  return; // nothing to change
126 
127  switch (m_DataType)
128  {
129  case DT_FLOAT:
130  case DT_DOUBLE:
131  {
132  adjustFactors(m_DecimalPlaces, showPercent);
133  break;
134  }
135  default:
136  {
137  break;
138  }
139  }
140 
141  DisplayNumber();
142 }
143 
145 {
146  return ROUND(QSpinBox::minimum() / m_FactorPropertyToSpinbox);
147 }
148 
150 {
151  QSpinBox::setMinimum(ROUND(value * m_FactorPropertyToSpinbox));
152 }
153 
155 {
156  return ROUND(QSpinBox::maximum() / m_FactorPropertyToSpinbox);
157 }
158 
160 {
161  QSpinBox::setMaximum(ROUND(value * m_FactorPropertyToSpinbox));
162 }
163 
165 {
166  return static_cast<double>((QSpinBox::value()) / m_FactorPropertyToSpinbox);
167 }
168 
170 {
171  QSpinBox::setValue(ROUND(value * m_FactorPropertyToSpinbox));
172 }
173 
175 {
176  QString displayedText;
177  QTextStream stream(&displayedText);
178 
179  double d(value * m_FactorSpinboxToDisplay);
180 
181  if (m_DecimalPlaces > 0)
182  {
183  stream.setRealNumberPrecision(m_DecimalPlaces);
184  stream << d;
185  }
186  else
187  {
188  stream << ROUND(d);
189  }
190 
191  return displayedText;
192 }
193 
194 int QmitkNumberPropertyEditor::valueFromText(const QString &text) const
195 {
196  return ROUND(text.toDouble() / m_FactorSpinboxToDisplay);
197 }
198 
200 {
201  if (m_SelfChangeLock)
202  return; // valueChanged is even emitted, when this widget initiates a change to its value
203  // this may be useful some times, but in this use, it would be wrong:
204  // (A) is an editor with 3 decimal places
205  // (B) is an editor with 2 decimal places
206  // User changes A's displayed value to 4.002
207  // A's onValueChanged gets called, sets the associated Property to 4.002
208  // B's onPropertyChanged gets called, sets its display to 4.00
209  // B's onValueChanged gets called and sets the associated Property to 4.00
210  // A's onPropertyChanged gets called, sets its display to 4.000
211 
213 
214  double newValue(value / m_FactorPropertyToSpinbox);
215 
216  switch (m_DataType)
217  {
218  case DT_INT:
219  {
220  m_IntProperty->SetValue(ROUND(newValue));
221  break;
222  }
223  case DT_FLOAT:
224  {
225  m_FloatProperty->SetValue(newValue);
226  break;
227  }
228  case DT_DOUBLE:
229  {
230  m_DoubleProperty->SetValue(newValue);
231  break;
232  }
233  }
235 
237 }
238 
240 {
241  DisplayNumber();
243 }
244 
246 {
247  m_Property = nullptr;
248 }
249 
251 {
252  if (!m_Property)
253  return;
254 
255  m_SelfChangeLock = true;
256  switch (m_DataType)
257  {
258  case DT_INT:
259  {
260  int i = m_IntProperty->GetValue();
261  QSpinBox::setValue(i);
262  break;
263  }
264  case DT_FLOAT:
265  {
266  float f = m_FloatProperty->GetValue();
267  setDoubleValue(f);
268  break;
269  }
270  case DT_DOUBLE:
271  {
272  double d = m_DoubleProperty->GetValue();
273  setDoubleValue(d);
274  break;
275  }
276  default:
277  break;
278  }
279  m_SelfChangeLock = false;
280 }
virtual QString textFromValue(int) const override
virtual int valueFromText(const QString &) const override
#define DT_INT
mitk::BaseProperty * m_Property
#define DT_DOUBLE
static RenderingManager * GetInstance()
virtual void PropertyRemoved() override
mitk::GenericProperty< float > * m_FloatProperty
#define DT_FLOAT
virtual void PropertyChanged() override
#define ROUND(x)
mitk::GenericProperty< double > * m_DoubleProperty
QmitkNumberPropertyEditor(mitk::IntProperty *, QWidget *parent)
mitk::GenericProperty< int > * m_IntProperty
virtual void SetValue(T _arg)
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
virtual T GetValue() const