Skip to content

Commit

Permalink
Fixes for PR 53
Browse files Browse the repository at this point in the history
  • Loading branch information
AmonRaNet committed Jun 4, 2024
1 parent 954619a commit 08d3768
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 224 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ project(QGeoView LANGUAGES C CXX)
option(BUILD_EXAMPLES "Build examples" ON)

find_package(GDAL CONFIG)

add_subdirectory(lib)

if (${BUILD_EXAMPLES})
message(STATUS "Enabled building of examples")
add_subdirectory(samples/shared)
Expand All @@ -24,6 +26,7 @@ if (${BUILD_EXAMPLES})
add_subdirectory(samples/mouse-actions)
add_subdirectory(samples/camera-actions)
add_subdirectory(samples/drag-and-drop)

if(GDAL_FOUND)
add_subdirectory(samples/gdal-shapefile)
endif()
Expand Down
10 changes: 5 additions & 5 deletions samples/gdal-shapefile/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

#include "mainwindow.h"

#include <QTimer>
#include <QApplication>
#include <QDir>
#include <QTimer>

#include "polygon.h"
#include <QGeoView/QGVLayerOSM.h>
#include <helpers.h>
#include "polygon.h"

#include "cpl_conv.h"
#include "ogrsf_frmts.h"
Expand Down Expand Up @@ -69,14 +69,14 @@ MainWindow::MainWindow()
CPLSetConfigOption("PROJ_DATA", PROJ_DATA.c_str());

GDALAllRegister(); // Load GDAL drivers
GDALDataset *poDS = static_cast<GDALDataset*>(GDALOpenEx("countries.shp", GDAL_OF_VECTOR, NULL, NULL, NULL)); // Open vector file
GDALDataset* poDS = static_cast<GDALDataset*>(
GDALOpenEx("countries.shp", GDAL_OF_VECTOR, NULL, NULL, NULL)); // Open vector file
if (poDS == NULL) {
printf("Open failed.\n");
exit(1);
}

for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++)
{
for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++) {
OGRLayer* poLayer = poDS->GetLayer(iLayer);
OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn();
poLayer->ResetReading();
Expand Down
326 changes: 163 additions & 163 deletions samples/gdal-shapefile/polygon.cpp
Original file line number Diff line number Diff line change
@@ -1,163 +1,163 @@
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2023 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 "polygon.h"

#include <QPainter>

Polygon::Polygon(const PointList& geoPoints, QColor stroke, QColor fill)
: mGeoPoints(geoPoints)
, mColorStroke(stroke)
, mColorFill(fill)
{
}

void Polygon::setPoints(const PointList& geoPoints)
{
mGeoPoints = geoPoints;

// Geo coordinates need to be converted manually again to projection
onProjection(getMap());

// Now we can inform QGV about changes for this
resetBoundary();
refresh();
}

PointList Polygon::getPoints() const
{
return mGeoPoints;
}

void Polygon::onProjection(QGVMap* geoMap)
{
QGVDrawItem::onProjection(geoMap);
mProjPoints.clear();
for (const QGV::GeoPos& pos : mGeoPoints)
mProjPoints << geoMap->getProjection()->geoToProj(pos);
}

QPainterPath Polygon::projShape() const
{
QPainterPath path;
path.addPolygon(mProjPoints);
return path;
}

void Polygon::projPaint(QPainter* painter)
{
QPen pen = QPen(QBrush(mColorStroke), 1);
pen.setCosmetic(true);
painter->setPen(pen);
painter->setBrush(QBrush(mColorFill));
painter->drawPolygon(mProjPoints);
}

QPointF Polygon::projAnchor() const
{
return mProjPoints.boundingRect().center();
}

QTransform Polygon::projTransform() const
{
// This method is optional (needed flag is QGV::ItemFlag::Transformed).
// Custom transformation for item.
// In this case we rotate item by 45 degree.

return QGV::createTransfromAzimuth(projAnchor(), 45);
}

QString Polygon::projTooltip(const QPointF& projPos) const
{
// This method is optional (when empty return then no tooltip).
// Text for mouse tool tip.

auto geo = getMap()->getProjection()->projToGeo(projPos);

return "Polygon with color " + mColorFill.name() + "\nPosition " + geo.latToString() + " " + geo.lonToString();
}

void Polygon::projOnMouseClick(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Clickable).
// Custom reaction to item single mouse click.
// To avoid collision with item selection this code applies only if item selection disabled.
// In this case we change opacity for item.

if (!isSelectable()) {
if (getOpacity() <= 0.5)
setOpacity(1.0);
else
setOpacity(0.5);

qInfo() << "single click" << projPos;
} else {
setOpacity(1.0);
}
}

void Polygon::projOnMouseDoubleClick(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Clickable).
// Custom reaction to item double mouse click.
// In this case we change color for item.

const QList<QColor> colors = { Qt::red, Qt::blue, Qt::green, Qt::gray, Qt::cyan, Qt::magenta, Qt::yellow };

