29 #include <QApplication>
32 #include <QPushButton>
33 #include <QColorDialog>
35 #include <QDoubleSpinBox>
36 #include <QStringList>
37 #include <QMessageBox>
45 ,
const QModelIndex &index)
const
48 QVariant data = index.data(Qt::DisplayRole);
50 QString name = data.value<QString>();
52 QStyledItemDelegate::paint(painter, option, index);
57 ,
const QModelIndex &index)
const
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>();
66 QWidget* editorWidget = NULL;
70 QSpinBox* spinBox =
new QSpinBox(parent);
71 spinBox->setSingleStep(1);
74 editorWidget = spinBox;
78 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
80 QDoubleSpinBox* spinBox =
new QDoubleSpinBox(parent);
81 spinBox->setDecimals(5);
82 spinBox->setSingleStep(0.1);
86 editorWidget = spinBox;
90 QStringList entries = data.value<QStringList>();
91 QComboBox* comboBox =
new QComboBox(parent);
92 comboBox->setEditable(
false);
93 comboBox->addItems(entries);
95 editorWidget = comboBox;
99 editorWidget = QStyledItemDelegate::createEditor(parent, option, index);
105 editorWidget->installEventFilter(const_cast<QmitkMapPropertyDelegate*>(
this));
112 return new QLabel(displayData.toString(), parent);
119 QVariant data = index.data(Qt::EditRole);
120 QVariant displayData = index.data(Qt::DisplayRole);
122 std::cout <<
"Set EDITOR DATA : " << data.toDouble() << std::endl;
130 QSpinBox* spinBox = qobject_cast<QSpinBox *>(editor);
131 spinBox->setValue(data.toInt());
135 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
137 QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox *>(editor);
138 spinBox->setValue(data.toDouble());
140 std::cout <<
"Set EDITOR DATA : " << spinBox->value() << std::endl;
145 QComboBox* comboBox = qobject_cast<QComboBox *>(editor);
146 QString displayString = displayData.value<QString>();
147 comboBox->setCurrentIndex(comboBox->findData(displayString));
151 return QStyledItemDelegate::setEditorData(editor, index);
156 ,
const QModelIndex &index)
const
158 QVariant data = index.data(Qt::EditRole);
159 QVariant displayData = index.data(Qt::DisplayRole);
166 QWidget *colorBtn = qobject_cast<QWidget *>(editor);
167 QVariant colorVariant;
168 colorVariant.setValue<QColor>(colorBtn->palette().color(QPalette::Button));
169 model->setData(index, colorVariant);
174 QSpinBox* spinBox = qobject_cast<QSpinBox *>(editor);
177 QVariant intValueVariant;
178 intValueVariant.setValue<
float>(
static_cast<float>(
intValue));
179 model->setData(index, intValueVariant);
182 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
184 QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox *>(editor);
185 double doubleValue = spinBox->value();
187 std::cout <<
"SET MODEL DATA << FLOAT : " << doubleValue << std::endl;
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);
197 QString displayData = data.value<QString>();
199 QComboBox* comboBox = qobject_cast<QComboBox *>(editor);
200 QString comboBoxValue = comboBox->currentText();
202 QVariant comboBoxValueVariant;
203 comboBoxValueVariant.setValue<QString>(comboBoxValue);
204 model->setData(index, comboBoxValueVariant);
208 QStyledItemDelegate::setModelData(editor, model, index);
213 void QmitkMapPropertyDelegate::commitAndCloseEditor()
216 if (QPushButton *pushBtn = qobject_cast<QPushButton *>(sender()))
223 emit commitData(editor);
224 emit closeEditor(editor);
230 const QStyleOptionViewItem &option,
231 const QModelIndex & )
const
233 editor->setGeometry(option.rect);
236 void QmitkMapPropertyDelegate::ComboBoxCurrentIndexChanged(
int )
238 if (QComboBox *comboBox = qobject_cast<QComboBox *>(sender()))
240 emit commitData(comboBox);
241 emit closeEditor(comboBox);
245 void QmitkMapPropertyDelegate::SpinBoxValueChanged(
const QString& )
247 QAbstractSpinBox *spinBox = 0;
248 if ((spinBox = qobject_cast<QSpinBox *>(sender()))
249 || (spinBox = qobject_cast<QDoubleSpinBox *>(sender())))
251 emit commitData(spinBox);
252 emit closeEditor(spinBox);
256 void QmitkMapPropertyDelegate::showColorDialog()
267 case QEvent::KeyRelease:
268 case QEvent::MouseButtonRelease:
269 case QEvent::MouseButtonDblClick:
271 case QEvent::FocusIn:
273 if (QWidget* editor = dynamic_cast<QWidget*>(o))
275 emit commitData(editor);
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
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
Fit an editor to some geometry (overwritten from QItemDelegate)
std::vector< std::string > StringList
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
QmitkMapPropertyDelegate(QObject *parent=0)