Skip to content

Commit

Permalink
Ability to add spectra to plot
Browse files Browse the repository at this point in the history
Refs #11036
  • Loading branch information
DanNixon committed Feb 9, 2015
1 parent 370128b commit 96e714e
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 28 deletions.
Expand Up @@ -4,6 +4,7 @@
#include "IndirectDataReductionTab.h"
#include "ui_IndirectTransmission.h"
#include "MantidKernel/System.h"
#include "MantidQtMantidWidgets/PreviewPlot.h"


namespace MantidQt
Expand Down Expand Up @@ -58,6 +59,7 @@ namespace CustomInterfaces

private:
Ui::IndirectTransmission m_uiForm;
MantidWidgets::PreviewPlot *m_plot;

};

Expand Down
Expand Up @@ -2,8 +2,6 @@

#include <QFileInfo>

#include "MantidQtMantidWidgets/PreviewPlot.h"

using namespace Mantid::API;

namespace MantidQt
Expand All @@ -27,10 +25,10 @@ namespace CustomInterfaces
/* m_uiForm.plotPreview->addWidget(m_plots["PreviewPlot"]); */

//TODO
MantidWidgets::PreviewPlot *plot = new MantidWidgets::PreviewPlot(m_parentWidget);
plot->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
plot->setCanvasColour(Qt::white);
m_uiForm.plotPreview->addWidget(plot);
m_plot = new MantidWidgets::PreviewPlot(m_parentWidget);
m_plot->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_plot->setCanvasColour(Qt::white);
m_uiForm.plotPreview->addWidget(m_plot);

// Update the preview plot when the algorithm is complete
connect(m_batchAlgoRunner, SIGNAL(batchComplete(bool)), this, SLOT(transAlgDone(bool)));
Expand Down Expand Up @@ -131,18 +129,18 @@ namespace CustomInterfaces
return;

// Plot each spectrum
plotMiniPlot(QString::fromStdString(resultWsNames[0]), 0, "PreviewPlot", "SamCurve");
plotMiniPlot(QString::fromStdString(resultWsNames[1]), 0, "PreviewPlot", "CanCurve");
plotMiniPlot(QString::fromStdString(resultWsNames[2]), 0, "PreviewPlot", "TransCurve");
m_plot->addSpectrum(QString::fromStdString(resultWsNames[0]), 0, Qt::red); //, "PreviewPlot", "SamCurve");
m_plot->addSpectrum(QString::fromStdString(resultWsNames[1]), 0, Qt::black); //, "PreviewPlot", "CanCurve");
m_plot->addSpectrum(QString::fromStdString(resultWsNames[2]), 0, Qt::green); //, "PreviewPlot", "TransCurve");

// Colour plots as per plot option
m_curves["SamCurve"]->setPen(QColor(Qt::red));
m_curves["CanCurve"]->setPen(QColor(Qt::black));
m_curves["TransCurve"]->setPen(QColor(Qt::green));
/* m_curves["SamCurve"]->setPen(QColor(Qt::red)); */
/* m_curves["CanCurve"]->setPen(QColor(Qt::black)); */
/* m_curves["TransCurve"]->setPen(QColor(Qt::green)); */

// Set X range to data range
setXAxisToCurve("PreviewPlot", "TransCurve");
m_plots["PreviewPlot"]->replot();
/* setXAxisToCurve("PreviewPlot", "TransCurve"); */
/* m_plots["PreviewPlot"]->replot(); */
}

} // namespace CustomInterfaces
Expand Down
Expand Up @@ -54,6 +54,13 @@ namespace MantidWidgets
Q_PROPERTY(bool allowZoom READ allowZoom WRITE setAllowZoom)

public:
enum ScaleType
{
LINEAR,
LOGARITHMIC,
X_SQUARED
};

PreviewPlot(QWidget *parent = NULL, bool init = true);
virtual ~PreviewPlot();

Expand All @@ -66,18 +73,21 @@ namespace MantidWidgets
bool allowZoom();
void setAllowZoom(bool allow);

