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