Commit 8b75adce authored by Samuel Jackson's avatar Samuel Jackson
Browse files

Refs #16962 Serialise shapes

parent 9462b1f3
......@@ -136,6 +136,10 @@ public:
void requestRedraw(bool resetPeakVisibility = false);
/// Enable lighting if the implementation allows it
void enableLighting(bool on);
/// Load settings for the widget tab from a project file
virtual void loadFromProject(const std::string &lines);
/// Save settings for the widget tab to a project file
virtual std::string saveToProject() const;
//-----------------------------------
// Mask methods
......
......@@ -2,6 +2,7 @@
#define MANTIDPLOT_SHAPE2D_H_
#include "RectF.h"
#include "MantidQtAPI/TSVSerialiser.h"
#include <QColor>
#include <QPointF>
......@@ -97,6 +98,8 @@ public:
virtual QColor getColor() const { return m_color; }
/// Set fill color.
virtual void setFillColor(const QColor &color) { m_fill_color = color; }
/// Get fill color.
virtual QColor getFillColor() const { return m_fill_color; }
// --- Public methods --- //
......@@ -117,6 +120,10 @@ public:
void setSelected(bool on) { m_selected = on; }
/// Is shape selected?
bool isSelected() const { return m_selected; }
/// Load settings for the widget tab from a project file
static Shape2D *loadFromProject(const std::string &lines);
/// Save settings for the widget tab to a project file
virtual std::string saveToProject() const;
// --- Properties. for gui interaction --- //
......@@ -171,6 +178,11 @@ protected:
bool m_editing; ///< shape is being edited
bool m_selected; ///< shape is selected
bool m_visible; ///< flag to show or hide the shape
private:
static Shape2D* loadShape2DRectangle(const std::string &lines);
static Shape2D* loadShape2DRing(const std::string &lines);
static Shape2D* loadShape2DFree(const std::string &lines);
};
/**
......@@ -195,6 +207,10 @@ public:
QStringList getPointNames() const override { return QStringList("center"); }
QPointF getPoint(const QString &prop) const override;
void setPoint(const QString &prop, const QPointF &value) override;
/// Load settings for the shape from a project file
static Shape2D* loadFromProject(const std::string &lines);
/// Save settings for the widget tab to a project file
virtual std::string saveToProject() const override;
protected:
void drawShape(QPainter &painter) const override;
......@@ -217,6 +233,10 @@ public:
return m_boundingRect.contains(p);
}
void addToPath(QPainterPath &path) const override;
/// Load settings for the shape from a project file
static Shape2D* loadFromProject(const std::string &lines);
/// Save settings for the widget tab to a project file
virtual std::string saveToProject() const override;
protected:
void drawShape(QPainter &painter) const override;
......@@ -248,6 +268,11 @@ public:
void setPoint(const QString &prop, const QPointF &value) override;
void setColor(const QColor &color) override;
QColor getColor() const override { return m_outer_shape->getColor(); }
const Shape2D *getOuterShape() const { return m_outer_shape; }
/// Load settings for the shape from a project file
static Shape2D* loadFromProject(const std::string &lines);
/// Save settings for the widget tab to a project file
virtual std::string saveToProject() const override;
protected:
void drawShape(QPainter &painter) const override;
......@@ -272,12 +297,17 @@ protected:
class Shape2DFree : public Shape2D {
public:
explicit Shape2DFree(const QPointF &p);
explicit Shape2DFree(const QPolygonF &polygon);
Shape2D *clone() const override { return new Shape2DFree(*this); }
bool selectAt(const QPointF &p) const override;
bool contains(const QPointF &p) const override;
void addToPath(QPainterPath &path) const override;
void addPolygon(const QPolygonF &polygon);
void subtractPolygon(const QPolygonF &polygon);
/// Load settings for the shape from a project file
static Shape2D* loadFromProject(const std::string &lines);
/// Save settings for the widget tab to a project file
virtual std::string saveToProject() const override;
protected:
void drawShape(QPainter &painter) const override;
......
......@@ -85,6 +85,10 @@ public:
/// Change border color of all shapes.
void changeBorderColor(const QColor &color);
/// Load settings for the widget tab from a project file
virtual void loadFromProject(const std::string &lines);
/// Save settings for the widget tab to a project file
virtual std::string saveToProject() const override;
signals:
......
......@@ -1295,12 +1295,13 @@ std::string InstrumentWidget::saveToProject() const {
// serialise widget properties
tsv.writeLine("WorkspaceName") << getWorkspaceNameStdString();
tsv.writeLine("SurfaceType") << getSurfaceType();
tsv.writeRaw(getSurface()->saveToProject());
tsv.writeLine("CurrentTab") << getCurrentTab();
tsv.writeLine("EnergyTransfer") << m_xIntegration->getMinimum()
<< m_xIntegration->getMaximum();
// serialise widget subsections
tsv.writeSection("actor", m_instrumentActor->saveToProject());
tsv.writeRaw(m_instrumentActor->saveToProject());
tsv.writeSection("tabs", saveTabs());
return tsv.outputLines();
......@@ -1341,6 +1342,12 @@ void InstrumentWidget::loadFromProject(const std::string &lines) {
setSurfaceType(surfaceType);
}
if (tsv.selectSection("Surface")) {
std::string surfaceLines;
tsv >> surfaceLines;
getSurface()->loadFromProject(surfaceLines);
}
if (tsv.selectLine("CurrentTab")) {
int tab;
tsv >> tab;
......
......@@ -3,6 +3,7 @@
#include "MantidQtMantidWidgets/InstrumentView/GLColor.h"
#include "MantidQtMantidWidgets/InstrumentView/MantidGLWidget.h"
#include "MantidQtMantidWidgets/InstrumentView/OpenGLError.h"
#include "MantidQtAPI/TSVSerialiser.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/IPeaksWorkspace.h"
......@@ -716,5 +717,26 @@ QStringList ProjectionSurface::getPeaksWorkspaceNames() const {
}
return names;
}
void MantidQt::MantidWidgets::ProjectionSurface::loadFromProject(const std::string &lines)
{
TSVSerialiser tsv(lines);
if(tsv.selectSection("shapes")) {
std::string shapesLines;
tsv >> shapesLines;
m_maskShapes.loadFromProject(shapesLines);
}
}
std::string MantidQt::MantidWidgets::ProjectionSurface::saveToProject() const
{
TSVSerialiser tsv, surface;
surface.writeSection("shapes", m_maskShapes.saveToProject());
// surface.writeSection("peaks", m_peakShapes.saveToProject());
tsv.writeSection("surface", surface.outputLines());
return tsv.outputLines();
}
} // MantidWidgets
} // MantidQt
......@@ -155,6 +155,78 @@ bool Shape2D::isMasked(const QPointF &p) const {
return m_fill_color != QColor() && contains(p);
}
Shape2D* Shape2D::loadFromProject(const std::string &lines)
{
TSVSerialiser tsv(lines);
Shape2D *shape = nullptr;
if(tsv.selectLine("Type")) {
std::string type;
tsv >> type;
if(type == "ellipse") {
shape = Shape2DEllipse::loadFromProject(lines);
} else if (type == "rectangle") {
shape = Shape2DRectangle::loadFromProject(lines);
} else if (type == "ring") {
shape = Shape2DRing::loadFromProject(lines);
} else if (type == "free") {
shape = Shape2DFree::loadFromProject(lines);
}
if(shape && tsv.selectLine("Properties")) {
bool scalable, editing, selected, visible;
tsv >> scalable >> editing >> selected >> visible;
shape->setScalable(scalable);
shape->edit(editing);
shape->setSelected(selected);
shape->setVisible(visible);
}
if(shape && tsv.selectLine("Color")) {
int r, g, b, a;
tsv >> r >> g >> b >> a;
QColor color(r, g, b, a);
shape->setColor(color);
}
if(shape && tsv.selectLine("FillColor")) {
int r, g, b, a;
tsv >> r >> g >> b >> a;
QColor color(r, g, b, a);
shape->setFillColor(color);
}
}
return shape;
}
std::string Shape2D::saveToProject() const
{
TSVSerialiser tsv;
bool props[] {
m_scalable,
m_editing,
m_selected,
m_visible };
tsv.writeLine("Properties");
for(auto prop : props) {
tsv << prop;
}
auto color = getColor();
tsv.writeLine("Color");
tsv << color.red() << color.green() << color.blue() << color.alpha();
auto fillColor = getFillColor();
tsv.writeLine("FillColor");
tsv << fillColor.red() << fillColor.green() << fillColor.blue() << fillColor.alpha();
return tsv.outputLines();
}
// --- Shape2DEllipse --- //
Shape2DEllipse::Shape2DEllipse(const QPointF &center, double radius1,
......@@ -261,6 +333,26 @@ void Shape2DEllipse::setPoint(const QString &prop, const QPointF &value) {
}
}
Shape2D* Shape2DEllipse::loadFromProject(const std::string &lines) {
TSVSerialiser tsv(lines);
tsv.selectLine("Parameters");
double radius1, radius2, x, y;
tsv >> radius1 >> radius2 >> x >> y;
return new Shape2DEllipse(QPointF(x, y), radius1, radius2);
}
std::string Shape2DEllipse::saveToProject() const
{
TSVSerialiser tsv;
double radius1 = getDouble("radius1");
double radius2 = getDouble("radius2");
auto centre = getPoint("centre");
tsv.writeLine("Type") << "ellipse";
tsv.writeLine("Parameters") << radius1 << radius2 << centre.x(), centre.y();
tsv.writeRaw(Shape2D::saveToProject());
return tsv.outputLines();
}
// --- Shape2DRectangle --- //
Shape2DRectangle::Shape2DRectangle() { m_boundingRect = RectF(); }
......@@ -299,6 +391,30 @@ void Shape2DRectangle::addToPath(QPainterPath &path) const {
path.addRect(m_boundingRect.toQRectF());
}
Shape2D* Shape2DRectangle::loadFromProject(const std::string &lines) {
TSVSerialiser tsv(lines);
tsv.selectLine("Parameters");
double x0, y0, x1, y1;
tsv >> x0 >> y0 >> x1 >> y1;
QPointF point1(x0, y0);
QPointF point2(x1, y1);
return new Shape2DRectangle(point1, point2);
}
std::string Shape2DRectangle::saveToProject() const
{
TSVSerialiser tsv;
auto x0 = m_boundingRect.x0();
auto x1 = m_boundingRect.x1();
auto y0 = m_boundingRect.y0();
auto y1 = m_boundingRect.y1();
tsv.writeLine("Type") << "rectangle";
tsv.writeLine("Parameters") << x0 << y0 << x1 << y1;
tsv.writeRaw(Shape2D::saveToProject());
return tsv.outputLines();
}
// --- Shape2DRing --- //
Shape2DRing::Shape2DRing(Shape2D *shape, double xWidth, double yWidth)
......@@ -438,6 +554,35 @@ void Shape2DRing::setColor(const QColor &color) {
m_outer_shape->setColor(color);
}
Shape2D* Shape2DRing::loadFromProject(const std::string &lines) {
TSVSerialiser tsv(lines);
tsv.selectLine("Parameters");
double xWidth, yWidth;
tsv >> xWidth >> yWidth;
tsv.selectSection("shape");
std::string baseShapeLines;
tsv >> baseShapeLines;
auto baseShape = Shape2D::loadFromProject(baseShapeLines);
return new Shape2DRing(baseShape, xWidth, yWidth);
}
std::string Shape2DRing::saveToProject() const
{
TSVSerialiser tsv;
auto xWidth = getDouble("xwidth");
auto yWidth = getDouble("ywidth");
auto baseShape = getOuterShape();
tsv.writeLine("Type") << "ring";
tsv.writeLine("Parameters") << xWidth << yWidth;
tsv.writeSection("shape", baseShape->saveToProject());
tsv.writeRaw(Shape2D::saveToProject());
return tsv.outputLines();
}
//------------------------------------------------------------------------------
/// Construct a zero-sized shape.
......@@ -571,5 +716,42 @@ void Shape2DFree::subtractPolygon(const QPolygonF &polygon) {
m_polygon = m_polygon.subtracted(polygon);
resetBoundingRect();
}
Shape2D* Shape2DFree::loadFromProject(const std::string &lines) {
TSVSerialiser tsv(lines);
QPolygonF polygon;
size_t paramCount = tsv.values("Parameters").size()-1;
tsv.selectLine("Parameters");
for(size_t i = 0; i < paramCount; i+=2) {
double x, y;
tsv >> x >> y;
polygon << QPointF(x, y);
}
return new Shape2DFree(polygon);
}
std::string Shape2DFree::saveToProject() const
{
TSVSerialiser tsv;
tsv.writeLine("Type") << "free";
tsv.writeLine("Parameters");
for(auto& point : m_polygon) {
tsv << point.x() << point.y();
}
tsv.writeRaw(Shape2D::saveToProject());
return tsv.outputLines();
}
Shape2DFree::Shape2DFree(const QPolygonF &polygon)
: m_polygon(polygon)
{
resetBoundingRect();
}
} // MantidWidgets
} // MantidQt
......@@ -664,5 +664,24 @@ void Shape2DCollection::eraseFree(const QPolygonF &polygon) {
emit shapeChanged();
}
}
void Shape2DCollection::loadFromProject(const std::string &lines)
{
TSVSerialiser tsv(lines);
for (auto shapeLines : tsv.sections("shape")) {
Shape2D *shape = Shape2D::loadFromProject(shapeLines);
addShape(shape, false);
}
}
std::string Shape2DCollection::saveToProject() const
{
TSVSerialiser tsv;
for(auto shape : m_shapes) {
tsv.writeSection("shape", shape->saveToProject());
}
return tsv.outputLines();
}
} // MantidWidgets
} // MantidQt
\ No newline at end of file
} // MantidQt
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment