Skip to content

Commit

Permalink
(#187): Decouple Qt shapes from our internal logic (#181)
Browse files Browse the repository at this point in the history
* Separating shapes from the core
  • Loading branch information
nagaina authored Apr 1, 2023
1 parent cc10613 commit 3bf2d28
Show file tree
Hide file tree
Showing 26 changed files with 1,227 additions and 1,172 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ project (painter)
include (etc/bsys/cmake_glavni.cm)

add_subdirectory (${PROJECT_SOURCE_DIR}/src/io)
add_subdirectory (${PROJECT_SOURCE_DIR}/src/core/qt_shapes)
add_subdirectory (${PROJECT_SOURCE_DIR}/src/core/rq)
add_subdirectory (${PROJECT_SOURCE_DIR}/src/core)
add_subdirectory (${PROJECT_SOURCE_DIR}/src/commands)
add_subdirectory (${PROJECT_SOURCE_DIR}/src/gui)

add_executable(painter ${PROJECT_SOURCE_DIR}/src/main.cpp )
add_dependencies(painter rq io core commands gui )
target_link_libraries (painter gui rq io core commands rq Qt5::Widgets Qt5::Core Qt5::Gui ${LD_AUX})
add_dependencies(painter qt_shapes rq io core commands gui )
target_link_libraries (painter gui qt_shapes rq io core commands rq Qt5::Widgets Qt5::Core Qt5::Gui ${LD_AUX})
1 change: 1 addition & 0 deletions etc/bsys/projects.lst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
src/commands
src/core
src/core/qt_shapes
src/core/rq
src/gui
src/io
2 changes: 1 addition & 1 deletion src/commands/shape_creation_directive_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#include "direct_command_base.hpp"

#include "../core/qt_shapes/shape_creator.hpp"
#include "../core/rq/RegionQueryService.hpp"
#include "../core/runtime_pool.hpp"
#include "../core/shape_creator.hpp"

#include "../io/messenger.hpp"

Expand Down
2 changes: 1 addition & 1 deletion src/commands/shape_creation_interactive_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "shape_creation_directive_commands.hpp"

#include "../core/postman.hpp"
#include "../core/shape_creator.hpp"
#include "../core/qt_shapes/shape_creator.hpp"
#include "../gui/controller.hpp"
#include "../gui/statusbar_manager.hpp"

Expand Down
3 changes: 3 additions & 0 deletions src/core/qt_shapes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project (qt_shapes)
file(GLOB SOURCES "*.cpp")
add_library (qt_shapes STATIC ${SOURCES})
69 changes: 69 additions & 0 deletions src/core/qt_shapes/ellipse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "ellipse.hpp"

#include <QPainter>
#include <QPen>

#include <iostream>

///////////////////////////////////////////////////////////////////////////////
//
// @Ellipse implementation
//
Ellipse::Ellipse(QRect r, ShapeProperties b) : IShape(ELLIPSE, b), m_waitForSecondClick(false)
{
m_object = r;
}

Ellipse *Ellipse::clone()
{
return new Ellipse(m_object, m_properties);
}

void Ellipse::draw(QPainter *p)
{
QPen pen(m_properties.pen_color, m_properties.pen_width, m_properties.pen_style, m_properties.pen_cap_style,
m_properties.pen_join_style);
QBrush brush(m_properties.brush_color, m_properties.brush_style);
p->setBrush(brush);
p->setPen(pen);
p->drawEllipse(m_object);
}

void Ellipse::reset()
{
m_object.setRect(0, 0, 0, 0);
m_waitForSecondClick = false;
}

void Ellipse::setTopLeft(const QPoint &p)
{
m_object.setTopLeft(p);
}

void Ellipse::setBottomRight(const QPoint &p)
{
m_object.setBottomRight(p);
}

QPoint Ellipse::getTopLeft() const
{
return m_object.topLeft();
}

QPoint Ellipse::getBottomRight() const
{
return m_object.bottomRight();
}

void Ellipse::addPoint(const QPoint &point)
{
if (m_waitForSecondClick)
{
m_object.setBottomRight(point);
}
else
{
m_object.setTopLeft(point);
m_waitForSecondClick = true;
}
}
67 changes: 67 additions & 0 deletions src/core/qt_shapes/ellipse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

///////////////////////////////////////////////////////////////////////////////
//
// Includes
//
#include "../ishape.hpp"

// Qt
#include <QMouseEvent>
#include <QObject>
#include <QPoint>
#include <QPolygonF>

///////////////////////////////////////////////////////////////////////////////
//
// @class ellipse, wrapper for OA/Qt object
//
class Ellipse : public IShape
{
public:
Ellipse(QRect = QRect(), ShapeProperties = ShapeProperties());
virtual ~Ellipse() = default;

public:
Ellipse *clone() override;
void draw(QPainter *) override;

public:
void reset() override;
void addPoint(const QPoint &) override;

public:
void setTopLeft(const QPoint &);
void setBottomRight(const QPoint &);

QPoint getTopLeft() const;
QPoint getBottomRight() const;

bool contains(const QPoint &point) const
{
return m_object.contains(point);
}
bool intersects(const QRect &oRect) const
{
return m_object.intersects(oRect);
}

virtual ObjectType getType() const override
{
return ELLIPSE;
}

virtual void moveCenterToPoint(QPoint &p)
{
m_object.moveTo(p);
}

std::vector<QPoint> getPoints() override
{
return std::vector<QPoint>(2) = {getBottomRight(), getTopLeft()};
}

private:
QRect m_object;
bool m_waitForSecondClick;
};
69 changes: 69 additions & 0 deletions src/core/qt_shapes/line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "line.hpp"

#include <QPainter>
#include <QPen>

#include <iostream>

///////////////////////////////////////////////////////////////////////////////
//
// @Line implementation
//
Line::Line(QLine l, ShapeProperties p) : IShape(LINE, p), m_waitForSecondClick(false)
{
m_object = l;
}

Line *Line::clone()
{
return new Line(m_object, m_properties);
}

void Line::draw(QPainter *p)
{
QPen pen(m_properties.pen_color, m_properties.pen_width, m_properties.pen_style, m_properties.pen_cap_style,
m_properties.pen_join_style);
QBrush brush(m_properties.brush_color, m_properties.brush_style);
p->setBrush(brush);
p->setPen(pen);
p->drawLine(m_object);
}

void Line::reset()
{
m_object.setLine(0, 0, 0, 0);
m_waitForSecondClick = false;
}

void Line::addPoint(const QPoint &point)
{
if (m_waitForSecondClick)
{
m_object.setP2(point);
}
else
{
m_object.setP1(point);
m_waitForSecondClick = true;
}
}

void Line::setP1(const QPoint &p)
{
m_object.setP1(p);
}

void Line::setP2(const QPoint &p)
{
m_object.setP2(p);
}

QPoint Line::getP1() const
{
return m_object.p1();
}

QPoint Line::getP2() const
{
return m_object.p2();
}
68 changes: 68 additions & 0 deletions src/core/qt_shapes/line.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pragma once

///////////////////////////////////////////////////////////////////////////////
//
// Includes
//
#include "../ishape.hpp"

// Qt
#include <QLine>
#include <QObject>

// stl
#include <vector>

///////////////////////////////////////////////////////////////////////////////
//
// @class line, wrapper for OA/Qt object
//
class Line : public IShape
{
public:
Line(QLine = QLine(), ShapeProperties = ShapeProperties());
virtual ~Line() = default;

public:
Line *clone() override;
void draw(QPainter *) override;

public:
void reset() override;
void addPoint(const QPoint &) override;

private:
void setP1(const QPoint &);
void setP2(const QPoint &);

public:
QPoint getP1() const;
QPoint getP2() const;

virtual ObjectType getType() const override
{
return LINE;
}

virtual void moveCenterToPoint(QPoint &)
{
// m_object.moveTo(p);
}

#ifdef NO_RQ
virtual bool contains(const QPoint &point) const
{
return false;
}
#endif

// FIXME need proper fix and member handling
std::vector<QPoint> getPoints() override
{
return std::vector<QPoint>(2) = {m_object.p1(), m_object.p2()};
}

private:
QLine m_object;
bool m_waitForSecondClick;
};
59 changes: 59 additions & 0 deletions src/core/qt_shapes/polygon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "polygon.hpp"

#include <QPainter>
#include <QPen>

#include <iostream>

///////////////////////////////////////////////////////////////////////////////
//
// @polygon implementation
//
Polygon::Polygon(QPolygon p, ShapeProperties b) : IShape(POLYGON, b)
{
m_object = p;
}

Polygon *Polygon::clone()
{
return new Polygon(m_object, m_properties);
}

void Polygon::draw(QPainter *p)
{
QPen pen(m_properties.pen_color, m_properties.pen_width, m_properties.pen_style, m_properties.pen_cap_style,
m_properties.pen_join_style);
QBrush brush(m_properties.brush_color, m_properties.brush_style);
p->setBrush(brush);
p->setPen(pen);
p->drawPolygon(m_object);
}

void Polygon::reset()
{
QPolygon p;
m_object.swap(p);
}

void Polygon::addPoint(const QPoint &p)
{
if (m_first)
m_object << p;
m_object << p;
m_first = false;
}

void Polygon::movePoint(const QPoint &p)
{
m_object.setPoint(m_object.size() - 1, p);
}

QPoint Polygon::getTopLeft() const
{
return m_object.boundingRect().topLeft();
}

QPoint Polygon::getBottomRight() const
{
return m_object.boundingRect().bottomRight();
}
Loading

0 comments on commit 3bf2d28

Please sign in to comment.