-
Notifications
You must be signed in to change notification settings - Fork 0
/
customcircle.cpp
201 lines (173 loc) · 5.71 KB
/
customcircle.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "customcircle.h"
CustomCircle::CustomCircle(){
m_isResizing = false;
}
CustomCircle::CustomCircle(qreal x, qreal y, qreal width, qreal height, qreal borderSize, QBrush brush, app::AppState *appState)
{
//set the member
this->setColor(brush.color());
this->setPoint(QPointF(x,y));
this->setWidth(width);
this->setHeight(height);
this->setBorderSize(borderSize);
m_isResizing = false;
//create a rectangle
setRect(x,y,width,height);
//create a pen for border color and border size
QPen pen;
pen.setWidth(3);
pen.setBrush(this->getColor());
setPen(pen);
setColor(brush.color());
this->setAppState(appState);
//setFlags
setFlag(QGraphicsItem::ItemIsSelectable, false);
setFlag(QGraphicsItem::ItemIsMovable, false);
}
QColor CustomCircle::getColor()
{
return this->m_color;
}
void CustomCircle::setColor(QColor color)
{
this->m_color = color;
}
QPointF CustomCircle::getPoint()
{
return this->m_point;
}
void CustomCircle::setPoint(QPointF point)
{
this->m_point = point;
}
qreal CustomCircle::getWidth()
{
return this->m_width;
}
void CustomCircle::setWidth(qreal width)
{
this->m_width = width;
}
qreal CustomCircle::getHeight()
{
return this->m_height;
}
void CustomCircle::setHeight(qreal height)
{
this->m_height = height;
}
qreal CustomCircle::getBorderSize()
{
return this->m_borderSize;
}
void CustomCircle::setBorderSize(qreal borderSize)
{
this->m_borderSize = borderSize;
}
app::AppState *CustomCircle::getAppState()
{
return this->m_appState;
}
void CustomCircle::setAppState(app::AppState *appState)
{
m_appState = appState;
}
void CustomCircle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
switch(this->m_appState->getSelectedTool()){
case app::RECTANGLE: {break;} // not used here
case app::CIRCLE: {
setFlag(QGraphicsItem::ItemIsMovable, false);
setFlag(QGraphicsItem::ItemIsSelectable, false);
break;
}
case app::TEXT: {break;} // not used here
case app::RECTANGLE_TEXT: {break;} // not used here
case app::CIRCLE_TEXT: {break;} // not used here
case app::POINT_TEXT: {break;} // not used here
case app::DELETE: {break;} // not used here // deletion in customScene
case app::SELECT: {
setFlag(QGraphicsItem::ItemIsMovable, false);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setSelected(true);
break;
}
case app::RESIZE: {
setFlag(QGraphicsItem::ItemIsSelectable, true);
qreal radius = boundingRect().width() / 2.0;
m_center = QPointF(boundingRect().topLeft().x()+pos().x()+radius, boundingRect().topLeft().y()+pos().y() + radius);
m_isResizing = true;
break;
}
case app::MOVE: {
setFlag(QGraphicsItem::ItemIsMovable, true);
break;
}
case app::PAINT: {
setFlag(QGraphicsItem::ItemIsMovable, false);
this->m_color = this->m_appState->getToolPropertieSelectedColor();
this->update();
break;
}
case app::RENAME: {break;} // not used here
case app::BORDER_SIZE: {
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsMovable, false);
qreal radius = boundingRect().width() / 2.0;
m_center = QPointF(boundingRect().topLeft().x()+pos().x()+radius, boundingRect().topLeft().y()+pos().y() + radius);
m_isResizing = true;
break;
}
default: {
qWarning() << "Something went wrong - CustomCircle/mousePressEvent";
break;
}
}
}
QGraphicsEllipseItem::mousePressEvent(event);
}
void CustomCircle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// resize functions
if(m_isResizing == true && this->m_appState->getSelectedTool() == app::RESIZE){
QPointF pos = event->scenePos();
qreal dist = sqrt(pow(m_center.x()-pos.x(),2)+pow(m_center.y()-pos.y(),2)); //calculate the distance between center and mouse
setRect(m_center.x()-this->pos().x()-dist, m_center.y()-this->pos().y()-dist, dist*2, dist*2);
this->setHeight(boundingRect().height());
this->setWidth(boundingRect().width());
} else if(m_isResizing == true && this->m_appState->getSelectedTool() == app::BORDER_SIZE){
QPointF pos = event->scenePos();
qreal dist = sqrt(pow(m_center.x()-pos.x(),2)+pow(m_center.y()-pos.y(),2));//calculate the distance between center and mouse
this->setBorderSize(dist/10);
QPen pen;
pen.setWidth(this->getBorderSize());
setPen(pen);
}
this->setPoint(boundingRect().topLeft());
QGraphicsEllipseItem::mouseMoveEvent(event);
}
void CustomCircle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton && m_isResizing){
m_isResizing = false;
}
if(event->button() == Qt::LeftButton && event->modifiers() != Qt::ShiftModifier){
QGraphicsItem::mouseReleaseEvent(event);
}
this->setPoint(boundingRect().topLeft());
}
void CustomCircle::update(){
//change color
QPen pen;
pen.setWidth(getBorderSize());
pen.setBrush(this->getColor());
setPen(pen);
//change size
setRect(getPoint().x(), getPoint().y() , getWidth(), getHeight());
QGraphicsEllipseItem::update();
}
int CustomCircle::type() const
{
return UserType+2;
}