19 #include <QApplication>
20 #include <QColorDialog>
22 #include <QHBoxLayout>
24 #include <QPaintEvent>
27 #include <QToolButton>
47 : QWidget(parent), m_LineEdit(new QLineEdit), m_Button(new QToolButton)
49 m_LineEdit->setText(m_Color.name());
50 m_Button->setText(
"...");
52 QHBoxLayout *layout =
new QHBoxLayout;
53 layout->setContentsMargins(0, 0, 0, 0);
54 layout->setSpacing(0);
56 layout->addWidget(m_LineEdit);
57 layout->addWidget(m_Button);
59 this->setFocusProxy(m_LineEdit);
60 this->setLayout(layout);
62 connect(m_LineEdit, SIGNAL(editingFinished()),
this, SLOT(OnLineEditEditingFinished()));
63 connect(m_Button, SIGNAL(clicked()),
this, SLOT(OnButtonClicked()));
78 m_LineEdit->setText(color.name());
81 void QmitkColorWidget::OnLineEditEditingFinished()
83 if (!QColor::isValidColor(m_LineEdit->text()))
84 m_LineEdit->setText(
"#000000");
86 m_Color.setNamedColor(m_LineEdit->text());
89 void QmitkColorWidget::OnButtonClicked()
91 QColor color = QColorDialog::getColor(m_Color, QApplication::activeWindow());
110 if (m_ComboBox != NULL)
112 QStyleOptionComboBox option;
114 option.initFrom(m_ComboBox);
115 option.editable = m_ComboBox->isEditable();
117 if (m_ComboBox->style()->styleHint(QStyle::SH_ComboBox_Popup, &option, m_ComboBox))
119 QStyleOptionMenuItem menuOption;
120 menuOption.initFrom(
this);
121 menuOption.palette = this->palette();
122 menuOption.state = QStyle::State_None;
123 menuOption.checkType = QStyleOptionMenuItem::NotCheckable;
124 menuOption.menuRect =
event->rect();
125 menuOption.maxIconWidth = 0;
126 menuOption.tabWidth = 0;
128 QPainter painter(this->viewport());
129 m_ComboBox->style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOption, &painter,
this);
133 QListView::paintEvent(event);
138 int width = this->viewport()->width();
139 int height = this->contentsSize().height();
141 this->resizeContents(width, height);
143 QListView::resizeEvent(event);
148 QStyleOptionViewItem option = QListView::viewOptions();
149 option.showDecorationSelected =
true;
151 if (m_ComboBox != NULL)
152 option.font = m_ComboBox->font();
157 class PropertyEqualTo
163 return pair.second.GetPointer() == m_Property;
179 const QStyleOptionViewItem &option,
180 const QModelIndex &index)
const
182 QVariant data = index.data(Qt::EditRole);
188 QSpinBox *spinBox =
new QSpinBox(parent);
191 std::string name = this->GetPropertyName(index);
193 if (extensions != NULL && !name.empty() && extensions->
HasExtension(name))
198 if (extension.IsNotNull())
200 spinBox->setMinimum(extension->GetMinimum());
201 spinBox->setMaximum(extension->GetMaximum());
202 spinBox->setSingleStep(extension->GetSingleStep());
206 connect(spinBox, SIGNAL(editingFinished()),
this, SLOT(OnSpinBoxEditingFinished()));
211 if (data.type() == QVariant::Double ||
static_cast<QMetaType::Type
>(data.type()) == QMetaType::Float)
213 QDoubleSpinBox *spinBox =
new QDoubleSpinBox(parent);
216 std::string name = this->GetPropertyName(index);
218 if (extensions != NULL && !name.empty() && extensions->
HasExtension(name))
223 if (extension.IsNotNull())
225 spinBox->setMinimum(extension->GetMinimum());
226 spinBox->setMaximum(extension->GetMaximum());
227 spinBox->setSingleStep(extension->GetSingleStep());
228 spinBox->setDecimals(extension->GetDecimals());
233 spinBox->setSingleStep(0.1);
234 spinBox->setDecimals(4);
237 if (name ==
"opacity")
239 spinBox->setMinimum(0.0);
240 spinBox->setMaximum(1.0);
243 connect(spinBox, SIGNAL(editingFinished()),
this, SLOT(OnSpinBoxEditingFinished()));
250 QComboBox *comboBox =
new QComboBox(parent);
253 comboBox->addItems(data.toStringList());
255 connect(comboBox, SIGNAL(currentIndexChanged(
int)),
this, SLOT(OnComboBoxCurrentIndexChanged(
int)));
264 connect(colorWidget, SIGNAL(ColorPicked()),
this, SLOT(OnColorPicked()));
270 return QStyledItemDelegate::createEditor(parent, option, index);
273 std::string QmitkPropertyItemDelegate::GetPropertyName(
const QModelIndex &index)
const
281 mitk::PropertyList::PropertyMap::const_iterator it =
282 std::find_if(propertyMap->begin(), propertyMap->end(), PropertyEqualTo(property));
284 if (it != propertyMap->end())
291 void QmitkPropertyItemDelegate::OnComboBoxCurrentIndexChanged(
int)
293 QComboBox *comboBox = qobject_cast<QComboBox *>(sender());
295 emit commitData(comboBox);
296 emit closeEditor(comboBox);
299 void QmitkPropertyItemDelegate::OnSpinBoxEditingFinished()
301 QAbstractSpinBox *spinBox = qobject_cast<QAbstractSpinBox *>(sender());
303 emit commitData(spinBox);
304 emit closeEditor(spinBox);
307 void QmitkPropertyItemDelegate::OnColorPicked()
311 emit commitData(colorWidget);
312 emit closeEditor(colorWidget);
316 const QStyleOptionViewItem &option,
317 const QModelIndex &index)
const
319 QVariant data = index.data();
323 painter->fillRect(option.rect, data.value<QColor>());
327 QStyledItemDelegate::paint(painter, option, index);
332 QVariant data = index.data(Qt::EditRole);
339 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
340 comboBox->setCurrentIndex(comboBox->findText(index.data().toString()));
345 colorWidget->
SetColor(data.value<QColor>());
349 QStyledItemDelegate::setEditorData(editor, index);
355 QVariant data = index.data(Qt::EditRole);
362 QSpinBox *spinBox = qobject_cast<QSpinBox *>(editor);
363 model->setData(index, spinBox->value());
365 else if (data.type() == QVariant::Double)
367 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
368 model->setData(index, spinBox->value());
370 else if (static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
372 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
373 model->setData(index, static_cast<float>(spinBox->value()));
377 QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
378 model->setData(index, comboBox->currentText());
383 model->setData(index, colorWidget->
GetColor());
387 QStyledItemDelegate::setModelData(editor, model, index);
393 if (m_PropertyList.
GetPointer() != propertyList)
394 m_PropertyList = propertyList;
void resizeEvent(QResizeEvent *event) override
std::pair< std::string, BaseProperty::Pointer > PropertyMapElementType
mitk::IPropertyExtensions * GetPropertyService()
QmitkComboBoxListView(QComboBox *comboBox=NULL)
~QmitkPropertyItemDelegate()
void SetPropertyList(mitk::PropertyList *propertyList)
virtual PropertyExtension::Pointer GetExtension(const std::string &propertyName, const std::string &className="")=0
Get the extension of a specific property.
Key-value list holding instances of BaseProperty.
Interface of property extensions service.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
ObjectType * GetPointer() const
QStyleOptionViewItem viewOptions() const override
Abstract base class for properties.
std::map< std::string, BaseProperty::Pointer > PropertyMap
Property extension for mitk::IntProperty.
virtual bool HasExtension(const std::string &propertyName, const std::string &className="")=0
Check if a specific property has an extension.
QmitkPropertyItemDelegate(QObject *parent=NULL)
std::vector< std::string > StringList
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Property extension for mitk::FloatProperty.
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void paintEvent(QPaintEvent *event) override
void setEditorData(QWidget *editor, const QModelIndex &index) const override
const PropertyMap * GetMap() const