-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkerWidget.cpp
More file actions
141 lines (128 loc) · 2.91 KB
/
Copy pathMarkerWidget.cpp
File metadata and controls
141 lines (128 loc) · 2.91 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
#include "MarkerWidget.h"
#include <qpainter.h>
#include <QWheelEvent>
#include <vector>
enum MarkerWidget::defaultMarkerParameters { dScale = 0, dCM = 16, dMS = 7 };
MarkerWidget::MarkerWidget(QWidget* parent) : QWidget(parent), scale(dScale), coordMultiplyer(dCM), markerSize(dMS)
{
this->lines = QVector<QLineF>();
}
void MarkerWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.eraseRect(1,1, this->width(), this->height());
painter.setPen(Qt::black);
painter.drawLines(this->lines);
}
void MarkerWidget::createMarker(QPointF p)
/**
* Creates a marker regarding current scale and adds it to drawing list
*/
{
int x1, x2, y1, y2;
QPoint offset = this->getOffset();
x1 = p.x() * coordMultiplyer + offset.x() + markerSize / 2;
y1 = p.y() * coordMultiplyer + offset.y();
x2 = x1 - markerSize + 1;
y2 = y1;
QLineF hLine(x1, y1, x2, y2);
x1 = p.x() * coordMultiplyer + offset.x();
y1 = p.y() * coordMultiplyer + offset.y() + markerSize / 2;
x2 = x1;
y2 = y1 - markerSize + 1;
QLineF vLine(x1, y1, x2, y2);
this->lines.append(hLine);
this->lines.append(vLine);
}
void MarkerWidget::render(std::vector<QPointF>& points)
/**
* Render new list of markers
*/
{
this->lines.clear();
this->scale = dScale;
this->coordMultiplyer = dCM;
this->markerSize = dMS;
this->points = std::vector<QPointF>(points);
for (QPointF p : this->points)
createMarker(p);
this->update();
scaleChanged();
}
void MarkerWidget::render()
/**
* Rerender same list of markers with different scale applied
*/
{
this->lines.clear();
for (QPointF p : this->points)
createMarker(p);
this->update();
}
std::string MarkerWidget::getScale()
/**
* Returns a string representing current scale
*/
{
return std::to_string(static_cast<int>(100.f * this->coordMultiplyer / dCM)) + std::string("%");
}
void MarkerWidget::wheelEvent(QWheelEvent* event)
/**
* Handle mouse wheel events inside the markerArea
*/
{
if (event->delta() > 0)
if (this->increaseScale())
{
render();
scaleChanged();
event->accept();
}
else event->ignore();
else if (event->delta() < 0)
{
if (this->decreaseScale())
{
render();
scaleChanged();
event->accept();
}
else event->ignore();
}
else event->ignore();
}
bool MarkerWidget::increaseScale()
/**
* Inscrease scale if not at maximal already (returns True if scale was changed)
*/
{
if (this->scale < maxScaleSteps)
{
this->scale++;
this->coordMultiplyer*=2;
this->markerSize+=2;
return true;
}
else return false;
}
bool MarkerWidget::decreaseScale()
/**
* Descrease scale if not at minimal already (returns True if scale was changed)
*/
{
if (this->scale > -maxScaleSteps)
{
this->scale--;
this->coordMultiplyer /= 2;
this->markerSize -= 2;
return true;
}
else return false;
}
QPoint MarkerWidget::getOffset()
/**
* Calculate cooridnates offset to shift (0,0) for markers to the center of the widget
*/
{
return QPoint(this->width() / 2, this->height() / 2);
}