Newer
Older
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
208
209
210
211
212
213
214
215
216
217
218
#include "canvas.h"
#include "freehand.h"
#include "line.h"
#include "circle.h"
#include "rectangle.h"
#include "triangle.h"
bool CheckIfShapeIsAcceptable(GraphObj *obj){
// Free hand is allways true
FreeHand* freeHandObj;
if( (freeHandObj = dynamic_cast<FreeHand*>(obj)) ){return true;}
int length = std::sqrt( (obj->getStartPoint().x() - obj->getStopPoint().x()) *
(obj->getStartPoint().x() - obj->getStopPoint().x()) +
(obj->getStartPoint().y() - obj->getStopPoint().y()) *
(obj->getStartPoint().y() - obj->getStopPoint().y()) );
if(length > 20) return true;
else return false;
}
Canvas::Canvas(QWidget *parent): QFrame(parent){
setFrameStyle(QFrame::Box);
setMouseTracking(true);
scene = new Scene();
type = NONE;
dragging = false;
shape = nullptr;
}
Canvas::~Canvas(){
delete shape;
delete scene;
}
QSize Canvas::minimumSizeHint() const{
return QSize(200, 200);
}
QSize Canvas::sizeHint() const{
return QSize(640, 480);
}
void Canvas::clearCanvas(void){
scene->DeleteAllShape();
}
void Canvas::setPrimitiveMode(int mode){
type = (Canvas::PrimitiveMode)mode;
}
void Canvas::paintEvent(QPaintEvent *event){
QFrame::paintEvent(event); // parent class draws border
QPainter painter(this);
// draw all old Shapes
scene->draw(painter);
//draw the new Shape
if(shape != nullptr){shape->draw(painter);}
}
void Canvas::Create(QMouseEvent *event){
switch (type) {
case LINE:
shape = new Line();
break;
case CIRCLE:
shape = new Circle();
break;
case RECTANGLE:
shape = new Rectangle ();
break;
case FREE_HAND:
shape = new FreeHand ();
break;
case TRIANGLE:
shape = new Triangle();
break;
case NONE:
shape = nullptr;
break;
default:
break;
}
if(shape != nullptr){
shape->setStartPoint(event->pos());
shape->setStopPoint(event->pos());
shape->setColor(color);
shape->setOutline(outline);
}
}
void Canvas::Delete(QMouseEvent *event){
scene->deleteShape(event);
update();
}
void Canvas::Coloring(QMouseEvent *event){
scene->colorShape(event,getColor());
update();
}
void Canvas::Move(QPoint startPoint, QPoint endPoint ){
scene->moveShape(startPoint,endPoint);
update();
}
void Canvas::mousePressEvent(QMouseEvent *event){
if (event->button() == Qt::LeftButton) {
dragging = true;
// set The Points for the class Canvas (falls wir sie brauchen)
this->startPoint = event->pos();
this->StopPoint = event->pos();
switch (oparation) {
case CREAT:
Create(event);
break;
case DEL:
Delete(event);
break;
case COL:
Coloring(event);
break;
case TRAFO:
//set The Object that was selected
scene->setSelectedIndex(scene->selectObj(event->pos()));
Move( this->startPoint, this->StopPoint);
break;
default:
break;
}
update();
}
}
void Canvas::mouseMoveEvent(QMouseEvent *event){
if ((event->buttons() & Qt::LeftButton) && dragging) {
if(oparation == CREAT){
if(shape == nullptr){return;}
shape->setStopPoint(event->pos());
if(type == FREE_HAND && oparation == CREAT){
koordinate *k = new koordinate(shape->getStartPoint(),event->pos());
FreeHand* freeHandObj = dynamic_cast<FreeHand*>(shape);
freeHandObj->pointHestory.push_back(k);
shape->setStartPoint(event->pos());
}
}else if(oparation == TRAFO){
this->StopPoint = event->pos();
Move(this->startPoint,this->StopPoint);
this->startPoint = event->pos();
}
update();
}
}
void Canvas::mouseReleaseEvent(QMouseEvent *event){
if (event->button() == Qt::LeftButton && dragging) {
dragging = false;
//Create
if(shape != nullptr && type != NONE){
shape->setStopPoint(event->pos());
//Save Shape but need more Contorlle for how much big is my shape
if(CheckIfShapeIsAcceptable(shape)){
// Triangle* triangle;
// if( (triangle = dynamic_cast<Triangle*>(shape)) ){
// triangle->setReleasedClicked(true);
// }
if(shape->getDoneDrawing()){
scene->AddShape(shape);
shape = nullptr;
}
}
}
if(oparation == TRAFO){
this->StopPoint = event->pos();
Move(this->startPoint,this->StopPoint);
//reinitial The Object that was selected to -1
scene->setSelectedIndex(-1);
}
update();
}
}
void Canvas::setOparation(InteractionMode newOparation){
oparation = newOparation;
}
void Canvas::resizeEvent(QResizeEvent *event){
QFrame::resizeEvent(event);
}
bool Canvas::getOutline() const{
return outline;
}
void Canvas::setOutline(bool newOutline){
outline = newOutline;
}
QColor Canvas::getColor() const{
return color;
}
void Canvas::setColor(const QColor &newColor){
color = newColor;
}