Skip to content

Commit

Permalink
refs #6271. Draw background radius.
Browse files Browse the repository at this point in the history
Quite a few changes here. We now use QPainterPath to generate a more complex peak overlay to include the background peak.
  • Loading branch information
OwenArnold committed Jan 7, 2013
1 parent 4170f0a commit 1775fba
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace MantidQt
/// Get a copy of the palette in its current state.
PeakPalette getPalette() const;
/// Setter for indicating whether the background radius will be shown.
void setBackgroundRadiusShown(const bool shown);
void setBackgroundRadiusShown(boost::shared_ptr<const Mantid::API::IPeaksWorkspace> ws, const bool shown);
private:
/// Alias for container of subjects type.
typedef std::vector<PeaksPresenter_sptr> SubjectContainer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace SliceViewer

public:
/// Constructor
PeakOverlaySphere(QwtPlot * plot, QWidget * parent, const Mantid::Kernel::V3D& origin, const double& peakRadius, const double& backgroundInnerRadius, const double& backgroundOuterRadius, const QColor& peakColour);
PeakOverlaySphere(QwtPlot * plot, QWidget * parent, const Mantid::Kernel::V3D& origin, const double& peakRadius, const double& backgroundInnerRadius, const double& backgroundOuterRadius, const QColor& peakColour, const QColor& backColour);
/// Destructor
virtual ~PeakOverlaySphere();
/// Set the slice point at position.
Expand Down Expand Up @@ -85,6 +85,8 @@ namespace SliceViewer
PhysicalSphericalPeak m_physicalPeak;
/// Peak colour
QColor m_peakColour;
/// Back colour
QColor m_backColour;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace MantidQt
QwtPlot * m_plot;
QWidget * m_parent;
QColor m_peakColour;
QColor m_backColour;
public:
PeakOverlayViewFactoryBase(QwtPlot * plot, QWidget * parent, const size_t colourNumber=0);
virtual ~PeakOverlayViewFactoryBase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EXPORT_OPT_MANTIDQT_SLICEVIEWER PeaksViewer : public QWidget
public slots:
void onPeakColourChanged(Mantid::API::IPeaksWorkspace_const_sptr, QColor);
void onBackgroundColourChanged(Mantid::API::IPeaksWorkspace_const_sptr, QColor);
void onBackgroundRadiusShown(bool);
void onBackgroundRadiusShown(Mantid::API::IPeaksWorkspace_const_sptr, bool);
private:
boost::shared_ptr<ProxyCompositePeaksPresenter> m_presenter;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace SliceViewer
signals:
void peakColourChanged(Mantid::API::IPeaksWorkspace_const_sptr, QColor);
void backgroundColourChanged(Mantid::API::IPeaksWorkspace_const_sptr, QColor);
void backgroundRadiusShown(bool);
void backgroundRadiusShown(Mantid::API::IPeaksWorkspace_const_sptr, bool);
private:
/// Populate the widget with model data.
void populate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "MantidKernel/V3D.h"
#include "MantidKernel/ClassMacros.h"
#include "MantidQtSliceViewer/PeakTransform.h"
#include <boost/optional.hpp>

