Skip to content

Commit

Permalink
Improve the way scales are handled
Browse files Browse the repository at this point in the history
Refs #11036
  • Loading branch information
DanNixon committed Feb 9, 2015
1 parent 96e714e commit 3ce6463
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 50 deletions.
Expand Up @@ -18,15 +18,8 @@ namespace CustomInterfaces
m_uiForm.setupUi(parent);

// Preview plot
m_plots["PreviewPlot"] = new QwtPlot(0); //m_parentWidget);
m_plots["PreviewPlot"]->setAxisFont(QwtPlot::xBottom, parent->font());
m_plots["PreviewPlot"]->setAxisFont(QwtPlot::yLeft, parent->font());
m_plots["PreviewPlot"]->setCanvasBackground(Qt::white);
/* m_uiForm.plotPreview->addWidget(m_plots["PreviewPlot"]); */

//TODO
// TODO: Move to UI file
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);

Expand Down Expand Up @@ -128,19 +121,12 @@ namespace CustomInterfaces
if(resultWsNames.size() < 3)
return;

// Plot each spectrum
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)); */

// Set X range to data range
/* setXAxisToCurve("PreviewPlot", "TransCurve"); */
/* m_plots["PreviewPlot"]->replot(); */
// Do plotting
m_plot->clear();
m_plot->addSpectrum(QString::fromStdString(resultWsNames[0]), 0, Qt::red);
m_plot->addSpectrum(QString::fromStdString(resultWsNames[1]), 0, Qt::black);
m_plot->addSpectrum(QString::fromStdString(resultWsNames[2]), 0, Qt::green);
m_plot->resizeX();
}

} // namespace CustomInterfaces
Expand Down
Expand Up @@ -73,13 +73,20 @@ namespace MantidWidgets
bool allowZoom();
void setAllowZoom(bool allow);

void setAxisRange(QPair<double, double> range, int axisID = QwtPlot::xBottom);

QPair<double, double> getCurveRange(const Mantid::API::MatrixWorkspace_const_sptr ws);
QPair<double, double> getCurveRange(const QString & wsName);

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 removeSpectrum(const Mantid::API::MatrixWorkspace_const_sptr ws);
void removeSpectrum(const QString & wsName);

public slots:
void resizeX();
void clear();
void replot();

private:
Expand Down
133 changes: 104 additions & 29 deletions Code/Mantid/MantidQt/MantidWidgets/src/PreviewPlot.cpp
Expand Up @@ -89,6 +89,17 @@ void PreviewPlot::setCanvasColour(const QColor & colour)
}


/**
* Checks to see if the option to use the pan tool is enabled.
*
* @return True if tool is allowed
*/
bool PreviewPlot::allowPan()
{
return m_allowPan;
}


/**
* Enables or disables the option to use the pan tool on the plot.
*
Expand All @@ -101,13 +112,13 @@ void PreviewPlot::setAllowPan(bool allow)


/**
* Checks to see if the option to use the pan tool is enabled.
* Checks to see if the option to use the zoom tool is enabled.
*
* @return True if tool is allowed
*/
bool PreviewPlot::allowPan()
bool PreviewPlot::allowZoom()
{
return m_allowPan;
return m_allowZoom;
}


Expand All @@ -123,13 +134,58 @@ void PreviewPlot::setAllowZoom(bool allow)


/**
* Checks to see if the option to use the zoom tool is enabled.
* Sets the range of the given axis scale to a given range.
*
* @return True if tool is allowed
* @param range Pair of values for range
* @param axisID ID of axis
*/
bool PreviewPlot::allowZoom()
void PreviewPlot::setAxisRange(QPair<double, double> range, int axisID)
{
return m_allowZoom;
if(range.first > range.second)
throw std::runtime_error("Supplied range is invalid.");

m_plot->setAxisScale(axisID, range.first, range.second);
replot();
}


/**
* Gets the X range of a curve given a pointer to the workspace.
*
* @param ws Pointer to workspace
*/
QPair<double, double> PreviewPlot::getCurveRange(const Mantid::API::MatrixWorkspace_const_sptr ws)
{
if(!m_curves.contains(ws))
throw std::runtime_error("Workspace not on preview plot.");

size_t numPoints = m_curves[ws]->data().size();

if(numPoints < 2)
return qMakePair(0.0, 0.0);

double low = m_curves[ws]->data().x(0);
double high = m_curves[ws]->data().x(numPoints - 1);

return qMakePair(low, high);
}


/**
* Gets the X range of a curve given its name.
*
* @param wsName Name of workspace
*/
QPair<double, double> PreviewPlot::getCurveRange(const QString & wsName)
{
// Try to get a pointer from the name
std::string wsNameStr = wsName.toStdString();
auto ws = AnalysisDataService::Instance().retrieveWS<const MatrixWorkspace>(wsName.toStdString());

if(!ws)
throw std::runtime_error(wsNameStr + " is not a MatrixWorkspace, not supported by PreviewPlot.");

return getCurveRange(ws);
}


Expand All @@ -147,19 +203,11 @@ void PreviewPlot::addSpectrum(const MatrixWorkspace_const_sptr ws, const size_t

// 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;
}
throw std::runtime_error("Workspace index is out of range, cannot plot.");

// 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;
}
throw std::runtime_error("X axis is too small to generate a histogram plot.");

// Create the plot data
const bool logScale(false), distribution(false);
Expand Down Expand Up @@ -195,12 +243,7 @@ void PreviewPlot::addSpectrum(const QString & wsName, const size_t specIndex,
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;
}
throw std::runtime_error(wsNameStr + " is not a MatrixWorkspace, not supported by PreviewPlot.");

addSpectrum(ws, specIndex, curveColour);
}
Expand Down Expand Up @@ -238,14 +281,47 @@ void PreviewPlot::removeSpectrum(const QString & wsName)
auto ws = AnalysisDataService::Instance().retrieveWS<const MatrixWorkspace>(wsNameStr);

if(!ws)
throw std::runtime_error(wsNameStr + " is not a MatrixWorkspace, not supported by PreviewPlot.");

removeSpectrum(ws);
}


/**
* Resizes the X axis scale range to exactly fir the curves currently
* plotted on it.
*/
void PreviewPlot::resizeX()
{
double low = DBL_MAX;
double high = DBL_MIN;

for(auto it = m_curves.begin(); it != m_curves.end(); ++it)
{
g_log.error() << wsNameStr
<< " is not a MatrixWorkspace, not supported by PreviewPlot."
<< std::endl;
return;
auto range = getCurveRange(it.key());

if(range.first < low)
low = range.first;

if(range.second > high)
high = range.second;
}

removeSpectrum(ws);
setAxisRange(qMakePair(low, high), QwtPlot::xBottom);
}


/**
* Removes all curves from the plot.
*/
void PreviewPlot::clear()
{
for(auto it = m_curves.begin(); it != m_curves.end(); ++it)
removeCurve(it.value());

m_curves.clear();

replot();
}


Expand All @@ -255,7 +331,6 @@ void PreviewPlot::removeSpectrum(const QString & wsName)
void PreviewPlot::replot()
{
//TODO: replot curves?

m_plot->replot();
}

Expand Down

0 comments on commit 3ce6463

Please sign in to comment.