Skip to content

Commit

Permalink
Observe ADS delete & clear notifications in Spectrogram objects.
Browse files Browse the repository at this point in the history
This only applies to those created directly from a workspace.
Refs #9319
  • Loading branch information
martyngigg committed Apr 23, 2014
1 parent cf192e7 commit b407ebb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 40 deletions.
66 changes: 38 additions & 28 deletions Code/Mantid/MantidPlot/src/Graph.cpp
Expand Up @@ -3654,42 +3654,44 @@ void Graph::removeCurve(int index)
return;

PlotCurve * c = dynamic_cast<PlotCurve *>(it);
if (!c) return;
disconnect(c,SIGNAL(removeMe(PlotCurve*)),this,SLOT(removeCurve(PlotCurve*)));
disconnect(c,SIGNAL(dataUpdated()), this, SLOT(updatePlot()));
if(c) // Only 1D curves need to be considered here
{
disconnect(c,SIGNAL(removeMe(PlotCurve*)),this,SLOT(removeCurve(PlotCurve*)));
disconnect(c,SIGNAL(dataUpdated()), this, SLOT(updatePlot()));

DataCurve * dc = dynamic_cast<DataCurve *>(it);
DataCurve * dc = dynamic_cast<DataCurve *>(it);

removeLegendItem(index);
removeLegendItem(index);

if (it->rtti() != QwtPlotItem::Rtti_PlotSpectrogram)
{
if (dynamic_cast<PlotCurve *>(it)->type() == ErrorBars)
dynamic_cast<QwtErrorPlotCurve *>(it)->detachFromMasterCurve();
else if (c->type() != Function && dc){
dc->clearErrorBars();
dc->clearLabels();
if (it->rtti() != QwtPlotItem::Rtti_PlotSpectrogram)
{
if (dynamic_cast<PlotCurve *>(it)->type() == ErrorBars)
dynamic_cast<QwtErrorPlotCurve *>(it)->detachFromMasterCurve();
else if (c->type() != Function && dc){
dc->clearErrorBars();
dc->clearLabels();
}

if (d_fit_curves.contains(dynamic_cast<QwtPlotCurve *>(it)))
{
int i = d_fit_curves.indexOf(dynamic_cast<QwtPlotCurve *>(it));
if (i >= 0 && i < d_fit_curves.size())
d_fit_curves.removeAt(i);
}
}

if (d_fit_curves.contains(dynamic_cast<QwtPlotCurve *>(it)))
if (d_range_selector && curve(index) == d_range_selector->selectedCurve())
{
int i = d_fit_curves.indexOf(dynamic_cast<QwtPlotCurve *>(it));
if (i >= 0 && i < d_fit_curves.size())
d_fit_curves.removeAt(i);
if (n_curves > 1 && (index - 1) >= 0)
d_range_selector->setSelectedCurve(curve(index - 1));
else if (n_curves > 1 && index + 1 < n_curves)
d_range_selector->setSelectedCurve(curve(index + 1));
else
disableTools();
}
c->aboutToBeDeleted();
}

if (d_range_selector && curve(index) == d_range_selector->selectedCurve())
{
if (n_curves > 1 && (index - 1) >= 0)
d_range_selector->setSelectedCurve(curve(index - 1));
else if (n_curves > 1 && index + 1 < n_curves)
d_range_selector->setSelectedCurve(curve(index + 1));
else
disableTools();
}

c->aboutToBeDeleted();
d_plot->removeCurve(c_keys[index]);
d_plot->replot();
n_curves--;
Expand All @@ -3713,6 +3715,15 @@ void Graph::removeCurve(PlotCurve* c)
removeCurve(curveIndex(c));
}

/**
* Removes the spectrogram from being managed by this Graph
* @param sp A pointer to the Spectrogram to delete
*/
void Graph::removeSpectrogram(Spectrogram *sp)
{
removeCurve(plotItemIndex(sp));
}

void Graph::removeLegendItem(int index)
{
if (!d_legend || c_type[index] == ErrorBars)
Expand Down Expand Up @@ -5199,7 +5210,6 @@ Spectrogram* Graph::plotSpectrogram(Spectrogram *d_spectrogram, CurveType type)
{updatedaxis.push_back(0); }

enableFixedAspectRatio(multiLayer()->applicationWindow()->fixedAspectRatio2DPlots);

return d_spectrogram;
}

Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/MantidPlot/src/Graph.h
Expand Up @@ -250,6 +250,7 @@ public slots:
*/
void removeCurves(const QString& s);
void removeCurve(PlotCurve* c);
void removeSpectrogram(Spectrogram *sp);

void updateCurvesData(Table* w, const QString& yColName);

Expand Down
10 changes: 6 additions & 4 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp
Expand Up @@ -3221,15 +3221,17 @@ MultiLayer* MantidUI::drawSingleColorFillPlot(const QString & wsName, Graph::Cur
plot->setYAxisTitle(tr("Spectrum"));
}

Spectrogram *spgrm = new Spectrogram(workspace);

Spectrogram *spgrm = new Spectrogram(wsName, workspace);
plot->plotSpectrogram(spgrm, curveType);
connect(spgrm, SIGNAL(removeMe(Spectrogram*)),
plot, SLOT(removeSpectrogram(Spectrogram*)));
connect(plot, SIGNAL(curveRemoved()), ml,
SLOT(maybeNeedToClose()), Qt::QueuedConnection);

appWindow()->setSpectrogramTickStyle(plot);
plot->setAutoScale();

QApplication::restoreOverrideCursor();


return ml;
}

