diff --git a/Modules/SegmentationUI/Qmitk/QmitkLabelColorItemDelegate.cpp b/Modules/SegmentationUI/Qmitk/QmitkLabelColorItemDelegate.cpp new file mode 100644 index 0000000000..e879f7d91b --- /dev/null +++ b/Modules/SegmentationUI/Qmitk/QmitkLabelColorItemDelegate.cpp @@ -0,0 +1,76 @@ +/*============================================================================ + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center (DKFZ) +All rights reserved. + +Use of this source code is governed by a 3-clause BSD license that can be +found in the LICENSE file. + +============================================================================*/ + +#include "QmitkLabelColorItemDelegate.h" + +#include +#include +#include + +QmitkLabelColorItemDelegate::QmitkLabelColorItemDelegate(QObject * /*parent*/) +{ +} + +void QmitkLabelColorItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + QVariant data = index.data(Qt::EditRole); + + if (data.canConvert()) + { + QColor color = data.value(); + + painter->fillRect(option.rect, color); + } + else + { + QStyledItemDelegate::paint(painter, option, index); + } +} + +bool QmitkLabelColorItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &, + const QModelIndex &index) +{ + Q_ASSERT(event); + Q_ASSERT(model); + + // make sure that the item is checkable + Qt::ItemFlags flags = model->flags(index); + if (!(flags & Qt::ItemIsEditable) || !(flags & Qt::ItemIsEnabled)) + { + return false; + } + + // make sure that we have the right event type + QMouseEvent* mouseEvent = dynamic_cast(event); + if (!mouseEvent) + { + return false; + } + else + { + if (mouseEvent->type() != QEvent::MouseButtonRelease || mouseEvent->button() != Qt::LeftButton) + { + return false; + } + } + + QColor oldcolor = index.data(Qt::EditRole).value(); + QColor newColor = QColorDialog::getColor(oldcolor, nullptr); + + if (newColor.isValid()) + { + return model->setData(index, QVariant(newColor), Qt::EditRole); + } + + return false; +}; diff --git a/Modules/SegmentationUI/Qmitk/QmitkLabelColorItemDelegate.h b/Modules/SegmentationUI/Qmitk/QmitkLabelColorItemDelegate.h new file mode 100644 index 0000000000..865251e0fd --- /dev/null +++ b/Modules/SegmentationUI/Qmitk/QmitkLabelColorItemDelegate.h @@ -0,0 +1,40 @@ +/*============================================================================ + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center (DKFZ) +All rights reserved. + +Use of this source code is governed by a 3-clause BSD license that can be +found in the LICENSE file. + +============================================================================*/ + +#ifndef QmitkLabelColorItemDelegate_h +#define QmitkLabelColorItemDelegate_h + + +#include + +#include "MitkSegmentationUIExports.h" + +/** \class QmitkLabelColorItemDelegate +\brief An item delegate for rendering and editing label color in a QMultiLabelSegmentationTreeView.*/ +class MITKSEGMENTATIONUI_EXPORT QmitkLabelColorItemDelegate : public QStyledItemDelegate +{ + Q_OBJECT + +public: + /// + /// Creates a new PropertyDelegate. + /// + explicit QmitkLabelColorItemDelegate(QObject *parent = nullptr); + + bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, + const QModelIndex &index) override; + + void paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const override; +}; + +#endif diff --git a/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.cpp b/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.cpp new file mode 100644 index 0000000000..29cce59748 --- /dev/null +++ b/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.cpp @@ -0,0 +1,87 @@ +/*============================================================================ + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center (DKFZ) +All rights reserved. + +Use of this source code is governed by a 3-clause BSD license that can be +found in the LICENSE file. + +============================================================================*/ + +#include "QmitkLabelToggleItemDelegate.h" + + + +#include +#include + +QmitkLabelToggleItemDelegate::QmitkLabelToggleItemDelegate(const QIcon& onIcon, const QIcon& offIcon, QObject * /*parent*/) : m_OnIcon(onIcon), m_OffIcon(offIcon) +{ +} + +void QmitkLabelToggleItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + QVariant data = index.data(Qt::EditRole); + + if (data.canConvert()) + { + //if (option.state & QStyle::State_Selected) + // painter->fillRect(option.rect, option.palette.highlight()); + + if (data.toBool()) + { + m_OnIcon.paint(painter, option.rect); + } + else + { + m_OffIcon.paint(painter, option.rect); + } + } + else + { + QStyledItemDelegate::paint(painter, option, index); + } +} + +bool QmitkLabelToggleItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &, + const QModelIndex &index) +{ + Q_ASSERT(event); + Q_ASSERT(model); + + // make sure that the item is checkable + Qt::ItemFlags flags = model->flags(index); + if (!(flags & Qt::ItemIsEditable) || !(flags & Qt::ItemIsEnabled)) + { + return false; + } + + // make sure that we have the right event type + QMouseEvent* mouseEvent = dynamic_cast(event); + if (!mouseEvent) + { + return false; + } + else + { + if (mouseEvent->type() != QEvent::MouseButtonRelease || mouseEvent->button() != Qt::LeftButton) + { + return false; + } + } + + auto visVar = index.data(Qt::EditRole); + if (!visVar.isValid()) + { + return model->setData(index, QVariant(true), Qt::EditRole); + } + else + { + return model->setData(index, QVariant(!(visVar.toBool())), Qt::EditRole); + } + + return false; +}; diff --git a/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.h b/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.h new file mode 100644 index 0000000000..eb53cfd5cf --- /dev/null +++ b/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.h @@ -0,0 +1,45 @@ +/*============================================================================ + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center (DKFZ) +All rights reserved. + +Use of this source code is governed by a 3-clause BSD license that can be +found in the LICENSE file. + +============================================================================*/ + +#ifndef QmitkLabelToggleItemDelegate_h +#define QmitkLabelToggleItemDelegate_h + + +#include +#include + +#include "MitkSegmentationUIExports.h" + +/** \class QmitkLabelToggleItemDelegate +\brief An item delegate for rendering and editing properties that can be toggled (e.g. visibility).*/ +class MITKSEGMENTATIONUI_EXPORT QmitkLabelToggleItemDelegate : public QStyledItemDelegate +{ + Q_OBJECT + +public: + /// + /// Creates a new PropertyDelegate. + /// + explicit QmitkLabelToggleItemDelegate(const QIcon& onIcon, const QIcon& offIcon, QObject* parent = nullptr); + + bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, + const QModelIndex& index) override; + + void paint(QPainter* painter, const QStyleOptionViewItem& option, + const QModelIndex& index) const override; + +protected: + QIcon m_OnIcon; + QIcon m_OffIcon; +}; + +#endif