diff --git a/samples/layers/mainwindow.cpp b/samples/layers/mainwindow.cpp index a6f564a..e9d2acb 100644 --- a/samples/layers/mainwindow.cpp +++ b/samples/layers/mainwindow.cpp @@ -19,12 +19,16 @@ #include "mainwindow.h" #include +#include +#include +#include #include #include #include #include +#include #include #include #include @@ -45,34 +49,27 @@ MainWindow::MainWindow() // Widgets mMap->addWidget(new QGVWidgetCompass()); mMap->addWidget(new QGVWidgetZoom()); + mMap->addWidget(new QGVWidgetScale()); // Background layer auto osmLayer = new QGVLayerOSM(); mMap->addItem(osmLayer); - // Add layers - for (int l = 0; l < 3; l++) { - auto* itemsLayer = new QGVLayer(); - mMap->addItem(itemsLayer); - - // 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), - static_cast(Qt::red + l)); - item->setFlag(QGV::ItemFlag::IgnoreAzimuth); - item->setFlag(QGV::ItemFlag::IgnoreScale); - - itemsLayer->addItem(item); - } - } - // Options list - centralWidget()->layout()->addWidget(createLayersList()); + QWidget* widget = new QWidget(); + widget->setLayout(new QHBoxLayout()); + widget->layout()->addWidget(createOptionsList()); + widget->layout()->addWidget(createLayersList()); + centralWidget()->layout()->addWidget(widget); // Show target area - QTimer::singleShot(100, this, [this]() { mMap->cameraTo(QGVCameraActions(mMap).scaleTo(targetArea())); }); + QTimer::singleShot(100, this, [this]() { + mMap->cameraTo(QGVCameraActions(mMap).scaleTo(targetArea())); + + addLayer(); + addLayer(); + addLayer(); + }); } MainWindow::~MainWindow() @@ -84,30 +81,82 @@ QGV::GeoRect MainWindow::targetArea() const return QGV::GeoRect(QGV::GeoPos(51.848624, 14.7), QGV::GeoPos(51.743758, 14.9)); } -QGroupBox* MainWindow::createLayersList() +QGroupBox* MainWindow::createOptionsList() { - const QList> layers = { - { "Layer 1", mMap->getItem(1) }, - { "Layer 2", mMap->getItem(2) }, - { "Layer 3", mMap->getItem(3) }, - }; + QGroupBox* groupBox = new QGroupBox(tr("Control")); + groupBox->setLayout(new QVBoxLayout); + + { + QPushButton* addButton = new QPushButton("Add layer"); + QPushButton* removeButton = new QPushButton("Remove last layer"); + QWidget* widget = new QWidget(); + widget->setLayout(new QHBoxLayout()); + widget->layout()->addWidget(addButton); + widget->layout()->addWidget(removeButton); - QGroupBox* groupBox = new QGroupBox(tr("Layers")); + groupBox->layout()->addWidget(widget); + + connect(addButton, &QPushButton::clicked, this, &MainWindow::addLayer); + connect(removeButton, &QPushButton::clicked, this, &MainWindow::removeLayer); + } + + return groupBox; +} + +QGroupBox* MainWindow::createLayersList() +{ + QGroupBox* groupBox = new QGroupBox(tr("List of layers")); groupBox->setLayout(new QVBoxLayout); - for (auto pair : layers) { - auto name = pair.first; - auto layer = pair.second; + mList = new QListWidget(); + groupBox->layout()->addWidget(mList); + updateListOfLayers(); + + return groupBox; +} + +void MainWindow::addLayer() +{ + const QGV::GeoRect layerTargetArea = mMap->getProjection()->projToGeo(mMap->getCamera().projRect()); + const Qt::GlobalColor layerColor = static_cast(Qt::red + Helpers::randomInt(0, 10)); + + auto* itemsLayer = new QGVLayer(); + itemsLayer->setName("Layer with color " + QVariant::fromValue(layerColor).toString()); - QCheckBox* checkButton = new QCheckBox(name); - groupBox->layout()->addWidget(checkButton); + // Add items to custom layer + for (int i = 0; i < 10; i++) { + const int size = 50; - connect(checkButton, &QCheckBox::toggled, this, [layer, layers](const bool checked) { - layer->setVisible(checked); - }); + auto item = new Rectangle(Helpers::randRect(mMap, layerTargetArea, size), layerColor); + item->setFlag(QGV::ItemFlag::IgnoreAzimuth); + item->setFlag(QGV::ItemFlag::IgnoreScale); - checkButton->setChecked(true); + itemsLayer->addItem(item); } - return groupBox; + mMap->addItem(itemsLayer); + + updateListOfLayers(); +} + +void MainWindow::removeLayer() +{ + if (mMap->countItems() > 1) { + mMap->removeItem(mMap->getItem(mMap->countItems() - 1)); + } + + updateListOfLayers(); +} + +void MainWindow::updateListOfLayers() +{ + mList->clear(); + + for (int i = 0; i < mMap->countItems(); i++) { + QGVLayer* layer = dynamic_cast(mMap->getItem(i)); + if (layer == nullptr) + continue; + + mList->addItem(layer->getName()); + } } diff --git a/samples/layers/mainwindow.h b/samples/layers/mainwindow.h index 8a12649..d2cbbad 100644 --- a/samples/layers/mainwindow.h +++ b/samples/layers/mainwindow.h @@ -19,9 +19,9 @@ #pragma once #include +#include #include -#include #include class MainWindow : public QMainWindow @@ -34,8 +34,14 @@ class MainWindow : public QMainWindow QGV::GeoRect targetArea() const; + QGroupBox* createOptionsList(); QGroupBox* createLayersList(); + void addLayer(); + void removeLayer(); + void updateListOfLayers(); + private: QGVMap* mMap; + QListWidget* mList; };