Medical Imaging Interaction Toolkit  2018.4.99-f51274ea
Medical Imaging Interaction Toolkit
Helper/QmitkMapPropertyDelegate.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 ============================================================================*/
12 
13 #ifndef NOMINMAX
14 #define NOMINMAX
15 #endif
16 
18 
19 #include <QmitkCustomVariants.h>
20 
21 #include <mitkRenderingManager.h>
22 
23 #include <bitset>
24 #include <QPainter>
25 #include <QApplication>
26 #include <QCheckBox>
27 #include <QLabel>
28 #include <QPushButton>
29 #include <QColorDialog>
30 #include <QComboBox>
31 #include <QDoubleSpinBox>
32 #include <QStringList>
33 #include <QMessageBox>
34 #include <QPen>
35 
37 {
38 }
39 
40 void QmitkMapPropertyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option
41  , const QModelIndex &index) const
42 {
43 
44  QVariant data = index.data(Qt::DisplayRole);
45 
46  QString name = data.value<QString>();
47 
48  QStyledItemDelegate::paint(painter, option, index);
49 
50 }
51 
52 QWidget* QmitkMapPropertyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option
53  , const QModelIndex &index) const
54 {
55  QVariant data = index.data(Qt::EditRole);
56  QVariant displayData = index.data(Qt::DisplayRole);
57  QString name = index.model()->data(index.model()->index(index.row(), index.column() - 1)).value<QString>();
58 
59  if (data.isValid())
60  {
61 
62  QWidget* editorWidget = nullptr;
63 
64  if (data.type() == QVariant::Int)
65  {
66  QSpinBox* spinBox = new QSpinBox(parent);
67  spinBox->setSingleStep(1);
68  spinBox->setMinimum(std::numeric_limits<int>::min());
69  spinBox->setMaximum(std::numeric_limits<int>::max());
70  editorWidget = spinBox;
71  }
72  // see qt documentation. cast is correct, it would be obsolete if we
73  // store doubles
74  else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
75  {
76  QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
77  spinBox->setDecimals(5);
78  spinBox->setSingleStep(0.1);
79  spinBox->setMinimum(std::numeric_limits<float>::min());
80  spinBox->setMaximum(std::numeric_limits<float>::max());
81 
82  editorWidget = spinBox;
83  }
84  else if (data.type() == QVariant::StringList)
85  {
86  QStringList entries = data.value<QStringList>();
87  QComboBox* comboBox = new QComboBox(parent);
88  comboBox->setEditable(false);
89  comboBox->addItems(entries);
90 
91  editorWidget = comboBox;
92  }
93  else
94  {
95  editorWidget = QStyledItemDelegate::createEditor(parent, option, index);
96  }
97 
98  if (editorWidget)
99  {
100  // install event filter
101  editorWidget->installEventFilter(const_cast<QmitkMapPropertyDelegate*>(this));
102  }
103 
104  return editorWidget;
105 
106  }
107  else
108  return new QLabel(displayData.toString(), parent);
109 
110 }
111 
112 void QmitkMapPropertyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
113 {
114 
115  QVariant data = index.data(Qt::EditRole);
116  QVariant displayData = index.data(Qt::DisplayRole);
117 
118  std::cout << "Set EDITOR DATA : " << data.toDouble() << std::endl;
119 
120  if (data.isValid())
121  {
122 
123 
124  if (data.type() == QVariant::Int)
125  {
126  QSpinBox* spinBox = qobject_cast<QSpinBox *>(editor);
127  spinBox->setValue(data.toInt());
128  }
129  // see qt documentation. cast is correct, it would be obsolete if we
130  // store doubles
131  else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
132  {
133  QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox *>(editor);
134  spinBox->setValue(data.toDouble());
135 
136  std::cout << "Set EDITOR DATA : " << spinBox->value() << std::endl;
137  }
138 
139  else if (data.type() == QVariant::StringList)
140  {
141  QComboBox* comboBox = qobject_cast<QComboBox *>(editor);
142  QString displayString = displayData.value<QString>();
143  comboBox->setCurrentIndex(comboBox->findData(displayString));
144  }
145 
146  else
147  return QStyledItemDelegate::setEditorData(editor, index);
148  }
149 }
150 
151 void QmitkMapPropertyDelegate::setModelData(QWidget *editor, QAbstractItemModel* model
152  , const QModelIndex &index) const
153 {
154  QVariant data = index.data(Qt::EditRole);
155  QVariant displayData = index.data(Qt::DisplayRole);
156 
157  if (data.isValid())
158  {
159 
160  if (data.type() == QVariant::Color)
161  {
162  QWidget *colorBtn = qobject_cast<QWidget *>(editor);
163  QVariant colorVariant;
164  colorVariant.setValue<QColor>(colorBtn->palette().color(QPalette::Button));
165  model->setData(index, colorVariant);
166  }
167 
168  else if (data.type() == QVariant::Int)
169  {
170  QSpinBox* spinBox = qobject_cast<QSpinBox *>(editor);
171  int intValue = spinBox->value();
172 
173  QVariant intValueVariant;
174  intValueVariant.setValue<float>(static_cast<float>(intValue));
175  model->setData(index, intValueVariant);
176  }
177 
178  else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
179  {
180  QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox *>(editor);
181  double doubleValue = spinBox->value();
182 
183  std::cout << "SET MODEL DATA << FLOAT : " << doubleValue << std::endl;
184 
185  QVariant doubleValueVariant;
186  doubleValueVariant.setValue<float>(static_cast<float>(doubleValue));
187  std::cout << "SET MODEL DATA << Variant : " << doubleValue << std::endl;
188  model->setData(index, doubleValueVariant);
189  }
190 
191  else if (data.type() == QVariant::StringList)
192  {
193  QString displayData = data.value<QString>();
194 
195  QComboBox* comboBox = qobject_cast<QComboBox *>(editor);
196  QString comboBoxValue = comboBox->currentText();
197 
198  QVariant comboBoxValueVariant;
199  comboBoxValueVariant.setValue<QString>(comboBoxValue);
200  model->setData(index, comboBoxValueVariant);
201  }
202 
203  else
204  QStyledItemDelegate::setModelData(editor, model, index);
205  }
206 
207 }
208 
209 void QmitkMapPropertyDelegate::commitAndCloseEditor()
210 {
211  QWidget* editor = 0;
212  if (QPushButton *pushBtn = qobject_cast<QPushButton *>(sender()))
213  {
214  editor = pushBtn;
215  }
216 
217  if (editor)
218  {
219  emit commitData(editor);
220  emit closeEditor(editor);
221  }
222 
223 }
224 
226  const QStyleOptionViewItem &option,
227  const QModelIndex & /*index*/) const
228 {
229  editor->setGeometry(option.rect);
230 }
231 
232 void QmitkMapPropertyDelegate::ComboBoxCurrentIndexChanged(int /*index*/)
233 {
234  if (QComboBox *comboBox = qobject_cast<QComboBox *>(sender()))
235  {
236  emit commitData(comboBox);
237  emit closeEditor(comboBox);
238  }
239 }
240 
241 void QmitkMapPropertyDelegate::SpinBoxValueChanged(const QString& /*value*/)
242 {
243  QAbstractSpinBox *spinBox = 0;
244  if ((spinBox = qobject_cast<QSpinBox *>(sender()))
245  || (spinBox = qobject_cast<QDoubleSpinBox *>(sender())))
246  {
247  emit commitData(spinBox);
248  emit closeEditor(spinBox);
249  }
250 }
251 
252 void QmitkMapPropertyDelegate::showColorDialog()
253 {
254 
255 }
256 
257 bool QmitkMapPropertyDelegate::eventFilter(QObject *o, QEvent *e)
258 {
259  // filter all kind of events on our editor widgets
260  // when certain events occur, repaint all render windows, because rendering relevant properties might have changed
261  switch (e->type())
262  {
263  case QEvent::KeyRelease:
264  case QEvent::MouseButtonRelease:
265  case QEvent::MouseButtonDblClick:
266  case QEvent::Wheel:
267  case QEvent::FocusIn:
268  {
269  if (QWidget* editor = dynamic_cast<QWidget*>(o))
270  {
271  emit commitData(editor);
272  }
273  break;
274  }
275  default:
276  {
277  break;
278  }
279  }
280 
281  return false;
282 }
signed integer value
Definition: jsoncpp.h:348
bool eventFilter(QObject *o, QEvent *e)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
static T max(T x, T y)
Definition: svm.cpp:56
int Int
Definition: jsoncpp.h:158
std::vector< std::string > StringList
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
static T min(T x, T y)
Definition: svm.cpp:53
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
Fit an editor to some geometry (overwritten from QItemDelegate)
void setEditorData(QWidget *editor, const QModelIndex &index) const