-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrectangle.cpp
136 lines (114 loc) · 3.18 KB
/
rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "rectangle.h"
#include "scene.h"
#include "line.h"
Rectangle::Rectangle(const QPoint &topLeft, const QPoint &topRight, const QPoint &bottomLeft, const QPoint &bottomRight, Scene *scene, QGraphicsItem *parent)
: Item(scene, parent),
topLeft(topLeft), topRight(topRight), bottomLeft(bottomLeft), bottomRight(bottomRight)
{
drawRectangle();
}
Rectangle::Rectangle(const QPoint &topLeft, const QSize &size, Scene *scene, QGraphicsItem *parent)
: Item(scene, parent), topLeft(topLeft), topRight(topLeft.x() + size.width(), topLeft.y()),
bottomLeft(topLeft.x(), topLeft.y() - size.height()),
bottomRight(topLeft.x() + size.width(), topLeft.y() - size.height())
{
drawRectangle();
}
QRectF Rectangle::boundingRect() const
{
const int thickness = this->scene->getThickness();
int minX = std::min({topLeft.x(), topRight.x(), bottomLeft.x(), bottomRight.x()});
int minY = std::min({topLeft.y(), topRight.y(), bottomLeft.y(), bottomRight.y()});
int maxX = std::max({topLeft.x(), topRight.x(), bottomLeft.x(), bottomRight.x()});
int maxY = std::max({topLeft.y(), topRight.y(), bottomLeft.y(), bottomRight.y()});
return QRectF(toScenePos({minX, maxY}),
toScenePos({maxX, minY})).adjusted(-1, -1, thickness, thickness);
}
QPainterPath Rectangle::shape() const{
return path + fillPath;
}
void Rectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Item::paint(painter, option, widget);
painter->fillPath(fillPath, fillColor);
}
void Rectangle::drawRectangle()
{
for(const auto &point : Drawer::drawRect(topLeft, topRight, bottomLeft, bottomRight)){
drawPixel(point);
}
}
void Rectangle::fillRectangle()
{
fillPath = QPainterPath();
for(const auto &point : Drawer::floodFill(
Drawer::drawRect(topLeft, topRight, bottomLeft, bottomRight), getCenter())){
drawPixel(point, fillPath);
}
}
void Rectangle::setFillColor(const QColor &value)
{
fillColor = value;
reDraw();
}
QPoint Rectangle::getBottomRight() const
{
return bottomRight;
}
void Rectangle::setBottomRight(const QPoint &value)
{
bottomRight = value;
}
int Rectangle::getWidth() const
{
return std::abs(topRight.x() - topLeft.x());
}
int Rectangle::getHeight() const
{
return std::abs(topLeft.y() - bottomLeft.y());
}
QPoint Rectangle::getCenter() const
{
return QPointF((topLeft + topRight + bottomLeft + bottomRight) / 4.0).toPoint();
}
QPoint Rectangle::getBottomLeft() const
{
return bottomLeft;
}
void Rectangle::setBottomLeft(const QPoint &value)
{
bottomLeft = value;
}
QPoint Rectangle::getTopRight() const
{
return topRight;
}
void Rectangle::setTopRight(const QPoint &value)
{
topRight = value;
}
QPoint Rectangle::getTopLeft() const
{
return topLeft;
}
void Rectangle::setTopLeft(const QPoint &value)
{
topLeft = value;
}
void Rectangle::reDraw()
{
Item::reDraw();
path = QPainterPath();
drawRectangle();
if (fillColor != Qt::color0) fillRectangle();
this->update();
scene->update();
}
Item::Type Rectangle::getType() const
{
return Type::RECT;
}
QColor Rectangle::getFillColor() const
{
return fillColor;
}