QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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#include "moc_qgscoordinateboundspreviewmapwidget.cpp"
13#include "qgsrubberband.h"
14#include "qgsvertexmarker.h"
15#include "qgsapplication.h"
16#include "qgsvectorlayer.h"
17#include "qgsmaptoolpan.h"
18
20 : QgsMapCanvas( parent )
21{
22 mPreviewBand = new QgsRubberBand( this, Qgis::GeometryType::Polygon );
23 mPreviewBand->setWidth( 4 );
24
25 mCanvasPreviewBand = new QgsRubberBand( this, Qgis::GeometryType::Polygon );
26 mCanvasPreviewBand->setWidth( 4 );
27 const QColor rectColor = QColor( 185, 84, 210, 60 );
28 mCanvasPreviewBand->setColor( rectColor );
29
30 mCanvasCenterMarker = new QgsVertexMarker( this );
31 mCanvasCenterMarker->setIconType( QgsVertexMarker::ICON_CROSS );
32 mCanvasCenterMarker->setColor( QColor( 185, 84, 210 ) );
33 mCanvasCenterMarker->setPenWidth( 3 );
34
35 const QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:4326" ) );
36 setDestinationCrs( srs );
37
38 const QString layerPath = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/world_map.gpkg|layername=countries" );
40 options.forceReadOnly = true;
41 mLayers << new QgsVectorLayer( layerPath, tr( "World Map" ), QStringLiteral( "ogr" ), options );
42 setLayers( mLayers );
43 mPanTool = new QgsMapToolPan( this );
44 setMapTool( mPanTool );
46}
47
49{
50 setMapTool( nullptr );
51 delete mPanTool;
52 mPanTool = nullptr;
53
54 qDeleteAll( mLayers );
55 delete mPreviewBand;
56 delete mCanvasPreviewBand;
57 delete mCanvasCenterMarker;
58}
59
61{
62 if ( !qgsDoubleNear( rect.area(), 0.0 ) )
63 {
64 QgsGeometry geom;
65 if ( rect.xMinimum() > rect.xMaximum() )
66 {
67 const QgsRectangle rect1 = QgsRectangle( -180, rect.yMinimum(), rect.xMaximum(), rect.yMaximum() );
68 const QgsRectangle rect2 = QgsRectangle( rect.xMinimum(), rect.yMinimum(), 180, rect.yMaximum() );
69 geom = QgsGeometry::fromRect( rect1 );
70 geom.addPart( QgsGeometry::fromRect( rect2 ) );
71 }
72 else
73 {
74 geom = QgsGeometry::fromRect( rect );
75 }
76 mPreviewBand->setToGeometry( geom, nullptr );
77 mPreviewBand->setColor( QColor( 255, 0, 0, 65 ) );
79 extent.scale( 1.1 );
81 refresh();
82 mPreviewBand->show();
83 }
84 else
85 {
86 mPreviewBand->hide();
88 }
89}
90
95
97{
98 mCanvasRect = rect;
99 mCanvasPreviewBand->setToGeometry( QgsGeometry::fromRect( mCanvasRect ), nullptr );
100 mCanvasPreviewBand->show();
101 mCanvasCenterMarker->setCenter( rect.center() );
102 mCanvasCenterMarker->show();
103}
@ 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.
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.
Map canvas is a class for displaying all GIS data types on a canvas.
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.
A rectangle specified with double values.
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
double area() const
Returns the area of the rectangle.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
QgsPointXY center() const
Returns the center point of the rectangle.
A class for drawing transient features (e.g.
void setWidth(double 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:5917
Setting options for loading vector layers.
bool forceReadOnly
Controls whether the layer is forced to be load as Read Only.