diff --git a/Modules/QtWidgets/include/QmitkMultiNodeSelectionWidget.h b/Modules/QtWidgets/include/QmitkMultiNodeSelectionWidget.h index 049f6bb643..54c9f87f0f 100644 --- a/Modules/QtWidgets/include/QmitkMultiNodeSelectionWidget.h +++ b/Modules/QtWidgets/include/QmitkMultiNodeSelectionWidget.h @@ -1,79 +1,82 @@ /*============================================================================ 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 QmitkMultiNodeSelectionWidget_h #define QmitkMultiNodeSelectionWidget_h #include #include #include #include #include #include #include class QmitkAbstractDataStorageModel; /** * @class QmitkMultiNodeSelectionWidget * @brief Widget that allows to perform and represents a multiple node selection. */ class MITKQTWIDGETS_EXPORT QmitkMultiNodeSelectionWidget : public QmitkAbstractNodeSelectionWidget { Q_OBJECT public: explicit QmitkMultiNodeSelectionWidget(QWidget* parent = nullptr); using NodeList = QmitkAbstractNodeSelectionWidget::NodeList; /** * @brief Helper function that is used to check the given selection for consistency. * Returning an empty string assumes that everything is alright and the selection * is valid. If the string is not empty, the content of the string will be used * as error message in the overlay to indicate the problem. */ using SelectionCheckFunctionType = std::function; /** * @brief A selection check function can be set. If set the widget uses this function to * check the made/set selection. If the selection is valid, everything is fine. * If selection is indicated as invalid, it will not be communicated by the widget * (no signal emission). */ void SetSelectionCheckFunction(const SelectionCheckFunctionType &checkFunction); + /** Returns if the current internal selection is violating the current check function, if set.*/ + bool CurrentSelectionViolatesCheckFunction() const; + public Q_SLOTS: void OnEditSelection(); protected Q_SLOTS: void OnClearSelection(const mitk::DataNode* node); protected: void changeEvent(QEvent *event) override; void UpdateInfo() override; void OnInternalSelectionChanged() override; bool AllowEmissionOfSelection(const NodeList& emissionCandidates) const override; QmitkSimpleTextOverlayWidget* m_Overlay; SelectionCheckFunctionType m_CheckFunction; mutable std::string m_CheckResponse; Ui_QmitkMultiNodeSelectionWidget m_Controls; }; #endif diff --git a/Modules/QtWidgets/src/QmitkMultiNodeSelectionWidget.cpp b/Modules/QtWidgets/src/QmitkMultiNodeSelectionWidget.cpp index 88bb1d389f..5bbf899831 100644 --- a/Modules/QtWidgets/src/QmitkMultiNodeSelectionWidget.cpp +++ b/Modules/QtWidgets/src/QmitkMultiNodeSelectionWidget.cpp @@ -1,159 +1,164 @@ /*============================================================================ 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 "QmitkMultiNodeSelectionWidget.h" #include #include #include #include QmitkMultiNodeSelectionWidget::QmitkMultiNodeSelectionWidget(QWidget* parent) : QmitkAbstractNodeSelectionWidget(parent) { m_Controls.setupUi(this); m_Overlay = new QmitkSimpleTextOverlayWidget(m_Controls.list); m_Overlay->setVisible(false); m_CheckFunction = [](const NodeList &) { return ""; }; this->OnInternalSelectionChanged(); this->UpdateInfo(); connect(m_Controls.btnChange, SIGNAL(clicked(bool)), this, SLOT(OnEditSelection())); } void QmitkMultiNodeSelectionWidget::SetSelectionCheckFunction(const SelectionCheckFunctionType &checkFunction) { m_CheckFunction = checkFunction; auto newEmission = this->CompileEmitSelection(); auto newCheckResponse = m_CheckFunction(newEmission); if (newCheckResponse.empty() && !m_CheckResponse.empty()) { this->EmitSelection(newEmission); } m_CheckResponse = newCheckResponse; this->UpdateInfo(); } void QmitkMultiNodeSelectionWidget::OnEditSelection() { QmitkNodeSelectionDialog* dialog = new QmitkNodeSelectionDialog(this, m_PopUpTitel, m_PopUpHint); dialog->SetDataStorage(m_DataStorage.Lock()); dialog->SetNodePredicate(m_NodePredicate); dialog->SetCurrentSelection(this->CompileEmitSelection()); dialog->SetSelectOnlyVisibleNodes(m_SelectOnlyVisibleNodes); dialog->SetSelectionMode(QAbstractItemView::MultiSelection); dialog->SetSelectionCheckFunction(m_CheckFunction); m_Controls.btnChange->setChecked(true); if (dialog->exec()) { this->HandleChangeOfInternalSelection(dialog->GetSelectedNodes()); } m_Controls.btnChange->setChecked(false); delete dialog; } void QmitkMultiNodeSelectionWidget::UpdateInfo() { if (!m_Controls.list->count()) { if (m_IsOptional) { if (this->isEnabled()) { m_Overlay->SetOverlayText(QStringLiteral("") + m_EmptyInfo + QStringLiteral("")); } else { m_Overlay->SetOverlayText(QStringLiteral("") + m_EmptyInfo + QStringLiteral("")); } } else { if (this->isEnabled()) { m_Overlay->SetOverlayText(QStringLiteral("") + m_InvalidInfo + QStringLiteral("")); } else { m_Overlay->SetOverlayText(QStringLiteral("") + m_InvalidInfo + QStringLiteral("")); } } } else { if (!m_CheckResponse.empty()) { m_Overlay->SetOverlayText(QString::fromStdString(m_CheckResponse)); } } m_Overlay->setVisible(m_Controls.list->count() == 0 || !m_CheckResponse.empty()); for (auto i = 0; i < m_Controls.list->count(); ++i) { auto item = m_Controls.list->item(i); auto widget = qobject_cast(m_Controls.list->itemWidget(item)); widget->SetClearAllowed(m_IsOptional || m_Controls.list->count() > 1); } } void QmitkMultiNodeSelectionWidget::OnInternalSelectionChanged() { m_Controls.list->clear(); auto currentSelection = this->GetCurrentInternalSelection(); for (auto& node : currentSelection) { if (m_NodePredicate.IsNull() || m_NodePredicate->CheckNode(node)) { QListWidgetItem *newItem = new QListWidgetItem; newItem->setSizeHint(QSize(0, 40)); QmitkNodeSelectionListItemWidget* widget = new QmitkNodeSelectionListItemWidget; widget->SetSelectedNode(node); widget->SetClearAllowed(m_IsOptional || currentSelection.size() > 1); connect(widget, &QmitkNodeSelectionListItemWidget::ClearSelection, this, &QmitkMultiNodeSelectionWidget::OnClearSelection); newItem->setData(Qt::UserRole, QVariant::fromValue(node)); m_Controls.list->addItem(newItem); m_Controls.list->setItemWidget(newItem, widget); } } } void QmitkMultiNodeSelectionWidget::OnClearSelection(const mitk::DataNode* node) { this->RemoveNodeFromSelection(node); } void QmitkMultiNodeSelectionWidget::changeEvent(QEvent *event) { if (event->type() == QEvent::EnabledChange) { this->UpdateInfo(); } QmitkAbstractNodeSelectionWidget::changeEvent(event); } bool QmitkMultiNodeSelectionWidget::AllowEmissionOfSelection(const NodeList& emissionCandidates) const { m_CheckResponse = m_CheckFunction(emissionCandidates); return m_CheckResponse.empty(); } + +bool QmitkMultiNodeSelectionWidget::CurrentSelectionViolatesCheckFunction() const +{ + return !m_CheckResponse.empty(); +}