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