namespace MantidQt
{
Expand All @@ -15,9 +16,12 @@ namespace MantidQt
*/
struct SphericalPeakPrimitives
{
double peakOuterRadiusX;
double peakOuterRadiusY;
double peakLineWidth;
double peakInnerRadiusX;
double peakInnerRadiusY;
double backgroundOuterRadiusX;
double backgroundOuterRadiusY;
double backgroundInnerRadiusX;
double backgroundInnerRadiusY;
double peakOpacityAtDistance;
Mantid::Kernel::V3D peakOrigin;
};
Expand Down Expand Up @@ -45,21 +49,25 @@ namespace MantidQt
distance between the plane and the origin is greater than the peak radius, then the peak is not visible.
@return True if the peak is visible in the current configuration.
*/
inline bool isViewable() const
inline bool isViewablePeak() const
{
if(m_showBackgroundRadius)
return (m_peakRadiusAtDistance <= this->m_peakRadius);
}

inline bool isViewableBackground() const
{
if(m_showBackgroundRadius && m_backgroundOuterRadiusAtDistance.is_initialized())
{
return (m_backgroundOuterRadiusAtDistance <= this->m_backgroundOuterRadius);
}
else
{
return (m_peakRadiusAtDistance <= this->m_peakRadius);
}
return false;
}

/// Setter to command whether the background radius should also be shown.
void showBackgroundRadius(const bool show);

bool showBackgroundRadius() const;

private:
/// Original origin x=h, y=k, z=l
const Mantid::Kernel::V3D m_originalOrigin;
Expand Down Expand Up @@ -92,7 +100,7 @@ namespace MantidQt
/// Inner radius at distance.
double m_backgroundInnerRadiusAtDistance;
/// Outer radius at distance.
double m_backgroundOuterRadiusAtDistance;
boost::optional<double> m_backgroundOuterRadiusAtDistance;
/// Current slicepoint.
double m_currentSlicePoint;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace MantidQt
/// Gets the transform name.
std::string getTransformName() const;
/// Change whether the background radius is shown.
void setBackgroundRadiusShown(const bool shown);
void setBackgroundRadiusShown(boost::shared_ptr<const Mantid::API::IPeaksWorkspace> ws, const bool shown);

private:
/// Wrapped composite to delegate to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ namespace MantidQt
return m_palette.backgroundIndexToColour(pos);
}

void CompositePeaksPresenter::setBackgroundRadiusShown(const bool shown)
void CompositePeaksPresenter::setBackgroundRadiusShown(boost::shared_ptr<const Mantid::API::IPeaksWorkspace> ws, const bool shown)
{

auto iterator = getPresenterIteratorFromWorkspace(ws);
(*iterator)->showBackgroundRadius(shown);
}
}
}
40 changes: 23 additions & 17 deletions Code/Mantid/MantidQt/SliceViewer/src/PeakOverlaySphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace SliceViewer
//----------------------------------------------------------------------------------------------
/** Constructor
*/
PeakOverlaySphere::PeakOverlaySphere(QwtPlot * plot, QWidget * parent, const Mantid::Kernel::V3D& origin, const double& peakRadius, const double& backgroundInnerRadius, const double& backgroundOuterRadius, const QColor& peakColour)
PeakOverlaySphere::PeakOverlaySphere(QwtPlot * plot, QWidget * parent, const Mantid::Kernel::V3D& origin, const double& peakRadius, const double& backgroundInnerRadius, const double& backgroundOuterRadius, const QColor& peakColour, const QColor& backColour)
: QWidget( parent ),
m_plot(plot),
m_physicalPeak(origin, peakRadius, backgroundInnerRadius, backgroundOuterRadius),
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace SliceViewer
/// Paint the overlay
void PeakOverlaySphere::paintEvent(QPaintEvent * /*event*/)
{
if(m_physicalPeak.isViewable())
if(m_physicalPeak.isViewablePeak())
{
const QwtDoubleInterval intervalY = m_plot->axisScaleDiv(QwtPlot::yLeft)->interval();
const QwtDoubleInterval intervalX = m_plot->axisScaleDiv(QwtPlot::xBottom)->interval();
Expand All @@ -79,21 +79,27 @@ namespace SliceViewer
const int yOrigin = m_plot->transform( QwtPlot::yLeft, drawObject.peakOrigin.Y() );
const QPointF originWindows(xOrigin, yOrigin);


QPainter painter(this);
painter.setRenderHint( QPainter::Antialiasing );

// Draw Outer circle
QPen pen(m_peakColour);
/* Note we are creating an ellipse here and generating a filled effect by controlling the line thickness.
Since the linewidth takes a single scalar value, we choose to use x as the scale value.
*/
pen.setWidth(static_cast<int>(std::abs(drawObject.peakLineWidth)));
painter.setPen( pen );

pen.setStyle(Qt::SolidLine);
painter.setOpacity(drawObject.peakOpacityAtDistance); //Set the pre-calculated opacity
painter.drawEllipse( originWindows, drawObject.peakOuterRadiusX, drawObject.peakOuterRadiusY );

QPainterPath peakRadiusInnerPath;
peakRadiusInnerPath.addEllipse(originWindows, drawObject.peakInnerRadiusX, drawObject.peakInnerRadiusY);
QPen pen(m_peakColour);
pen.setWidth(2);
pen.setStyle(Qt::DashLine);
painter.strokePath(peakRadiusInnerPath, pen);

if(m_physicalPeak.isViewableBackground())
{
QPainterPath backgroundOuterPath;
backgroundOuterPath.setFillRule(Qt::WindingFill);
backgroundOuterPath.addEllipse(originWindows, drawObject.backgroundOuterRadiusX, drawObject.backgroundOuterRadiusY);
QPainterPath backgroundInnerPath;
backgroundInnerPath.addEllipse(originWindows, drawObject.backgroundInnerRadiusX, drawObject.backgroundInnerRadiusY);
QPainterPath backgroundRadiusFill = backgroundOuterPath.subtracted(backgroundInnerPath);
painter.fillPath(backgroundRadiusFill, m_backColour);
}
}
}

