Skip to content

Commit

Permalink
Handle workspace events from ADS
Browse files Browse the repository at this point in the history
Refs #10802
  • Loading branch information
DanNixon committed Jan 23, 2015
1 parent 9a3ff9e commit 8d9a8bc
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
Expand Up @@ -6,10 +6,9 @@
//----------------------
#include "ui_DataComparison.h"
#include "MantidQtAPI/UserSubWindow.h"
#include "MantidQtAPI/WorkspaceObserver.h"
#include "MantidAPI/MatrixWorkspace.h"

#include <QPointer>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_magnifier.h>
Expand All @@ -21,7 +20,7 @@ namespace MantidQt
{
namespace CustomInterfaces
{
class DataComparison : public MantidQt::API::UserSubWindow
class DataComparison : public MantidQt::API::UserSubWindow, public MantidQt::API::WorkspaceObserver
{
Q_OBJECT

Expand Down Expand Up @@ -79,6 +78,11 @@ namespace CustomInterfaces
int getInitialColourIndex();

private:
// Handlers for ADS events
void preDeleteHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws);
void renameHandle(const std::string &oldName, const std::string &newName);
void afterReplaceHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws);

// The form generated by Qt Designer
Ui::DataComparison m_uiForm;

Expand Down
85 changes: 85 additions & 0 deletions Code/Mantid/MantidQt/CustomInterfaces/src/DataComparison.cpp
Expand Up @@ -31,9 +31,13 @@ using namespace Mantid::API;
///Constructor
DataComparison::DataComparison(QWidget *parent) :
UserSubWindow(parent),
WorkspaceObserver(),
m_plot(new QwtPlot(parent)),
m_diffWorkspaceNames(qMakePair(QString(), QString()))
{
observeAfterReplace();
observeRename();
observePreDelete();
}


Expand Down Expand Up @@ -590,3 +594,84 @@ void DataComparison::resetView()
// Set this as the default zoom level
m_zoomTool->setZoomBase(true);
}


/**
* Handles removing a workspace when it is deleted from ADS.
*
* @param wsName Name of the workspace being deleted
* @param ws Pointer to the workspace
*/
void DataComparison::preDeleteHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws)
{
UNUSED_ARG(ws);
QString oldWsName = QString::fromStdString(wsName);

// Find the row in the data table for the workspace
int numRows = m_uiForm.twCurrentData->rowCount();
for(int row = 0; row < numRows; row++)
{
// Remove the row
QString workspaceName = m_uiForm.twCurrentData->item(row, WORKSPACE_NAME)->text();
if(workspaceName == oldWsName)
{
m_uiForm.twCurrentData->removeRow(row);
break;
}
}

// Detach the old curve from the plot if it exists
if(m_curves.contains(oldWsName))
m_curves[oldWsName]->attach(NULL);

// Update the plot
plotWorkspaces();
}


/**
* Handle a workspace being renamed.
*
* @param oldName Old name for the workspace
* @param newName New name for the workspace
*/
void DataComparison::renameHandle(const std::string &oldName, const std::string &newName)
{
QString oldWsName = QString::fromStdString(oldName);

// Find the row in the data table for the workspace
int numRows = m_uiForm.twCurrentData->rowCount();
for(int row = 0; row < numRows; row++)
{
// Rename the workspace in the data table
QString workspaceName = m_uiForm.twCurrentData->item(row, WORKSPACE_NAME)->text();
if(workspaceName == oldWsName)
{
m_uiForm.twCurrentData->item(row, WORKSPACE_NAME)->setText(QString::fromStdString(newName));
break;
}
}

// Detach the old curve from the plot if it exists
if(m_curves.contains(oldWsName))
m_curves[oldWsName]->attach(NULL);

// Update the plot
plotWorkspaces();
}


/**
* Handle replotting after a workspace has been changed.
*
* @param wsName Name of changed workspace
* @ws Pointer to changed workspace
*/
void DataComparison::afterReplaceHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws)
{
UNUSED_ARG(wsName);
UNUSED_ARG(ws);

// Update the plot
plotWorkspaces();
}

0 comments on commit 8d9a8bc

Please sign in to comment.