diff --git a/Plugins/org.mitk.gui.qt.cli/src/internal/QmitkDataStorageComboBoxWithSelectNone.cpp b/Plugins/org.mitk.gui.qt.cli/src/internal/QmitkDataStorageComboBoxWithSelectNone.cpp index ded152b187..fd8f1c3bf2 100644 --- a/Plugins/org.mitk.gui.qt.cli/src/internal/QmitkDataStorageComboBoxWithSelectNone.cpp +++ b/Plugins/org.mitk.gui.qt.cli/src/internal/QmitkDataStorageComboBoxWithSelectNone.cpp @@ -1,165 +1,189 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) University College London (UCL). All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkDataStorageComboBoxWithSelectNone.h" #include const QString QmitkDataStorageComboBoxWithSelectNone::ZERO_ENTRY_STRING = "please select"; //----------------------------------------------------------------------------- QmitkDataStorageComboBoxWithSelectNone::QmitkDataStorageComboBoxWithSelectNone( QWidget* parent, bool autoSelectNewNodes ) : QmitkDataStorageComboBox(parent, autoSelectNewNodes) , m_CurrentPath("") { } //----------------------------------------------------------------------------- QmitkDataStorageComboBoxWithSelectNone::QmitkDataStorageComboBoxWithSelectNone( mitk::DataStorage* dataStorage, const mitk::NodePredicateBase* predicate, QWidget* parent, bool autoSelectNewNodes ) : QmitkDataStorageComboBox(dataStorage, predicate, parent, autoSelectNewNodes) { } //----------------------------------------------------------------------------- QmitkDataStorageComboBoxWithSelectNone::~QmitkDataStorageComboBoxWithSelectNone() { } //----------------------------------------------------------------------------- int QmitkDataStorageComboBoxWithSelectNone::Find( const mitk::DataNode* dataNode ) const { int index = QmitkDataStorageComboBox::Find(dataNode); if (index != -1) { index += 1; } return index; } //----------------------------------------------------------------------------- mitk::DataNode::Pointer QmitkDataStorageComboBoxWithSelectNone::GetNode( int index ) const { mitk::DataNode::Pointer result = NULL; if (this->HasIndex(index)) { if (index != 0) { result = m_Nodes.at(index - 1); } } return result; } //----------------------------------------------------------------------------- mitk::DataNode::Pointer QmitkDataStorageComboBoxWithSelectNone::GetSelectedNode() const { return this->GetNode(this->currentIndex()); } +//----------------------------------------------------------------------------- +void QmitkDataStorageComboBoxWithSelectNone::SetSelectedNode(const mitk::DataNode::Pointer& node) +{ + int currentIndex = -1; + for (int i = 0; i < m_Nodes.size(); i++) + { + if (m_Nodes[i] == node.GetPointer()) + { + currentIndex = i; + break; + } + } + if (currentIndex == -1) + { + // didn't find it, so set the value to 0. + currentIndex = 0; + } + else + { + currentIndex += 1; // because the combo box contains "please select" at position zero. + } + this->setCurrentIndex(currentIndex); +} + //----------------------------------------------------------------------------- void QmitkDataStorageComboBoxWithSelectNone::RemoveNode( int index ) { if(index > 0 && this->HasIndex(index)) { // remove itk::Event observer mitk::DataNode* dataNode = m_Nodes.at(index - 1); // get name property first mitk::BaseProperty* nameProperty = dataNode->GetProperty("name"); // if prop exists remove modified listener if(nameProperty) { nameProperty->RemoveObserver(m_NodesModifiedObserverTags[index-1]); // remove name property map m_PropertyToNode.erase(dataNode); } // then remove delete listener on the node itself dataNode->RemoveObserver(m_NodesDeleteObserverTags[index-1]); // remove observer tags from lists m_NodesModifiedObserverTags.erase(m_NodesModifiedObserverTags.begin()+index-1); m_NodesDeleteObserverTags.erase(m_NodesDeleteObserverTags.begin()+index-1); // remove node name from combobox this->removeItem(index); // remove node from node vector m_Nodes.erase(m_Nodes.begin()+index-1); } } //----------------------------------------------------------------------------- void QmitkDataStorageComboBoxWithSelectNone::SetNode(int index, const mitk::DataNode* dataNode) { if(index > 0 && this->HasIndex(index)) { QmitkDataStorageComboBox::InsertNode(index - 1, dataNode); } } //----------------------------------------------------------------------------- bool QmitkDataStorageComboBoxWithSelectNone::HasIndex(unsigned int index) const { return (m_Nodes.size() > 0 && index <= m_Nodes.size()); } //----------------------------------------------------------------------------- void QmitkDataStorageComboBoxWithSelectNone::InsertNode(int index, const mitk::DataNode* dataNode) { if (index != 0) { QmitkDataStorageComboBox::InsertNode(index - 1, dataNode); } } //----------------------------------------------------------------------------- void QmitkDataStorageComboBoxWithSelectNone::Reset() { QmitkDataStorageComboBox::Reset(); this->insertItem(0, ZERO_ENTRY_STRING); } //----------------------------------------------------------------------------- QString QmitkDataStorageComboBoxWithSelectNone::currentValue() const { return m_CurrentPath; } //----------------------------------------------------------------------------- void QmitkDataStorageComboBoxWithSelectNone::setCurrentValue(const QString& path) { m_CurrentPath = path; } diff --git a/Plugins/org.mitk.gui.qt.cli/src/internal/QmitkDataStorageComboBoxWithSelectNone.h b/Plugins/org.mitk.gui.qt.cli/src/internal/QmitkDataStorageComboBoxWithSelectNone.h index 6f05d873f2..1b30456bc8 100644 --- a/Plugins/org.mitk.gui.qt.cli/src/internal/QmitkDataStorageComboBoxWithSelectNone.h +++ b/Plugins/org.mitk.gui.qt.cli/src/internal/QmitkDataStorageComboBoxWithSelectNone.h @@ -1,141 +1,146 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) University College London (UCL). All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef QmitkDataStorageComboBoxWithSelectNone_h #define QmitkDataStorageComboBoxWithSelectNone_h #include #include "QmitkDataStorageComboBox.h" #include "QmitkCustomVariants.h" #include "mitkDataNode.h" /** * \class QmitkDataStorageComboBoxWithSelectNone * \brief Displays all or a subset (defined by a predicate) of nodes of the Data Storage, * and additionally, index 0 is always "please select", indicating no selection, and will * hence always return a NULL mitk::DataNode* if asked for the node at index 0. * * In addition, to support the integration of CTK command line modules, the methods * currentValue() and setCurrentValue() are provided. See also the .xsl file * in org.mitk.gui.qt.cli/resources/QmitkDataStorageComboBox.xsl. * * \author Matt Clarkson (m.clarkson@ucl.ac.uk) * \ingroup org_mitk_gui_qt_cli_internal * \sa QmitkDataStorageComboBox */ class QmitkDataStorageComboBoxWithSelectNone : public QmitkDataStorageComboBox { Q_OBJECT - Q_PROPERTY(mitk::DataNode::Pointer GetSelectedNode READ GetSelectedNode) + Q_PROPERTY(mitk::DataNode::Pointer SelectedNode READ GetSelectedNode WRITE SetSelectedNode) Q_PROPERTY(QString currentValue READ currentValue WRITE setCurrentValue) public: /** * \brief Calls base class constructor. * \see QmitkDataStorageComboBox */ QmitkDataStorageComboBoxWithSelectNone(QWidget* parent = 0, bool autoSelectNewNodes=false); /** * \brief Calls base class constructor. * \see QmitkDataStorageComboBox */ QmitkDataStorageComboBoxWithSelectNone( mitk::DataStorage* _DataStorage, const mitk::NodePredicateBase* predicate, QWidget* parent = 0, bool autoSelectNewNodes=false); /** * \brief Nothing to do. * \see QmitkDataStorageComboBox */ ~QmitkDataStorageComboBoxWithSelectNone(); /** * \brief Stores the string that will be present on index 0, currently equal to "please select". */ static const QString ZERO_ENTRY_STRING; /** * \brief Searches for a given node, returning the index if found. * \param dataNode an mitk::DataNode, can be NULL. * \return int -1 if not found, and compared to base class, will add 1 onto the retrieved index. */ virtual int Find( const mitk::DataNode* dataNode ) const; /** * \brief Retrieves the node at a given index, where if index is zero, will always return NULL. * \param index An integer between 0 and n = number of nodes. * \return mitk::DataNode::Pointer NULL or a data node pointer. */ virtual mitk::DataNode::Pointer GetNode(int index) const; /** * \brief Returns the selected DataNode or NULL if there is none, or the current index is zero. */ virtual mitk::DataNode::Pointer GetSelectedNode() const; + /** + * \brief Sets the combo box to the index that contains the specified node, or 0 if the node cannot be found. + */ + virtual void SetSelectedNode(const mitk::DataNode::Pointer& node); + /** * \brief Removes a node from the ComboBox at a specified index (if the index exists). * Gets called when a DataStorage Remove Event was thrown. */ virtual void RemoveNode(int index); /** * \brief Set a DataNode in the ComboBox at the specified index (if the index exists). * Internally the method just calls InsertNode(unsigned int) */ virtual void SetNode(int index, const mitk::DataNode* dataNode); /** * \brief Get the current file path. */ virtual QString currentValue() const; /** * \brief Set the current file path. */ virtual void setCurrentValue(const QString& path); protected: /** * \brief Checks if the given index is within range. */ bool HasIndex(unsigned int index) const; /** * \brief Inserts a new node at the given index, unless index is 0, which is silently ignored. */ virtual void InsertNode(int index, const mitk::DataNode* dataNode); /** * \brief Reset function whenever datastorage or predicate changes. */ virtual void Reset(); private: /** * \brief This should store the current file path of the current image. * * * The reason is so that we can store and retrieve a temporary file name. */ QString m_CurrentPath; }; #endif // QmitkDataStorageComboBoxWithSelectNone_h