-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtool.cpp
More file actions
115 lines (106 loc) · 3.45 KB
/
tool.cpp
File metadata and controls
115 lines (106 loc) · 3.45 KB
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
#include <QPainter>
#include "tool.h"
#include "draw_area.h"
/**
* @brief PenTool::drawTo - Draws line from startPoint to endPoint, where
* startpoint is either:
*
* -where mouse was clicked (the initial left click)
* -where mouse was moved FROM (last event's endPoint)
*
* -endPoint is where the mouse was moved TO on this event.
*
*/
void PenTool::drawTo(const QPoint &endPoint, DrawArea *drawArea, QPixmap *image)
{
QPainter painter(image);
painter.setPen(static_cast<QPen>(*this));
painter.drawLine(getStartPoint(), endPoint);
// speed things up a bit by only updating the immediate
// radius of the DrawArea
int rad = (this->width() / 2) + 2;
drawArea->update(QRect(getStartPoint(), endPoint).normalized()
.adjusted(-rad, -rad, +rad, +rad));
setStartPoint(endPoint);
}
/**
* @brief LineTool::drawTo - Draws line from startPoint to endPoint, where:
* -startpoint is where mouse was clicked, and
* -endPoint is where the mouse was released
*
*/
void LineTool::drawTo(const QPoint &endPoint, DrawArea *drawArea, QPixmap *image)
{
QPainter painter(image);
painter.setPen(static_cast<QPen>(*this));
painter.drawLine(getStartPoint(), endPoint);
drawArea->update();
}
/**
* @brief RectTool::RectTool - Constructor for a rectangle tool.
*
*/
RectTool::RectTool(const QBrush &brush, qreal width, Qt::PenStyle s,
Qt::PenCapStyle c, Qt::PenJoinStyle j, QColor fill,
ShapeType shape, FillColor mode, int curve)
: Tool(brush, width, s, c, j)
{
fillColor = fill;
fillMode = mode;
shapeType = shape;
roundedCurve = curve;
}
/**
* @brief RectTool::drawTo - Draws shape from startPoint to endPoint, where:
* -startpoint is where mouse was clicked, and
* -endPoint is where the mouse was released
*
*/
void RectTool::drawTo(const QPoint &endPoint, DrawArea *drawArea, QPixmap *image)
{
QPainter painter(image);
painter.setPen(static_cast<QPen>(*this));
QRect rect = adjustPoints(endPoint);
//draw a rectangle, square, or ellipse--fill or no fill--based on settings
switch(shapeType)
{
case rectangle:
{
if(fillColor != no_fill)
painter.fillRect(rect, fillColor);
painter.drawRect(rect);
} break;
case rounded_rectangle:
{
if(fillMode != no_fill)
painter.setBrush(QBrush(fillColor));
painter.drawRoundedRect(rect, roundedCurve, roundedCurve,
Qt::RelativeSize); break;
}
case ellipse:
{
if(fillMode != no_fill)
painter.setBrush(QBrush(fillColor));
painter.drawEllipse(rect);
} break;
default:
break;
}
drawArea->update();
}
/**
* @brief RectTool::adjustPoints - adjusts the points when constructing
* a rectangle
*
*/
QRect RectTool::adjustPoints(const QPoint &endPoint)
{
// 'top left' and 'bottom right' are relative, so we may need to
// switch the points
QRect rect;
if(endPoint.x() < getStartPoint().x())
rect = QRect(endPoint, getStartPoint());
else
rect = QRect(getStartPoint(), endPoint);
return rect;
}