23 #include <QApplication> 25 #include <QColorDialog> 27 #include <QDoubleSpinBox> 29 #include <QMessageBox> 32 #include <QPushButton> 33 #include <QStringList> 42 QVariant data = index.data(Qt::DisplayRole);
44 QString name = data.value<QString>();
48 QColor qcol = data.value<QColor>();
51 painter->fillRect(option.rect, qcol);
52 QRect rect = option.rect;
53 rect.setWidth(rect.width() - 1);
54 rect.setHeight(rect.height() - 1);
58 painter->drawRect(rect);
63 QStyledItemDelegate::paint(painter, option, index);
68 const QStyleOptionViewItem &option,
69 const QModelIndex &index)
const 71 QVariant data = index.data(Qt::EditRole);
72 QVariant displayData = index.data(Qt::DisplayRole);
73 QString name = index.model()->data(index.model()->index(index.row(), index.column() - 1)).value<QString>();
77 QWidget *editorWidget =
nullptr;
81 auto colorBtn =
new QPushButton(parent);
82 QColor color = data.value<QColor>();
84 QColor result = QColorDialog::getColor(color);
87 QPalette palette = colorBtn->palette();
88 palette.setColor(QPalette::Button, result);
89 colorBtn->setPalette(palette);
90 colorBtn->setStyleSheet(
91 QString(
"background-color: %1;foreground-color: %1; border-style: none;").arg(result.name()));
96 QPalette palette = colorBtn->palette();
97 palette.setColor(QPalette::Button, color);
98 colorBtn->setPalette(palette);
99 colorBtn->setStyleSheet(
100 QString(
"background-color: %1;foreground-color: %1; border-style: none;").arg(color.name()));
103 connect(colorBtn, SIGNAL(pressed()),
this, SLOT(commitAndCloseEditor()));
105 editorWidget = colorBtn;
110 auto spinBox =
new QSpinBox(parent);
111 spinBox->setSingleStep(1);
114 editorWidget = spinBox;
118 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
120 auto spinBox =
new QDoubleSpinBox(parent);
121 spinBox->setDecimals(2);
122 spinBox->setSingleStep(0.1);
123 if (name ==
"opacity")
125 spinBox->setMinimum(0.0);
126 spinBox->setMaximum(1.0);
134 editorWidget = spinBox;
139 QStringList entries = data.value<QStringList>();
140 auto comboBox =
new QComboBox(parent);
141 comboBox->setEditable(
false);
142 comboBox->addItems(entries);
144 editorWidget = comboBox;
149 editorWidget = QStyledItemDelegate::createEditor(parent, option, index);
155 editorWidget->installEventFilter(const_cast<QmitkPropertyDelegate *>(
this));
161 return new QLabel(displayData.toString(), parent);
166 QVariant data = index.data(Qt::EditRole);
167 QVariant displayData = index.data(Qt::DisplayRole);
173 QSpinBox *spinBox = qobject_cast<QSpinBox *>(editor);
174 spinBox->setValue(data.toInt());
178 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
180 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
181 spinBox->setValue(data.toDouble());
186 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
187 QString displayString = displayData.value<QString>();
188 comboBox->setCurrentIndex(comboBox->findData(displayString));
192 return QStyledItemDelegate::setEditorData(editor, index);
198 QVariant data = index.data(Qt::EditRole);
199 QVariant displayData = index.data(Qt::DisplayRole);
205 QWidget *colorBtn = qobject_cast<QWidget *>(editor);
206 QVariant colorVariant;
207 colorVariant.setValue<QColor>(colorBtn->palette().color(QPalette::Button));
208 model->setData(index, colorVariant);
213 QSpinBox *spinBox = qobject_cast<QSpinBox *>(editor);
216 QVariant intValueVariant;
217 intValueVariant.setValue<
float>(
static_cast<float>(
intValue));
218 model->setData(index, intValueVariant);
221 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
223 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
224 double doubleValue = spinBox->value();
226 QVariant doubleValueVariant;
227 doubleValueVariant.setValue<
float>(
static_cast<float>(doubleValue));
228 model->setData(index, doubleValueVariant);
233 QString displayData = data.value<QString>();
235 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
236 QString comboBoxValue = comboBox->currentText();
238 QVariant comboBoxValueVariant;
239 comboBoxValueVariant.setValue<QString>(comboBoxValue);
240 model->setData(index, comboBoxValueVariant);
244 QStyledItemDelegate::setModelData(editor, model, index);
248 void QmitkPropertyDelegate::commitAndCloseEditor()
250 QWidget *editor =
nullptr;
251 if (QPushButton *pushBtn = qobject_cast<QPushButton *>(sender()))
258 emit commitData(editor);
259 emit closeEditor(editor);
264 const QStyleOptionViewItem &option,
265 const QModelIndex & )
const 267 editor->setGeometry(option.rect);
270 void QmitkPropertyDelegate::ComboBoxCurrentIndexChanged(
int )
272 if (QComboBox *comboBox = qobject_cast<QComboBox *>(sender()))
274 emit commitData(comboBox);
275 emit closeEditor(comboBox);
279 void QmitkPropertyDelegate::SpinBoxValueChanged(
const QString & )
281 QAbstractSpinBox *spinBox =
nullptr;
282 if ((spinBox = qobject_cast<QSpinBox *>(sender())) || (spinBox = qobject_cast<QDoubleSpinBox *>(sender())))
284 emit commitData(spinBox);
285 emit closeEditor(spinBox);
289 void QmitkPropertyDelegate::showColorDialog()
299 case QEvent::KeyRelease:
300 case QEvent::MouseButtonRelease:
301 case QEvent::MouseButtonDblClick:
303 case QEvent::FocusIn:
305 if (QWidget *editor = dynamic_cast<QWidget *>(o))
307 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)