forked from vojtechsimetka/QT-simple-vector-drawing-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openglwidget.cpp
1577 lines (1339 loc) · 41.2 KB
/
openglwidget.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* PGR 2013 project
* OpenGL widget class responsible for displaying drawing
*
* @author xskota05 Klara Vankova
* xsimet00 Vojtech Simetka
* @date 2013/11/26
* @version 1
* @file openglwidget.cpp
*/
#include "openglwidget.h"
#include "math.h"
#include "changeslog.h"
#include "mainwindow.h"
#include <QtDebug>
float OpenGLWidget::treshold_value = MINDISTANCE;
/**
* @brief OpenGL Widget constructor
* @param parent Reference to parent component
*/
OpenGLWidget::OpenGLWidget(Gui *gui, QWidget *parent)
: QGLWidget(parent)
, selection(new Selection(gui))
, offset(0, 0)
, aux_offset(0, 0)
, mouse_start_position(0, 0)
, mouse_end_position(0, 0)
, scale(1)
, gui(gui)
{
// Initializes status
this->status = SELECT_E;
this->catchStatus = CLASSIC;
// Creates data set
this->data = new Data();
// Init Changes log
ChangesLog::sharedInstance()->init(this->data, this->selection);
// Init dotted line
this->vertical_guideline = new GuideLine(1,0,0);
this->horizontal_guideline = new GuideLine(0,1,0);
// Sets mouse to be tracked even without any mouse button pressed
this->setMouseTracking(true);
}
/**
* @brief OpenGL Widget destructor
*/
OpenGLWidget::~OpenGLWidget()
{
// Unallocate data
delete this->data;
// Deletes selection
delete this->selection;
}
/**
* @brief Initializes OpenGL
*/
void OpenGLWidget::initializeGL()
{
// Sets backround color
glClearColor(1,1,1,1);
// Sets which matrix stack is used
glMatrixMode(GL_PROJECTION);
// Resizes OpenGL window
this->resizeGL(500,500);
// Disables Z-buffer
glDisable(GL_DEPTH_TEST);
// Load font
this->font = new Font();
}
/**
* @brief Repaints scene
*/
void OpenGLWidget::paintGL()
{
// Saves matrix
glPushMatrix();
glScalef(this->scale, this->scale, 1);
// Translates scene by offset
glTranslatef(this->offset.getX(),
this->offset.getY(),
0);
// Clean the screen
glClear(GL_COLOR_BUFFER_BIT);
// Paints all elements on screen
data->paintAll();
// Paints element that is being drawn or resized
this->metaElement.paintMe();
// Paints guiding lines
this->vertical_guideline->paintMe();
this->horizontal_guideline->paintMe();
this->selection->paintBoundingRectangle(this->translateX(this->mouse_end_position.getX()),
this->translateY(this->mouse_end_position.getY()));
// Restores matrix context
glPopMatrix();
// Draws selection rectangle
this->selection->paintMe();
this->paintRuler();
}
/**
* @brief Resizes OpenGL window
* @param w New width
* @param h New height
*/
void OpenGLWidget::resizeGL(int w, int h)
{
glLoadIdentity();
glOrtho (0, w, h, 0, 0, 1);
}
/**
* @brief Mouse pressed handler
* @param event Reference to event descriptor
*/
void OpenGLWidget::mouseReleaseEvent(QMouseEvent *event)
{
// Save mouse point
float x = this->translateX(event->x());
float y = this->translateY(event->y());
// Right mouse button release or pan tool and left mouse button release
if (event->button() == Qt::RightButton ||
(event->button() == Qt::LeftButton &&
this->status == PAN))
this->aux_offset.setLocation(0, 0);
// Left mouse button release
else if (event->button() == Qt::LeftButton)
{
switch(this->status)
{
case SELECT_E:
this->mouseReleaseSelect();
break;
case DRAW:
this->mouseReleaseDraw(x, y);
break;
default:
break;
}
}
// Repaint scene
this->repaint();
}
/**
* @brief Mouse pressed event handler
* @param event Reference to event descriptor
*/
void OpenGLWidget::mousePressEvent(QMouseEvent *event)
{
// Stores mouse position for drawing selection rectangle
this->mouse_start_position.setLocation(event->x(), event->y());
this->mouse_end_position.setLocation(event->x(), event->y());
// Left mouse button pressed
if (event->buttons() & Qt::LeftButton)
{
switch(this->status)
{
case SELECT_E:
try
{
this->mousePressSelect();
}
catch (QString* str)
{
qDebug()<<*str;
}
break;
default:
break;
}
}
}
/**
* @brief Implements drawing in mouse release
* @param x Coordinate x in model's coordinates system
* @param y Coordinate y in model's coordinates system
*/
void OpenGLWidget::mouseReleaseDraw(float x, float y)
{
// There isn't any element that is being drawn
if (this->metaElement.isEmpty())
{
this->createNewElement(x, y);
this->gui->setUndo(false);
this->gui->setRedo(false);
}
// There is element being drawn, finish it
else
{
// Save coordinates
Line *l = (Line *)this->metaElement.getElement();
x = l->getP2().getX();
y = l->getP2().getY();
switch (this->type)
{
case ElementType::CIRCLE:
case ElementType::POLYGON:
case ElementType::LINE:
// Add element to data structure
this->data->add(metaElement.getElement());
// Logs addition
ChangesLog::sharedInstance()->doStep(ADD,0,0,metaElement.getElement());
// Disables redo and enables undo
this->gui->setRedo(false);
// Dehighlight all elements
this->data->deHighlightAll();
// Clear dotted lines
this->vertical_guideline->invalidate();
this->horizontal_guideline->invalidate();
// Clear information in metaelement
this->metaElement.clear();
if (this->catchStatus != CLASSIC)
this->catchStatus = CLASSIC;
// Starts new element from same coordinates where we've finnished
this->createNewElement(x, y);
break;
case ElementType::SELECTION_RECTANGLE:
throw QString("There shall never be instance of selection rectangle as a drawable object");
break;
}
}
}
/**
* @brief Creates new element that is being drawn
* @param x Coordinate x of the newly created element
* @param y Coordinate y of the newly created element
*/
void OpenGLWidget::createNewElement(float x, float y)
{
Element *e = NULL;
// Determine what to do
switch (this->type)
{
case ElementType::LINE:
// Catch to close point
catchToClosePoint(&x,&y);
// Create new line
e = new Line(x,y,x,y);
break;
case ElementType::CIRCLE:
break;
case ElementType::POLYGON:
break;
case ElementType::SELECTION_RECTANGLE:
throw QString("There shall never be instance of selection rectangle as a drawable object");
break;
}
// Some element was created, set it for modification to metaelement
if (e != NULL)
this->metaElement.init(e,x,y);
}
QString OpenGLWidget::qKeyEventToQString(QKeyEvent *keyEvent)
{
switch(keyEvent->key())
{
case Qt::Key_0:
case Qt::Key_Launch0:
return "0";
break;
case Qt::Key_1:
case Qt::Key_Launch1:
return "1";
break;
case Qt::Key_2:
case Qt::Key_Launch2:
return "2";
break;
case Qt::Key_3:
case Qt::Key_Launch3:
return "3";
break;
case Qt::Key_4:
case Qt::Key_Launch4:
return "4";
break;
case Qt::Key_5:
case Qt::Key_Launch5:
return "5";
break;
case Qt::Key_6:
case Qt::Key_Launch6:
return "6";
break;
case Qt::Key_7:
case Qt::Key_Launch7:
return "7";
break;
case Qt::Key_8:
case Qt::Key_Launch8:
return "8";
break;
case Qt::Key_9:
case Qt::Key_Launch9:
return "9";
break;
}
return "";
}
/**
* @brief Keyboard pressed event handler
* @param keyEvent Reference to event descriptor
*/
void OpenGLWidget::keyPressEvent(QKeyEvent *keyEvent)
{
static float len = 0;
// Key was pressed
switch(keyEvent->key())
{
case Qt::Key_Space:
// Stop with drawing lines
this->catchStatus = CLASSIC;
if (this->status == DRAW && this->type == ElementType::LINE)
{
if (ChangesLog::sharedInstance()->canUndo())
this->gui->setUndo(true);
this->metaElement.clear();
this->data->deHighlightAll();
this->vertical_guideline->invalidate();
this->horizontal_guideline->invalidate();
this->gui->setLineEditText("");
this->repaint();
}
break;
case Qt::Key_P:
this->data->deHighlightAll();
this->vertical_guideline->invalidate();
this->horizontal_guideline->invalidate();
if (this->catchStatus != PARALLEL)
this->catchStatus = PARALLEL;
else this->catchStatus = CLASSIC;
break;
case Qt::Key_R:
this->data->deHighlightAll();
this->vertical_guideline->invalidate();
this->horizontal_guideline->invalidate();
if (this->catchStatus != PERPENDICULAR)
this->catchStatus = PERPENDICULAR;
else this->catchStatus = CLASSIC;
break;
case Qt::Key_M:
this->data->deHighlightAll();
this->vertical_guideline->invalidate();
this->horizontal_guideline->invalidate();
if (this->catchStatus != MIDDLE)
this->catchStatus = MIDDLE;
else this->catchStatus = CLASSIC;
break;
case Qt::Key_0:
case Qt::Key_Launch0:
case Qt::Key_1:
case Qt::Key_Launch1:
case Qt::Key_2:
case Qt::Key_Launch2:
case Qt::Key_3:
case Qt::Key_Launch3:
case Qt::Key_4:
case Qt::Key_Launch4:
case Qt::Key_5:
case Qt::Key_Launch5:
case Qt::Key_6:
case Qt::Key_Launch6:
case Qt::Key_7:
case Qt::Key_Launch7:
case Qt::Key_8:
case Qt::Key_Launch8:
case Qt::Key_9:
case Qt::Key_Launch9:
// Switch state to changeSize - stop drawing
if (this->status == DRAW && this->type == ElementType::LINE)
{
this->status = CHANGESIZE;
len = this->gui->getLineEditText().toFloat();
this->gui->setLineEditText("");
}
// Set new text
if (this->status == CHANGESIZE)
this->gui->appendTextToLineEdit(this->qKeyEventToQString(keyEvent));
break;
case Qt::Key_Enter:
case Qt::Key_Q:
if (this->status == CHANGESIZE)
{
this->vertical_guideline->invalidate();
this->horizontal_guideline->invalidate();
this->catchStatus = FIXEDLENGTH;
this->setAction(ElementType::LINE);
this->changeLength(this->gui->getLineEditText().toFloat());
this->repaint();
}
break;
case Qt::Key_Escape:
if (this->status == CHANGESIZE)
{
this->setAction(ElementType::LINE);
this->gui->setLineEditText(QString::number(len));
}
else
{
this->data->deHighlightAll();
this->vertical_guideline->invalidate();
this->horizontal_guideline->invalidate();
this->catchStatus = CLASSIC;
}
break;
case Qt::Key_Backspace:
case Qt::Key_Delete:
case Qt::Key_Back:
case Qt::Key_B:
if (this->status == CHANGESIZE)
this->gui->deleteLastCharOfLineEdit();
break;
case Qt::Key_Period:
case Qt::Key_Comma:
if (this->status == CHANGESIZE)
{
if (this->gui->getLineEditText().contains("."))
break;
this->gui->appendTextToLineEdit(".");
}
break;
}
}
/**
* @brief Mouse wheel event handler
* @param event Reference to event descriptor
*/
void OpenGLWidget::wheelEvent(QWheelEvent* event)
{
// Sets scale to new
float delta_scale = (float)(event->delta())/1000;
this->scale += delta_scale;
// Limit minimal maxification
if (this->scale < 0.1)
{
this->scale = 0.1;
this->treshold_value = MINDISTANCE / this->scale;
return;
}
// Limit maximal magnification
else if (this->scale > 50)
{
this->scale = 50;
this->treshold_value = MINDISTANCE / this->scale;
return;
}
// Reevaluates treshold value used for guiding functions
this->treshold_value = MINDISTANCE / this->scale;
// Alternates offset so that zooming mouse cursor stays on same place
this->offset.setLocation(this->offset.getX() - (delta_scale * event->x())/this->scale,
this->offset.getY() - (delta_scale * event->y())/this->scale);
// Repaints scene
this->repaint();
}
/**
* @brief Mouse moved event handler
* @param event Reference to event descriptor
*/
void OpenGLWidget::mouseMoveEvent(QMouseEvent *event)
{
// Save mouse coordinates
float x = this->translateX(event->x());
float y = this->translateY(event->y());
this->mouse_end_position.setLocation(event->x(), event->y());
if (!(event->buttons() & Qt::LeftButton ||
event->buttons() & Qt::RightButton))
this->mouse_start_position.setLocation(event->x(), event->y());
// Right button
if (event->buttons() & Qt::RightButton ||
(event->buttons() & Qt::LeftButton &&
this->status == PAN))
{
// Determines how far the mouse was dragged
float x1 = (event->x() - this->mouse_start_position.getX()) / this->scale;
float y1 = (event->y() - this->mouse_start_position.getY()) / this->scale;
// Determines change in x and y according to previous drag
float dx = (this->aux_offset.getX() - x1);
float dy = (this->aux_offset.getY() - y1);
// Sets previous drag
this->aux_offset.setLocation(this->aux_offset.getX() - dx,
this->aux_offset.getY() - dy);
// Sets new offset
this->offset.setLocation(this->offset.getX() - dx,
this->offset.getY() - dy);
}
// Left button
else
{
if (this->selection->isCentreOfRotationDragged())
this->selection->dragCentreOfRotation(x,y);
else if (this->selection->isRotationPointDragged())
this->selection->dragRotationPoint(x,y);
else if (this->selection->isResized())
{
this->selection->resizeSelectedItems(x, y);
}
// Some element is being modified, change its size
else if (!this->metaElement.isEmpty())
{
switch (this->status)
{
case DRAW:
this->mouseMoveDraw(&x, &y);
break;
default:
break;
}
if (this->catchStatus == FIXEDLENGTH)
{
float len = this->gui->getLineEditText().toFloat();
// Resizes element (change direction)
this->metaElement.resizeTo(x,y);
this->changeLength(len);
}
else if (this->status != CHANGESIZE)
{
// Resizes element
this->metaElement.resizeTo(x,y);
this->gui->setLineEditText(QString::number(this->metaElement.getOrigin().distance(x,y)));
}
}
// There is not any element being modified or drawn
else
{
switch(this->status)
{
case DRAW:
catchToClosePoint(&x,&y);
break;
case SELECT_E:
if (event->buttons() & Qt::LeftButton)
{
if (this->selection->isDragged())
this->selection->drag(x, y);
else
{
this->selection->resizeSelectionRectangle(this->mouse_start_position.getX(),
this->mouse_start_position.getY(),
this->mouse_end_position.getX(),
this->mouse_end_position.getY(),
this->offset,
this->scale);
// Select or deselect element
foreach (Element *e, this->data->getElements())
{
if (this->selection->selectionIntersects(e))
e->selectMe();
else
e->deSelectMe();
}
}
}
break;
default:
break;
}
}
}
// Repaint scene
this->repaint();
}
/**
* @brief Change line's direction and set specified length
* @param k New direction
* @param len Line's length
* @param x1 Line's first coordinate x
* @param y1 Line's first coordinate y
* @param x2 Line's second coordinate x
* @param y2 Line's second coordinate y
*/
void OpenGLWidget::changeLengthNotMoveMouse(float len, float x1, float y1, float *x2, float *y2)
{
if (len < 1)
return;
// Straight lines
if (y1 == *y2)
{
if (*x2 >= x1)
*x2 = x1 + len;
else *x2 = x1 - len;
return;
}
else if (x1 == *x2)
{
if (*y2 >= y1)
*y2 = y1 + len;
else *y2 = y1 - len;
return;
}
// Direction
float k = fabs((*y2 - y1) / (*x2 - x1));
// count dinstance for x2,y2 from start point
float dx = sqrt(pow(len,2.) / (pow(k,2.) + 1));
float dy = k * dx;
// 1st quadrant
if ((*x2 > x1) && (*y2 >= y1))
{
*x2 = x1 + dx;
*y2 = y1 + dy;
}
// 2nd quadrant
else if ((*x2 <= x1) && (*y2 > y1))
{
*x2 = x1 - dx;
*y2 = y1 + dy;
}
// 3rd quadrant
else if ((*x2 < x1) && (*y2 < y1))
{
*x2 = x1 - dx;
*y2 = y1 - dy;
}
// 4th quadrant
else if ((*x2 > x1) && (*y2 < y1))
{
*x2 = x1 + dx;
*y2 = y1 - dy;
}
}
/**
* @brief Find out if line specified with two coordinates is almost horizontal
* @param y1 First point's coordinate y
* @param y2 Second point's coordinate y
*/
bool OpenGLWidget::isHorizontal(float y1, float y2)
{
if (fabs(y2 - y1) < this->treshold_value)
return true;
return false;
}
/**
* @brief Find out if line specified with two coordinates is almost vertical
* @param x1 First point's coordinate x
* @param x2 Second point's coordinate x
*/
bool OpenGLWidget::isVertical(float x1, float x2)
{
if (fabs(x2 - x1) < this->treshold_value)
return true;
return false;
}
/**
* @brief Catches line to diagonal
* @param x1 Coordinate x of point of origin
* @param y1 Coordinate y of point of origin
* @param x2 Coordinate x of second point
* @param y2 Coordinate y of second point
* @return True if element catched to a diagonal
*/
bool OpenGLWidget::catchToDiagonal(float *x1, float *y1, float x2, float y2)
{
// Axis of 2nd and 4th quadrant
if (fabs((*x1 - x2) - (*y1 - y2)) < this->treshold_value)
{
int d = *x1 - x2;
*x1 = x2 + d;
*y1 = y2 + d;
return true;
}
// Axis of 1st and 3th quadrant
else if (fabs((x2 - *x1) - (*y1 - y2)) < this->treshold_value)
{
int d = x2 - *x1;
*x1 = x2 - d;
*y1 = y2 + d;
return true;
}
return false;
}
/**
* @brief Tests if there is a nearly perpendicular line to the line being drawn, makes drawn line perpendicular
* @param x11 Coordinate x of point of origin for line being drawn
* @param y11 Coordinate y of point of origin for line being drawn
* @param x21 Coordinate x of second point for line being drawn
* @param y21 Coordinate y of second point for line being drawn
* @return True if there is nearly perpendicular line to the one being drawn
*/
bool OpenGLWidget::catchToPerpendicular(float x11, float y11, float *x21, float *y21)
{
// Direction of first line
float k1 = (*y21 - y11) / (*x21 - x11);
bool perpendicularFound = false;
// Find if some line is perpendicular to current painted line
foreach (Element *element, this->data->getElements())
{
Line *e = (Line *)element;
// Get lines coordinates
float x12 = e->getP1().getX();
float y12 = e->getP1().getY();
float x22 = e->getP2().getX();
float y22 = e->getP2().getY();
if (!(((x12 == x11) && (y12 == y11))
|| ((x22 == x11) && (y22 == y11))))
continue;
// Direction of second line
float k2 = (y22 - y12) / (x22 - x12);
if (fabs(k1 + (1/k2)) < 0.5)
{
this->metaElement.highlightMe();
e->highlightMe();
perpendicularFound = true;
if (k1 > 1)
{
*x21 = (-k2 * (*y21 - y11)) + x11;
}
else
{
*y21 = ((*x21 - x11) / -k2) + y11;
}
}
}
if (!perpendicularFound)
{
this->metaElement.deHighlightMe();
this->data->deHighlightAll();
}
return perpendicularFound;
}
/**
* @brief Tests if there is a nearly parallel line to the line being drawn, makes drawn line parallel
* @param x11 Coordinate x of point of origin for line being drawn
* @param y11 Coordinate y of point of origin for line being drawn
* @param x21 Coordinate x of second point for line being drawn
* @param y21 Coordinate y of second point for line being drawn
* @return True if there is nearly parallel line to the one being drawn
*/
bool OpenGLWidget::catchToParallelLine(float x11, float y11, float *x21, float *y21)
{
bool parallelFound = false;
// Find if some line is parallel to current painted line
foreach (Element *element, this->data->getElements())
{
// First ratio
float firstRatio = (*y21 - y11) / (*x21 - x11);
// Get line
Line *e = (Line *) element;
// Get lines coordinates
float x12 = e->getP1().getX();
float y12 = e->getP1().getY();
float x22 = e->getP2().getX();
float y22 = e->getP2().getY();
// Second ratio
float secondRatio = (y22 - y12) / (x22 - x12);
// First ratio is almost equal to second ratio
if (fabs(secondRatio - firstRatio) < 0.1)
{
// Highlight lines
e->highlightMe();
// This is first parallel line found, change coordinates of the line being drawn
if (!parallelFound)
{
// Change y
if (firstRatio < 1)
// firstRatio must be equal to secondRatio
*y21 = (secondRatio * (*x21 - x11)) + y11;
// Change x
else
// firstRatio must be equal to secondRatio
*x21 = ((*y21 - y11) / secondRatio) + x11;
parallelFound = true;
this->metaElement.highlightMe();
}
}
// Lines are not parallel, gehighlight
else
e->deHighlightMe();
}
// There isn't any parallel line to the one being drawn, dehighlight it
if (!parallelFound)
this->metaElement.deHighlightMe();
return parallelFound;
}
// NEFUNGUJE - JEN NA VODOROVNY CARY
// TODO: PREPSAT!!
void OpenGLWidget::catchToMiddleOfLine(float x11, float y11, float *x, float *y)
{
bool middleFound = false;
// Find if some line is perpendicular to current painted line
foreach (Element *element, this->data->getElements())
{
Line *e = (Line *)element;
// Get lines coordinates
float x1 = e->getP1().getX();
float y1 = e->getP1().getY();
float x2 = e->getP2().getX();
float y2 = e->getP2().getY();
if (!(((x1 == x11) && (y1 == y11))
|| ((x2 == x11) && (y2 == y11))))
continue;
// Get coordinates of middle of line
float sx = x1 + (x2 - x1) / 2;
float sy = y1 + (y2 - y1) / 2;
// Direction from middle of line to point
float k1 = (*y - sy) / (*x - sx);
// Direction of line
float k2 = (y2 - y1) / (x2 - x1);
//qDebug() << "TEST";
if (fabs(k1 + (1/k2)) < 0.5)
{
//qDebug() << "MIDDLE";
this->metaElement.highlightMe();
e->highlightMe();
if (k1 > 1)
{
*x = (-k2 * (*y - sy)) + sx;
}
else
{
*y = ((*x - sx) / -k2) + sy;
}
}
}
}
/**
* @brief Find out if poit is close to another
* @param x Point's X coordinate
* @param y Point's Y coordinate
*/
void OpenGLWidget::catchToClosePoint(float *x, float *y)
{
// Get all elements in window
std::vector<Element *> allElements = this->data->getElements();
std::vector<Element *>::iterator it = allElements.begin();
bool closeXPointFound = false;
bool closeYPointFound = false;
float k;
float factor = *x - this->metaElement.getOrigin().getX();
if (fabs(factor) > 1)
k = ((*y - this->metaElement.getOrigin().getY())
/ (factor));
else k = INF;
// Find if some line is parallel to current painted line
for (; it != allElements.end(); ++it) {
// Get line
Line *e = (Line *)(*it);
// Get lines coordinates
float x1 = e->getP1().getX();
float y1 = e->getP1().getY();
float x2 = e->getP2().getX();
float y2 = e->getP2().getY();
// Close to line's first x coordinate
if (fabs(x1 - *x) < this->treshold_value)
{
// No line currently painting
if ((this->metaElement.isEmpty())
|| (k == INF)
|| (this->catchStatus == CLASSIC))
{