void addSpectra(Mantid::API::MatrixWorkspace_sptr ws, int specIndex = 0);
void addSpectra(const QString & wsName, int specIndex = 0);
void addSpectrum(const Mantid::API::MatrixWorkspace_const_sptr ws, const size_t specIndex = 0, const QColor & curveColour = QColor());
void addSpectrum(const QString & wsName, const size_t specIndex = 0, const QColor & curveColour = QColor());

void removeSpectra(Mantid::API::MatrixWorkspace_sptr ws);
void removeSpectra(const QString & wsName);
void removeSpectrum(const Mantid::API::MatrixWorkspace_const_sptr ws);
void removeSpectrum(const QString & wsName);

public slots:
void replot();

private:
void handleRemoveEvent(Mantid::API::WorkspacePreDeleteNotification_ptr pNf);
void handleReplaceEvent(Mantid::API::WorkspaceAfterReplaceNotification_ptr pNf);

void removeCurve(QwtPlotCurve *curve);

private:
/// Poco Observers for ADS Notifications
Poco::NObserver<PreviewPlot, Mantid::API::WorkspacePreDeleteNotification> m_removeObserver;
Expand All @@ -94,7 +104,7 @@ namespace MantidWidgets
QwtPlot *m_plot;

/// Map of workspaces to plot curves
QMap<Mantid::API::MatrixWorkspace_sptr, QwtPlotCurve> m_curves;
QMap<Mantid::API::MatrixWorkspace_const_sptr, QwtPlotCurve *> m_curves;

/// Plot manipulation tools
QwtPlotMagnifier *m_magnifyTool;
Expand Down
150 changes: 141 additions & 9 deletions Code/Mantid/MantidQt/MantidWidgets/src/PreviewPlot.cpp
Expand Up @@ -3,6 +3,9 @@
//------------------------------------------------------
#include "MantidQtMantidWidgets/PreviewPlot.h"

#include "MantidAPI/AnalysisDataService.h"
#include "MantidQtAPI/QwtWorkspaceSpectrumData.h"

#include <Poco/Notification.h>
#include <Poco/NotificationCenter.h>
#include <Poco/AutoPtr.h>
Expand All @@ -12,6 +15,12 @@
#include <QHBoxLayout>

using namespace MantidQt::MantidWidgets;
using namespace Mantid::API;

namespace
{
Mantid::Kernel::Logger g_log("PreviewPlot");
}


PreviewPlot::PreviewPlot(QWidget *parent, bool init) : API::MantidWidget(parent),
Expand All @@ -23,7 +32,7 @@ PreviewPlot::PreviewPlot(QWidget *parent, bool init) : API::MantidWidget(parent)
{
if(init)
{
Mantid::API::AnalysisDataServiceImpl& ads = Mantid::API::AnalysisDataService::Instance();
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance();
ads.notificationCenter.addObserver(m_removeObserver);
ads.notificationCenter.addObserver(m_replaceObserver);

Expand All @@ -48,8 +57,8 @@ PreviewPlot::~PreviewPlot()
{
if(m_init)
{
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_removeObserver);
Mantid::API::AnalysisDataService::Instance().notificationCenter.removeObserver(m_replaceObserver);
AnalysisDataService::Instance().notificationCenter.removeObserver(m_removeObserver);
AnalysisDataService::Instance().notificationCenter.removeObserver(m_replaceObserver);
}
}

Expand Down Expand Up @@ -124,36 +133,159 @@ bool PreviewPlot::allowZoom()
}


