27 #include <QApplication>
29 #include <QColorDialog>
31 #include <QDoubleSpinBox>
33 #include <QMessageBox>
36 #include <QPushButton>
37 #include <QStringList>
46 QVariant data = index.data(Qt::DisplayRole);
48 QString name = data.value<QString>();
52 QColor qcol = data.value<QColor>();
55 painter->fillRect(option.rect, qcol);
56 QRect rect = option.rect;
57 rect.setWidth(rect.width() - 1);
58 rect.setHeight(rect.height() - 1);
62 painter->drawRect(rect);
67 QStyledItemDelegate::paint(painter, option, index);
72 const QStyleOptionViewItem &option,
73 const QModelIndex &index)
const
75 QVariant data = index.data(Qt::EditRole);
76 QVariant displayData = index.data(Qt::DisplayRole);
77 QString name = index.model()->data(index.model()->index(index.row(), index.column() - 1)).value<QString>();
81 QWidget *editorWidget =
nullptr;
85 auto colorBtn =
new QPushButton(parent);
86 QColor color = data.value<QColor>();
88 QColor result = QColorDialog::getColor(color);
91 QPalette palette = colorBtn->palette();
92 palette.setColor(QPalette::Button, result);
93 colorBtn->setPalette(palette);
94 colorBtn->setStyleSheet(
95 QString(
"background-color: %1;foreground-color: %1; border-style: none;").arg(result.name()));
100 QPalette palette = colorBtn->palette();
101 palette.setColor(QPalette::Button, color);
102 colorBtn->setPalette(palette);
103 colorBtn->setStyleSheet(
104 QString(
"background-color: %1;foreground-color: %1; border-style: none;").arg(color.name()));
107 connect(colorBtn, SIGNAL(pressed()),
this, SLOT(commitAndCloseEditor()));
109 editorWidget = colorBtn;
114 auto spinBox =
new QSpinBox(parent);
115 spinBox->setSingleStep(1);
118 editorWidget = spinBox;
122 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
124 auto spinBox =
new QDoubleSpinBox(parent);
125 spinBox->setDecimals(2);
126 spinBox->setSingleStep(0.1);
127 if (name ==
"opacity")
129 spinBox->setMinimum(0.0);
130 spinBox->setMaximum(1.0);
138 editorWidget = spinBox;
143 QStringList entries = data.value<QStringList>();
144 auto comboBox =
new QComboBox(parent);
145 comboBox->setEditable(
false);
146 comboBox->addItems(entries);
148 editorWidget = comboBox;
153 editorWidget = QStyledItemDelegate::createEditor(parent, option, index);
159 editorWidget->installEventFilter(const_cast<QmitkPropertyDelegate *>(
this));
165 return new QLabel(displayData.toString(), parent);
170 QVariant data = index.data(Qt::EditRole);
171 QVariant displayData = index.data(Qt::DisplayRole);
177 QSpinBox *spinBox = qobject_cast<QSpinBox *>(editor);
178 spinBox->setValue(data.toInt());
182 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
184 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
185 spinBox->setValue(data.toDouble());
190 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
191 QString displayString = displayData.value<QString>();
192 comboBox->setCurrentIndex(comboBox->findData(displayString));
196 return QStyledItemDelegate::setEditorData(editor, index);
202 QVariant data = index.data(Qt::EditRole);
203 QVariant displayData = index.data(Qt::DisplayRole);
209 QWidget *colorBtn = qobject_cast<QWidget *>(editor);
210 QVariant colorVariant;
211 colorVariant.setValue<QColor>(colorBtn->palette().color(QPalette::Button));
212 model->setData(index, colorVariant);
217 QSpinBox *spinBox = qobject_cast<QSpinBox *>(editor);
220 QVariant intValueVariant;
221 intValueVariant.setValue<
float>(
static_cast<float>(
intValue));
222 model->setData(index, intValueVariant);
225 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
227 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
228 double doubleValue = spinBox->value();
230 QVariant doubleValueVariant;
231 doubleValueVariant.setValue<
float>(
static_cast<float>(doubleValue));
232 model->setData(index, doubleValueVariant);
237 QString displayData = data.value<QString>();
239 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
240 QString comboBoxValue = comboBox->currentText();
242 QVariant comboBoxValueVariant;
243 comboBoxValueVariant.setValue<QString>(comboBoxValue);
244 model->setData(index, comboBoxValueVariant);
248 QStyledItemDelegate::setModelData(editor, model, index);
252 void QmitkPropertyDelegate::commitAndCloseEditor()
254 QWidget *editor =
nullptr;
255 if (QPushButton *pushBtn = qobject_cast<QPushButton *>(sender()))
262 emit commitData(editor);
263 emit closeEditor(editor);
268 const QStyleOptionViewItem &option,
269 const QModelIndex & )
const
271 editor->setGeometry(option.rect);
274 void QmitkPropertyDelegate::ComboBoxCurrentIndexChanged(
int )
276 if (QComboBox *comboBox = qobject_cast<QComboBox *>(sender()))
278 emit commitData(comboBox);
279 emit closeEditor(comboBox);
283 void QmitkPropertyDelegate::SpinBoxValueChanged(
const QString & )
285 QAbstractSpinBox *spinBox =
nullptr;
286 if ((spinBox = qobject_cast<QSpinBox *>(sender())) || (spinBox = qobject_cast<QDoubleSpinBox *>(sender())))
288 emit commitData(spinBox);
289 emit closeEditor(spinBox);
293 void QmitkPropertyDelegate::showColorDialog()
303 case QEvent::KeyRelease:
304 case QEvent::MouseButtonRelease:
305 case QEvent::MouseButtonDblClick:
307 case QEvent::FocusIn:
309 if (QWidget *editor = dynamic_cast<QWidget *>(o))
311 emit commitData(editor);
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Fit an editor to some geometry (overwritten from QItemDelegate)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
bool eventFilter(QObject *o, QEvent *e) override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
static RenderingManager * GetInstance()
std::vector< std::string > StringList
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
QmitkPropertyDelegate(QObject *parent=nullptr)
void setEditorData(QWidget *editor, const QModelIndex &index) const override
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)