Expand All @@ -119,12 +125,12 @@ namespace SliceViewer

void PeakOverlaySphere::changeForegroundColour(const QColor colour)
{
this->m_peakColour = QColor(colour);
this->m_peakColour = colour;
}

void PeakOverlaySphere::changeBackgroundColour(const QColor)
void PeakOverlaySphere::changeBackgroundColour(const QColor colour)
{
// Not being drawn at the moment, TODO.
this->m_backColour = colour;
}

void PeakOverlaySphere::showBackgroundRadius(const bool show)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace MantidQt

boost::shared_ptr<PeakOverlayView> PeakOverlaySphereFactory::createView(const Mantid::Kernel::V3D& position) const
{
return boost::make_shared<PeakOverlaySphere>(m_plot, m_parent, position, this->m_peakRadius, this->m_backgroundInnerRadius, this->m_backgroundOuterRadius, this->m_peakColour);
return boost::make_shared<PeakOverlaySphere>(m_plot, m_parent, position, this->m_peakRadius, this->m_backgroundInnerRadius, this->m_backgroundOuterRadius, this->m_peakColour, this->m_backColour);
}

PeakOverlaySphereFactory::~PeakOverlaySphereFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ namespace MantidQt
throw std::invalid_argument("PeakOverlayViewFactoryBase parent widget is null");

PeakPalette defaultPalette;
auto colourEnum = defaultPalette.foregroundIndexToColour(static_cast<int>(workspaceNumber));
//Qt::GlobalColor qtColourEnum = colourEnum;
m_peakColour = QColor(colourEnum);
auto peakColourEnum = defaultPalette.foregroundIndexToColour(static_cast<int>(workspaceNumber));
auto backColourEnum = defaultPalette.backgroundIndexToColour(static_cast<int>(workspaceNumber));
m_peakColour = QColor(peakColourEnum);
m_backColour = QColor(backColourEnum);
}

std::string PeakOverlayViewFactoryBase::getPlotXLabel() const
Expand Down
14 changes: 12 additions & 2 deletions Code/Mantid/MantidQt/SliceViewer/src/PeakPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ namespace MantidQt
m_foregroundMap.insert(std::make_pair(index++, QColor(Qt::darkRed)));
m_foregroundMap.insert(std::make_pair(index++, QColor(Qt::black)));
m_foregroundMap.insert(std::make_pair(index++, QColor(Qt::white)));
m_foregroundMap.insert(std::make_pair(index++, QColor(Qt::darkGray)));
m_backgroundMap = m_foregroundMap;
m_foregroundMap.insert(std::make_pair(index, QColor(Qt::darkGray)));

m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::green)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::darkMagenta)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::cyan)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::darkGreen)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::darkCyan)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::darkYellow)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::darkRed)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::black)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::white)));
m_backgroundMap.insert(std::make_pair(index--, QColor(Qt::darkGray)));
}

