forked from zsnjuts/PaintWorks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineControl.cpp
192 lines (176 loc) · 3.84 KB
/
LineControl.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
#include "LineControl.h"
LineControl::LineControl()
{
curLine = NULL;
}
LineControl::LineControl(std::vector<Figure *> *figures):FigureControl(figures)
{
curLine = NULL;
}
LineControl::LineControl(int width, int height):FigureControl(width, height)
{
curLine = NULL;
}
LineControl::LineControl(std::vector<Figure *> *figures, int width, int height):FigureControl(figures, width, height)
{
curLine = NULL;
}
bool LineControl::setFocus(Figure *fg)
{
for(Line *line:lines)
if(line==fg)
{
curLine = line;
return true;
}
return false;
}
void LineControl::onMousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
if(curLine!=NULL)
{
Point curPoint(event->x(), height-event->y());
if(curLine->getBeginPoint().distanceTo(curPoint)<=5)
{
setLP = SETBEGIN;
pushForward(curLine);
return;
}
else if(curLine->getEndPoint().distanceTo(curPoint)<=5)
{
setLP = SETEND;
pushForward(curLine);
return;
}
else if(curLine->getCenterPoint().distanceTo(curPoint)<=5)
{
setLP = SETCENTER;
pushForward(curLine);
return;
}
else if(curLine->getHandlePoint().distanceTo(curPoint)<=5)
{
setLP = SETHANDLE;
pushForward(curLine);
return;
}
else if(curLine->isOn(curPoint))
{
pushForward(curLine);
return;
}
}
curLine = new Line(Point(event->x(), height-event->y()), Point(event->x(), height-event->y()));
lines.push_back(curLine);
allFigures->push_back(curLine);
setLP = SETEND;
}
}
void LineControl::onMouseMoveEvent(QMouseEvent *event)
{
if (curLine == NULL)
return;
Point curPoint(event->x(), height-event->y());
switch(setLP)
{
case SETBEGIN: curLine->setBeginPoint(curPoint); break;
case SETEND: curLine->setEndPoint(curPoint); break;
case SETCENTER: curLine->translate(curPoint-curLine->getCenterPoint()); break;
case SETHANDLE: curLine->setHandlePointByRef(curPoint); break;
default: ;
}
}
void LineControl::onKeyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Left: curLine->translate(Point(-2,0)); break;
case Qt::Key_Right: curLine->translate(Point(2,0)); break;
case Qt::Key_Up: curLine->translate(Point(0,2)); break;
case Qt::Key_Down: curLine->translate(Point(0,-2)); break;
case Qt::Key_Q: curLine->rotate(-2); break;
case Qt::Key_E: curLine->rotate(2); break;
case Qt::Key_Plus: curLine->scale(1.25); break; //放大为原先的5/4
case Qt::Key_Minus: curLine->scale(0.8); break; //缩小为原先的4/5
default: ;
}
}
void LineControl::onDraw()
{
for(Line *line:lines)
line->draw();
}
void LineControl::onMarkDraw()
{
if(curLine!=NULL)
curLine->markDraw();
}
void LineControl::onCut(const Point &leftDown, int width, int height)
{
if(curLine==NULL)
return;
if(curLine->cut(leftDown, width, height)==false)
{
for(vector<Line*>::iterator it=lines.begin();it!=lines.end();it++)
if(curLine==*it)
{
lines.erase(it);
break;
}
for(vector<Figure*>::iterator it=allFigures->begin();it!=allFigures->end();it++)
if(curLine==*it)
{
allFigures->erase(it);
break;
}
delete curLine;
curLine = NULL;
}
}
void LineControl::onScale(double s)
{
if(curLine==NULL)
return;
curLine->scale(s);
}
void LineControl::onDelete()
{
if(curLine==NULL)
return;
for(vector<Line*>::iterator it=lines.begin();it!=lines.end();it++)
if(curLine==*it)
{
lines.erase(it);
break;
}
for(vector<Figure*>::iterator it=allFigures->begin();it!=allFigures->end();it++)
if(curLine==*it)
{
allFigures->erase(it);
break;
}
delete curLine;
curLine = NULL;
}
void LineControl::onClear()
{
for(Line *line:lines)
{
for(vector<Figure*>::iterator it=allFigures->begin();it!=allFigures->end();)
{
if(line==*it)
it = allFigures->erase(it);
else
it++;
}
delete line;
}
lines.clear();
curLine = NULL;
}
const vector<Line*> &LineControl::getLines()
{
return lines;
}