27 #include <QApplication>
29 #include <QColorDialog>
31 #include <QDoubleSpinBox>
33 #include <QMessageBox>
36 #include <QPushButton>
37 #include <QStringList>
45 const QStyleOptionViewItem &option,
46 const QModelIndex &index)
const
48 QVariant data = index.data(Qt::DisplayRole);
50 QString name = data.value<QString>();
52 QStyledItemDelegate::paint(painter, option, index);
56 const QStyleOptionViewItem &option,
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>();
65 QWidget *editorWidget = NULL;
69 QSpinBox *spinBox =
new QSpinBox(parent);
70 spinBox->setSingleStep(1);
73 editorWidget = spinBox;
77 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
79 QDoubleSpinBox *spinBox =
new QDoubleSpinBox(parent);
80 spinBox->setDecimals(5);
81 spinBox->setSingleStep(0.1);
85 editorWidget = spinBox;
89 QStringList entries = data.value<QStringList>();
90 QComboBox *comboBox =
new QComboBox(parent);
91 comboBox->setEditable(
false);
92 comboBox->addItems(entries);
94 editorWidget = comboBox;
98 editorWidget = QStyledItemDelegate::createEditor(parent, option, index);
104 editorWidget->installEventFilter(const_cast<QmitkMapPropertyDelegate *>(
this));
110 return new QLabel(displayData.toString(), parent);
115 QVariant data = index.data(Qt::EditRole);
116 QVariant displayData = index.data(Qt::DisplayRole);
118 std::cout <<
"Set EDITOR DATA : " << data.toDouble() << std::endl;
124 QSpinBox *spinBox = qobject_cast<QSpinBox *>(editor);
125 spinBox->setValue(data.toInt());
129 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
131 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
132 spinBox->setValue(data.toDouble());
134 std::cout <<
"Set EDITOR DATA : " << spinBox->value() << std::endl;
139 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
140 QString displayString = displayData.value<QString>();
141 comboBox->setCurrentIndex(comboBox->findData(displayString));
145 return QStyledItemDelegate::setEditorData(editor, index);
151 QVariant data = index.data(Qt::EditRole);
152 QVariant displayData = index.data(Qt::DisplayRole);
158 QWidget *colorBtn = qobject_cast<QWidget *>(editor);
159 QVariant colorVariant;
160 colorVariant.setValue<QColor>(colorBtn->palette().color(QPalette::Button));
161 model->setData(index, colorVariant);
166 QSpinBox *spinBox = qobject_cast<QSpinBox *>(editor);
169 QVariant intValueVariant;
170 intValueVariant.setValue<
float>(
static_cast<float>(
intValue));
171 model->setData(index, intValueVariant);
174 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
176 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
177 double doubleValue = spinBox->value();
179 std::cout <<
"SET MODEL DATA << FLOAT : " << doubleValue << std::endl;
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);
189 QString displayData = data.value<QString>();
191 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
192 QString comboBoxValue = comboBox->currentText();
194 QVariant comboBoxValueVariant;
195 comboBoxValueVariant.setValue<QString>(comboBoxValue);
196 model->setData(index, comboBoxValueVariant);
200 QStyledItemDelegate::setModelData(editor, model, index);
204 void QmitkMapPropertyDelegate::commitAndCloseEditor()
207 if (QPushButton *pushBtn = qobject_cast<QPushButton *>(sender()))
214 emit commitData(editor);
215 emit closeEditor(editor);
220 const QStyleOptionViewItem &option,
221 const QModelIndex & )
const
223 editor->setGeometry(option.rect);
226 void QmitkMapPropertyDelegate::ComboBoxCurrentIndexChanged(
int )
228 if (QComboBox *comboBox = qobject_cast<QComboBox *>(sender()))
230 emit commitData(comboBox);
231 emit closeEditor(comboBox);
235 void QmitkMapPropertyDelegate::SpinBoxValueChanged(
const QString & )
237 QAbstractSpinBox *spinBox = 0;
238 if ((spinBox = qobject_cast<QSpinBox *>(sender())) || (spinBox = qobject_cast<QDoubleSpinBox *>(sender())))
240 emit commitData(spinBox);
241 emit closeEditor(spinBox);
245 void QmitkMapPropertyDelegate::showColorDialog()
255 case QEvent::KeyRelease:
256 case QEvent::MouseButtonRelease:
257 case QEvent::MouseButtonDblClick:
259 case QEvent::FocusIn:
261 if (QWidget *editor = dynamic_cast<QWidget *>(o))
263 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)