Expand Down
40 changes: 34 additions & 6 deletions Code/Mantid/MantidPlot/src/Spectrogram.cpp
Expand Up @@ -47,7 +47,7 @@
Spectrogram::Spectrogram():
QObject(), QwtPlotSpectrogram(),
d_color_map_pen(false),
d_matrix(0),d_funct(0),d_wsData(0),//Mantid
d_matrix(0),d_funct(0),d_wsData(0), d_wsName(),
color_axis(QwtPlot::yRight),
color_map_policy(Default),
color_map(QwtLinearColorMap()),
Expand All @@ -66,9 +66,9 @@ Spectrogram::Spectrogram():
{
}

Spectrogram::Spectrogram(const Mantid::API::IMDWorkspace_const_sptr &workspace) :
Spectrogram::Spectrogram(const QString & wsName, const Mantid::API::IMDWorkspace_const_sptr &workspace) :
QObject(), QwtPlotSpectrogram(),
d_matrix(NULL),d_funct(NULL),d_wsData(NULL),
d_matrix(NULL),d_funct(NULL),d_wsData(NULL), d_wsName(),
color_axis(QwtPlot::yRight),
color_map_policy(Default),mColorMap()
{
Expand All @@ -90,6 +90,7 @@ Spectrogram::Spectrogram(const Mantid::API::IMDWorkspace_const_sptr &workspace)
Mantid::coord_t height = dim1->getMaximum() - minY;
d_wsData->setBoundingRect(QwtDoubleRect(minX, minY, width, height));
setData(*d_wsData);
d_wsName = wsName.toStdString();

double step = fabs(data().range().maxValue() - data().range().minValue())/5.0;
QwtValueList contourLevels;
Expand All @@ -98,11 +99,15 @@ Spectrogram::Spectrogram(const Mantid::API::IMDWorkspace_const_sptr &workspace)
contourLevels += level;

setContourLevels(contourLevels);

observePostDelete();
observeADSClear();

}

