diff --git a/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.cpp b/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.cpp index 29cce59748..b3ec0bdb96 100644 --- a/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.cpp +++ b/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.cpp @@ -1,87 +1,90 @@ /*============================================================================ 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 <QPainter> #include <QMouseEvent> -QmitkLabelToggleItemDelegate::QmitkLabelToggleItemDelegate(const QIcon& onIcon, const QIcon& offIcon, QObject * /*parent*/) : m_OnIcon(onIcon), m_OffIcon(offIcon) +QmitkLabelToggleItemDelegate::QmitkLabelToggleItemDelegate(const QIcon& onIcon, const QIcon& offIcon, QObject * parent) : QStyledItemDelegate(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<bool>()) { - //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); } } +QSize QmitkLabelToggleItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const +{ + auto defaultSize = QStyledItemDelegate::sizeHint(option, index); + return QSize(defaultSize.height(), defaultSize.height()); +} + 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<QMouseEvent*>(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 index eb53cfd5cf..a69287826d 100644 --- a/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.h +++ b/Modules/SegmentationUI/Qmitk/QmitkLabelToggleItemDelegate.h @@ -1,45 +1,47 @@ /*============================================================================ 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 <QStyledItemDelegate> #include <QIcon> #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; + QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override; + protected: QIcon m_OnIcon; QIcon m_OffIcon; }; #endif diff --git a/Modules/SegmentationUI/Qmitk/QmitkMultiLabelSegmentationInspector.cpp b/Modules/SegmentationUI/Qmitk/QmitkMultiLabelSegmentationInspector.cpp index 7d007f3e5a..6e7be2c9dd 100644 --- a/Modules/SegmentationUI/Qmitk/QmitkMultiLabelSegmentationInspector.cpp +++ b/Modules/SegmentationUI/Qmitk/QmitkMultiLabelSegmentationInspector.cpp @@ -1,135 +1,140 @@ /*============================================================================ 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 <QmitkMultiLabelSegmentationInspector.h> #include <QmitkMultiLabelSegmentationTreeModel.h> #include <QmitkLabelColorItemDelegate.h> #include <QmitkLabelToggleItemDelegate.h> #include <QmitkStyleManager.h> QmitkMultiLabelSegmentationInspector::QmitkMultiLabelSegmentationInspector(QWidget* parent/* = nullptr*/) : QWidget(parent) { m_Controls.setupUi(this); m_Model = new QmitkMultiLabelSegmentationTreeModel(this); m_Controls.view->setModel(m_Model); m_ColorItemDelegate = new QmitkLabelColorItemDelegate(this); auto visibleIcon = QmitkStyleManager::ThemeIcon(QLatin1String(":/Qmitk/visible.svg")); auto invisibleIcon = QmitkStyleManager::ThemeIcon(QLatin1String(":/Qmitk/invisible.svg")); m_VisibilityItemDelegate = new QmitkLabelToggleItemDelegate(visibleIcon, invisibleIcon, this); auto lockIcon = QmitkStyleManager::ThemeIcon(QLatin1String(":/Qmitk/lock.svg")); auto unlockIcon = QmitkStyleManager::ThemeIcon(QLatin1String(":/Qmitk/unlock.svg")); m_LockItemDelegate = new QmitkLabelToggleItemDelegate(lockIcon, unlockIcon, this); this->m_Controls.view->setItemDelegateForColumn(1, m_LockItemDelegate); this->m_Controls.view->setItemDelegateForColumn(2, m_ColorItemDelegate); this->m_Controls.view->setItemDelegateForColumn(3, m_VisibilityItemDelegate); + this->m_Controls.view->header()->setSectionResizeMode(0,QHeaderView::Stretch); + this->m_Controls.view->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents); + this->m_Controls.view->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents); + this->m_Controls.view->header()->setSectionResizeMode(3, QHeaderView::ResizeToContents); + connect(m_Model, &QAbstractItemModel::modelReset, this, &QmitkMultiLabelSegmentationInspector::OnModelReset); //connect(m_Controls.view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), SLOT(ChangeModelSelection(const QItemSelection&, const QItemSelection&))); } void QmitkMultiLabelSegmentationInspector::Initialize() { m_Model->SetSegmentation(m_Segmentation.Lock()); m_Controls.view->expandAll(); } void QmitkMultiLabelSegmentationInspector::SetSelectionMode(SelectionMode mode) { m_Controls.view->setSelectionMode(mode); } QmitkMultiLabelSegmentationInspector::SelectionMode QmitkMultiLabelSegmentationInspector::GetSelectionMode() const { return m_Controls.view->selectionMode(); } void QmitkMultiLabelSegmentationInspector::SetMultiLabelSegmentation(mitk::LabelSetImage* segmentation) { if (segmentation != m_Segmentation.Lock()) { m_Segmentation = segmentation; this->Initialize(); } } void QmitkMultiLabelSegmentationInspector::OnModelReset() { } bool EqualLabelSelections(const QmitkMultiLabelSegmentationInspector::LabelValueVectorType& selection1, const QmitkMultiLabelSegmentationInspector::LabelValueVectorType& selection2) { if (selection1.size() == selection2.size()) { // lambda to compare node pointer inside both lists auto lambda = [](mitk::LabelSetImage::LabelValueType lhs, mitk::LabelSetImage::LabelValueType rhs) { return lhs == rhs; }; return std::is_permutation(selection1.begin(), selection1.end(), selection2.begin(), selection2.end(), lambda); } return false; } void QmitkMultiLabelSegmentationInspector::SetSelectedLabels(LabelValueVectorType selectedLabels) { bool equal = EqualLabelSelections(this->GetSelectedLabels(), selectedLabels); if (equal) { return; } // create new selection by retrieving the corresponding indices of the labels QItemSelection newCurrentSelection; for (const auto& labelID : selectedLabels) { QModelIndexList matched = m_Model->match(m_Model->index(0, 0), QmitkMultiLabelSegmentationTreeModel::ItemModelRole::LabelValueRole, QVariant(labelID), 1, Qt::MatchRecursive); if (!matched.empty()) { newCurrentSelection.select(matched.front(), matched.front()); } } m_Controls.view->selectionModel()->select(newCurrentSelection, QItemSelectionModel::ClearAndSelect); } void QmitkMultiLabelSegmentationInspector::SetSelectedLabel(mitk::LabelSetImage::LabelValueType selectedLabel) { this->SetSelectedLabels({ selectedLabel }); } QmitkMultiLabelSegmentationInspector::LabelValueVectorType QmitkMultiLabelSegmentationInspector::GetSelectedLabels() const { LabelValueVectorType result; QModelIndexList selectedIndexes = m_Controls.view->selectionModel()->selectedIndexes(); for (const auto& index : qAsConst(selectedIndexes)) { QVariant qvariantDataNode = m_Model->data(index, QmitkMultiLabelSegmentationTreeModel::ItemModelRole::LabelValueRole); if (qvariantDataNode.canConvert<mitk::LabelSetImage::LabelValueType>()) { result.push_back(qvariantDataNode.value<mitk::LabelSetImage::LabelValueType>()); } } return result; } void QmitkMultiLabelSegmentationInspector::ChangeModelSelection(const QItemSelection& selected, const QItemSelection& deselected) { emit CurrentSelectionChanged(GetSelectedLabels()); } diff --git a/Modules/SegmentationUI/Qmitk/QmitkMultiLabelSegmentationInspector.ui b/Modules/SegmentationUI/Qmitk/QmitkMultiLabelSegmentationInspector.ui index e5dbe37bb2..8ba8f2588a 100644 --- a/Modules/SegmentationUI/Qmitk/QmitkMultiLabelSegmentationInspector.ui +++ b/Modules/SegmentationUI/Qmitk/QmitkMultiLabelSegmentationInspector.ui @@ -1,62 +1,74 @@ <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>QmitkMultiLabelSegmentationInspector</class> <widget class="QWidget" name="QmitkMultiLabelSegmentationInspector"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <property name="spacing"> <number>0</number> </property> <property name="leftMargin"> <number>0</number> </property> <property name="topMargin"> <number>0</number> </property> <property name="rightMargin"> <number>0</number> </property> <property name="bottomMargin"> <number>0</number> </property> <item> <widget class="QmitkMultiLabelSegmentationTreeView" name="view"> <property name="editTriggers"> <set>QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set> </property> <property name="showDropIndicator" stdset="0"> <bool>false</bool> </property> <property name="alternatingRowColors"> <bool>false</bool> </property> <property name="selectionMode"> <enum>QAbstractItemView::SingleSelection</enum> </property> <property name="selectionBehavior"> - <enum>QAbstractItemView::SelectItems</enum> + <enum>QAbstractItemView::SelectRows</enum> </property> + <attribute name="headerVisible"> + <bool>false</bool> + </attribute> + <attribute name="headerMinimumSectionSize"> + <number>10</number> + </attribute> + <attribute name="headerDefaultSectionSize"> + <number>50</number> + </attribute> + <attribute name="headerStretchLastSection"> + <bool>false</bool> + </attribute> </widget> </item> </layout> </widget> <customwidgets> <customwidget> <class>QmitkMultiLabelSegmentationTreeView</class> <extends>QTreeView</extends> <header>QmitkMultiLabelSegmentationTreeView.h</header> </customwidget> </customwidgets> <resources/> <connections/> </ui>