-
Notifications
You must be signed in to change notification settings - Fork 105
/
draw_freehand_tool.cpp
295 lines (224 loc) · 6.3 KB
/
draw_freehand_tool.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
/*
* Copyright 2014 Thomas Schöps
* Copyright 2014-2017 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include "draw_freehand_tool.h"
#include <Qt>
#include <QCursor>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPixmap>
#include <QRectF>
#include <QString>
#include "core/map.h"
#include "core/map_view.h"
#include "core/objects/object.h"
#include "gui/modifier_key.h"
#include "gui/map/map_editor.h"
#include "gui/map/map_widget.h"
#include "tools/tool.h"
namespace OpenOrienteering {
DrawFreehandTool::DrawFreehandTool(MapEditorController* editor, QAction* tool_action, bool is_helper_tool)
: DrawLineAndAreaTool(editor, DrawFreehand, tool_action, is_helper_tool)
{
// nothing else
}
DrawFreehandTool::~DrawFreehandTool() = default;
void DrawFreehandTool::init()
{
updateStatusText();
MapEditorTool::init();
}
const QCursor& DrawFreehandTool::getCursor() const
{
static auto const cursor = scaledToScreen(QCursor{ QPixmap(QString::fromLatin1(":/images/cursor-draw-path.png")), 11, 11 });
return cursor;
}
bool DrawFreehandTool::mousePressEvent(QMouseEvent* event, const MapCoordF& map_coord, MapWidget* widget)
{
Q_UNUSED(widget);
if (event->button() == Qt::LeftButton && !editingInProgress())
{
last_pos = cur_pos = event->pos();
cur_pos_map = map_coord;
startDrawing();
preview_path->addCoordinate(MapCoord(cur_pos_map));
hidePreviewPoints();
return true;
}
return false;
}
bool DrawFreehandTool::mouseMoveEvent(QMouseEvent* event, const MapCoordF& map_coord, MapWidget* widget)
{
Q_UNUSED(widget);
if (editingInProgress())
{
cur_pos = event->pos();
cur_pos_map = map_coord;
updatePath();
return true;
}
setPreviewPointsPosition(map_coord);
setDirtyRect();
return false;
}
bool DrawFreehandTool::mouseReleaseEvent(QMouseEvent* event, const MapCoordF& map_coord, MapWidget* widget)
{
Q_UNUSED(widget);
if (event->button() == Qt::LeftButton && editingInProgress())
{
if (preview_path->getCoordinateCount() < 2)
{
abortDrawing();
return true;
}
cur_pos = event->pos();
cur_pos_map = map_coord;
updatePath();
finishDrawing();
return true;
}
return false;
}
bool DrawFreehandTool::keyPressEvent(QKeyEvent* event)
{
switch (event->key())
{
case Qt::Key_Escape:
abortDrawing();
return true;
case Qt::Key_Tab:
deactivate();
return true;
}
return false;
}
void DrawFreehandTool::draw(QPainter* painter, MapWidget* widget)
{
drawPreviewObjects(painter, widget);
}
void DrawFreehandTool::finishDrawing()
{
updateStatusText();
// Clean up path: remove superfluous points
if (preview_path->getCoordinateCount() > 2)
{
// Use 999 instead of 1000, and save the rounding.
split_distance_sq = editor->getMainWidget()->getMapView()->pixelToLength(1) / 999;
split_distance_sq *= split_distance_sq;
point_mask.assign(preview_path->getCoordinateCount(), false);
point_mask.front() = true;
point_mask.back() = true;
checkLineSegment(0, point_mask.size() - 1);
for (auto i = point_mask.size() - 1; i != 0; )
{
--i;
if (!point_mask[i])
preview_path->deleteCoordinate(i, false);
}
}
DrawLineAndAreaTool::finishDrawing();
// Do not add stuff here as the tool might get deleted in DrawLineAndAreaTool::finishDrawing()!
}
void DrawFreehandTool::abortDrawing()
{
updateStatusText();
DrawLineAndAreaTool::abortDrawing();
}
void DrawFreehandTool::checkLineSegment(std::size_t first, std::size_t last)
{
if (last <= first + 1)
return;
// 'best_index' indicates the point on the path between first and last
// with the highest distance from the direct line between first and last.
auto best_index = first;
// This block ensures the stack is cleaned up before going into recursion.
{
auto max_distance_sq = qreal(0);
const auto start_coord = MapCoordF(preview_path->getRawCoordinateVector()[first]);
const auto end_coord = MapCoordF(preview_path->getRawCoordinateVector()[last]);
auto tangent = end_coord - start_coord;
tangent.normalize();
for (auto i = first + 1; i < last; ++i)
{
const auto coord = MapCoordF(preview_path->getRawCoordinateVector()[i]);
const auto to_coord = coord - start_coord;
qreal distance_sq;
auto dist_along_line = MapCoordF::dotProduct(to_coord, tangent);
if (dist_along_line <= 0)
{
distance_sq = to_coord.lengthSquared();
}
else if (dist_along_line >= end_coord.distanceTo(start_coord))
{
distance_sq = coord.distanceSquaredTo(end_coord);
}
else
{
auto right = tangent.perpRight();
distance_sq = qAbs(MapCoordF::dotProduct(right, to_coord));
distance_sq = distance_sq*distance_sq;
}
if (distance_sq > max_distance_sq)
{
max_distance_sq = distance_sq;
best_index = i;
}
}
// Make new segment?
if (max_distance_sq < split_distance_sq)
return;
}
point_mask[best_index] = true;
checkLineSegment(first, best_index);
checkLineSegment(best_index, last);
}
void DrawFreehandTool::updatePath()
{
if ((last_pos - cur_pos).manhattanLength() <= 2)
return;
preview_path->addCoordinate(MapCoord(cur_pos_map));
last_pos = cur_pos;
updatePreviewPath();
setDirtyRect();
}
void DrawFreehandTool::setDirtyRect()
{
QRectF rect;
includePreviewRects(rect);
if (is_helper_tool)
{
emit dirtyRectChanged(rect);
}
else if (rect.isValid())
{
map()->setDrawingBoundingBox(rect, 0, true);
}
else
{
map()->clearDrawingBoundingBox();
}
}
void DrawFreehandTool::updateStatusText()
{
QString text;
text = tr("<b>Drag</b>: Draw a path. ") +
OpenOrienteering::MapEditorTool::tr("<b>%1</b>: Abort. ").arg(ModifierKey::escape());
setStatusBarText(text);
}
} // namespace OpenOrienteering