Spectrogram::Spectrogram(Matrix *m):
QObject(), QwtPlotSpectrogram(QString(m->objectName())),
d_matrix(m),d_funct(0),d_wsData(NULL),//Mantid
d_matrix(m),d_funct(0),d_wsData(NULL), d_wsName(),
color_axis(QwtPlot::yRight),
color_map_policy(Default),mColorMap()
{
Expand All @@ -120,7 +125,7 @@ Spectrogram::Spectrogram(Matrix *m):

Spectrogram::Spectrogram(Function2D *f,int nrows, int ncols,double left, double top, double width, double height,double minz,double maxz)
: QObject(), QwtPlotSpectrogram(),
d_matrix(0),d_funct(f), d_wsData(NULL),
d_matrix(0),d_funct(f), d_wsData(NULL), d_wsName(),
color_axis(QwtPlot::yRight),
color_map_policy(Default),
color_map(QwtLinearColorMap())
Expand All @@ -141,7 +146,7 @@ Spectrogram::Spectrogram(Function2D *f,int nrows, int ncols,QwtDoubleRect bRect,
: QObject(), QwtPlotSpectrogram(),
d_color_map_pen(false),
d_matrix(0),
d_funct(f),d_wsData(NULL),
d_funct(f),d_wsData(NULL), d_wsName(),
color_axis(QwtPlot::yRight),
color_map_policy(Default),
d_show_labels(true),
Expand Down Expand Up @@ -172,8 +177,31 @@ Spectrogram::Spectrogram(Function2D *f,int nrows, int ncols,QwtDoubleRect bRect,

Spectrogram::~Spectrogram()
{
observePostDelete(false);
observeADSClear(false);
}

/**
* Called after a workspace has been deleted
* @param wsName The name of the workspace that has been deleted
*/
void Spectrogram::postDeleteHandle(const std::string &wsName)
{
if (wsName == d_wsName)
{
observePostDelete(false);
emit removeMe(this);
}
}

/**
* Called after a the ADS has been cleared
*/
void Spectrogram::clearADSHandle()
{
observeADSClear(false);
postDeleteHandle(d_wsName);
}

void Spectrogram::setContourLevels (const QwtValueList & levels)
{
Expand Down
14 changes: 12 additions & 2 deletions Code/Mantid/MantidPlot/src/Spectrogram.h
Expand Up @@ -41,6 +41,7 @@

#include "MantidAPI/IMDWorkspace.h"
#include "MantidQtAPI/MantidColorMap.h"
#include "MantidQtAPI/WorkspaceObserver.h"
#include "Mantid/InstrumentWidget/GLColor.h"

#include <fstream>
Expand All @@ -60,18 +61,22 @@ namespace MantidQt
}
}

class Spectrogram: public QObject, public QwtPlotSpectrogram
class Spectrogram: public QObject, public QwtPlotSpectrogram, public MantidQt::API::WorkspaceObserver
{
Q_OBJECT

public:
Spectrogram();
Spectrogram(Matrix *m);
Spectrogram(const Mantid::API::IMDWorkspace_const_sptr & workspace);
Spectrogram(const QString &wsName, const Mantid::API::IMDWorkspace_const_sptr & workspace);
Spectrogram(Function2D *f,int nrows, int ncols,double left, double top, double width, double height,double minz,double maxz);//Mantid
Spectrogram(Function2D *f,int nrows, int ncols,QwtDoubleRect bRect,double minz,double maxz);//Mantid
~Spectrogram();

/// Handles delete notification
void postDeleteHandle(const std::string& wsName);
/// Handle an ADS clear notificiation
void clearADSHandle();

enum ColorMapPolicy{GrayScale, Default, Custom};

Expand Down Expand Up @@ -156,6 +161,9 @@ class Spectrogram: public QObject, public QwtPlotSpectrogram
/// returns boolan flag intensity change
bool isIntensityChanged();

signals:
void removeMe(Spectrogram*);

protected:
virtual void drawContourLines (QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
void updateLabels(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
Expand All @@ -166,6 +174,8 @@ class Spectrogram: public QObject, public QwtPlotSpectrogram
Function2D *d_funct;
/// Pointer to data source for a workspace
MantidQt::API::QwtRasterDataMD *d_wsData;
/// Name of the workspace backing the spectrogram
std::string d_wsName;

//! Axis used to display the color scale
int color_axis;
Expand Down

0 comments on commit b407ebb

Please sign in to comment.