QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgscoordinateboundspreviewmapwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * qgscoordinateboundspreviewmapwidget.h *
3 * Copyright (C) 2019 by Nyall Dawson *
4 * nyall dot dawson at gmail dot com *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 ***************************************************************************/
12
13#include "qgsapplication.h"
14#include "qgsmaptoolpan.h"
15#include "qgsrubberband.h"
16#include "qgsvectorlayer.h"
17#include "qgsvertexmarker.h"
18
19#include <QString>
20
21#include "moc_qgscoordinateboundspreviewmapwidget.cpp"
22
23using namespace Qt::StringLiterals;
24
26 : QgsMapCanvas( parent )
27{
28 mPreviewBand = new QgsRubberBand( this, Qgis::GeometryType::Polygon );
29 mPreviewBand->setWidth( 4 );
30
31 mCanvasPreviewBand = new QgsRubberBand( this, Qgis::GeometryType::Polygon );
32 mCanvasPreviewBand->setWidth( 4 );
33 const QColor rectColor = QColor( 185, 84, 210, 60 );
34 mCanvasPreviewBand->setColor( rectColor );
35
36 mCanvasCenterMarker = new QgsVertexMarker( this );
37 mCanvasCenterMarker->setIconType( QgsVertexMarker::ICON_CROSS );
38 mCanvasCenterMarker->setColor( QColor( 185, 84, 210 ) );
39 mCanvasCenterMarker->setPenWidth( 3 );
40
41 const QgsCoordinateReferenceSystem srs( u"EPSG:4326"_s );
42 setDestinationCrs( srs );
43
44 const QString layerPath = QgsApplication::pkgDataPath() + u"/resources/data/world_map.gpkg|layername=countries"_s;
46 options.forceReadOnly = true;
47 mLayers << new QgsVectorLayer( layerPath, tr( "World Map" ), u"ogr"_s, options );
48 setLayers( mLayers );
49 mPanTool = new QgsMapToolPan( this );
50 setMapTool( mPanTool );
52}
53
55{
56 setMapTool( nullptr );
57 delete mPanTool;
58 mPanTool = nullptr;
59
60 qDeleteAll( mLayers );
61 delete mPreviewBand;
62 delete mCanvasPreviewBand;
63 delete mCanvasCenterMarker;
64}
65
67{
68 if ( !qgsDoubleNear( rect.area(), 0.0 ) )
69 {
70 QgsGeometry geom;
71 if ( rect.xMinimum() > rect.xMaximum() )
72 {
73 const QgsRectangle rect1 = QgsRectangle( -180, rect.yMinimum(), rect.xMaximum(), rect.yMaximum() );
74 const QgsRectangle rect2 = QgsRectangle( rect.xMinimum(), rect.yMinimum(), 180, rect.yMaximum() );
75 geom = QgsGeometry::fromRect( rect1 );
76 geom.addPart( QgsGeometry::fromRect( rect2 ) );
77 }
78 else
79 {
80 geom = QgsGeometry::fromRect( rect );
81 }
82 mPreviewBand->setToGeometry( geom, nullptr );
83 mPreviewBand->setColor( QColor( 255, 0, 0, 65 ) );
85 extent.scale( 1.1 );
87 refresh();
88 mPreviewBand->show();
89 }
90 else
91 {
92 mPreviewBand->hide();
94 }
95}
96
98{
99 return mCanvasRect;
100}
101
103{
104 mCanvasRect = rect;
105 mCanvasPreviewBand->setToGeometry( QgsGeometry::fromRect( mCanvasRect ), nullptr );
106 mCanvasPreviewBand->show();
107 mCanvasCenterMarker->setCenter( rect.center() );
108 mCanvasCenterMarker->show();
109}
@ Polygon
Polygons.
Definition qgis.h:368
static QString pkgDataPath()
Returns the common root path of all application data directories.
void setPreviewRect(const QgsRectangle &rect)
Sets the "preview" rectangle for the bounds overview map.
QgsCoordinateBoundsPreviewMapWidget(QWidget *parent=nullptr)
Constructor for QgsCoordinateBoundsPreviewMapWidget.
void setCanvasRect(const QgsRectangle &rect)
Sets the canvas bounds rectangle for the bounds overview map.
QgsRectangle canvasRect() const
Returns the current canvas bounds rectangle shown in the map.
Represents a coordinate reference system (CRS).
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
Q_DECL_DEPRECATED Qgis::GeometryOperationResult addPart(const QVector< QgsPointXY > &points, Qgis::GeometryType geomType=Qgis::GeometryType::Unknown)
Adds a new part to a the geometry.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
void zoomToFullExtent()
Zoom to the full extent of all layers currently visible in the canvas.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers that should be shown in the canvas.
void setExtent(const QgsRectangle &r, bool magnified=false)
Sets the extent of the map canvas to the specified rectangle.
QgsMapCanvas(QWidget *parent=nullptr)
Constructor.
void setPreviewJobsEnabled(bool enabled)
Sets whether canvas map preview jobs (low priority render jobs which render portions of the view just...
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets destination coordinate reference system.
void setMapTool(QgsMapTool *mapTool, bool clean=false)
Sets the map tool currently being used on the canvas.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
void refresh()
Repaints the canvas map.
A map tool for panning the map.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
QgsPointXY center
Responsible for drawing transient features (e.g.
Represents a vector layer which manages a vector based dataset.
A map canvas item for marking vertices of features using e.g.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6924
Setting options for loading vector layers.
bool forceReadOnly
Controls whether the layer is forced to be load as Read Only.