Skip to content

Commit

Permalink
Re #6162. Refactored 2d shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Jan 3, 2013
1 parent 8d2c5a5 commit 45f3601
Show file tree
Hide file tree
Showing 17 changed files with 471 additions and 199 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/MantidPlot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ set ( MANTID_SRCS src/Mantid/AbstractMantidLog.cpp
src/Mantid/InstrumentWidget/Projection3D.cpp
src/Mantid/InstrumentWidget/PeakMarker2D.cpp
src/Mantid/InstrumentWidget/PeakOverlay.cpp
src/Mantid/InstrumentWidget/RectF.cpp
src/Mantid/InstrumentWidget/OneCurvePlot.cpp
src/Mantid/InstrumentWidget/CollapsiblePanel.cpp
src/Mantid/InstrumentWidget/XIntegrationControl.cpp
Expand Down Expand Up @@ -447,6 +448,7 @@ set ( MANTID_HDRS src/Mantid/AbstractMantidLog.h
src/Mantid/InstrumentWidget/PeakOverlay.h
src/Mantid/InstrumentWidget/ProjectionSurface.h
src/Mantid/InstrumentWidget/Projection3D.h
src/Mantid/InstrumentWidget/RectF.h
src/Mantid/InstrumentWidget/OneCurvePlot.h
src/Mantid/InstrumentWidget/CollapsiblePanel.h
src/Mantid/InstrumentWidget/XIntegrationControl.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ void InstrumentWindowMaskTab::shapeChanged()
{
if (!m_left) return; // check that everything is ok
m_userEditing = false; // this prevents resetting shape proeprties by doubleChanged(...)
QRectF rect = m_instrWindow->getSurface()->getCurrentBoundingRect();
m_doubleManager->setValue(m_left,rect.left());
m_doubleManager->setValue(m_top,rect.top());
m_doubleManager->setValue(m_right,rect.right());
m_doubleManager->setValue(m_bottom,rect.bottom());
RectF rect = m_instrWindow->getSurface()->getCurrentBoundingRect();
m_doubleManager->setValue(m_left,rect.x0());
m_doubleManager->setValue(m_top,rect.y0());
m_doubleManager->setValue(m_right,rect.x1());
m_doubleManager->setValue(m_bottom,rect.y1());
for(QMap<QtProperty *,QString>::iterator it = m_doublePropertyMap.begin(); it != m_doublePropertyMap.end(); ++it)
{
m_doubleManager->setValue(it.key(),m_instrWindow->getSurface()->getCurrentDouble(it.value()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ m_freezePlot(false)
m_peakSelect->setCheckable(true);
m_peakSelect->setAutoExclusive(true);
m_peakSelect->setIcon(QIcon(":/PickTools/eraser.png"));
m_peakSelect->setToolTip("Select single crystal peak(s)");
m_peakSelect->setToolTip("Erase single crystal peak(s)");

QHBoxLayout* toolBox = new QHBoxLayout();
toolBox->addWidget(m_one);
Expand Down Expand Up @@ -630,7 +630,6 @@ void InstrumentWindowPickTab::addPeak(double x,double y)
{
using namespace Mantid::PhysicalConstants;

UNUSED_ARG(y)
if (!m_peak->isChecked() || m_currentDetID < 0) return;
Mantid::API::IPeaksWorkspace_sptr tw;
std::string peakTableName = "SingleCrystalPeakTable";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ m_row(-1)
{
m_markerSize = g_defaultMarkerSize;
}
const QPointF& centre = peakOverlay.realToUntransformed(QPointF(u,v));
m_boundingRect = QRectF(centre - QPointF((qreal)m_markerSize/2,(qreal)m_markerSize/2),
const QPointF& centre = QPointF(u,v);
m_boundingRect = RectF(centre - QPointF((qreal)m_markerSize/2,(qreal)m_markerSize/2),
QSizeF((qreal)m_markerSize,(qreal)m_markerSize));
setScalable(false);
std::cerr << "Centre " << centre.x() << ' ' << centre.y() << std::endl;
}

/**
Expand Down Expand Up @@ -72,12 +73,12 @@ void PeakMarker2D::drawShape(QPainter& painter) const
QFontMetrics fm(painter.font());
QRect r = fm.boundingRect(m_label);
m_labelRect = QRectF(r);
m_labelRect.moveTo(m_boundingRect.right() + m_markerSize,m_boundingRect.top() - m_markerSize);
m_labelRect.moveTo(m_boundingRect.x1() + m_markerSize,m_boundingRect.y1() - m_markerSize);
}

void PeakMarker2D::addToPath(QPainterPath& path) const
{
path.addRect(m_boundingRect);
path.addRect(m_boundingRect.toQRectF());
}

/// Set new marker size to s
Expand All @@ -93,7 +94,7 @@ void PeakMarker2D::setMarkerSize(const int& s)
void PeakMarker2D::drawCircle(QPainter& painter)const
{
QPainterPath path;
path.addEllipse(m_boundingRect);
path.addEllipse(m_boundingRect.toQRectF());
painter.fillPath(path,m_color);
}

Expand All @@ -108,7 +109,7 @@ void PeakMarker2D::drawDiamond(QPainter& painter)const
painter.rotate(45);
painter.translate(mdp);
QPainterPath path;
path.addRect(m_boundingRect);
path.addRect(m_boundingRect.toQRectF());
painter.fillPath(path,m_color);
painter.restore();
}
Expand All @@ -117,7 +118,7 @@ void PeakMarker2D::drawDiamond(QPainter& painter)const
void PeakMarker2D::drawSquare(QPainter& painter)const
{
QPainterPath path;
path.addRect(m_boundingRect);
path.addRect(m_boundingRect.toQRectF());
painter.fillPath(path,m_color);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@ void PeakOverlay::removeShapes(const QList<Shape2D*>& shapeList)
{
// vectors of rows to delete from the peaks workspace.
std::vector<size_t> rows;
std::cerr << "Removing ";
foreach(Shape2D* shape, shapeList)
{
PeakMarker2D* marker = dynamic_cast<PeakMarker2D*>(shape);
if ( !marker ) throw std::logic_error("Wrong shape type found.");
rows.push_back( static_cast<size_t>( marker->getRow() ) );
std::cerr << rows.back() << ' ';
}
std::cerr << std::endl;

// Run the DeleteTableRows algorithm to delete the peak.
auto alg = Mantid::API::AlgorithmManager::Instance().create("DeleteTableRows",-1);
Expand Down Expand Up @@ -214,6 +217,7 @@ void PeakOverlay::draw(QPainter& painter) const
if (!marker) continue;

QPointF p0 = marker->origin();
std::cerr << p0.x() << ' ' << p0.y() << std::endl;
QPointF p1 = m_transform.map(p0);
QRectF rect = marker->getLabelRect();
QPointF dp = rect.topLeft() - p0;
Expand Down Expand Up @@ -242,7 +246,7 @@ void PeakOverlay::draw(QPainter& painter) const
{
PeakHKL& hkl = m_labels[i];
hkl.draw(painter,m_precision);
//hkl.print();
hkl.print();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,11 @@ void Projection3D::rotate(int x, int y)
m_viewChanged = true;
}

QRectF Projection3D::getSurfaceBounds()const
RectF Projection3D::getSurfaceBounds()const
{
double xmin,xmax,ymin,ymax,zmin,zmax;
m_viewport->getInstantProjection(xmin,xmax,ymin,ymax,zmin,zmax);
return QRectF(QPointF(xmin,ymin),QPointF(xmax,ymax));
return RectF( QPointF(xmin, ymin), QPointF(xmax, ymax) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Projection3D : public ProjectionSurface
public:
Projection3D(const InstrumentActor* rootActor,int winWidth,int winHeight);
~Projection3D();
virtual QRectF getSurfaceBounds()const;
virtual RectF getSurfaceBounds()const;

void setViewDirection(const QString& vd);
void set3DAxesState(bool on);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void ProjectionSurface::clear()
m_pickImage = NULL;
}
m_viewChanged = true;
m_viewRect = QRectF();
m_viewRect = RectF();
m_selectRect = QRect();
}

Expand Down Expand Up @@ -172,7 +172,7 @@ void ProjectionSurface::draw(MantidGLWidget *widget,bool picking)const
if (!picking)
{
QPainter painter(widget);
QRectF windowRect = getSurfaceBounds();
RectF windowRect = getSurfaceBounds();
m_maskShapes.setWindow(windowRect,painter.viewport());
m_maskShapes.draw(painter);
for(int i=0;i < m_peakShapes.size(); ++i)
Expand All @@ -190,7 +190,7 @@ void ProjectionSurface::draw(MantidGLWidget *widget,bool picking)const
QPainter painter(widget);
painter.drawImage(0,0,**image);

QRectF windowRect = getSurfaceBounds();
RectF windowRect = getSurfaceBounds();
m_maskShapes.setWindow(windowRect,painter.viewport());
m_maskShapes.draw(painter);

Expand Down Expand Up @@ -250,7 +250,7 @@ void ProjectionSurface::drawSimple(QWidget* widget)const
QPainter painter(widget);
painter.drawImage(0,0,*m_viewImage);

QRectF windowRect = getSurfaceBounds();
RectF windowRect = getSurfaceBounds();
m_maskShapes.setWindow(windowRect,painter.viewport());
m_maskShapes.draw(painter);

Expand Down Expand Up @@ -356,7 +356,7 @@ QRect ProjectionSurface::selectionRect()const
return QRect(x_min,y_min,x_size,y_size);
}

QRectF ProjectionSurface::selectionRectUV()const
RectF ProjectionSurface::selectionRectUV()const
{
double left = static_cast<double>(m_selectRect.left());
double right = static_cast<double>(m_selectRect.right());
Expand All @@ -373,19 +373,17 @@ QRectF ProjectionSurface::selectionRectUV()const
std::swap( top, bottom );
}

if ( abs(m_selectRect.width()) <= 1 || abs(m_selectRect.height()) <= 1) return QRectF();
if ( abs(m_selectRect.width()) <= 1 || abs(m_selectRect.height()) <= 1) return RectF();

double x_min = left / m_viewImage->width();
double x_size = (right - left) / m_viewImage->width();
double y_min = top / m_viewImage->height();
double y_size = (bottom - top)/m_viewImage->height();
double sx = m_viewRect.xSpan() / m_viewImage->width();
double sy = m_viewRect.ySpan() / m_viewImage->height();

x_min = m_viewRect.left() + x_min * m_viewRect.width();
x_size = x_size * m_viewRect.width();
y_min = m_viewRect.top() + y_min * m_viewRect.height();
y_size = y_size * m_viewRect.height();
double x_min = left * sx + m_viewRect.x0();
double x_max = right * sx + m_viewRect.x0();
double y_min = (m_viewImage->height() - bottom) * sy + m_viewRect.y0();
double y_max = (m_viewImage->height() - top) * sy + m_viewRect.y0();

return QRectF(x_min,y_min,x_size,y_size);
return RectF( QPointF(x_min, y_min), QPointF(x_max, y_max) );
}

bool ProjectionSurface::hasSelection()const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "InstrumentActor.h"
#include "Shape2DCollection.h"
#include "PeakOverlay.h"
#include "RectF.h"
#include "../MantidAlgorithmMetatype.h"

#include <QImage>
Expand Down Expand Up @@ -71,7 +72,7 @@ class ProjectionSurface: public QObject
/// full update and redraw of the surface
virtual void updateDetectors();
/// returns the bounding rectangle in the real coordinates
virtual QRectF getSurfaceBounds()const{return m_viewRect;}
virtual RectF getSurfaceBounds()const{return m_viewRect;}

virtual void mousePressEvent(QMouseEvent*);
virtual void mouseMoveEvent(QMouseEvent*);
Expand Down Expand Up @@ -118,11 +119,11 @@ class ProjectionSurface: public QObject

/// Return bounding rect of the currently selected shape in the "original" coord system.
/// It doesn't depend on the zooming of the surface
QRectF getCurrentBoundingRect()const{return m_maskShapes.getCurrentBoundingRect();}
RectF getCurrentBoundingRect()const{return m_maskShapes.getCurrentBoundingRect();}

/// Set new bounding rect of the currently selected shape in the "original" coord system.
/// This method resizes the shape to fit into the new rectangle.
void setCurrentBoundingRect(const QRectF& rect){m_maskShapes.setCurrentBoundingRect(rect);}
void setCurrentBoundingRect(const RectF& rect){m_maskShapes.setCurrentBoundingRect(rect);}

/// Initialize interactive shape creation.
/// @param type :: Type of the shape. For available types see code of Shape2DCollection::createShape(const QString& type,int x,int y) const
Expand Down Expand Up @@ -227,7 +228,7 @@ protected slots:
void draw(MantidGLWidget* widget,bool picking)const;
void clear();
QRect selectionRect()const;
QRectF selectionRectUV()const;
RectF selectionRectUV()const;
int getDetectorIndex(unsigned char r,unsigned char g,unsigned char b)const;
int getDetectorID(unsigned char r,unsigned char g,unsigned char b)const;
QString getPickInfoText()const;
Expand All @@ -246,7 +247,7 @@ protected slots:
mutable QImage* m_pickImage; ///< storage for picking image
mutable bool m_viewChanged; ///< set when the image must be redrawn
QColor m_backgroundColor; ///< The background colour
QRectF m_viewRect; ///< Keeps the physical dimensions of the surface
RectF m_viewRect; ///< Keeps the physical dimensions of the surface
QRect m_selectRect;
int m_interactionMode; ///< mode of interaction - index in m_inputControllers

Expand Down
93 changes: 93 additions & 0 deletions Code/Mantid/MantidPlot/src/Mantid/InstrumentWidget/RectF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "RectF.h"


void RectF::moveCenter(const QPointF &p)
{
double xShift = xSpan() / 2;
double yShift = ySpan() / 2;
m_x0 = p.x() - xShift;
m_x1 = p.x() + xShift;
m_y0 = p.y() - yShift;
m_y1 = p.y() + yShift;
}

QPointF RectF::vertex(size_t i) const
{
switch(i)
{
case 0: return QPointF(m_x0, m_y0);
case 1: return QPointF(m_x0, m_y1);
case 2: return QPointF(m_x1, m_y1);
case 3: return QPointF(m_x1, m_y0);
}
throw std::range_error( "Rectangle vertex index is out of range" );
}

void RectF::setVertex(size_t i, const QPointF &p)
{
switch(i)
{
case 0: m_x0 = p.x(); m_y0 = p.y(); return;
case 1: m_x0 = p.x(); m_y1 = p.y(); return;
case 2: m_x1 = p.x(); m_y1 = p.y(); return;
case 3: m_x1 = p.x(); m_y0 = p.y(); return;
}
throw std::range_error( "Rectangle vertex index is out of range" );
}


bool RectF::contains(double x, double y) const
{
double dx = m_x0 < m_x1 ? x - m_x0 : x - m_x1;
if ( dx < 0 || dx > width() ) return false;
double dy = m_y0 < m_y1 ? y - m_y0 : y - m_y1;
if ( dy < 0 || dy > height() ) return false;
return true;
}

bool RectF::contains(const RectF &rect)
{
return contains( rect.p0() ) && contains( rect.p1() );
}

/**
* Create a transformation from this rectangle to a QRectF.
* @param trans :: The result transform.
* @param rect :: The transform's destination QRectF.
*/
void RectF::findTransform(QTransform& trans, const QRectF& rect)
{
double m11 = rect.width() / xSpan();
double m22 = -rect.height() / ySpan();
trans.reset();
trans.translate(rect.left(), rect.bottom());
trans.scale(m11, m22);
trans = trans.inverted();
}

void RectF::include(const QPointF &p)
{
if ( (p.x() - m_x0) / xSpan() < 0 )
{
m_x0 = p.x();
}
else if ( (p.x() - m_x1) / xSpan() > 0 )
{
m_x1 = p.x();
}
if ( (p.y() - m_y0) / ySpan() < 0 )
{
m_y0 = p.y();
}
else if ( (p.y() - m_y1) / ySpan() > 0 )
{
m_y1 = p.y();
}
}

void RectF::unite(const RectF &rect)
{
include(rect.p0());
include(rect.p1());
}

0 comments on commit 45f3601

Please sign in to comment.