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