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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2010-09-18
* Description : A tool to calibrate the tiling levels
* used in geolocation interface
*
* SPDX-FileCopyrightText: 2010 by Michael G. Hansen <mike at mghansen dot de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "calibrator.h"
// Qt includes
#include <QAction>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
#include <QStandardItemModel>
#include <QTimer>
#include <QToolButton>
#include <QVBoxLayout>
#include <QApplication>
#include <QCommandLineParser>
#include <QLineEdit>
// Local includes
#include "digikam_debug.h"
#include "abstractmarkertiler.h"
#include "itemmarkertiler.h"
#include "mapwidget.h"
#include "geocoordinates.h"
#include "digikam_version.h"
using namespace Digikam;
const int CoordinatesRole = Qt::UserRole + 1;
class Q_DECL_HIDDEN CalibratorModelHelper::Private
{
public:
explicit Private()
: model(nullptr)
{
}
QStandardItemModel* model;
};
CalibratorModelHelper::CalibratorModelHelper(QStandardItemModel* const model,
QObject* const parent)
: GeoModelHelper(parent),
d (new Private)
{
d->model = model;
}
CalibratorModelHelper::~CalibratorModelHelper()
{
delete d;
}
QAbstractItemModel* CalibratorModelHelper::model() const
{
return d->model;
}
QItemSelectionModel* CalibratorModelHelper::selectionModel() const
{
return nullptr;
}
bool CalibratorModelHelper::itemCoordinates(const QModelIndex& index,
GeoCoordinates* const coordinates) const
{
if (!index.isValid())
return false;
const QVariant coordinatesVariant = index.data(CoordinatesRole);
GeoCoordinates itemCoordinates = coordinatesVariant.value<GeoCoordinates>();
if (coordinates)
*coordinates = itemCoordinates;
return itemCoordinates.hasCoordinates();
}
void CalibratorModelHelper::setItemCoordinates(const QModelIndex& index,
const GeoCoordinates& coordinates)
{
if (!index.isValid())
return;
d->model->setData(index, QVariant::fromValue(coordinates), CoordinatesRole);
}
GeoModelHelper::PropertyFlags CalibratorModelHelper::modelFlags() const
{
return FlagVisible;
}
// ---------------------------------------------------------------------------------------------
class Q_DECL_HIDDEN Calibrator::Private
{
public:
explicit Private()
: hBoxLayout (nullptr),
model (nullptr),
modelHelper (nullptr),
markerTiler (nullptr),
groupingMode (nullptr),
sbLevel (nullptr),
zoomDisplay (nullptr),
zoomDisplayTimer (nullptr)
{
}
QHBoxLayout* hBoxLayout;
QList<QPair<QWidget*, MapWidget*> > extraWidgetHolders;
QStandardItemModel* model;
CalibratorModelHelper* modelHelper;
ItemMarkerTiler* markerTiler;
QButtonGroup* groupingMode;
QSpinBox* sbLevel;
QLineEdit* zoomDisplay;
QTimer* zoomDisplayTimer;
};
Calibrator::Calibrator(QWidget* const parent)
: QMainWindow(parent),
d (new Private)
{
d->model = new QStandardItemModel(this);
d->modelHelper = new CalibratorModelHelper(d->model, this);
d->markerTiler = new ItemMarkerTiler(d->modelHelper, this);
QVBoxLayout* const vboxLayout1 = new QVBoxLayout();
QWidget* const dummy1 = new QWidget(this);
dummy1->setLayout(vboxLayout1);
setCentralWidget(dummy1);
d->hBoxLayout = new QHBoxLayout();
vboxLayout1->addLayout(d->hBoxLayout);
d->groupingMode = new QButtonGroup(this);
d->groupingMode->setExclusive(true);
QRadioButton* const buttonGrouped = new QRadioButton(QLatin1String("Grouped"), this);
d->groupingMode->addButton(buttonGrouped, 0);
QRadioButton* const buttonUngrouped = new QRadioButton(QLatin1String("Ungrouped"), this);
d->groupingMode->addButton(buttonUngrouped, 1);
buttonGrouped->setChecked(true);
d->sbLevel = new QSpinBox(this);
d->sbLevel->setMinimum(1);
d->sbLevel->setMaximum(TileIndex::MaxLevel);
QLabel* const labelsbLevel = new QLabel(QLatin1String("Level:"), this);
labelsbLevel->setBuddy(d->sbLevel);
d->zoomDisplay = new QLineEdit(this);
d->zoomDisplay->setReadOnly(true);
QLabel* const labelZoomDisplay = new QLabel(QLatin1String("Zoom:"), this);
labelZoomDisplay->setBuddy(d->zoomDisplay);
QHBoxLayout* const hboxLayout1 = new QHBoxLayout(this);
hboxLayout1->addWidget(new QLabel(QLatin1String("Display mode:"), this));
hboxLayout1->addWidget(buttonGrouped);
hboxLayout1->addWidget(buttonUngrouped);
hboxLayout1->addWidget(labelsbLevel);
hboxLayout1->addWidget(d->sbLevel);
hboxLayout1->addWidget(labelZoomDisplay);
hboxLayout1->addWidget(d->zoomDisplay);
hboxLayout1->addStretch(10);
vboxLayout1->addLayout(hboxLayout1);
QHBoxLayout* const hboxLayout2 = new QHBoxLayout(this);
QPushButton* const pbAddMap = new QPushButton(QLatin1String("Add Map Widget"), this);<--- Variable 'pbAddMap' can be declared as pointer to const
hboxLayout2->addWidget(pbAddMap);
QPushButton* const pbRemoveMap = new QPushButton(QLatin1String("Remove Map Widget"), this);<--- Variable 'pbRemoveMap' can be declared as pointer to const
hboxLayout2->addWidget(pbRemoveMap);
vboxLayout1->addLayout(hboxLayout2);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
connect(d->groupingMode, SIGNAL(idClicked(int)),
this, SLOT(updateGroupingMode()));
#else
connect(d->groupingMode, SIGNAL(buttonClicked(int)),
this, SLOT(updateGroupingMode()));
#endif
connect(d->sbLevel, SIGNAL(valueChanged(int)),
this, SLOT(updateMarkers()));
connect(pbAddMap, SIGNAL(clicked()),
this, SLOT(slotAddMapWidget()));
connect(pbRemoveMap, SIGNAL(clicked()),
this, SLOT(slotRemoveMapWidget()));
updateMarkers();
updateGroupingMode();
slotAddMapWidget();
d->zoomDisplayTimer = new QTimer(this);
d->zoomDisplayTimer->start(200);
connect(d->zoomDisplayTimer, SIGNAL(timeout()),
this, SLOT(updateZoomView()));
}
Calibrator::~Calibrator()
{
delete d;
}
void Calibrator::updateGroupingMode()
{
const bool shouldBeGrouped = (d->groupingMode->checkedId() == 0);
for (int i = 0 ; i < d->extraWidgetHolders.count() ; ++i)
{
MapWidget* const mapWidget = d->extraWidgetHolders.at(i).second;
if (shouldBeGrouped)
{
mapWidget->removeUngroupedModel(d->modelHelper);
mapWidget->setGroupedModel(d->markerTiler);
}
else
{
mapWidget->setGroupedModel(nullptr);
mapWidget->addUngroupedModel(d->modelHelper);
}
}
}
void Calibrator::addMarkerAt(const GeoCoordinates& coordinates)
{
qCDebug(DIGIKAM_TESTS_LOG) << coordinates;
QStandardItem* const item = new QStandardItem(coordinates.geoUrl());
item->setData(QVariant::fromValue(coordinates), CoordinatesRole);
d->model->appendRow(item);
}
void Calibrator::updateMarkers()
{
d->model->clear();
const int newLevel = d->sbLevel->value();
const int Tiling = TileIndex::Tiling;
// add markers in all four corners and in the middle of the edges:
typedef QPair<int, int> QIntPair;
QList<QIntPair> partialTilePositions;
// corners:
partialTilePositions
<< QIntPair(0, 0)
<< QIntPair(Tiling-1, Tiling-1)
<< QIntPair(Tiling*(Tiling-1), Tiling*(Tiling-1))
<< QIntPair(Tiling*Tiling-1, Tiling*Tiling-1);
// middle of edges:
partialTilePositions
<< QIntPair(Tiling/2, 0)
<< QIntPair(Tiling*(Tiling/2), 0)
<< QIntPair(Tiling*(Tiling/2)+Tiling-1, (Tiling-1))
<< QIntPair(Tiling*Tiling-Tiling/2-1, Tiling*Tiling-1);
// center of the map:
partialTilePositions
<< QIntPair(Tiling*(Tiling/2)+Tiling/2, 0)
<< QIntPair(Tiling*(Tiling/2-1)+Tiling/2, Tiling*(Tiling-1))
<< QIntPair(Tiling*(Tiling/2-1)+Tiling/2-1, Tiling*Tiling-1)
<< QIntPair(Tiling*(Tiling/2)+Tiling/2-1, Tiling-1);
// at +/- ~70 degrees (cutoff of Mercator projection is at 80):
partialTilePositions
<< QIntPair(Tiling, 0)
<< QIntPair(2*Tiling-1, Tiling-1)
<< QIntPair(Tiling*(Tiling-2), Tiling*(Tiling-1))
<< QIntPair(Tiling*(Tiling-1)-1, Tiling*Tiling-1);
for (int ptp = 0; ptp<partialTilePositions.count(); ++ptp)
{
QIntPair currentPair = partialTilePositions.at(ptp);
const int level0Index = currentPair.first;
const int followingIndex = currentPair.second;
TileIndex markerIndex;
markerIndex.appendLinearIndex(level0Index);
for (int level = 1 ; level < newLevel-2 ; level++)
{
markerIndex.appendLinearIndex(followingIndex);
}
const int smallPart = followingIndex % Tiling;
for (int i = -1 ; i <= 1 ; ++i)
{
if ((smallPart+i >= 0) && (smallPart+i < Tiling))
{
for (int j = -1 ; j <= 1 ; ++j)
{
const int newLinIndex = followingIndex + i + j*Tiling;
if ((newLinIndex >= 0) && (newLinIndex < Tiling*Tiling))
{
TileIndex newIndex = markerIndex;
newIndex.appendLinearIndex(newLinIndex);
addMarkerAt(newIndex.toCoordinates());
/*
for (int corner = 1 ; corner <= 4 ; ++corner)
{
addMarkerAt(newIndex.toCoordinates(TileIndex::CornerPosition(corner)));
}
*/
}
}
}
}
}
qCDebug(DIGIKAM_TESTS_LOG)<<d->model->rowCount();
}
void Calibrator::updateZoomView()
{
if (d->extraWidgetHolders.isEmpty())
{
return;
}
MapWidget* const firstMapWidget = d->extraWidgetHolders.first().second;
const QString newZoom = firstMapWidget->getZoom();
if (newZoom!=d->zoomDisplay->text())
{
d->zoomDisplay->setText(newZoom);
}
}
void Calibrator::slotAddMapWidget()
{
QVBoxLayout* const boxLayout = new QVBoxLayout();
MapWidget* const mapWidget = new MapWidget();
boxLayout->addWidget(mapWidget);
boxLayout->addWidget(mapWidget->getControlWidget());
QAction* const activateMapAction = new QAction(QLatin1String("Active"), mapWidget);
activateMapAction->setData(QVariant::fromValue<void*>(mapWidget));
activateMapAction->setCheckable(true);
QToolButton* const toolButton = new QToolButton(mapWidget);
toolButton->setDefaultAction(activateMapAction);
mapWidget->addWidgetToControlWidget(toolButton);
connect(activateMapAction, SIGNAL(triggered(bool)),
this, SLOT(slotActivateMapActionTriggered(bool)));
QWidget* const dummyWidget = new QWidget();
dummyWidget->setLayout(boxLayout);
d->extraWidgetHolders.append(QPair<QWidget*, MapWidget*>(dummyWidget, mapWidget));
d->hBoxLayout->addWidget(dummyWidget);
updateGroupingMode();
}
void Calibrator::slotRemoveMapWidget()
{
if (d->extraWidgetHolders.isEmpty())
{
return;
}
QPair<QWidget*, MapWidget*> info = d->extraWidgetHolders.takeLast();
d->hBoxLayout->removeWidget(info.first);
delete info.first;
}
void Calibrator::slotActivateMapActionTriggered(bool state)
{
QAction* const senderAction = qobject_cast<QAction*>(sender());
if (!senderAction)
{
return;
}
MapWidget* const mapWidget = static_cast<MapWidget*>(senderAction->data().value<void*>());
mapWidget->setActive(state);
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QCommandLineParser parser;
parser.addVersionOption();
parser.addHelpOption();
parser.process(app);
Calibrator* const calibrator = new Calibrator();
calibrator->show();
return app.exec();
}
#include "moc_calibrator.cpp"
|