-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathscale_tool.cpp
More file actions
249 lines (200 loc) · 6.14 KB
/
scale_tool.cpp
File metadata and controls
249 lines (200 loc) · 6.14 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
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
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2013-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 "scale_tool.h"
#include <Qt>
#include <QtGlobal>
#include <QCursor>
#include <QKeyEvent>
#include <QLocale>
#include <QPixmap>
#include <QRectF>
#include <QString>
#include "core/map.h"
#include "core/map_view.h"
#include "gui/map/map_widget.h"
#include "gui/util_gui.h"
#include "core/objects/object.h"
#include "gui/modifier_key.h"
#include "tools/tool.h"
#include "util/util.h"
namespace OpenOrienteering {
ScaleTool::ScaleTool(MapEditorController* editor, QAction* tool_action)
: MapEditorToolBase { scaledToScreen(QCursor{ QPixmap(QString::fromLatin1(":/images/cursor-scale.png")), 1, 1 }), Other, editor, tool_action }
{
// nothing else
}
ScaleTool::~ScaleTool() = default;
void ScaleTool::initImpl()
{
objectSelectionChangedImpl();
}
// This function contains translations. Keep it close to the top of the file so
// that line numbers remain stable here when changing other parts of the file.
void ScaleTool::updateStatusText()
{
if (editingInProgress())
{
if (uniform_scaling)
setStatusBarText(tr("<b>Scaling:</b> %1%").arg(QLocale().toString(qSqrt(scaling_factor.x()*scaling_factor.x() + scaling_factor.y()*scaling_factor.y()) * 100 / qSqrt(2), 'f', 1)));
else
setStatusBarText(tr("<b>Scaling:</b> x: %1% y: %2%").arg(QLocale().toString(scaling_factor.x() * 100, 'f', 1), QLocale().toString(scaling_factor.y() * 100, 'f', 1)));
}
else
{
auto status_text = tr("<b>Drag</b>: Scale the selected objects. ");
if (using_scaling_center)
status_text = tr("<b>Click</b>: Set the scaling center. ") +
status_text +
tr("<b>%1</b>: Switch to individual object scaling. ").arg(ModifierKey::control());
if (uniform_scaling)
status_text = status_text + tr("<b>%1</b>: Enable non-uniform scaling. ").arg(ModifierKey::shift());
setStatusBarText(status_text);
}
}
void ScaleTool::clickRelease()
{
scaling_center = cur_pos_map;
updateDirtyRect();
updateStatusText();
}
void ScaleTool::dragStart()
{
scaling_start_point = click_pos_map;
startEditing(map()->selectedObjects());
}
double absmax(double v, double max)
{
// a bipolar qMax() equivalent that returns v when v is larger than +max,
// or smaller than -max, otherwise returns +max or -max depending on sign of v
if (v >= 0)
return qMax(v, max);
else
return qMin(v, -max);
}
void ScaleTool::dragMove()
{
resetEditedObjects();
// minimum_length will replace any shorter length,
// in order to avoid extreme values and division by zero.
auto minimum_length = 1.0 / cur_map_widget->getMapView()->getZoom();
auto scaling_point = cur_pos_map - scaling_center;
auto reference_point = scaling_start_point - scaling_center;
if (uniform_scaling)
{
// scaling_point and reference_point are converted to contain the respective vector length in both x and y components,
// instead of having independent x and y components.
scaling_point.setX(qSqrt(scaling_point.x()*scaling_point.x() + scaling_point.y()*scaling_point.y()));
scaling_point.setY(scaling_point.x());
reference_point.setX(qSqrt(reference_point.x()*reference_point.x() + reference_point.y()*reference_point.y()));
reference_point.setY(reference_point.x());
}
scaling_factor.setX(absmax(scaling_point.x(), minimum_length) / absmax(reference_point.x(), minimum_length));
scaling_factor.setY(absmax(scaling_point.y(), minimum_length) / absmax(reference_point.y(), minimum_length));
if (using_scaling_center)
{
for (auto* object : editedObjects())
object->scale(scaling_center, scaling_factor.x(), scaling_factor.y());
}
else
{
for (auto* object : editedObjects())
object->scale(MapCoordF(object->getExtent().center()), scaling_factor.x(), scaling_factor.y());
}
updatePreviewObjects();
updateStatusText();
}
void ScaleTool::dragFinish()
{
finishEditing();
updateStatusText();
}
bool ScaleTool::keyPressEvent(QKeyEvent* event)
{
switch (event->key())
{
case Qt::Key_Control:
using_scaling_center = false;
updateStatusText();
updateDirtyRect();
return false; // not consuming Ctrl
case Qt::Key_Shift:
uniform_scaling = false;
updateStatusText();
updateDirtyRect();
return false;
}
return false;
}
bool ScaleTool::keyReleaseEvent(QKeyEvent* event)
{
switch (event->key())
{
case Qt::Key_Control:
using_scaling_center = true;
updateStatusText();
updateDirtyRect();
return false; // not consuming Ctrl
case Qt::Key_Shift:
uniform_scaling = true;
updateStatusText();
updateDirtyRect();
return false;
}
return false;
}
void ScaleTool::drawImpl(QPainter* painter, MapWidget* widget)
{
drawSelectionOrPreviewObjects(painter, widget);
if (using_scaling_center)
{
Util::Marker::drawCenterMarker(painter, widget->mapToViewport(scaling_center));
}
else
{
for (const auto* object : map()->selectedObjects())
Util::Marker::drawCenterMarker(painter, widget->mapToViewport(object->getExtent().center()));
}
}
int ScaleTool::updateDirtyRectImpl(QRectF& rect)
{
rectIncludeSafe(rect, scaling_center);
return 5;
}
void ScaleTool::objectSelectionChangedImpl()
{
if (editingInProgress())
{
abortEditing();
cancelDragging();
}
if (map()->getNumSelectedObjects() == 0)
{
deactivate();
}
else
{
// Set initial scaling center to the center of the bounding box of the selected objects
QRectF rect;
map()->includeSelectionRect(rect);
scaling_center = rect.center();
updateDirtyRect();
}
}
} // namespace OpenOrienteering