void PreviewPlot::addSpectra(Mantid::API::MatrixWorkspace_sptr ws, int specIndex)
/**
* Adds a workspace to the preview plot given a pointer to it.
*
* @param wsName Name of workspace in ADS
* @param specIndex Spectrrum index to plot
* @param curveColour Colour of curve to plot
*/
void PreviewPlot::addSpectrum(const MatrixWorkspace_const_sptr ws, const size_t specIndex,
const QColor & curveColour)
{
using Mantid::MantidVec;

// Check the spectrum index is in range
if(specIndex >= ws->getNumberHistograms())
{
g_log.error() << "Workspace index is out of range, cannot plot."
<< std::endl;
return;
}

// Check the X axis is large enough
if(ws->readX(0).size() < 2)
{
g_log.error() << "X axis is too small to generate a histogram plot."
<< std::endl;
return;
}

// Create the plot data
const bool logScale(false), distribution(false);
QwtWorkspaceSpectrumData wsData(*ws, static_cast<int>(specIndex), logScale, distribution);

// Remove any existing curves
if(m_curves.contains(ws))
removeCurve(m_curves[ws]);

// Create the new curve
m_curves[ws] = new QwtPlotCurve();
m_curves[ws]->setData(wsData);
m_curves[ws]->setPen(curveColour);
m_curves[ws]->attach(m_plot);

// Replot
m_plot->replot();
}


void PreviewPlot::addSpectra(const QString & wsName, int specIndex)
/**
* Adds a workspace to the preview plot given its name.
*
* @param wsName Name of workspace in ADS
* @param specIndex Spectrrum index to plot
* @param curveColour Colour of curve to plot
*/
void PreviewPlot::addSpectrum(const QString & wsName, const size_t specIndex,
const QColor & curveColour)
{
// Try to get a pointer from the name
std::string wsNameStr = wsName.toStdString();
auto ws = AnalysisDataService::Instance().retrieveWS<const MatrixWorkspace>(wsName.toStdString());

if(!ws)
{
g_log.error() << wsNameStr
<< " is not a MatrixWorkspace, not supported by PreviewPlot."
<< std::endl;
return;
}

addSpectrum(ws, specIndex, curveColour);
}


void PreviewPlot::removeSpectra(Mantid::API::MatrixWorkspace_sptr ws)
/**
* Removes spectra from a gievn workspace from the plot given a pointer to it.
*
* @param ws Pointer to workspace
*/
void PreviewPlot::removeSpectrum(const MatrixWorkspace_const_sptr ws)
{
// Remove the curve object
if(m_curves.contains(ws))
removeCurve(m_curves[ws]);

// Get the curve from the map
auto it = m_curves.find(ws);

// Remove the curve from the map
if(it != m_curves.end())
m_curves.erase(it);
}


void PreviewPlot::removeSpectra(const QString & wsName)
/**
* Removes spectra from a gievn workspace from the plot given its name.
*
* @param wsName Name of workspace
*/
void PreviewPlot::removeSpectrum(const QString & wsName)
{
// Try to get a pointer from the name
std::string wsNameStr = wsName.toStdString();
auto ws = AnalysisDataService::Instance().retrieveWS<const MatrixWorkspace>(wsNameStr);

if(!ws)
{
g_log.error() << wsNameStr
<< " is not a MatrixWorkspace, not supported by PreviewPlot."
<< std::endl;
return;
}

removeSpectrum(ws);
}


/**
* Replots the curves shown on the plot.
*/
void PreviewPlot::replot()
{
//TODO: replot curves?

m_plot->replot();
}


void PreviewPlot::handleRemoveEvent(WorkspacePreDeleteNotification_ptr pNf)
{
//TODO
}


void PreviewPlot::handleRemoveEvent(Mantid::API::WorkspacePreDeleteNotification_ptr pNf)
void PreviewPlot::handleReplaceEvent(WorkspaceAfterReplaceNotification_ptr pNf)
{
//TODO
}


void PreviewPlot::handleReplaceEvent(Mantid::API::WorkspaceAfterReplaceNotification_ptr pNf)
/**
* Removes a curve from the plot.
*
* @param curve Curve to remove
*/
void PreviewPlot::removeCurve(QwtPlotCurve * curve)
{
if(!curve)
return;

// Take it off the plot
curve->attach(NULL);

// Delete it
delete curve;
curve = NULL;
}

0 comments on commit 96e714e

Please sign in to comment.