-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEllipseControl.cpp
More file actions
154 lines (141 loc) · 3.43 KB
/
EllipseControl.cpp
File metadata and controls
154 lines (141 loc) · 3.43 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
#include "EllipseControl.h"
#include <GL/glu.h>
EllipseControl::EllipseControl()
{
curEllipse = NULL;
}
EllipseControl::EllipseControl(std::vector<Figure *> *figures):FigureControl(figures)
{
curEllipse = NULL;
}
EllipseControl::EllipseControl(int width, int height):FigureControl(width, height)
{
curEllipse = NULL;
}
bool EllipseControl::setFocus(Figure *fg)
{
for(MyEllipse *ellipse:ellipses)
if(ellipse==fg)
{
curEllipse = ellipse;
return true;
}
return false;
}
void EllipseControl::onMousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
if(curEllipse!=NULL)
{
Point curPoint(event->x(), height-event->y());
for(Point p:curEllipse->getMarkPoints())
if(p.distanceTo(curPoint)<=5)
{
setEP = MARKPOINT;
pushForward(curEllipse);
return;
}
if(curPoint.distanceTo(curEllipse->getCenter())<=5)
{
setEP = CENTERPOINT;
pushForward(curEllipse);
return;
}
else if(curPoint.distanceTo(curEllipse->getHandlePoint())<=5)
{
setEP = HANDLEPOINT;
pushForward(curEllipse);
return;
}
else if(curEllipse->isOn(curPoint))
{
pushForward(curEllipse);
return;
}
}
curEllipse = new MyEllipse(Point(event->x(), height-event->y()), Point(event->x(), height-event->y()));
ellipses.push_back(curEllipse);
allFigures->push_back(curEllipse);
setEP = ENDPOINT;
}
}
void EllipseControl::onMouseMoveEvent(QMouseEvent *event)
{
if (curEllipse == NULL)
return;
Point center = curEllipse->getCenter();
switch(setEP)
{
case ENDPOINT: curEllipse->setEndPoint(Point(event->x(), height-event->y())); break;
case MARKPOINT: curEllipse->setAxes(abs(event->x()-center.getX()), abs((height-event->y())-center.getY())); break;
case CENTERPOINT: curEllipse->translate(Point(event->x(), height-event->y()) - center); break;
case HANDLEPOINT: curEllipse->setHandlePointByRef(Point(event->x(), height-event->y())); break;
default: ;
}
}
void EllipseControl::onKeyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Left: curEllipse->translate(Point(-2,0)); break;
case Qt::Key_Right: curEllipse->translate(Point(2,0)); break;
case Qt::Key_Up: curEllipse->translate(Point(0,2)); break;
case Qt::Key_Down: curEllipse->translate(Point(0,-2)); break;
case Qt::Key_Q: curEllipse->rotate(-90); break;
case Qt::Key_E: curEllipse->rotate(90); break;
case Qt::Key_Plus: curEllipse->scale(1.25); break; //放大为原先的5/4
case Qt::Key_Minus: curEllipse->scale(0.8); break; //缩小为原先的4/5
default: ;
}
}
void EllipseControl::onDraw()
{
for(MyEllipse *ellipse : ellipses)
ellipse->draw();
}
void EllipseControl::onMarkDraw()
{
if(curEllipse!=NULL)
curEllipse->markDraw();
}
void EllipseControl::onScale(double s)
{
if(curEllipse!=NULL)
curEllipse->scale(s);
}
void EllipseControl::onDelete()
{
if(curEllipse==NULL)
return;
for(vector<MyEllipse*>::iterator it=ellipses.begin();it!=ellipses.end();it++)
if(curEllipse==*it)
{
ellipses.erase(it);
break;
}
for(vector<Figure*>::iterator it=allFigures->begin();it!=allFigures->end();it++)
if(curEllipse==*it)
{
allFigures->erase(it);
break;
}
delete curEllipse;
curEllipse = NULL;
}
void EllipseControl::onClear()
{
for(MyEllipse *ellipse:ellipses)
{
for(vector<Figure*>::iterator it=allFigures->begin();it!=allFigures->end();)
{
if(ellipse==*it)
it = allFigures->erase(it);
else
it++;
}
delete ellipse;
}
ellipses.clear();
curEllipse = NULL;
}