Skip to content

Commit

Permalink
Extended sample for layers
Browse files Browse the repository at this point in the history
  • Loading branch information
AmonRaNet committed Jan 7, 2024
1 parent c7662d6 commit eb02e42
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 38 deletions.
123 changes: 86 additions & 37 deletions samples/layers/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
#include "mainwindow.h"

#include <QCheckBox>
#include <QHBoxLayout>
#include <QListWidget>
#include <QPushButton>
#include <QTimer>
#include <QVBoxLayout>

#include <helpers.h>
#include <rectangle.h>

#include <QGeoView/QGVLayer.h>
#include <QGeoView/QGVLayerOSM.h>
#include <QGeoView/QGVWidgetCompass.h>
#include <QGeoView/QGVWidgetScale.h>
Expand All @@ -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::GlobalColor>(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()
Expand All @@ -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<QPair<QString, QGVItem*>> 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::GlobalColor>(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<QGVLayer*>(mMap->getItem(i));
if (layer == nullptr)
continue;

mList->addItem(layer->getName());
}
}
8 changes: 7 additions & 1 deletion samples/layers/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#pragma once

#include <QGroupBox>
#include <QListWidget>
#include <QMainWindow>

#include <QGeoView/QGVLayer.h>
#include <QGeoView/QGVMap.h>

class MainWindow : public QMainWindow
Expand All @@ -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;
};

0 comments on commit eb02e42

Please sign in to comment.