PeakPalette::PeakPalette(const PeakPalette& other) : m_backgroundMap(other.m_backgroundMap), m_foregroundMap(other.m_foregroundMap)
Expand Down
6 changes: 3 additions & 3 deletions Code/Mantid/MantidQt/SliceViewer/src/PeaksViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace MantidQt

connect(widget, SIGNAL(peakColourChanged(Mantid::API::IPeaksWorkspace_const_sptr, QColor)), this, SLOT(onPeakColourChanged(Mantid::API::IPeaksWorkspace_const_sptr, QColor)));
connect(widget, SIGNAL(backgroundColourChanged(Mantid::API::IPeaksWorkspace_const_sptr, QColor)), this, SLOT(onBackgroundColourChanged(Mantid::API::IPeaksWorkspace_const_sptr, QColor)));
connect(widget, SIGNAL(backgroundRadiusShown(bool)), this, SLOT(onBackgroundRadiusShown(bool)));
connect(widget, SIGNAL(backgroundRadiusShown(Mantid::API::IPeaksWorkspace_const_sptr, bool)), this, SLOT(onBackgroundRadiusShown(Mantid::API::IPeaksWorkspace_const_sptr, bool)));
layout()->addWidget(widget);
++it;
}
Expand Down Expand Up @@ -91,9 +91,9 @@ namespace MantidQt
m_presenter->setBackgroundColour(peaksWS, newColour);
}

void PeaksViewer::onBackgroundRadiusShown(bool show)
void PeaksViewer::onBackgroundRadiusShown(Mantid::API::IPeaksWorkspace_const_sptr peaksWS, bool show)
{
m_presenter->setBackgroundRadiusShown(show);
m_presenter->setBackgroundRadiusShown(peaksWS, show);
}

} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace MantidQt

void PeaksWorkspaceWidget::onShowBackgroundChanged(bool show)
{
emit backgroundRadiusShown(show);
emit backgroundRadiusShown(this->m_ws, show);
}

} // namespace
Expand Down
46 changes: 24 additions & 22 deletions Code/Mantid/MantidQt/SliceViewer/src/PhysicalSphericalPeak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace MantidQt
else
{
m_cachedOpacityAtDistance = m_opacityMin;
m_backgroundOuterRadiusAtDistance.reset();
}
}

