-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathPolygonControl.cpp
More file actions
207 lines (191 loc) · 4.7 KB
/
PolygonControl.cpp
File metadata and controls
207 lines (191 loc) · 4.7 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
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
202
203
204
205
206
207
#include "PolygonControl.h"
PolygonControl::PolygonControl()
{
setPV = -1; curIdx = -1;
}
PolygonControl::PolygonControl(std::vector<Figure *> *figures):FigureControl(figures)
{
setPV = -1; curIdx = -1;
}
PolygonControl::PolygonControl(int width, int height):FigureControl(width, height)
{
setPV = -1; curIdx = -1;
}
bool PolygonControl::setFocus(Figure *fg)
{
for(int i=0;i<polygons.size();i++)
if(polygons[i]==fg)
{
curIdx = i;
return true;
}
return false;
}
void PolygonControl::onMousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
//选中折线起始点,成为多边形
Point curPoint(event->x(), height-event->y());
if(!curLines.empty() && curLines.front()->getBeginPoint().distanceTo(curPoint)<=10)
{
curLines.back()->setEndPoint(curLines.front()->getBeginPoint());
polygons.push_back(new MyPolygon(curLines));
curIdx = polygons.size()-1;
allFigures->push_back(polygons.back());
for(Line *line:curLines)
for(vector<Figure*>::iterator it=allFigures->begin();it!=allFigures->end();it++) //只有一个相同的,故只删除一次
if(*it==line)
{
it = allFigures->erase(it);
break;
}
curLines.erase(curLines.begin(), curLines.end());
setPV = -1;
return;
}
else if(curLines.empty() && !polygons.empty()) //选中当前多边形的标记点
{
vector<Point> vtxs = polygons[curIdx]->getVertexes();
for(int i=0;i<vtxs.size();i++)
if(curPoint.distanceTo(vtxs[i])<=5)
{
setPV = i;
pushForward(polygons[curIdx]);
return;
}
if(polygons[curIdx]->getCenter().distanceTo(curPoint)<=5)
{
setPV = -2;
pushForward(polygons[curIdx]);
return;
}
else if(polygons[curIdx]->getHandlePoint().distanceTo(curPoint)<=5)
{
setPV = -3;
pushForward(polygons[curIdx]);
return;
}
else if(polygons[curIdx]->isOn(curPoint))
{
pushForward(polygons[curIdx]);
return;
}
}
//未选中任何点,新建折线
curLines.push_back(new Line(Point(event->x(), height-event->y()), Point(event->x(), height-event->y())));
allFigures->push_back(curLines.back());
setPV = -1;
}
}
void PolygonControl::onMouseMoveEvent(QMouseEvent *event)
{
if(!polygons.empty())
{
Point curPoint(event->x(), height-event->y());
if(setPV>=0)
polygons[curIdx]->setVertex(setPV, curPoint);
else if(setPV==-2)
polygons[curIdx]->translate(curPoint - polygons[curIdx]->getCenter());
else if(setPV==-3)
polygons[curIdx]->setHandlePointByRef(curPoint);
}
}
void PolygonControl::onMousePassiveMoveEvent(QMouseEvent *event)
{
if(curLines.empty())
return;
curLines.back()->setEndPoint(Point(event->x(), height-event->y()));
}
void PolygonControl::onKeyPressEvent(QKeyEvent *event)
{
if(!curLines.empty())//TODO:改一下glwidget里面的,调试一下
return;
switch(event->key())
{
case Qt::Key_Left: polygons[curIdx]->translate(Point(-2,0)); break;
case Qt::Key_Right: polygons[curIdx]->translate(Point(2,0)); break;
case Qt::Key_Up: polygons[curIdx]->translate(Point(0,2)); break;
case Qt::Key_Down: polygons[curIdx]->translate(Point(0,-2)); break;
case Qt::Key_Q: polygons[curIdx]->rotate(-2); break;
case Qt::Key_E: polygons[curIdx]->rotate(2); break;
case Qt::Key_Plus: polygons[curIdx]->scale(1.25); break; //放大为原先的5/4
case Qt::Key_Minus: polygons[curIdx]->scale(0.8); break; //缩小为原先的4/5
default: ;
}
}
void PolygonControl::onDraw()
{
for(Line *line:curLines)
line->draw();
for(MyPolygon *polygon : polygons)
polygon->draw();
}
void PolygonControl::onMarkDraw()
{
if(!curLines.empty())
{
for(Line *line:curLines)
line->plainMarkDraw();
}
else if(curIdx>=0)
{
polygons[curIdx]->markDraw();
}
}
void PolygonControl::onFill()
{
if(curIdx>=0)
polygons[curIdx]->fillColor();
}
void PolygonControl::onCut(const Point &leftDown, int width, int height)
{
if(polygons.empty() || curIdx<0)
return;
if(polygons[curIdx]->cut(leftDown, width, height)==false)
{
deletePolygon(curIdx);
curIdx = -1;
}
}
void PolygonControl::onScale(double s)
{
if(curIdx>=0)
polygons[curIdx]->scale(s);
}
void PolygonControl::onDelete()
{
if(curIdx<0)
return;
deletePolygon(curIdx);
curIdx = -1;
}
void PolygonControl::onClear()
{
for(MyPolygon *polygon:polygons)
{
for(vector<Figure*>::iterator it=allFigures->begin();it!=allFigures->end();)
{
if(polygon==*it)
it = allFigures->erase(it);
else
it++;
}
delete polygon;
}
polygons.clear();
curIdx = -1;
}
void PolygonControl::deletePolygon(int idx)
{
if(idx<0)
return;
polygons.erase(polygons.begin()+idx);
for(vector<Figure*>::iterator it=allFigures->begin();it!=allFigures->end();it++)
if(*it==polygons[idx])
{
allFigures->erase(it);
break;
}
delete polygons[idx];
}