QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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#include "qgsrubberband.h"
13#include "qgsvertexmarker.h"
14#include "qgsapplication.h"
15#include "qgsvectorlayer.h"
16#include "qgsmaptoolpan.h"
17
19 : QgsMapCanvas( parent )
20{
21 mPreviewBand = new QgsRubberBand( this, Qgis::GeometryType::Polygon );
22 mPreviewBand->setWidth( 4 );
23
24 mCanvasPreviewBand = new QgsRubberBand( this, Qgis::GeometryType::Polygon );
25 mCanvasPreviewBand->setWidth( 4 );
26 const QColor rectColor = QColor( 185, 84, 210, 60 );
27 mCanvasPreviewBand->setColor( rectColor );
28
29 mCanvasCenterMarker = new QgsVertexMarker( this );
30 mCanvasCenterMarker->setIconType( QgsVertexMarker::ICON_CROSS );
31 mCanvasCenterMarker->setColor( QColor( 185, 84, 210 ) );
32 mCanvasCenterMarker->setPenWidth( 3 );
33
34 const QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:4326" ) );
35 setDestinationCrs( srs );
36
37 const QString layerPath = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/world_map.gpkg|layername=countries" );
39 options.forceReadOnly = true;
40 mLayers << new QgsVectorLayer( layerPath, tr( "World Map" ), QStringLiteral( "ogr" ), options );
41 setLayers( mLayers );
42 mPanTool = new QgsMapToolPan( this );
43 setMapTool( mPanTool );
45}
46
48{
49 setMapTool( nullptr );
50 delete mPanTool;
51 mPanTool = nullptr;
52
53 qDeleteAll( mLayers );
54 delete mPreviewBand;
55 delete mCanvasPreviewBand;
56 delete mCanvasCenterMarker;
57}
58
60{
61 if ( !qgsDoubleNear( rect.area(), 0.0 ) )
62 {
63 QgsGeometry geom;
64 if ( rect.xMinimum() > rect.xMaximum() )
65 {
66 const QgsRectangle rect1 = QgsRectangle( -180, rect.yMinimum(), rect.xMaximum(), rect.yMaximum() );
67 const QgsRectangle rect2 = QgsRectangle( rect.xMinimum(), rect.yMinimum(), 180, rect.yMaximum() );
68 geom = QgsGeometry::fromRect( rect1 );
69 geom.addPart( QgsGeometry::fromRect( rect2 ) );
70 }
71 else
72 {
73 geom = QgsGeometry::fromRect( rect );
74 }
75 mPreviewBand->setToGeometry( geom, nullptr );
76 mPreviewBand->setColor( QColor( 255, 0, 0, 65 ) );
78 extent.scale( 1.1 );
80 refresh();
81 mPreviewBand->show();
82 }
83 else
84 {
85 mPreviewBand->hide();
87 }
88}
89
91{
92 return mCanvasRect;
93}
94
96{
97 mCanvasRect = rect;
98 mCanvasPreviewBand->setToGeometry( QgsGeometry::fromRect( mCanvasRect ), nullptr );
99 mCanvasPreviewBand->show();
100 mCanvasCenterMarker->setCenter( rect.center() );
101 mCanvasCenterMarker->show();
102}
@ Polygon
Polygons.
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.
This class represents a coordinate reference system (CRS).
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
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.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:93
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.
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.
Definition: qgsmaptoolpan.h:31
A rectangle specified with double values.
Definition: qgsrectangle.h:42
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:267
double area() const
Returns the area of the rectangle.
Definition: qgsrectangle.h:251
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:201
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:211
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:196
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:206
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:262
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:54
void setWidth(int width)
Sets the width of the line.
void setColor(const QColor &color)
Sets the color for the rubberband.
void setToGeometry(const QgsGeometry &geom, QgsVectorLayer *layer)
Sets this rubber band to geom.
Represents a vector layer which manages a vector based data sets.
A class for marking vertices of features using e.g.
void setPenWidth(int width)
void setCenter(const QgsPointXY &point)
Sets the center point of the marker, in map coordinates.
void setIconType(int iconType)
void setColor(const QColor &color)
Sets the stroke color for the marker.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:5207
Setting options for loading vector layers.
bool forceReadOnly
Controls whether the layer is forced to be load as Read Only.