QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 "moc_qgscoordinateboundspreviewmapwidget.cpp"
20
22 : QgsMapCanvas( parent )
23{
24 mPreviewBand = new QgsRubberBand( this, Qgis::GeometryType::Polygon );
25 mPreviewBand->setWidth( 4 );
26
27 mCanvasPreviewBand = new QgsRubberBand( this, Qgis::GeometryType::Polygon );
28 mCanvasPreviewBand->setWidth( 4 );
29 const QColor rectColor = QColor( 185, 84, 210, 60 );
30 mCanvasPreviewBand->setColor( rectColor );
31
32 mCanvasCenterMarker = new QgsVertexMarker( this );
33 mCanvasCenterMarker->setIconType( QgsVertexMarker::ICON_CROSS );
34 mCanvasCenterMarker->setColor( QColor( 185, 84, 210 ) );
35 mCanvasCenterMarker->setPenWidth( 3 );
36
37 const QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:4326" ) );
38 setDestinationCrs( srs );
39
40 const QString layerPath = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/world_map.gpkg|layername=countries" );
42 options.forceReadOnly = true;
43 mLayers << new QgsVectorLayer( layerPath, tr( "World Map" ), QStringLiteral( "ogr" ), options );
44 setLayers( mLayers );
45 mPanTool = new QgsMapToolPan( this );
46 setMapTool( mPanTool );
48}
49
51{
52 setMapTool( nullptr );
53 delete mPanTool;
54 mPanTool = nullptr;
55
56 qDeleteAll( mLayers );
57 delete mPreviewBand;
58 delete mCanvasPreviewBand;
59 delete mCanvasCenterMarker;
60}
61
63{
64 if ( !qgsDoubleNear( rect.area(), 0.0 ) )
65 {
66 QgsGeometry geom;
67 if ( rect.xMinimum() > rect.xMaximum() )
68 {
69 const QgsRectangle rect1 = QgsRectangle( -180, rect.yMinimum(), rect.xMaximum(), rect.yMaximum() );
70 const QgsRectangle rect2 = QgsRectangle( rect.xMinimum(), rect.yMinimum(), 180, rect.yMaximum() );
71 geom = QgsGeometry::fromRect( rect1 );
72 geom.addPart( QgsGeometry::fromRect( rect2 ) );
73 }
74 else
75 {
76 geom = QgsGeometry::fromRect( rect );
77 }
78 mPreviewBand->setToGeometry( geom, nullptr );
79 mPreviewBand->setColor( QColor( 255, 0, 0, 65 ) );
81 extent.scale( 1.1 );
83 refresh();
84 mPreviewBand->show();
85 }
86 else
87 {
88 mPreviewBand->hide();
90 }
91}
92
97
99{
100 mCanvasRect = rect;
101 mCanvasPreviewBand->setToGeometry( QgsGeometry::fromRect( mCanvasRect ), nullptr );
102 mCanvasPreviewBand->show();
103 mCanvasCenterMarker->setCenter( rect.center() );
104 mCanvasCenterMarker->show();
105}
@ Polygon
Polygons.
Definition qgis.h:361
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:6607
Setting options for loading vector layers.
bool forceReadOnly
Controls whether the layer is forced to be load as Read Only.