diff --git a/Modules/QtWidgets/files.cmake b/Modules/QtWidgets/files.cmake index 970d05401a..73069607e5 100644 --- a/Modules/QtWidgets/files.cmake +++ b/Modules/QtWidgets/files.cmake @@ -1,135 +1,137 @@ file(GLOB_RECURSE H_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/include/*") set(CPP_FILES QmitkAbstractDataStorageModel.cpp QmitkAbstractMultiWidget.cpp + QmitkAbstractNodeSelectionWidget.cpp QmitkApplicationCursor.cpp QmitkDataStorageComboBox.cpp QmitkDataStorageDefaultListModel.cpp QmitkDataStorageHistoryModel.cpp QmitkDataStorageListModel.cpp QmitkDataStorageTableModel.cpp QmitkDataStorageSimpleTreeModel.cpp QmitkDataStorageTreeModel.cpp QmitkDataStorageTreeModelInternalItem.cpp QmitkDnDDataNodeWidget.cpp QmitkFileReaderOptionsDialog.cpp QmitkFileReaderWriterOptionsWidget.cpp QmitkFileWriterOptionsDialog.cpp QmitkInteractionSchemeToolBar.cpp QmitkIOUtil.cpp QmitkLevelWindowPresetDefinitionDialog.cpp QmitkLevelWindowRangeChangeDialog.cpp QmitkLevelWindowWidgetContextMenu.cpp QmitkLevelWindowWidget.cpp QmitkLineEditLevelWindowWidget.cpp QmitkMemoryUsageIndicatorView.cpp QmitkMouseModeSwitcher.cpp QmitkMimeTypes.cpp QmitkMultiWidgetConfigurationToolBar.cpp QmitkMultiWidgetLayoutManager.cpp QmitkMultiWidgetLayoutSelectionWidget.cpp QmitkNodeDescriptor.cpp QmitkColoredNodeDescriptor.cpp QmitkNodeDescriptorManager.cpp QmitkProgressBar.cpp QmitkPropertiesTableEditor.cpp QmitkPropertiesTableModel.cpp QmitkPropertyDelegate.cpp QmitkRegisterClasses.cpp QmitkRenderingManager.cpp QmitkRenderingManagerFactory.cpp QmitkRenderWindow.cpp QmitkRenderWindowMenu.cpp QmitkRenderWindowWidget.cpp QmitkServiceListWidget.cpp QmitkSliderLevelWindowWidget.cpp QmitkStdMultiWidget.cpp QmitkMxNMultiWidget.cpp QmitkDataStorageComboBoxWithSelectNone.cpp QmitkDataStorageFilterProxyModel.cpp QmitkPropertyItem.cpp QmitkPropertyItemDelegate.cpp QmitkPropertyItemModel.cpp QmitkStyleManager.cpp QmitkAbstractDataStorageInspector.cpp QmitkDataStorageFavoriteNodesInspector.cpp QmitkDataStorageListInspector.cpp QmitkDataStorageTreeInspector.cpp QmitkDataStorageSelectionHistoryInspector.cpp QmitkModelViewSelectionConnector.cpp mitkIDataStorageInspectorProvider.cpp mitkQtWidgetsActivator.cpp mitkDataStorageInspectorGenerator.cpp QmitkOverlayWidget.cpp QmitkNodeDetailsDialog.cpp ) set(MOC_H_FILES include/QmitkAbstractDataStorageModel.h include/QmitkAbstractMultiWidget.h + include/QmitkAbstractNodeSelectionWidget.h include/QmitkDataStorageComboBox.h include/QmitkDataStorageTableModel.h include/QmitkDataStorageTreeModel.h include/QmitkDataStorageSimpleTreeModel.h include/QmitkDataStorageDefaultListModel.h include/QmitkDnDDataNodeWidget.h include/QmitkFileReaderOptionsDialog.h include/QmitkFileReaderWriterOptionsWidget.h include/QmitkFileWriterOptionsDialog.h include/QmitkInteractionSchemeToolBar.h include/QmitkLevelWindowPresetDefinitionDialog.h include/QmitkLevelWindowRangeChangeDialog.h include/QmitkLevelWindowWidgetContextMenu.h include/QmitkLevelWindowWidget.h include/QmitkLineEditLevelWindowWidget.h include/QmitkMemoryUsageIndicatorView.h include/QmitkMouseModeSwitcher.h include/QmitkMultiWidgetConfigurationToolBar.h include/QmitkMultiWidgetLayoutManager.h include/QmitkMultiWidgetLayoutSelectionWidget.h include/QmitkNodeDescriptor.h include/QmitkColoredNodeDescriptor.h include/QmitkNodeDescriptorManager.h include/QmitkProgressBar.h include/QmitkPropertiesTableEditor.h include/QmitkPropertyDelegate.h include/QmitkRenderingManager.h include/QmitkRenderWindow.h include/QmitkRenderWindowMenu.h include/QmitkRenderWindowWidget.h include/QmitkServiceListWidget.h include/QmitkSliderLevelWindowWidget.h include/QmitkStdMultiWidget.h include/QmitkMxNMultiWidget.h include/QmitkDataStorageComboBoxWithSelectNone.h include/QmitkPropertyItemDelegate.h include/QmitkPropertyItemModel.h include/QmitkAbstractDataStorageInspector.h include/QmitkDataStorageFavoriteNodesInspector.h include/QmitkDataStorageListInspector.h include/QmitkDataStorageTreeInspector.h include/QmitkDataStorageHistoryModel.h include/QmitkDataStorageSelectionHistoryInspector.h include/QmitkModelViewSelectionConnector.h include/QmitkOverlayWidget.h include/QmitkNodeDetailsDialog.h ) set(UI_FILES src/QmitkFileReaderOptionsDialog.ui src/QmitkFileWriterOptionsDialog.ui src/QmitkLevelWindowPresetDefinition.ui src/QmitkLevelWindowWidget.ui src/QmitkLevelWindowRangeChange.ui src/QmitkMemoryUsageIndicator.ui src/QmitkMultiWidgetLayoutSelectionWidget.ui src/QmitkServiceListWidgetControls.ui src/QmitkDataStorageListInspector.ui src/QmitkDataStorageTreeInspector.ui src/QmitkDataStorageSelectionHistoryInspector.ui ) set(QRC_FILES resource/Qmitk.qrc ) diff --git a/Plugins/org.mitk.gui.qt.common/src/QmitkAbstractNodeSelectionWidget.h b/Modules/QtWidgets/include/QmitkAbstractNodeSelectionWidget.h similarity index 51% rename from Plugins/org.mitk.gui.qt.common/src/QmitkAbstractNodeSelectionWidget.h rename to Modules/QtWidgets/include/QmitkAbstractNodeSelectionWidget.h index 017cd17b6e..0890dbb9ea 100644 --- a/Plugins/org.mitk.gui.qt.common/src/QmitkAbstractNodeSelectionWidget.h +++ b/Modules/QtWidgets/include/QmitkAbstractNodeSelectionWidget.h @@ -1,166 +1,233 @@ /*============================================================================ 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 QMITK_ABSTRACT_NODE_SELECTION_WIDGET_H #define QMITK_ABSTRACT_NODE_SELECTION_WIDGET_H #include #include #include -#include "org_mitk_gui_qt_common_Export.h" +#include #include class QmitkAbstractDataStorageModel; class QAbstractItemVew; /** * \class QmitkAbstractNodeSelectionWidget * \brief Abstract base class for the selection of data from a data storage. */ -class MITK_QT_COMMON QmitkAbstractNodeSelectionWidget : public QWidget +class MITKQTWIDGETS_EXPORT QmitkAbstractNodeSelectionWidget : public QWidget { Q_OBJECT public: explicit QmitkAbstractNodeSelectionWidget(QWidget* parent = nullptr); - ~QmitkAbstractNodeSelectionWidget() override; + virtual ~QmitkAbstractNodeSelectionWidget() override; /** * @brief Sets the data storage that will be used /monitored by widget. * * @par dataStorage A pointer to the data storage to set. */ void SetDataStorage(mitk::DataStorage* dataStorage); /** * Sets the node predicate and updates the widget, according to the node predicate. * Implement OnNodePredicateChange() for custom actualization of a derived widget class. * * @par nodePredicate A pointer to node predicate. */ void SetNodePredicate(const mitk::NodePredicateBase* nodePredicate); const mitk::NodePredicateBase* GetNodePredicate() const; QString GetInvalidInfo() const; QString GetEmptyInfo() const; QString GetPopUpTitel() const; QString GetPopUpHint() const; bool GetSelectionIsOptional() const; bool GetSelectOnlyVisibleNodes() const; using NodeList = QList; + /** Returns the selected nodes, as emitted with CurrentSelectionChanged*/ + NodeList GetSelectedNodes() const; + Q_SIGNALS: /* * @brief A signal that will be emitted if the selected node has changed. * * @par nodes A list of data nodes that are newly selected. */ void CurrentSelectionChanged(QList nodes); public Q_SLOTS: /* * @brief Change the selection modus of the item view's selection model. * * If true, an incoming selection will be filtered (reduced) to only those nodes that are visible by the current view. * An outgoing selection can then at most contain the filtered nodes. * If false, the incoming non-visible selection will be stored and later added to the outgoing selection, * to include the original selection that could not be modified. * The part of the original selection, that is non-visible are the nodes that are not * * @par selectOnlyVisibleNodes The bool value to define the selection modus. */ - virtual void SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes); + void SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes); /* * @brief Transform a list of data nodes into a model selection and set this as a new selection of the * selection model of the private member item view. * * The function filters the given list of nodes according to the 'm_SelectOnlyVisibleNodes' member variable. If * necessary, the non-visible nodes are stored. This is done if 'm_SelectOnlyVisibleNodes' is false: In this case * the selection may be filtered and only a subset of the selected nodes may be visible and therefore (de-)selectable * in the data storage viewer. By storing the non-visible nodes it is possible to send the new, modified selection * but also include the selected nodes from the original selection that could not be modified (see 'SetSelectOnlyVisibleNodes'). * * @par nodes A list of data nodes that should be newly selected. */ - virtual void SetCurrentSelection(NodeList selectedNodes) = 0; + void SetCurrentSelection(NodeList selectedNodes); /** Set the info text that should be displayed if no (valid) node is selected, * but a selection is mandatory. * The string can contain HTML code. if wanted*/ void SetInvalidInfo(QString info); /** Set the info text that should be displayed if no (valid) node is selected, * but a selection is optional. * The string can contain HTML code. if wanted*/ void SetEmptyInfo(QString info); /** Set the caption of the popup that is displayed to alter the selection. * The string can contain HTML code. if wanted*/ void SetPopUpTitel(QString info); /** Set the hint text of the popup that is displayed to alter the selection. * The string can contain HTML code. if wanted*/ void SetPopUpHint(QString info); /** Set the widget into an optional mode. Optional means that the selection of no valid node does not mean an invalid state. Thus no node is a valid "node" selection too.*/ void SetSelectionIsOptional(bool isOptional); +protected Q_SLOTS: + /** Call to remove a node from the current selection. If the node is part of the current selection. + this will trigger ReviseSelectionChanged(), AllowEmissionOfSelection() and if there is really a change, + will also emit CurrentSelectionChanged.*/ + void RemoveNodeFromSelection(const mitk::DataNode* node); + protected: - /**Member is called if the display of the selected nodes should be updated.*/ + /**Method is called if the display of the selected nodes should be updated (e.g. because the selection changed)*/ virtual void UpdateInfo() = 0; - /**Member is called if the predicate has changed. Thus the selection might change to. The new (changed) predicate - is passed with the function call. It is the same like this->GetNodePredicate() called in the function call.*/ - virtual void OnNodePredicateChanged(const mitk::NodePredicateBase* newPredicate) = 0; - - /**Member is called if the data storage has changed. Thus the selection might change to.*/ - virtual void OnDataStorageChanged() = 0; - - /**Member is called when a node is added to the storage. Default implementation does nothing. - Derived widgets can override the method if they want to react on new nodes in the storage.*/ - virtual void NodeAddedToStorage(const mitk::DataNode* node); - - /**Member is called when a node is removed from the storage. The removed node is passed as - variable. Derived classes have to implement this method to react on the fact that there selection - might change, because the removed node is part of there selection. */ - virtual void NodeRemovedFromStorage(const mitk::DataNode* node) = 0; + /**Method is called if the predicate has changed, before the selection will be updated according to the new predicate. + The default implementation does nothing. + @remark If you are only interested to know when the selection has changed, overwrite OnInternalSelectionChange().*/ + virtual void OnNodePredicateChanged(); + + /**Method is called if the data storage has changed. The selection will be automatically be reseted afterwards. + The default implementation does nothing.*/ + virtual void OnDataStorageChanged(); + + /** This member function will called when ever the a new internal selection has been determined. This can be + used to update the state of internal widgets. The default implementation does nothing.*/ + virtual void OnInternalSelectionChanged(); + + /**Method is called when a node is added to the storage. Default implementation does nothing. + Derived widgets can override the method if they want to react on new nodes in the storage.*/ + virtual void OnNodeAddedToStorage(const mitk::DataNode* node); + + /**Method is called when a node is removed from the storage. The removed node is passed as + variable. This member is called directly bevore the node will be removed from the current selection if + he was a part. Default implementation does nothing. */ + virtual void OnNodeRemovedFromStorage(const mitk::DataNode* node); + + /**Method is called if the internal selection has changed. It will call the methods ReviseSelectionChanged(), + OnInternalSelectionChanged(), UpdateInfo() and EmitSelection() with the new emission candidates.*/ + void HandleChangeOfInternalSelection(NodeList newInternalSelection); + + /**Compiles the list of node that would be emitted. It always contains the internal selection. + Depending on SelectOnlyVisivleNodes it also adds all external select nodes that weren't visible + (failed the predicate).*/ + NodeList CompileEmitSelection() const; + + /** This member function is called if the internal selection is about to be changed by the + base implementation. + This is the slot where derived classes can revise and change the internal selection before widget updates, + signal emissions and other things are triggered. Default implementation does nothing, thus it keeps the + passed internal selection as compiled by the base implementation.*/ + virtual void ReviseSelectionChanged(const NodeList& oldInternalSelection, NodeList& newInternalSelection); + + /** This function will be called befor the CurrentSelectionChanged signal is emitted. The return value indicates + if the signal should be emitted (true = emission; false = no emission). The default implementation always + returns true. + @param emissionCandidates The nodes that will be emitted if the function returns true.*/ + virtual bool AllowEmissionOfSelection(const NodeList& emissionCandidates) const; + + /** Checks if the new emission differs from the last emission. If this is the case and AllowEmissionOfSelection() + returns true the new selection will be emited. */ + void EmitSelection(const NodeList& emissionCandidates); + + void SetCurrentInternalSelection(NodeList selectedNodes); + const NodeList& GetCurrentInternalSelection() const; + const NodeList& GetCurrentExternalSelection() const; mitk::WeakPointer m_DataStorage; mitk::NodePredicateBase::ConstPointer m_NodePredicate; QString m_InvalidInfo; QString m_EmptyInfo; QString m_PopUpTitel; QString m_PopUpHint; /** See documentation of SetSelectOnlyVisibleNodes for details*/ bool m_IsOptional; /** See documentation of SetSelectionIsOptional for details*/ bool m_SelectOnlyVisibleNodes; private: /** Helper triggered on the storage delete event */ void SetDataStorageDeleted(); + /**Member is called when a node is added to the storage. Default implementation does nothing. + Derived widgets can override the method if they want to react on new nodes in the storage.*/ + void NodeAddedToStorage(const mitk::DataNode* node); + + /**Member is called when a node is removed from the storage. The removed node is passed as + variable. Derived classes have to implement this method to react on the fact that there selection + might change, because the removed node is part of there selection. */ + void NodeRemovedFromStorage(const mitk::DataNode* node); + + void OnNodeModified(const itk::Object * /*caller*/, const itk::EventObject &); + + void AddNodeObserver(mitk::DataNode* node); + void RemoveNodeObserver(mitk::DataNode* node); + unsigned long m_DataStorageDeletedTag; + NodeList m_CurrentInternalSelection; + NodeList m_CurrentExternalSelection; + + NodeList m_LastEmission; + bool m_LastEmissionAllowance; + + using NodeObserverTagMapType = std::map; + NodeObserverTagMapType m_NodeObserverTags; }; #endif // QmitkAbstractNodeSelectionWidget_H diff --git a/Modules/QtWidgets/src/QmitkAbstractNodeSelectionWidget.cpp b/Modules/QtWidgets/src/QmitkAbstractNodeSelectionWidget.cpp new file mode 100644 index 0000000000..7b9545a373 --- /dev/null +++ b/Modules/QtWidgets/src/QmitkAbstractNodeSelectionWidget.cpp @@ -0,0 +1,394 @@ +/*============================================================================ + +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 "QmitkAbstractNodeSelectionWidget.h" +#include "QmitkModelViewSelectionConnector.h" + +QmitkAbstractNodeSelectionWidget::QmitkAbstractNodeSelectionWidget(QWidget* parent) : QWidget(parent), m_InvalidInfo("Error. Select data."), +m_EmptyInfo("Empty. Make a selection."), m_PopUpTitel("Select a data node"), m_PopUpHint(""), +m_IsOptional(false), m_SelectOnlyVisibleNodes(true), m_DataStorageDeletedTag(0), m_LastEmissionAllowance(true) +{ +} + +QmitkAbstractNodeSelectionWidget::~QmitkAbstractNodeSelectionWidget() +{ + auto dataStorage = m_DataStorage.Lock(); + if (dataStorage.IsNotNull()) + { + // remove Listener for the data storage itself + dataStorage->RemoveObserver(m_DataStorageDeletedTag); + + // remove "add node listener" from data storage + dataStorage->AddNodeEvent.RemoveListener( + mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeAddedToStorage)); + + // remove "remove node listener" from data storage + dataStorage->RemoveNodeEvent.RemoveListener( + mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeRemovedFromStorage)); + } + + for (auto& node : m_CurrentInternalSelection) + { + this->RemoveNodeObserver(node); + } +} + +QmitkAbstractNodeSelectionWidget::NodeList QmitkAbstractNodeSelectionWidget::GetSelectedNodes() const +{ + return this->CompileEmitSelection(); +} + +void QmitkAbstractNodeSelectionWidget::SetDataStorage(mitk::DataStorage* dataStorage) +{ + if (m_DataStorage == dataStorage) + { + return; + } + + auto oldStorage = m_DataStorage.Lock(); + if (oldStorage.IsNotNull()) + { + // remove Listener for the data storage itself + oldStorage->RemoveObserver(m_DataStorageDeletedTag); + + // remove "add node listener" from old data storage + oldStorage->AddNodeEvent.RemoveListener( + mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeAddedToStorage)); + + // remove "remove node listener" from old data storage + oldStorage->RemoveNodeEvent.RemoveListener( + mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeRemovedFromStorage)); + } + + m_DataStorage = dataStorage; + + auto newStorage = m_DataStorage.Lock(); + + if (newStorage.IsNotNull()) + { + // add Listener for the data storage itself + auto command = itk::SimpleMemberCommand::New(); + command->SetCallbackFunction(this, &QmitkAbstractNodeSelectionWidget::SetDataStorageDeleted); + m_DataStorageDeletedTag = newStorage->AddObserver(itk::DeleteEvent(), command); + + // add "add node listener" for new data storage + newStorage->AddNodeEvent.AddListener( + mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeAddedToStorage)); + + // add remove node listener for new data storage + newStorage->RemoveNodeEvent.AddListener( + mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeRemovedFromStorage)); + } + + this->OnDataStorageChanged(); + + this->HandleChangeOfInternalSelection({}); +} + +void QmitkAbstractNodeSelectionWidget::SetNodePredicate(const mitk::NodePredicateBase* nodePredicate) +{ + if (m_NodePredicate != nodePredicate) + { + m_NodePredicate = nodePredicate; + + this->OnNodePredicateChanged(); + + NodeList newInternalNodes; + + for (auto& node : m_CurrentInternalSelection) + { + if (m_NodePredicate.IsNull() || m_NodePredicate->CheckNode(node)) + { + newInternalNodes.append(node); + } + } + + this->HandleChangeOfInternalSelection(newInternalNodes); + } +} + +void QmitkAbstractNodeSelectionWidget::HandleChangeOfInternalSelection(NodeList newInternalSelection) +{ + this->ReviseSelectionChanged(m_CurrentInternalSelection, newInternalSelection); + + this->SetCurrentInternalSelection(newInternalSelection); + + this->OnInternalSelectionChanged(); + + auto newEmission = this->CompileEmitSelection(); + + this->EmitSelection(newEmission); + + this->UpdateInfo(); +} + +void QmitkAbstractNodeSelectionWidget::SetCurrentSelection(NodeList selectedNodes) +{ + m_CurrentExternalSelection = selectedNodes; + + auto dataStorage = m_DataStorage.Lock(); + NodeList newInternalSelection; + for (auto node : selectedNodes) + { + if (dataStorage.IsNotNull() && dataStorage->Exists(node) && (m_NodePredicate.IsNull() || m_NodePredicate->CheckNode(node))) + { + newInternalSelection.append(node); + } + } + + this->HandleChangeOfInternalSelection(newInternalSelection); +}; + +const mitk::NodePredicateBase* QmitkAbstractNodeSelectionWidget::GetNodePredicate() const +{ + return m_NodePredicate; +} + +QString QmitkAbstractNodeSelectionWidget::GetInvalidInfo() const +{ + return m_InvalidInfo; +} + +QString QmitkAbstractNodeSelectionWidget::GetEmptyInfo() const +{ + return m_EmptyInfo; +} + +QString QmitkAbstractNodeSelectionWidget::GetPopUpTitel() const +{ + return m_PopUpTitel; +} + +QString QmitkAbstractNodeSelectionWidget::GetPopUpHint() const +{ + return m_PopUpHint; +} + +bool QmitkAbstractNodeSelectionWidget::GetSelectionIsOptional() const +{ + return m_IsOptional; +} + +bool QmitkAbstractNodeSelectionWidget::GetSelectOnlyVisibleNodes() const +{ + return m_SelectOnlyVisibleNodes; +} + +void QmitkAbstractNodeSelectionWidget::SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes) +{ + if (m_SelectOnlyVisibleNodes != selectOnlyVisibleNodes) + { + m_SelectOnlyVisibleNodes = selectOnlyVisibleNodes; + + auto newEmission = this->CompileEmitSelection(); + + this->EmitSelection(newEmission); + } +} + +void QmitkAbstractNodeSelectionWidget::SetInvalidInfo(QString info) +{ + m_InvalidInfo = info; + this->UpdateInfo(); +} + +void QmitkAbstractNodeSelectionWidget::SetEmptyInfo(QString info) +{ + m_EmptyInfo = info; + this->UpdateInfo(); +} + +void QmitkAbstractNodeSelectionWidget::SetPopUpTitel(QString info) +{ + m_PopUpTitel = info; +} + +void QmitkAbstractNodeSelectionWidget::SetPopUpHint(QString info) +{ + m_PopUpHint = info; +} + +void QmitkAbstractNodeSelectionWidget::SetSelectionIsOptional(bool isOptional) +{ + m_IsOptional = isOptional; + this->UpdateInfo(); +} + +void QmitkAbstractNodeSelectionWidget::SetDataStorageDeleted() +{ + this->OnDataStorageChanged(); + this->HandleChangeOfInternalSelection({}); +} + +void QmitkAbstractNodeSelectionWidget::ReviseSelectionChanged(const NodeList& /*oldInternalSelection*/, NodeList& /*newInternalSelection*/) +{ +} + +bool QmitkAbstractNodeSelectionWidget::AllowEmissionOfSelection(const NodeList& emissionCandidates) const +{ + return true; +} + +void QmitkAbstractNodeSelectionWidget::EmitSelection(const NodeList& emissionCandidates) +{ + m_LastEmissionAllowance = this->AllowEmissionOfSelection(emissionCandidates); + if (m_LastEmissionAllowance && !EqualNodeSelections(m_LastEmission, emissionCandidates)) + { + emit CurrentSelectionChanged(emissionCandidates); + m_LastEmission = emissionCandidates; + } +} + +void QmitkAbstractNodeSelectionWidget::SetCurrentInternalSelection(NodeList selectedNodes) +{ + for (auto& node : m_CurrentInternalSelection) + { + this->RemoveNodeObserver(node); + } + + m_CurrentInternalSelection = selectedNodes; + + for (auto& node : m_CurrentInternalSelection) + { + this->AddNodeObserver(node); + } +} + +const QmitkAbstractNodeSelectionWidget::NodeList& QmitkAbstractNodeSelectionWidget::GetCurrentInternalSelection() const +{ + return m_CurrentInternalSelection; +} + +const QmitkAbstractNodeSelectionWidget::NodeList& QmitkAbstractNodeSelectionWidget::GetCurrentExternalSelection() const +{ + return m_CurrentExternalSelection; +} + +void QmitkAbstractNodeSelectionWidget::OnNodePredicateChanged() +{ +} + +void QmitkAbstractNodeSelectionWidget::OnDataStorageChanged() +{ +} + +void QmitkAbstractNodeSelectionWidget::OnInternalSelectionChanged() +{ +} + +void QmitkAbstractNodeSelectionWidget::NodeAddedToStorage(const mitk::DataNode* node) +{ + this->OnNodeAddedToStorage(node); +} + +void QmitkAbstractNodeSelectionWidget::OnNodeAddedToStorage(const mitk::DataNode* /*node*/) +{ +} + +void QmitkAbstractNodeSelectionWidget::NodeRemovedFromStorage(const mitk::DataNode* node) +{ + this->OnNodeRemovedFromStorage(node); + this->RemoveNodeFromSelection(node); +} + +void QmitkAbstractNodeSelectionWidget::OnNodeRemovedFromStorage(const mitk::DataNode* /*node*/) +{ +} + +QmitkAbstractNodeSelectionWidget::NodeList QmitkAbstractNodeSelectionWidget::CompileEmitSelection() const +{ + NodeList result = m_CurrentInternalSelection; + + if (!m_SelectOnlyVisibleNodes) + { + for (auto node : m_CurrentExternalSelection) + { + if (!result.contains(node) && m_NodePredicate.IsNotNull() && !m_NodePredicate->CheckNode(node)) + { + result.append(node); + } + } + } + + return result; +} + +void QmitkAbstractNodeSelectionWidget::RemoveNodeFromSelection(const mitk::DataNode* node) +{ + auto newSelection = m_CurrentInternalSelection; + + auto finding = std::find(std::begin(newSelection), std::end(newSelection), node); + + if (finding != std::end(newSelection)) + { + newSelection.erase(finding); + this->HandleChangeOfInternalSelection(newSelection); + } +} + +void QmitkAbstractNodeSelectionWidget::OnNodeModified(const itk::Object * caller, const itk::EventObject & event) +{ + if (itk::ModifiedEvent().CheckEvent(&event)) + { + auto node = dynamic_cast(caller); + + if (node) + { + if (m_NodePredicate.IsNotNull() && !m_NodePredicate->CheckNode(node)) + { + this->RemoveNodeFromSelection(node); + } + else + { + auto oldAllowance = m_LastEmissionAllowance; + auto newEmission = this->CompileEmitSelection(); + auto nonConstNode = const_cast(node); + if (newEmission.contains(nonConstNode) && (oldAllowance != this->AllowEmissionOfSelection(newEmission))) + { + this->EmitSelection(newEmission); + this->UpdateInfo(); + } + } + } + } +} + +void QmitkAbstractNodeSelectionWidget::AddNodeObserver(mitk::DataNode* node) +{ + if (node) + { + auto modifiedCommand = itk::MemberCommand::New(); + modifiedCommand->SetCallbackFunction(this, &QmitkAbstractNodeSelectionWidget::OnNodeModified); + + auto nodeModifiedObserverTag = node->AddObserver(itk::ModifiedEvent(), modifiedCommand); + + m_NodeObserverTags.insert(std::make_pair(node, nodeModifiedObserverTag)); + } +} + +void QmitkAbstractNodeSelectionWidget::RemoveNodeObserver(mitk::DataNode* node) +{ + if (node) + { + auto finding = m_NodeObserverTags.find(node); + if (finding != std::end(m_NodeObserverTags)) + { + node->RemoveObserver(finding->second); + } + else + { + MITK_ERROR << "Selection widget is in a wrong state. A node should be removed from the internal selection but seems to have no observer. Node:" << node; + } + m_NodeObserverTags.erase(node); + } +} diff --git a/Plugins/org.mitk.gui.qt.common/files.cmake b/Plugins/org.mitk.gui.qt.common/files.cmake index 0bde8b2087..b9621a8d79 100755 --- a/Plugins/org.mitk.gui.qt.common/files.cmake +++ b/Plugins/org.mitk.gui.qt.common/files.cmake @@ -1,71 +1,69 @@ set(SRC_CPP_FILES QmitkAbstractMultiWidgetEditor.cpp QmitkAbstractRenderEditor.cpp QmitkAbstractView.cpp QmitkDataNodeSelectionProvider.cpp QmitkDnDFrameWidget.cpp QmitkSelectionServiceConnector.cpp QmitkSliceNavigationListener.cpp QmitkSingleNodeSelectionWidget.cpp QmitkNodeSelectionDialog.cpp - QmitkAbstractNodeSelectionWidget.cpp QmitkMultiNodeSelectionWidget.cpp QmitkMultiWidgetDecorationManager.cpp QmitkNodeSelectionPreferenceHelper.cpp QmitkNodeSelectionButton.cpp QmitkSimpleTextOverlayWidget.cpp ) set(INTERNAL_CPP_FILES QmitkCommonActivator.cpp QmitkDataNodeItemModel.cpp QmitkDataNodeSelection.cpp QmitkViewCoordinator.cpp QmitkNodeSelectionConstants.cpp QmitkNodeSelectionPreferencePage.cpp QmitkNodeSelectionListItemWidget.cpp ) set(UI_FILES src/QmitkSingleNodeSelectionWidget.ui src/QmitkMultiNodeSelectionWidget.ui src/QmitkNodeSelectionDialog.ui src/internal/QmitkNodeSelectionPreferencePage.ui src/internal/QmitkNodeSelectionListItemWidget.ui ) set(MOC_H_FILES src/QmitkAbstractMultiWidgetEditor.h src/QmitkAbstractRenderEditor.h src/QmitkDnDFrameWidget.h src/QmitkSelectionServiceConnector.h src/QmitkSliceNavigationListener.h src/QmitkSingleNodeSelectionWidget.h src/QmitkNodeSelectionDialog.h - src/QmitkAbstractNodeSelectionWidget.h src/QmitkMultiNodeSelectionWidget.h src/QmitkNodeSelectionButton.h src/QmitkSimpleTextOverlayWidget.h src/internal/QmitkCommonActivator.h src/internal/QmitkNodeSelectionPreferencePage.h src/internal/QmitkNodeSelectionListItemWidget.h ) set(CACHED_RESOURCE_FILES plugin.xml resources/times.svg ) set(QRC_FILES resources/common.qrc ) set(CPP_FILES ) foreach(file ${SRC_CPP_FILES}) set(CPP_FILES ${CPP_FILES} src/${file}) endforeach(file ${SRC_CPP_FILES}) foreach(file ${INTERNAL_CPP_FILES}) set(CPP_FILES ${CPP_FILES} src/internal/${file}) endforeach(file ${INTERNAL_CPP_FILES}) diff --git a/Plugins/org.mitk.gui.qt.common/src/QmitkAbstractNodeSelectionWidget.cpp b/Plugins/org.mitk.gui.qt.common/src/QmitkAbstractNodeSelectionWidget.cpp deleted file mode 100644 index 94a53a1194..0000000000 --- a/Plugins/org.mitk.gui.qt.common/src/QmitkAbstractNodeSelectionWidget.cpp +++ /dev/null @@ -1,186 +0,0 @@ -/*============================================================================ - -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 "QmitkAbstractNodeSelectionWidget.h" - -QmitkAbstractNodeSelectionWidget::QmitkAbstractNodeSelectionWidget(QWidget* parent) : QWidget(parent), m_InvalidInfo("Error. Select data."), -m_EmptyInfo("Empty. Make a selection."), m_PopUpTitel("Select a data node"), m_PopUpHint(""), -m_IsOptional(false), m_SelectOnlyVisibleNodes(true) -{ -} - -QmitkAbstractNodeSelectionWidget::~QmitkAbstractNodeSelectionWidget() -{ - if (!m_DataStorage.IsExpired()) - { - auto dataStorage = m_DataStorage.Lock(); - if (dataStorage.IsNotNull()) - { - // remove Listener for the data storage itself - dataStorage->RemoveObserver(m_DataStorageDeletedTag); - - // remove "add node listener" from data storage - dataStorage->AddNodeEvent.RemoveListener( - mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeAddedToStorage)); - - // remove "remove node listener" from data storage - dataStorage->RemoveNodeEvent.RemoveListener( - mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeRemovedFromStorage)); - } - } -} - - -void QmitkAbstractNodeSelectionWidget::SetDataStorage(mitk::DataStorage* dataStorage) -{ - if (m_DataStorage == dataStorage) - { - return; - } - - if (!m_DataStorage.IsExpired()) - { - auto oldStorage = m_DataStorage.Lock(); - - if (oldStorage.IsNotNull()) - { - // remove Listener for the data storage itself - oldStorage->RemoveObserver(m_DataStorageDeletedTag); - - // remove "add node listener" from old data storage - oldStorage->AddNodeEvent.RemoveListener( - mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeAddedToStorage)); - - // remove "remove node listener" from old data storage - oldStorage->RemoveNodeEvent.RemoveListener( - mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeRemovedFromStorage)); - } - } - - m_DataStorage = dataStorage; - - if (!m_DataStorage.IsExpired()) - { - auto newStorage = m_DataStorage.Lock(); - - if (newStorage.IsNotNull()) - { - // add Listener for the data storage itself - auto command = itk::SimpleMemberCommand::New(); - command->SetCallbackFunction(this, &QmitkAbstractNodeSelectionWidget::SetDataStorageDeleted); - m_DataStorageDeletedTag = newStorage->AddObserver(itk::DeleteEvent(), command); - - // add "add node listener" for new data storage - newStorage->AddNodeEvent.AddListener( - mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeAddedToStorage)); - - // add remove node listener for new data storage - newStorage->RemoveNodeEvent.AddListener( - mitk::MessageDelegate1(this, &QmitkAbstractNodeSelectionWidget::NodeRemovedFromStorage)); - } - } - - // update model if the data storage has been changed - m_DataStorage = dataStorage; - this->OnDataStorageChanged(); - this->UpdateInfo(); -} - -void QmitkAbstractNodeSelectionWidget::SetNodePredicate(const mitk::NodePredicateBase* nodePredicate) -{ - if (m_NodePredicate != nodePredicate) - { - m_NodePredicate = nodePredicate; - - this->OnNodePredicateChanged(nodePredicate); - this->UpdateInfo(); - } -} - -const mitk::NodePredicateBase* QmitkAbstractNodeSelectionWidget::GetNodePredicate() const -{ - return m_NodePredicate; -} - -QString QmitkAbstractNodeSelectionWidget::GetInvalidInfo() const -{ - return m_InvalidInfo; -} - -QString QmitkAbstractNodeSelectionWidget::GetEmptyInfo() const -{ - return m_EmptyInfo; -} - -QString QmitkAbstractNodeSelectionWidget::GetPopUpTitel() const -{ - return m_PopUpTitel; -} - -QString QmitkAbstractNodeSelectionWidget::GetPopUpHint() const -{ - return m_PopUpHint; -} - -bool QmitkAbstractNodeSelectionWidget::GetSelectionIsOptional() const -{ - return m_IsOptional; -} - -bool QmitkAbstractNodeSelectionWidget::GetSelectOnlyVisibleNodes() const -{ - return m_SelectOnlyVisibleNodes; -} - -void QmitkAbstractNodeSelectionWidget::SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes) -{ - m_SelectOnlyVisibleNodes = selectOnlyVisibleNodes; -} - -void QmitkAbstractNodeSelectionWidget::SetInvalidInfo(QString info) -{ - m_InvalidInfo = info; - this->UpdateInfo(); -} - -void QmitkAbstractNodeSelectionWidget::SetEmptyInfo(QString info) -{ - m_EmptyInfo = info; - this->UpdateInfo(); -} - -void QmitkAbstractNodeSelectionWidget::SetPopUpTitel(QString info) -{ - m_PopUpTitel = info; -} - -void QmitkAbstractNodeSelectionWidget::SetPopUpHint(QString info) -{ - m_PopUpHint = info; -} - -void QmitkAbstractNodeSelectionWidget::SetSelectionIsOptional(bool isOptional) -{ - m_IsOptional = isOptional; - this->UpdateInfo(); -} - -void QmitkAbstractNodeSelectionWidget::SetDataStorageDeleted() -{ - this->SetDataStorage(nullptr); -} - -void QmitkAbstractNodeSelectionWidget::NodeAddedToStorage(const mitk::DataNode* /*node*/) -{ - //default implementation does nothing; -} diff --git a/Plugins/org.mitk.gui.qt.common/src/QmitkMultiNodeSelectionWidget.cpp b/Plugins/org.mitk.gui.qt.common/src/QmitkMultiNodeSelectionWidget.cpp index 5700679abe..422ae1ae2e 100644 --- a/Plugins/org.mitk.gui.qt.common/src/QmitkMultiNodeSelectionWidget.cpp +++ b/Plugins/org.mitk.gui.qt.common/src/QmitkMultiNodeSelectionWidget.cpp @@ -1,273 +1,158 @@ /*============================================================================ 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 "QmitkNodeSelectionDialog.h" #include "QmitkCustomVariants.h" #include "internal/QmitkNodeSelectionListItemWidget.h" 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->UpdateList(); + this->OnInternalSelectionChanged(); this->UpdateInfo(); connect(m_Controls.btnChange, SIGNAL(clicked(bool)), this, SLOT(OnEditSelection())); } -QmitkMultiNodeSelectionWidget::NodeList QmitkMultiNodeSelectionWidget::CompileEmitSelection() const -{ - NodeList result; - - for (int i = 0; i < m_Controls.list->count(); ++i) - { - QListWidgetItem* item = m_Controls.list->item(i); - - auto node = item->data(Qt::UserRole).value(); - result.append(node); - } - - - if (!m_SelectOnlyVisibleNodes) - { - for (auto node : m_CurrentSelection) - { - if (!result.contains(node)) - { - result.append(node); - } - } - } - - return result; -} - -void QmitkMultiNodeSelectionWidget::OnNodePredicateChanged(const mitk::NodePredicateBase* /*newPredicate*/) -{ - this->UpdateInfo(); - this->UpdateList(); -}; - -void QmitkMultiNodeSelectionWidget::OnDataStorageChanged() -{ - this->UpdateInfo(); - this->UpdateList(); -}; - -QmitkMultiNodeSelectionWidget::NodeList QmitkMultiNodeSelectionWidget::GetSelectedNodes() const -{ - return m_CurrentSelection; -}; - void QmitkMultiNodeSelectionWidget::SetSelectionCheckFunction(const SelectionCheckFunctionType &checkFunction) { m_CheckFunction = checkFunction; auto newEmission = this->CompileEmitSelection(); auto newCheckResponse = m_CheckFunction(newEmission); if (newCheckResponse.empty() && !m_CheckResponse.empty()) { - emit CurrentSelectionChanged(newEmission); + 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); m_Controls.btnChange->setChecked(true); if (dialog->exec()) { - auto lastEmission = this->CompileEmitSelection(); - - m_CurrentSelection = dialog->GetSelectedNodes(); - this->UpdateList(); - - auto newEmission = this->CompileEmitSelection(); - - m_CheckResponse = m_CheckFunction(newEmission); - this->UpdateInfo(); - - if (!EqualNodeSelections(lastEmission, newEmission)) - { - if (m_CheckResponse.empty()) - { - emit CurrentSelectionChanged(newEmission); - } - } + 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_CurrentSelection.size() > 1); + widget->SetClearAllowed(m_IsOptional || m_Controls.list->count() > 1); } -}; +} -void QmitkMultiNodeSelectionWidget::UpdateList() +void QmitkMultiNodeSelectionWidget::OnInternalSelectionChanged() { m_Controls.list->clear(); - - for (auto node : m_CurrentSelection) + 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 || m_CurrentSelection.size() > 1); + 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::SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes) -{ - auto lastEmission = this->CompileEmitSelection(); - - m_SelectOnlyVisibleNodes = selectOnlyVisibleNodes; - - auto newEmission = this->CompileEmitSelection(); - - if (!EqualNodeSelections(lastEmission, newEmission)) - { - m_CheckResponse = m_CheckFunction(newEmission); - if (m_CheckResponse.empty()) - { - emit CurrentSelectionChanged(newEmission); - } - this->UpdateList(); - this->UpdateInfo(); - } -}; - -void QmitkMultiNodeSelectionWidget::SetCurrentSelection(NodeList selectedNodes) -{ - auto lastEmission = this->CompileEmitSelection(); - - m_CurrentSelection = selectedNodes; - this->UpdateList(); - - auto newEmission = this->CompileEmitSelection(); - - if (!EqualNodeSelections(lastEmission, newEmission)) - { - m_CheckResponse = m_CheckFunction(newEmission); - if (m_CheckResponse.empty()) - { - emit CurrentSelectionChanged(newEmission); - } - this->UpdateInfo(); - } -}; +} void QmitkMultiNodeSelectionWidget::OnClearSelection(const mitk::DataNode* node) { - auto finding = std::find(std::begin(m_CurrentSelection), std::end(m_CurrentSelection), node); - m_CurrentSelection.erase(finding); - - this->UpdateList(); - auto newEmission = this->CompileEmitSelection(); - m_CheckResponse = m_CheckFunction(newEmission); - - if (m_CheckResponse.empty()) - { - emit CurrentSelectionChanged(newEmission); - } - this->UpdateInfo(); -}; - -void QmitkMultiNodeSelectionWidget::NodeRemovedFromStorage(const mitk::DataNode* node) -{ - auto finding = std::find(std::begin(m_CurrentSelection), std::end(m_CurrentSelection), node); - - if (finding != std::end(m_CurrentSelection)) - { - this->OnClearSelection(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(); +} diff --git a/Plugins/org.mitk.gui.qt.common/src/QmitkMultiNodeSelectionWidget.h b/Plugins/org.mitk.gui.qt.common/src/QmitkMultiNodeSelectionWidget.h index f4e411234a..747c9c1951 100644 --- a/Plugins/org.mitk.gui.qt.common/src/QmitkMultiNodeSelectionWidget.h +++ b/Plugins/org.mitk.gui.qt.common/src/QmitkMultiNodeSelectionWidget.h @@ -1,87 +1,77 @@ /*============================================================================ 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 QMITK_MULTI_NODE_SELECTION_WIDGET_H #define QMITK_MULTI_NODE_SELECTION_WIDGET_H #include #include #include #include "QmitkSimpleTextOverlayWidget.h" #include "org_mitk_gui_qt_common_Export.h" #include "ui_QmitkMultiNodeSelectionWidget.h" #include class QmitkAbstractDataStorageModel; class QAbstractItemVew; /** * \class QmitkMultiNodeSelectionWidget * \brief Widget that allows to perform and represents a multiple node selection. */ class MITK_QT_COMMON QmitkMultiNodeSelectionWidget : public QmitkAbstractNodeSelectionWidget { Q_OBJECT public: explicit QmitkMultiNodeSelectionWidget(QWidget* parent = nullptr); using NodeList = QmitkAbstractNodeSelectionWidget::NodeList; - NodeList GetSelectedNodes() const; - /**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; /**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); public Q_SLOTS: - void SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes) override; - void SetCurrentSelection(NodeList selectedNodes) override; void OnEditSelection(); protected Q_SLOTS: void OnClearSelection(const mitk::DataNode* node); protected: - NodeList CompileEmitSelection() const; - void changeEvent(QEvent *event) override; void UpdateInfo() override; - virtual void UpdateList(); - - void OnNodePredicateChanged(const mitk::NodePredicateBase* newPredicate) override; - void OnDataStorageChanged() override; - void NodeRemovedFromStorage(const mitk::DataNode* node) override; + void OnInternalSelectionChanged() override; - NodeList m_CurrentSelection; + bool AllowEmissionOfSelection(const NodeList& emissionCandidates) const override; QmitkSimpleTextOverlayWidget* m_Overlay; SelectionCheckFunctionType m_CheckFunction; - std::string m_CheckResponse; + mutable std::string m_CheckResponse; Ui_QmitkMultiNodeSelectionWidget m_Controls; }; #endif // QmitkMultiNodeSelectionWidget_H diff --git a/Plugins/org.mitk.gui.qt.common/src/QmitkSingleNodeSelectionWidget.cpp b/Plugins/org.mitk.gui.qt.common/src/QmitkSingleNodeSelectionWidget.cpp index fbd19ddff3..6275c5b19d 100644 --- a/Plugins/org.mitk.gui.qt.common/src/QmitkSingleNodeSelectionWidget.cpp +++ b/Plugins/org.mitk.gui.qt.common/src/QmitkSingleNodeSelectionWidget.cpp @@ -1,356 +1,230 @@ /*============================================================================ 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 "QmitkSingleNodeSelectionWidget.h" #include #include "mitkNodePredicateFunction.h" #include "mitkNodePredicateAnd.h" #include #include "QmitkNodeSelectionDialog.h" #include "QmitkNodeDetailsDialog.h" QmitkSingleNodeSelectionWidget::QmitkSingleNodeSelectionWidget(QWidget* parent) : QmitkAbstractNodeSelectionWidget(parent), m_AutoSelectNewNodes(false) { m_Controls.setupUi(this); m_Controls.btnSelect->installEventFilter(this); m_Controls.btnSelect->setVisible(true); m_Controls.btnClear->setVisible(false); m_Controls.btnClear->setIcon(berry::QtStyleManager::ThemeIcon(QStringLiteral(":/org.mitk.gui.qt.common/times.svg"))); this->UpdateInfo(); connect(m_Controls.btnClear, SIGNAL(clicked(bool)), this, SLOT(OnClearSelection())); } -QmitkSingleNodeSelectionWidget::~QmitkSingleNodeSelectionWidget() +void QmitkSingleNodeSelectionWidget::ReviseSelectionChanged(const NodeList& oldInternalSelection, NodeList& newInternalSelection) { -} - -mitk::DataNode::Pointer QmitkSingleNodeSelectionWidget::ExtractCurrentValidSelection(const NodeList& nodes) const -{ - mitk::DataNode::Pointer result = nullptr; - - if (!m_DataStorage.IsExpired()) + if (newInternalSelection.empty()) { - auto storage = m_DataStorage.Lock(); - if (storage.IsNotNull()) + if (m_AutoSelectNewNodes) { - for (auto node : nodes) - { - bool valid = storage->Exists(node); - if (valid && m_NodePredicate.IsNotNull()) - { - valid = m_NodePredicate->CheckNode(node); - } + auto autoSelectedNode = this->DeterminAutoSelectNode(oldInternalSelection); - if (valid) - { - result = node; - break; - } + if (autoSelectedNode.IsNotNull()) + { + newInternalSelection.append(autoSelectedNode); } } } - - return result; -} - -QmitkSingleNodeSelectionWidget::NodeList QmitkSingleNodeSelectionWidget::CompileEmitSelection() const -{ - NodeList result; - - if (!m_SelectOnlyVisibleNodes) + else if (newInternalSelection.size()>1) { - result = m_ExternalSelection; + //this widget only allows one internal selected node. + newInternalSelection = { newInternalSelection.front() }; } - - if (m_SelectedNode.IsNotNull() && !result.contains(m_SelectedNode)) - { - result.append(m_SelectedNode); - } - - return result; } -void QmitkSingleNodeSelectionWidget::OnNodePredicateChanged(const mitk::NodePredicateBase* /*newPredicate*/) -{ - auto lastEmission = this->CompileEmitSelection(); - - if (m_NodePredicate.IsNotNull() && m_SelectedNode.IsNotNull() && !m_NodePredicate->CheckNode(m_SelectedNode)) - { - m_SelectedNode = nullptr; - } - - if (m_SelectedNode.IsNull()) - { - m_SelectedNode = this->ExtractCurrentValidSelection(m_ExternalSelection); - } - - this->DoAutoSelectIfNeeded(); - - this->EmitAndUpdateIfNeeded(lastEmission); -}; - -void QmitkSingleNodeSelectionWidget::OnDataStorageChanged() -{ - auto lastEmission = this->CompileEmitSelection(); - - if (m_DataStorage.IsExpired()) - { - m_SelectedNode = nullptr; - } - else if (m_SelectedNode.IsNotNull()) - { - auto storage = m_DataStorage.Lock(); - - if (storage.IsNotNull() && !storage->Exists(m_SelectedNode)) - { - m_SelectedNode = nullptr; - } - } - - if (m_SelectedNode.IsNull()) - { - m_SelectedNode = this->ExtractCurrentValidSelection(m_ExternalSelection); - } - - this->DoAutoSelectIfNeeded(); - - this->EmitAndUpdateIfNeeded(lastEmission); -}; - void QmitkSingleNodeSelectionWidget::OnClearSelection() { if (m_IsOptional) { - NodeList emptyList; - this->SetCurrentSelection(emptyList); + this->SetCurrentSelection({}); } this->UpdateInfo(); } mitk::DataNode::Pointer QmitkSingleNodeSelectionWidget::GetSelectedNode() const { - return m_SelectedNode; -}; + mitk::DataNode::Pointer result; + + auto selection = GetCurrentInternalSelection(); + if (!selection.empty()) + { + result = selection.front(); + } + return result; +} bool QmitkSingleNodeSelectionWidget::eventFilter(QObject *obj, QEvent *ev) { if (obj == m_Controls.btnSelect) { if (ev->type() == QEvent::MouseButtonRelease) { auto mouseEv = dynamic_cast(ev); if (!mouseEv) { return false; } if (mouseEv->button() == Qt::LeftButton) { if (this->isEnabled()) { this->EditSelection(); return true; } } else { auto selection = this->CompileEmitSelection(); if (!selection.empty()) { QmitkNodeDetailsDialog infoDialog(selection, this); infoDialog.exec(); return true; } } } } return false; } void QmitkSingleNodeSelectionWidget::EditSelection() { QmitkNodeSelectionDialog* dialog = new QmitkNodeSelectionDialog(this, m_PopUpTitel, m_PopUpHint); dialog->SetDataStorage(m_DataStorage.Lock()); dialog->SetNodePredicate(m_NodePredicate); - NodeList list; - if (m_SelectedNode.IsNotNull()) - { - list.append(m_SelectedNode); - } - dialog->SetCurrentSelection(list); + dialog->SetCurrentSelection(this->GetCurrentInternalSelection()); dialog->SetSelectOnlyVisibleNodes(m_SelectOnlyVisibleNodes); dialog->SetSelectionMode(QAbstractItemView::SingleSelection); m_Controls.btnSelect->setChecked(true); if (dialog->exec()) { - auto lastEmission = this->CompileEmitSelection(); - - auto nodes = dialog->GetSelectedNodes(); - if (nodes.empty()) - { - m_SelectedNode = nullptr; - } - else - { - m_SelectedNode = nodes.first(); - } - - this->EmitAndUpdateIfNeeded(lastEmission); + this->HandleChangeOfInternalSelection(dialog->GetSelectedNodes()); } m_Controls.btnSelect->setChecked(false); delete dialog; -}; +} void QmitkSingleNodeSelectionWidget::UpdateInfo() { - if (m_SelectedNode.IsNull()) + if (this->GetSelectedNode().IsNull()) { if (m_IsOptional) { m_Controls.btnSelect->SetNodeInfo(m_EmptyInfo); } else { m_Controls.btnSelect->SetNodeInfo(m_InvalidInfo); } m_Controls.btnSelect->SetSelectionIsOptional(m_IsOptional); m_Controls.btnClear->setVisible(false); } else { m_Controls.btnClear->setVisible(m_IsOptional); } - m_Controls.btnSelect->SetSelectedNode(m_SelectedNode); -}; - -void QmitkSingleNodeSelectionWidget::SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes) -{ - auto lastEmission = this->CompileEmitSelection(); - - m_SelectOnlyVisibleNodes = selectOnlyVisibleNodes; - - this->EmitAndUpdateIfNeeded(lastEmission); -}; + m_Controls.btnSelect->SetSelectedNode(this->GetSelectedNode()); +} -void QmitkSingleNodeSelectionWidget::DoAutoSelectIfNeeded(const mitk::DataNode* ignoreNode) +mitk::DataNode::Pointer QmitkSingleNodeSelectionWidget::DeterminAutoSelectNode(const NodeList& ignoreNodes) { - if (m_SelectedNode.IsNull() && m_AutoSelectNewNodes && !m_DataStorage.IsExpired()) + mitk::DataNode::Pointer result; + auto storage = m_DataStorage.Lock(); + if (storage.IsNotNull()) { - auto storage = m_DataStorage.Lock(); - if (storage.IsNotNull()) + auto ignoreCheck = [ignoreNodes](const mitk::DataNode * node) { - auto ignoreCheck = [ignoreNode](const mitk::DataNode * node) + bool result = true; + for (const auto& ignoreNode : ignoreNodes) { - return node != ignoreNode; - }; - mitk::NodePredicateFunction::Pointer isNotIgnoredNode = mitk::NodePredicateFunction::New(ignoreCheck); - mitk::NodePredicateBase::Pointer predicate = isNotIgnoredNode.GetPointer(); - - if (m_NodePredicate.IsNotNull()) - { - predicate = mitk::NodePredicateAnd::New(m_NodePredicate.GetPointer(), predicate.GetPointer()).GetPointer(); + if (node == ignoreNode) + { + result = false; + break; + } } + return result; + }; - m_SelectedNode = storage->GetNode(predicate); - } - } -} + mitk::NodePredicateFunction::Pointer isNotIgnoredNode = mitk::NodePredicateFunction::New(ignoreCheck); + mitk::NodePredicateBase::Pointer predicate = isNotIgnoredNode.GetPointer(); -void QmitkSingleNodeSelectionWidget::EmitAndUpdateIfNeeded(const NodeList& lastEmission) -{ - auto newEmission = this->CompileEmitSelection(); + if (m_NodePredicate.IsNotNull()) + { + predicate = mitk::NodePredicateAnd::New(m_NodePredicate.GetPointer(), predicate.GetPointer()).GetPointer(); + } - if (!EqualNodeSelections(lastEmission, newEmission)) - { - this->UpdateInfo(); - emit CurrentSelectionChanged(newEmission); + result = storage->GetNode(predicate); } -}; + return result; +} void QmitkSingleNodeSelectionWidget::SetCurrentSelectedNode(mitk::DataNode* selectedNode) { NodeList selection; if (selectedNode) { selection.append(selectedNode); } this->SetCurrentSelection(selection); }; -void QmitkSingleNodeSelectionWidget::SetCurrentSelection(NodeList selectedNodes) -{ - auto lastEmission = this->CompileEmitSelection(); - - m_ExternalSelection = selectedNodes; - m_SelectedNode = this->ExtractCurrentValidSelection(selectedNodes); - - this->DoAutoSelectIfNeeded(); - - this->EmitAndUpdateIfNeeded(lastEmission); -}; - -void QmitkSingleNodeSelectionWidget::NodeAddedToStorage(const mitk::DataNode* /*node*/) +void QmitkSingleNodeSelectionWidget::OnNodeAddedToStorage(const mitk::DataNode* /*node*/) { - if (m_SelectedNode.IsNull() && m_AutoSelectNewNodes && !m_DataStorage.IsExpired()) + if (this->GetSelectedNode().IsNull() && m_AutoSelectNewNodes) { - auto lastEmission = this->CompileEmitSelection(); + auto autoNode = this->DeterminAutoSelectNode(); - this->DoAutoSelectIfNeeded(); - - this->EmitAndUpdateIfNeeded(lastEmission); - } -} - -void QmitkSingleNodeSelectionWidget::NodeRemovedFromStorage(const mitk::DataNode* node) -{ - if (m_SelectedNode == node && node != nullptr) - { - m_SelectedNode = nullptr; - - //This event is triggerd before the node is realy removed from storage. - //Therefore we have to explictly ignore him in the autoselect search. - this->DoAutoSelectIfNeeded(node); - - auto newEmission = this->CompileEmitSelection(); - - emit CurrentSelectionChanged(newEmission); - this->UpdateInfo(); + if (autoNode.IsNotNull()) + { + this->HandleChangeOfInternalSelection({ autoNode }); + } } } bool QmitkSingleNodeSelectionWidget::GetAutoSelectNewNodes() const { return m_AutoSelectNewNodes; } void QmitkSingleNodeSelectionWidget::SetAutoSelectNewNodes(bool autoSelect) { m_AutoSelectNewNodes = autoSelect; - this->NodeAddedToStorage(nullptr); + this->OnNodeAddedToStorage(nullptr); } diff --git a/Plugins/org.mitk.gui.qt.common/src/QmitkSingleNodeSelectionWidget.h b/Plugins/org.mitk.gui.qt.common/src/QmitkSingleNodeSelectionWidget.h index 3653dbf262..6cdbcf2536 100644 --- a/Plugins/org.mitk.gui.qt.common/src/QmitkSingleNodeSelectionWidget.h +++ b/Plugins/org.mitk.gui.qt.common/src/QmitkSingleNodeSelectionWidget.h @@ -1,88 +1,78 @@ /*============================================================================ 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 QMITK_SINGLE_NODE_SELECTION_WIDGET_H #define QMITK_SINGLE_NODE_SELECTION_WIDGET_H #include #include #include #include "org_mitk_gui_qt_common_Export.h" #include "ui_QmitkSingleNodeSelectionWidget.h" #include #include class QmitkAbstractDataStorageModel; /** * \class QmitkSingleNodeSelectionWidget * \brief Widget that represents a node selection of (max) one node. It acts like a button. Clicking on it * allows to change the selection. */ class MITK_QT_COMMON QmitkSingleNodeSelectionWidget : public QmitkAbstractNodeSelectionWidget { Q_OBJECT public: explicit QmitkSingleNodeSelectionWidget(QWidget* parent = nullptr); - ~QmitkSingleNodeSelectionWidget() override; mitk::DataNode::Pointer GetSelectedNode() const; bool GetAutoSelectNewNodes() const; using NodeList = QmitkAbstractNodeSelectionWidget::NodeList; public Q_SLOTS: - void SetSelectOnlyVisibleNodes(bool selectOnlyVisibleNodes) override; - void SetCurrentSelection(NodeList selectedNodes) override; void SetCurrentSelectedNode(mitk::DataNode* selectedNode); /** Sets the auto selection mode (Default is false). If auto select is true and the following conditions are fullfilled, the widget will select a node automatically from the data storage: - a data storage is set - data storage contains at least one node that matches the given predicate - no selection is set.*/ void SetAutoSelectNewNodes(bool autoSelect); protected Q_SLOTS: virtual void OnClearSelection(); protected: - mitk::DataNode::Pointer ExtractCurrentValidSelection(const NodeList& nodes) const; - NodeList CompileEmitSelection() const; + void ReviseSelectionChanged(const NodeList& oldInternalSelection, NodeList& newInternalSelection) override; bool eventFilter(QObject *obj, QEvent *ev) override; void EditSelection(); void UpdateInfo() override; - void OnNodePredicateChanged(const mitk::NodePredicateBase* newPredicate) override; - void OnDataStorageChanged() override; - void NodeAddedToStorage(const mitk::DataNode* node) override; - void NodeRemovedFromStorage(const mitk::DataNode* node) override; + void OnNodeAddedToStorage(const mitk::DataNode* node) override; - void DoAutoSelectIfNeeded(const mitk::DataNode* ignoreNode = nullptr); - void EmitAndUpdateIfNeeded(const NodeList& lastEmission); + mitk::DataNode::Pointer DeterminAutoSelectNode(const NodeList& ignoreNodes = {}); - NodeList m_ExternalSelection; - mitk::DataNode::Pointer m_SelectedNode; /** See documentation of SetAutoSelectNewNodes for details*/ bool m_AutoSelectNewNodes; Ui_QmitkSingleNodeSelectionWidget m_Controls; }; #endif // QmitkSingleNodeSelectionWidget_H