const auto iter =
std::find_if(colors.begin(), colors.end(), [this](const QColor& color) { return color == mColorFill; });
mColorFill = colors[(iter - colors.begin() + 1) % colors.size()];
repaint();

setOpacity(1.0);

qInfo() << "double click" << projPos;
}

void Polygon::projOnObjectStartMove(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Movable).
// Custom reaction to item move start.
// In this case we only log message.

qInfo() << "object move started at" << projPos;
}

void Polygon::projOnObjectMovePos(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Movable).
// Custom reaction to mouse pos change when item move is started.
// In this case actually changing location of object.

PointList newPoints;
for (const QPointF& pt : mProjPoints)
newPoints << getMap()->getProjection()->projToGeo(pt + projPos);

setPoints(newPoints);

qInfo() << "object moved" << mProjPoints;
}

void Polygon::projOnObjectStopMove(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Movable).
// Custom reaction to item move finished.
// In this case we only log message.

qInfo() << "object move stopped" << projPos;
}
/***************************************************************************
* QGeoView is a Qt / C ++ widget for visualizing geographic data.
* Copyright (C) 2018-2023 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 "polygon.h"

#include <QPainter>

Polygon::Polygon(const PointList& geoPoints, QColor stroke, QColor fill)
: mGeoPoints(geoPoints)
, mColorStroke(stroke)
, mColorFill(fill)
{
}

void Polygon::setPoints(const PointList& geoPoints)
{
mGeoPoints = geoPoints;

// Geo coordinates need to be converted manually again to projection
onProjection(getMap());

// Now we can inform QGV about changes for this
resetBoundary();
refresh();
}

PointList Polygon::getPoints() const
{
return mGeoPoints;
}

void Polygon::onProjection(QGVMap* geoMap)
{
QGVDrawItem::onProjection(geoMap);
mProjPoints.clear();
for (const QGV::GeoPos& pos : mGeoPoints)
mProjPoints << geoMap->getProjection()->geoToProj(pos);
}

QPainterPath Polygon::projShape() const
{
QPainterPath path;
path.addPolygon(mProjPoints);
return path;
}

void Polygon::projPaint(QPainter* painter)
{
QPen pen = QPen(QBrush(mColorStroke), 1);
pen.setCosmetic(true);
painter->setPen(pen);
painter->setBrush(QBrush(mColorFill));
painter->drawPolygon(mProjPoints);
}

QPointF Polygon::projAnchor() const
{
return mProjPoints.boundingRect().center();
}

QTransform Polygon::projTransform() const
{
// This method is optional (needed flag is QGV::ItemFlag::Transformed).
// Custom transformation for item.
// In this case we rotate item by 45 degree.

return QGV::createTransfromAzimuth(projAnchor(), 45);
}

QString Polygon::projTooltip(const QPointF& projPos) const
{
// This method is optional (when empty return then no tooltip).
// Text for mouse tool tip.

auto geo = getMap()->getProjection()->projToGeo(projPos);

return "Polygon with color " + mColorFill.name() + "\nPosition " + geo.latToString() + " " + geo.lonToString();
}

void Polygon::projOnMouseClick(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Clickable).
// Custom reaction to item single mouse click.
// To avoid collision with item selection this code applies only if item selection disabled.
// In this case we change opacity for item.

if (!isSelectable()) {
if (getOpacity() <= 0.5)
setOpacity(1.0);
else
setOpacity(0.5);

qInfo() << "single click" << projPos;
} else {
setOpacity(1.0);
}
}

void Polygon::projOnMouseDoubleClick(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Clickable).
// Custom reaction to item double mouse click.
// In this case we change color for item.

const QList<QColor> colors = { Qt::red, Qt::blue, Qt::green, Qt::gray, Qt::cyan, Qt::magenta, Qt::yellow };

const auto iter =
std::find_if(colors.begin(), colors.end(), [this](const QColor& color) { return color == mColorFill; });
mColorFill = colors[(iter - colors.begin() + 1) % colors.size()];
repaint();

setOpacity(1.0);

qInfo() << "double click" << projPos;
}

void Polygon::projOnObjectStartMove(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Movable).
// Custom reaction to item move start.
// In this case we only log message.

qInfo() << "object move started at" << projPos;
}

void Polygon::projOnObjectMovePos(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Movable).
// Custom reaction to mouse pos change when item move is started.
// In this case actually changing location of object.

PointList newPoints;
for (const QPointF& pt : mProjPoints)
newPoints << getMap()->getProjection()->projToGeo(pt + projPos);

setPoints(newPoints);

qInfo() << "object moved" << mProjPoints;
}

void Polygon::projOnObjectStopMove(const QPointF& projPos)
{
// This method is optional (needed flag is QGV::ItemFlag::Movable).
// Custom reaction to item move finished.
// In this case we only log message.

qInfo() << "object move stopped" << projPos;
}
Loading

0 comments on commit 08d3768

Please sign in to comment.