| 1 | /* ============================================================ |
| 2 | * |
| 3 | * This file is a part of digiKam project |
| 4 | * https://www.digikam.org |
| 5 | * |
| 6 | * Date : 2010-09-18 |
| 7 | * Description : A tool to calibrate the tiling levels |
| 8 | * used in geolocation interface |
| 9 | * |
| 10 | * SPDX-FileCopyrightText: 2010 by Michael G. Hansen <mike at mghansen dot de> |
| 11 | * |
| 12 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 13 | * |
| 14 | * ============================================================ */ |
| 15 | |
| 16 | #include "calibrator.h" |
| 17 | |
| 18 | // Qt includes |
| 19 | |
| 20 | #include <QAction> |
| 21 | #include <QButtonGroup> |
| 22 | #include <QHBoxLayout> |
| 23 | #include <QLabel> |
| 24 | #include <QPushButton> |
| 25 | #include <QRadioButton> |
| 26 | #include <QSpinBox> |
| 27 | #include <QStandardItemModel> |
| 28 | #include <QTimer> |
| 29 | #include <QToolButton> |
| 30 | #include <QVBoxLayout> |
| 31 | #include <QApplication> |
| 32 | #include <QCommandLineParser> |
| 33 | #include <QLineEdit> |
| 34 | |
| 35 | // Local includes |
| 36 | |
| 37 | #include "digikam_debug.h" |
| 38 | #include "abstractmarkertiler.h" |
| 39 | #include "itemmarkertiler.h" |
| 40 | #include "mapwidget.h" |
| 41 | #include "geocoordinates.h" |
| 42 | #include "digikam_version.h" |
| 43 | |
| 44 | using namespace Digikam; |
| 45 | |
| 46 | const int CoordinatesRole = Qt::UserRole + 1; |
| 47 | |
| 48 | class Q_DECL_HIDDEN CalibratorModelHelper::Private |
| 49 | { |
| 50 | public: |
| 51 | |
| 52 | explicit Private() |
| 53 | : model(nullptr) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | QStandardItemModel* model; |
| 58 | }; |
| 59 | |
| 60 | CalibratorModelHelper::CalibratorModelHelper(QStandardItemModel* const model, |
| 61 | QObject* const parent) |
| 62 | : GeoModelHelper(parent), |
| 63 | d (new Private) |
| 64 | { |
| 65 | d->model = model; |
| 66 | } |
| 67 | |
| 68 | CalibratorModelHelper::~CalibratorModelHelper() |
| 69 | { |
| 70 | delete d; |
| 71 | } |
| 72 | |
| 73 | QAbstractItemModel* CalibratorModelHelper::model() const |
| 74 | { |
| 75 | return d->model; |
| 76 | } |
| 77 | |
| 78 | QItemSelectionModel* CalibratorModelHelper::selectionModel() const |
| 79 | { |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
| 83 | bool CalibratorModelHelper::itemCoordinates(const QModelIndex& index, |
| 84 | GeoCoordinates* const coordinates) const |
| 85 | { |
| 86 | if (!index.isValid()) |
| 87 | return false; |
| 88 | |
| 89 | const QVariant coordinatesVariant = index.data(CoordinatesRole); |
| 90 | GeoCoordinates itemCoordinates = coordinatesVariant.value<GeoCoordinates>(); |
| 91 | |
| 92 | if (coordinates) |
| 93 | *coordinates = itemCoordinates; |
| 94 | |
| 95 | return itemCoordinates.hasCoordinates(); |
| 96 | } |
| 97 | |
| 98 | void CalibratorModelHelper::setItemCoordinates(const QModelIndex& index, |
| 99 | const GeoCoordinates& coordinates) |
| 100 | { |
| 101 | if (!index.isValid()) |
| 102 | return; |
| 103 | |
| 104 | d->model->setData(index, QVariant::fromValue(coordinates), CoordinatesRole); |
| 105 | } |
| 106 | |
| 107 | GeoModelHelper::PropertyFlags CalibratorModelHelper::modelFlags() const |
| 108 | { |
| 109 | return FlagVisible; |
| 110 | } |
| 111 | |
| 112 | // --------------------------------------------------------------------------------------------- |
| 113 | |
| 114 | class Q_DECL_HIDDEN Calibrator::Private |
| 115 | { |
| 116 | public: |
| 117 | |
| 118 | explicit Private() |
| 119 | : hBoxLayout (nullptr), |
| 120 | model (nullptr), |
| 121 | modelHelper (nullptr), |
| 122 | markerTiler (nullptr), |
| 123 | groupingMode (nullptr), |
| 124 | sbLevel (nullptr), |
| 125 | zoomDisplay (nullptr), |
| 126 | zoomDisplayTimer (nullptr) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | QHBoxLayout* hBoxLayout; |
| 131 | QList<QPair<QWidget*, MapWidget*> > extraWidgetHolders; |
| 132 | QStandardItemModel* model; |
| 133 | CalibratorModelHelper* modelHelper; |
| 134 | ItemMarkerTiler* markerTiler; |
| 135 | |
| 136 | QButtonGroup* groupingMode; |
| 137 | QSpinBox* sbLevel; |
| 138 | QLineEdit* zoomDisplay; |
| 139 | QTimer* zoomDisplayTimer; |
| 140 | }; |
| 141 | |
| 142 | Calibrator::Calibrator(QWidget* const parent) |
| 143 | : QMainWindow(parent), |
| 144 | d (new Private) |
| 145 | { |
| 146 | d->model = new QStandardItemModel(this); |
| 147 | d->modelHelper = new CalibratorModelHelper(d->model, this); |
| 148 | d->markerTiler = new ItemMarkerTiler(d->modelHelper, this); |
| 149 | |
| 150 | QVBoxLayout* const vboxLayout1 = new QVBoxLayout(); |
| 151 | QWidget* const dummy1 = new QWidget(this); |
| 152 | dummy1->setLayout(vboxLayout1); |
| 153 | setCentralWidget(dummy1); |
| 154 | |
| 155 | d->hBoxLayout = new QHBoxLayout(); |
| 156 | vboxLayout1->addLayout(d->hBoxLayout); |
| 157 | |
| 158 | d->groupingMode = new QButtonGroup(this); |
| 159 | d->groupingMode->setExclusive(true); |
| 160 | QRadioButton* const buttonGrouped = new QRadioButton(QLatin1String("Grouped"), this); |
| 161 | d->groupingMode->addButton(buttonGrouped, 0); |
| 162 | QRadioButton* const buttonUngrouped = new QRadioButton(QLatin1String("Ungrouped"), this); |
| 163 | d->groupingMode->addButton(buttonUngrouped, 1); |
| 164 | buttonGrouped->setChecked(true); |
| 165 | |
| 166 | d->sbLevel = new QSpinBox(this); |
| 167 | d->sbLevel->setMinimum(1); |
| 168 | d->sbLevel->setMaximum(TileIndex::MaxLevel); |
| 169 | QLabel* const labelsbLevel = new QLabel(QLatin1String("Level:"), this); |
| 170 | labelsbLevel->setBuddy(d->sbLevel); |
| 171 | |
| 172 | d->zoomDisplay = new QLineEdit(this); |
| 173 | d->zoomDisplay->setReadOnly(true); |
| 174 | QLabel* const labelZoomDisplay = new QLabel(QLatin1String("Zoom:"), this); |
| 175 | labelZoomDisplay->setBuddy(d->zoomDisplay); |
| 176 | |
| 177 | QHBoxLayout* const hboxLayout1 = new QHBoxLayout(this); |
| 178 | hboxLayout1->addWidget(new QLabel(QLatin1String("Display mode:"), this)); |
| 179 | hboxLayout1->addWidget(buttonGrouped); |
| 180 | hboxLayout1->addWidget(buttonUngrouped); |
| 181 | hboxLayout1->addWidget(labelsbLevel); |
| 182 | hboxLayout1->addWidget(d->sbLevel); |
| 183 | hboxLayout1->addWidget(labelZoomDisplay); |
| 184 | hboxLayout1->addWidget(d->zoomDisplay); |
| 185 | hboxLayout1->addStretch(10); |
| 186 | vboxLayout1->addLayout(hboxLayout1); |
| 187 | |
| 188 | QHBoxLayout* const hboxLayout2 = new QHBoxLayout(this); |
| 189 | QPushButton* const pbAddMap = new QPushButton(QLatin1String("Add Map Widget"), this); |
| 190 | hboxLayout2->addWidget(pbAddMap); |
| 191 | QPushButton* const pbRemoveMap = new QPushButton(QLatin1String("Remove Map Widget"), this); |
| 192 | hboxLayout2->addWidget(pbRemoveMap); |
| 193 | vboxLayout1->addLayout(hboxLayout2); |
| 194 | |
| 195 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) |
| 196 | |
| 197 | connect(d->groupingMode, SIGNAL(idClicked(int)), |
| 198 | this, SLOT(updateGroupingMode())); |
| 199 | |
| 200 | #else |
| 201 | |
| 202 | connect(d->groupingMode, SIGNAL(buttonClicked(int)), |
| 203 | this, SLOT(updateGroupingMode())); |
| 204 | |
| 205 | #endif |
| 206 | |
| 207 | connect(d->sbLevel, SIGNAL(valueChanged(int)), |
| 208 | this, SLOT(updateMarkers())); |
| 209 | |
| 210 | connect(pbAddMap, SIGNAL(clicked()), |
| 211 | this, SLOT(slotAddMapWidget())); |
| 212 | |
| 213 | connect(pbRemoveMap, SIGNAL(clicked()), |
| 214 | this, SLOT(slotRemoveMapWidget())); |
| 215 | |
| 216 | updateMarkers(); |
| 217 | updateGroupingMode(); |
| 218 | |
| 219 | slotAddMapWidget(); |
| 220 | |
| 221 | d->zoomDisplayTimer = new QTimer(this); |
| 222 | d->zoomDisplayTimer->start(200); |
| 223 | |
| 224 | connect(d->zoomDisplayTimer, SIGNAL(timeout()), |
| 225 | this, SLOT(updateZoomView())); |
| 226 | } |
| 227 | |
| 228 | Calibrator::~Calibrator() |
| 229 | { |
| 230 | delete d; |
| 231 | } |
| 232 | |
| 233 | void Calibrator::updateGroupingMode() |
| 234 | { |
| 235 | const bool shouldBeGrouped = (d->groupingMode->checkedId() == 0); |
| 236 | |
| 237 | for (int i = 0 ; i < d->extraWidgetHolders.count() ; ++i) |
| 238 | { |
| 239 | MapWidget* const mapWidget = d->extraWidgetHolders.at(i).second; |
| 240 | |
| 241 | if (shouldBeGrouped) |
| 242 | { |
| 243 | mapWidget->removeUngroupedModel(d->modelHelper); |
| 244 | mapWidget->setGroupedModel(d->markerTiler); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | mapWidget->setGroupedModel(nullptr); |
| 249 | mapWidget->addUngroupedModel(d->modelHelper); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void Calibrator::addMarkerAt(const GeoCoordinates& coordinates) |
| 255 | { |
| 256 | qCDebug(DIGIKAM_TESTS_LOG) << coordinates; |
| 257 | QStandardItem* const item = new QStandardItem(coordinates.geoUrl()); |
| 258 | item->setData(QVariant::fromValue(coordinates), CoordinatesRole); |
| 259 | |
| 260 | d->model->appendRow(item); |
| 261 | } |
| 262 | |
| 263 | void Calibrator::updateMarkers() |
| 264 | { |
| 265 | d->model->clear(); |
| 266 | |
| 267 | const int newLevel = d->sbLevel->value(); |
| 268 | const int Tiling = TileIndex::Tiling; |
| 269 | |
| 270 | // add markers in all four corners and in the middle of the edges: |
| 271 | typedef QPair<int, int> QIntPair; |
| 272 | QList<QIntPair> partialTilePositions; |
| 273 | |
| 274 | // corners: |
| 275 | partialTilePositions |
| 276 | << QIntPair(0, 0) |
| 277 | << QIntPair(Tiling-1, Tiling-1) |
| 278 | << QIntPair(Tiling*(Tiling-1), Tiling*(Tiling-1)) |
| 279 | << QIntPair(Tiling*Tiling-1, Tiling*Tiling-1); |
| 280 | |
| 281 | // middle of edges: |
| 282 | partialTilePositions |
| 283 | << QIntPair(Tiling/2, 0) |
| 284 | << QIntPair(Tiling*(Tiling/2), 0) |
| 285 | << QIntPair(Tiling*(Tiling/2)+Tiling-1, (Tiling-1)) |
| 286 | << QIntPair(Tiling*Tiling-Tiling/2-1, Tiling*Tiling-1); |
| 287 | |
| 288 | // center of the map: |
| 289 | partialTilePositions |
| 290 | << QIntPair(Tiling*(Tiling/2)+Tiling/2, 0) |
| 291 | << QIntPair(Tiling*(Tiling/2-1)+Tiling/2, Tiling*(Tiling-1)) |
| 292 | << QIntPair(Tiling*(Tiling/2-1)+Tiling/2-1, Tiling*Tiling-1) |
| 293 | << QIntPair(Tiling*(Tiling/2)+Tiling/2-1, Tiling-1); |
| 294 | |
| 295 | // at +/- ~70 degrees (cutoff of Mercator projection is at 80): |
| 296 | partialTilePositions |
| 297 | << QIntPair(Tiling, 0) |
| 298 | << QIntPair(2*Tiling-1, Tiling-1) |
| 299 | << QIntPair(Tiling*(Tiling-2), Tiling*(Tiling-1)) |
| 300 | << QIntPair(Tiling*(Tiling-1)-1, Tiling*Tiling-1); |
| 301 | |
| 302 | for (int ptp = 0; ptp<partialTilePositions.count(); ++ptp) |
| 303 | { |
| 304 | QIntPair currentPair = partialTilePositions.at(ptp); |
| 305 | const int level0Index = currentPair.first; |
| 306 | const int followingIndex = currentPair.second; |
| 307 | |
| 308 | TileIndex markerIndex; |
| 309 | markerIndex.appendLinearIndex(level0Index); |
| 310 | |
| 311 | for (int level = 1 ; level < newLevel-2 ; level++) |
| 312 | { |
| 313 | markerIndex.appendLinearIndex(followingIndex); |
| 314 | } |
| 315 | |
| 316 | const int smallPart = followingIndex % Tiling; |
| 317 | |
| 318 | for (int i = -1 ; i <= 1 ; ++i) |
| 319 | { |
| 320 | if ((smallPart+i >= 0) && (smallPart+i < Tiling)) |
| 321 | { |
| 322 | for (int j = -1 ; j <= 1 ; ++j) |
| 323 | { |
| 324 | const int newLinIndex = followingIndex + i + j*Tiling; |
| 325 | |
| 326 | if ((newLinIndex >= 0) && (newLinIndex < Tiling*Tiling)) |
| 327 | { |
| 328 | TileIndex newIndex = markerIndex; |
| 329 | newIndex.appendLinearIndex(newLinIndex); |
| 330 | addMarkerAt(newIndex.toCoordinates()); |
| 331 | /* |
| 332 | for (int corner = 1 ; corner <= 4 ; ++corner) |
| 333 | { |
| 334 | addMarkerAt(newIndex.toCoordinates(TileIndex::CornerPosition(corner))); |
| 335 | } |
| 336 | */ |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | qCDebug(DIGIKAM_TESTS_LOG)<<d->model->rowCount(); |
| 344 | } |
| 345 | |
| 346 | void Calibrator::updateZoomView() |
| 347 | { |
| 348 | if (d->extraWidgetHolders.isEmpty()) |
| 349 | { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | MapWidget* const firstMapWidget = d->extraWidgetHolders.first().second; |
| 354 | const QString newZoom = firstMapWidget->getZoom(); |
| 355 | |
| 356 | if (newZoom!=d->zoomDisplay->text()) |
| 357 | { |
| 358 | d->zoomDisplay->setText(newZoom); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void Calibrator::slotAddMapWidget() |
| 363 | { |
| 364 | QVBoxLayout* const boxLayout = new QVBoxLayout(); |
| 365 | MapWidget* const mapWidget = new MapWidget(); |
| 366 | boxLayout->addWidget(mapWidget); |
| 367 | boxLayout->addWidget(mapWidget->getControlWidget()); |
| 368 | |
| 369 | QAction* const activateMapAction = new QAction(QLatin1String("Active"), mapWidget); |
| 370 | activateMapAction->setData(QVariant::fromValue<void*>(mapWidget)); |
| 371 | activateMapAction->setCheckable(true); |
| 372 | QToolButton* const toolButton = new QToolButton(mapWidget); |
| 373 | toolButton->setDefaultAction(activateMapAction); |
| 374 | mapWidget->addWidgetToControlWidget(toolButton); |
| 375 | |
| 376 | connect(activateMapAction, SIGNAL(triggered(bool)), |
| 377 | this, SLOT(slotActivateMapActionTriggered(bool))); |
| 378 | |
| 379 | QWidget* const dummyWidget = new QWidget(); |
| 380 | dummyWidget->setLayout(boxLayout); |
| 381 | d->extraWidgetHolders.append(QPair<QWidget*, MapWidget*>(dummyWidget, mapWidget)); |
| 382 | |
| 383 | d->hBoxLayout->addWidget(dummyWidget); |
| 384 | |
| 385 | updateGroupingMode(); |
| 386 | } |
| 387 | |
| 388 | void Calibrator::slotRemoveMapWidget() |
| 389 | { |
| 390 | if (d->extraWidgetHolders.isEmpty()) |
| 391 | { |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | QPair<QWidget*, MapWidget*> info = d->extraWidgetHolders.takeLast(); |
| 396 | d->hBoxLayout->removeWidget(info.first); |
| 397 | delete info.first; |
| 398 | } |
| 399 | |
| 400 | void Calibrator::slotActivateMapActionTriggered(bool state) |
| 401 | { |
| 402 | QAction* const senderAction = qobject_cast<QAction*>(sender()); |
| 403 | |
| 404 | if (!senderAction) |
| 405 | { |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | MapWidget* const mapWidget = static_cast<MapWidget*>(senderAction->data().value<void*>()); |
| 410 | mapWidget->setActive(state); |
| 411 | } |
| 412 | |
| 413 | int main(int argc, char* argv[]) |
| 414 | { |
| 415 | QApplication app(argc, argv); |
| 416 | QCommandLineParser parser; |
| 417 | parser.addVersionOption(); |
| 418 | parser.addHelpOption(); |
| 419 | parser.process(app); |
| 420 | |
| 421 | Calibrator* const calibrator = new Calibrator(); |
| 422 | calibrator->show(); |
| 423 | |
| 424 | return app.exec(); |
| 425 | } |
| 426 | |
| 427 | #include "moc_calibrator.cpp" |