Expand All @@ -93,31 +94,27 @@ namespace MantidQt
*/
MantidQt::SliceViewer::SphericalPeakPrimitives PhysicalSphericalPeak::draw(const double& windowHeight, const double& windowWidth, const double& viewWidth, const double& viewHeight) const
{
SphericalPeakPrimitives drawingObjects = {0.0,0.0,0.0,0.0,Mantid::Kernel::V3D()};
if(this->isViewable())
{
// Scale factor for going from viewX to windowX
const double scaleY = windowHeight/viewHeight;
// Scale factor for going from viewY to windowY
const double scaleX = windowWidth/viewWidth;

const double innerRadiusX = scaleX * m_peakRadiusAtDistance;
const double innerRadiusY = scaleY * m_peakRadiusAtDistance;

double outerRadiusX = scaleX * m_peakRadius;
double outerRadiusY = scaleY * m_peakRadius;
SphericalPeakPrimitives drawingObjects = {};

const double lineWidthX = outerRadiusX - innerRadiusX;
const double lineWidthY = outerRadiusY - innerRadiusY;
outerRadiusX -= lineWidthX/2;
outerRadiusY -= lineWidthY/2;
// Scale factor for going from viewX to windowX
const double scaleY = windowHeight/viewHeight;
// Scale factor for going from viewY to windowY
const double scaleX = windowWidth/viewWidth;
drawingObjects.peakOpacityAtDistance = m_cachedOpacityAtDistance;
drawingObjects.peakOrigin = m_origin;

if(this->isViewablePeak())
{
// Create the return object.
drawingObjects.peakOuterRadiusX = outerRadiusX;
drawingObjects.peakOuterRadiusY = outerRadiusY;
drawingObjects.peakLineWidth = lineWidthX;
drawingObjects.peakOpacityAtDistance = m_cachedOpacityAtDistance;
drawingObjects.peakOrigin = m_origin;
drawingObjects.peakInnerRadiusX = scaleX * m_peakRadiusAtDistance;
drawingObjects.peakInnerRadiusY = scaleY * m_peakRadiusAtDistance;
}
if(this->isViewableBackground())
{
drawingObjects.backgroundOuterRadiusX = scaleX * m_backgroundOuterRadiusAtDistance.get();
drawingObjects.backgroundOuterRadiusY = scaleY * m_backgroundOuterRadiusAtDistance.get();
drawingObjects.backgroundInnerRadiusX = scaleX * m_backgroundInnerRadiusAtDistance;
drawingObjects.backgroundInnerRadiusY = scaleY * m_backgroundInnerRadiusAtDistance;
}
return drawingObjects;
}
Expand All @@ -126,5 +123,10 @@ namespace MantidQt
{
m_showBackgroundRadius = show;
}

bool PhysicalSphericalPeak::showBackgroundRadius() const
{
return m_showBackgroundRadius;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ namespace MantidQt
return m_compositePresenter->getTransformName();
}

void ProxyCompositePeaksPresenter::setBackgroundRadiusShown(const bool shown)
void ProxyCompositePeaksPresenter::setBackgroundRadiusShown(boost::shared_ptr<const Mantid::API::IPeaksWorkspace> ws, const bool shown)
{
m_compositePresenter->setBackgroundRadiusShown(shown);
m_compositePresenter->setBackgroundRadiusShown(ws, shown);
}
}
}
20 changes: 10 additions & 10 deletions Code/Mantid/MantidQt/SliceViewer/test/PeakPaletteTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ class PeakPaletteTest : public CxxTest::TestSuite
void test_default_backgroundIndexToColour()
{
PeakPalette palette;
TS_ASSERT_EQUALS(QColor(Qt::green), palette.backgroundIndexToColour(0));
TS_ASSERT_EQUALS(QColor(Qt::darkMagenta), palette.backgroundIndexToColour(1));
TS_ASSERT_EQUALS(QColor(Qt::cyan), palette.backgroundIndexToColour(2));
TS_ASSERT_EQUALS(QColor(Qt::darkGreen), palette.backgroundIndexToColour(3));
TS_ASSERT_EQUALS(QColor(Qt::darkCyan), palette.backgroundIndexToColour(4));
TS_ASSERT_EQUALS(QColor(Qt::darkYellow), palette.backgroundIndexToColour(5));
TS_ASSERT_EQUALS(QColor(Qt::darkRed), palette.backgroundIndexToColour(6));
TS_ASSERT_EQUALS(QColor(Qt::black), palette.backgroundIndexToColour(7));
TS_ASSERT_EQUALS(QColor(Qt::white), palette.backgroundIndexToColour(8));
TS_ASSERT_EQUALS(QColor(Qt::darkGray), palette.backgroundIndexToColour(9));
TS_ASSERT_EQUALS(QColor(Qt::green), palette.backgroundIndexToColour(9));
TS_ASSERT_EQUALS(QColor(Qt::darkMagenta), palette.backgroundIndexToColour(8));
TS_ASSERT_EQUALS(QColor(Qt::cyan), palette.backgroundIndexToColour(7));
TS_ASSERT_EQUALS(QColor(Qt::darkGreen), palette.backgroundIndexToColour(6));
TS_ASSERT_EQUALS(QColor(Qt::darkCyan), palette.backgroundIndexToColour(5));
TS_ASSERT_EQUALS(QColor(Qt::darkYellow), palette.backgroundIndexToColour(4));
TS_ASSERT_EQUALS(QColor(Qt::darkRed), palette.backgroundIndexToColour(3));
TS_ASSERT_EQUALS(QColor(Qt::black), palette.backgroundIndexToColour(2));
TS_ASSERT_EQUALS(QColor(Qt::white), palette.backgroundIndexToColour(1));
TS_ASSERT_EQUALS(QColor(Qt::darkGray), palette.backgroundIndexToColour(0));
}

void test_foregroundIndexToColour_throws_if_out_of_range()
Expand Down

0 comments on commit 1775fba

Please sign in to comment.