-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
223 lines (187 loc) · 7.88 KB
/
mainwindow.cpp
File metadata and controls
223 lines (187 loc) · 7.88 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
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2025 Andrey Yaroshenko.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License
* along with this program; if not, see https://www.gnu.org/licenses.
****************************************************************************/
#include "mainwindow.h"
#include <QAction>
#include <QCheckBox>
#include <QClipboard>
#include <QGuiApplication>
#include <QTimer>
#include <QVBoxLayout>
#include <helpers.h>
#include <rectangle.h>
#include <QGeoView/QGVLayerOSM.h>
#include <QGeoView/QGVWidgetCompass.h>
#include <QGeoView/QGVWidgetScale.h>
#include <QGeoView/QGVWidgetText.h>
#include <QGeoView/QGVWidgetZoom.h>
MainWindow::MainWindow()
{
setWindowTitle("QGeoView Samples - mouse actions");
setCentralWidget(new QWidget());
centralWidget()->setLayout(new QVBoxLayout());
Helpers::setupCachedNetworkAccessManager(this);
mMap = new QGVMap(this);
centralWidget()->layout()->addWidget(mMap);
// Widgets
mMap->addWidget(new QGVWidgetCompass());
mMap->addWidget(new QGVWidgetZoom());
mMap->addWidget(new QGVWidgetScale());
// Background layer
auto osmLayer = new QGVLayerOSM();
mMap->addItem(osmLayer);
// Custom layer
mItemsLayer = new QGVLayer();
mMap->addItem(mItemsLayer);
// Add items to custom layer
for (int i = 0; i < 10; i++) {
const int size = 50;
auto item = new Rectangle(Helpers::randRect(mMap, targetArea(), size), Qt::red);
item->setFlags(QGV::ItemFlag::IgnoreScale | QGV::ItemFlag::IgnoreAzimuth | QGV::ItemFlag::Movable);
item->setSelectable(true);
mItemsLayer->addItem(item);
}
// Options list
centralWidget()->layout()->addWidget(createOptionsList());
// Extra components for test
createContextMenu();
createTrackingWidget();
// Show target area
QTimer::singleShot(100, this, [this]() { mMap->cameraTo(QGVCameraActions(mMap).scaleTo(targetArea())); });
}
MainWindow::~MainWindow()
{
}
QGV::GeoRect MainWindow::targetArea() const
{
return QGV::GeoRect(QGV::GeoPos(51.848624, 14.7), QGV::GeoPos(51.743758, 14.9));
}
void MainWindow::createContextMenu()
{
static QVariant lastRect;
auto actPosition = new QAction("Print position", this);
mMap->addAction(actPosition);
connect(actPosition, &QAction::triggered, actPosition, [this]() {
auto cam = mMap->getCamera();
auto rect = cam.getProjection()->projToGeo(cam.projRect());
auto pos = cam.getProjection()->projToGeo(cam.projRect().center());
lastRect = QVariant::fromValue(rect);
qInfo() << "current geo-rect" << rect;
qInfo() << "current geo-center" << pos;
});
auto actFlyToLast = new QAction("Fly to last printed area", this);
mMap->addAction(actFlyToLast);
connect(actFlyToLast, &QAction::triggered, actFlyToLast, [this]() {
if (lastRect.isNull()) {
return;
}
mMap->flyTo(QGVCameraActions(mMap).scaleTo(lastRect.value<QGV::GeoRect>()));
});
auto actSelectView = new QAction("Select all (view)", this);
mMap->addAction(actSelectView);
connect(actSelectView, &QAction::triggered, actSelectView, [this]() {
mMap->unselectAll();
auto selection = mMap->search(mMap->getCamera().projRect());
for (auto item : selection) {
item->select();
}
});
auto actUnselect = new QAction("Unselect all", this);
mMap->addAction(actUnselect);
connect(actUnselect, &QAction::triggered, actUnselect, [this]() { mMap->unselectAll(); });
auto actFront = new QAction("Bring to front (selected)", this);
mMap->addAction(actFront);
connect(actFront, &QAction::triggered, actFront, [this]() {
for (QGVItem* item : mMap->getSelections()) {
item->bringToFront();
}
});
auto actBack = new QAction("Send to back (selected)", this);
mMap->addAction(actBack);
connect(actBack, &QAction::triggered, actBack, [this]() {
for (QGVItem* item : mMap->getSelections()) {
item->sendToBack();
}
});
auto actOpacityPlus = new QAction("+25% opacity (selected)", this);
mMap->addAction(actOpacityPlus);
connect(actOpacityPlus, &QAction::triggered, actOpacityPlus, [this]() {
for (QGVItem* item : mMap->getSelections()) {
item->setOpacity(item->getOpacity() + 0.25);
}
});
auto actOpacityMinus = new QAction("-25% opacity (selected)", this);
mMap->addAction(actOpacityMinus);
connect(actOpacityMinus, &QAction::triggered, actOpacityMinus, [this]() {
for (QGVItem* item : mMap->getSelections()) {
item->setOpacity(item->getOpacity() - 0.25);
}
});
auto actImage = new QAction("Copy as image", this);
mMap->addAction(actImage);
connect(actImage, &QAction::triggered, actPosition, [this]() {
QGuiApplication::clipboard()->setImage(mMap->grabMapView(true).toImage());
});
}
void MainWindow::createTrackingWidget()
{
// QGVWidgetText will be used to show current position.
QGVWidgetText* text = new QGVWidgetText();
text->setAnchor(QPoint(0, 0), { Qt::TopEdge });
mMap->addWidget(text);
connect(mMap, &QGVMap::mapMouseMove, text, [this, text](QPointF projPos) {
// Current projection position can be converted to geo-coordinates and printed by corresponding functions.
auto geoPos = mMap->getProjection()->projToGeo(projPos);
text->setText(QString("<b>%1, %2</b>").arg(geoPos.latToString()).arg(geoPos.lonToString()));
});
}
QGroupBox* MainWindow::createOptionsList()
{
const QList<QPair<QString, std::function<void(bool)>>> actions = {
{ "Move position tracking", [this](bool enabled) { startTracking(enabled); } },
{ "Map move (LButton hold + move)", [this](bool enabled) { enableAction(enabled, QGV::MouseAction::Move); } },
{ "Zoom with mouse wheel", [this](bool enabled) { enableAction(enabled, QGV::MouseAction::ZoomWheel); } },
{ "Zoom with selection rect (RButton hold + move)",
[this](bool enabled) { enableAction(enabled, QGV::MouseAction::ZoomRect); } },
{ "Item selection by mouse (single LButton or Shift/Ctrl + RButton hold + move)",
[this](bool enabled) { enableAction(enabled, QGV::MouseAction::Selection); } },
{ "Item move by mouse (Alt + LButton hold)",
[this](bool enabled) { enableAction(enabled, QGV::MouseAction::MoveObjects); } },
{ "Context menu", [this](bool enabled) { enableAction(enabled, QGV::MouseAction::ContextMenu); } },
{ "Show item tooltip (mouse hover)",
[this](bool enabled) { enableAction(enabled, QGV::MouseAction::Tooltip); } },
};
QGroupBox* groupBox = new QGroupBox(tr("Mouse options"));
groupBox->setLayout(new QVBoxLayout);
for (auto action : actions) {
auto name = action.first;
auto actionFunc = action.second;
QCheckBox* checkButton = new QCheckBox(name);
connect(checkButton, &QCheckBox::toggled, this, actionFunc);
groupBox->layout()->addWidget(checkButton);
checkButton->setChecked(true);
}
return groupBox;
}
void MainWindow::startTracking(bool start)
{
mMap->setMouseTracking(start);
}
void MainWindow::enableAction(bool enable, QGV::MouseAction action)
{
mMap->setMouseAction(action, enable);
}