diff --git a/Code/Mantid/MantidPlot/src/Mantid/MantidDock.cpp b/Code/Mantid/MantidPlot/src/Mantid/MantidDock.cpp index 1ff52d2fb93cac8fd17bbd12e9a78d6c95c9abfc..091cadc80cc2d72d949add5df1851596c3a6a3e4 100644 --- a/Code/Mantid/MantidPlot/src/Mantid/MantidDock.cpp +++ b/Code/Mantid/MantidPlot/src/Mantid/MantidDock.cpp @@ -87,6 +87,8 @@ MantidDockWidget::MantidDockWidget(MantidUI *mui, ApplicationWindow *parent) : m_tree->setContextMenuPolicy(Qt::CustomContextMenu); connect(m_tree, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(popupMenu(const QPoint &))); + // call this slot directly after the signal is received. just increment the update counter + connect(m_mantidUI, SIGNAL(workspace_renamed(QString,QString)), this, SLOT(recordWorkspaceRename(QString,QString)),Qt::DirectConnection); // call this slot directly after the signal is received. just increment the update counter connect(m_mantidUI, SIGNAL(ADS_updated()), this, SLOT(incrementUpdateCount()), Qt::DirectConnection); // this slot is called when the GUI thread is free. decrement the counter. do nothing until the counter == 0 @@ -286,7 +288,7 @@ void MantidDockWidget::populateChildData(QTreeWidgetItem* item) auto ws = group->getItem(i); auto * node = addTreeEntry(std::make_pair(ws->name(), ws), item); excludeItemFromSort(node); - if(m_selectedNames.contains(node->text(0))) node->setSelected(true); + if (shouldBeSelected(node->text(0))) node->setSelected(true); } } else @@ -361,6 +363,31 @@ void MantidDockWidget::incrementUpdateCount() m_updateCount.ref(); } +/** + * Save the old and the new name in m_renameMap. This is needed to restore selection + * of the renamed workspace (if it was selected before renaming). + * @param old_name :: Old name of a renamed workspace. + * @param new_name :: New name of a renamed workspace. + */ +void MantidDockWidget::recordWorkspaceRename(QString old_name, QString new_name) +{ + // check if old_name has been recently a new name + QList<QString> oldNames = m_renameMap.keys(old_name); + // non-empty list of oldNames become new_name + if ( !oldNames.isEmpty() ) + { + foreach(QString name, oldNames) + { + m_renameMap[name] = new_name; + } + } + else + { + // record a new rename pair + m_renameMap[old_name] = new_name; + } +} + /** * Flips the flag indicating whether a tree update is in progress. Actions such as sorting * are disabled while an update is in progress. @@ -393,10 +420,13 @@ void MantidDockWidget::populateTopLevel(const std::map<std::string,Mantid::API:: for(auto it = topLevelItems.begin(); it != iend; ++it) { auto *node = addTreeEntry(*it); - if(expanded.contains(node->text(0))) node->setExpanded(true); - if(m_selectedNames.contains(node->text(0))) node->setSelected(true); + QString name = node->text(0); + if(expanded.contains(name)) node->setExpanded(true); + // see if item must be selected + if ( shouldBeSelected(name) ) node->setSelected(true); } m_selectedNames.clear(); + m_renameMap.clear(); } /** @@ -428,6 +458,30 @@ MantidTreeWidgetItem * MantidDockWidget::addTreeEntry(const std::pair<std::strin return node; } +/** + * Check if a workspace should be selected after dock update. + * @param name :: Name of a workspace to check. + */ +bool MantidDockWidget::shouldBeSelected(QString name) const +{ + QStringList renamed = m_renameMap.keys(name); + if ( !renamed.isEmpty() ) + { + foreach(QString oldName,renamed) + { + if ( m_selectedNames.contains(oldName) ) + { + return true; + } + } + } + else if(m_selectedNames.contains(name)) + { + return true; + } + return false; +} + /** * Add the actions that are appropriate for a MatrixWorkspace * @param menu :: The menu to store the items diff --git a/Code/Mantid/MantidPlot/src/Mantid/MantidDock.h b/Code/Mantid/MantidPlot/src/Mantid/MantidDock.h index 8afc620f9038c8b1babed830a01c53b10cee2c3a..4ad6c66b1be4606d80e0e70bfb6f69e2c1903849 100644 --- a/Code/Mantid/MantidPlot/src/Mantid/MantidDock.h +++ b/Code/Mantid/MantidPlot/src/Mantid/MantidDock.h @@ -21,6 +21,7 @@ #include <QTreeWidgetItem> #include <QSortFilterProxyModel> #include <QStringList> +#include <QMap> #include <set> @@ -80,6 +81,7 @@ private slots: void convertMDHistoToMatrixWorkspace(); void updateTree(); void incrementUpdateCount(); + void recordWorkspaceRename(QString,QString); void clearUB(); private: @@ -87,6 +89,7 @@ private: inline bool isTreeUpdating() const { return m_treeUpdating; } void populateTopLevel(const std::map<std::string,Mantid::API::Workspace_sptr> & topLevelItems, const QStringList & expanded); MantidTreeWidgetItem * addTreeEntry(const std::pair<std::string,Mantid::API::Workspace_sptr> & item, QTreeWidgetItem* parent = NULL); + bool shouldBeSelected(QString name) const; void createWorkspaceMenuActions(); void createSortMenuActions(); void setItemIcon(QTreeWidgetItem *item, const std::string & wsID); @@ -137,6 +140,8 @@ private: /// Temporarily keeps names of selected workspaces during tree update /// in order to restore selection after update QStringList m_selectedNames; + /// Keep a map of renamed workspaces between updates + QMap<QString,QString> m_renameMap; static Mantid::Kernel::Logger& logObject; }; diff --git a/Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp b/Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp index b7226d584335463650befa05c453577a55e6939e..c95a68310af8baa6670daabb80dd027a60b589df 100644 --- a/Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp +++ b/Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp @@ -1969,8 +1969,9 @@ void MantidUI::handleClearADS(Mantid::API::ClearADSNotification_ptr) emit workspaces_cleared(); } -void MantidUI::handleRenameWorkspace(Mantid::API::WorkspaceRenameNotification_ptr ) +void MantidUI::handleRenameWorkspace(Mantid::API::WorkspaceRenameNotification_ptr msg) { + emit workspace_renamed(QString::fromStdString(msg->object_name()),QString::fromStdString(msg->new_objectname())); emit ADS_updated(); } void MantidUI::handleGroupWorkspaces(Mantid::API::WorkspacesGroupedNotification_ptr) diff --git a/Code/Mantid/MantidPlot/src/Mantid/MantidUI.h b/Code/Mantid/MantidPlot/src/Mantid/MantidUI.h index 5f990877f83301b266b69978de6e6fb696230731..392bccc024b31ad4424f31dd86781ba43767fec6 100644 --- a/Code/Mantid/MantidPlot/src/Mantid/MantidUI.h +++ b/Code/Mantid/MantidPlot/src/Mantid/MantidUI.h @@ -311,6 +311,7 @@ signals: void workspaces_cleared(); void ADS_updated(); + void workspace_renamed(QString,QString); void needToCreateLoadDAEMantidMatrix(const QString&);