QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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, QgsWkbTypes::PolygonGeometry );
22  mPreviewBand->setWidth( 4 );
23 
24  mCanvasPreviewBand = new QgsRubberBand( this, QgsWkbTypes::PolygonGeometry );
25  mCanvasPreviewBand->setWidth( 4 );
26  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  QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:4326" ) );
35  setDestinationCrs( srs );
36 
37  QString layerPath = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/world_map.gpkg|layername=countries" );
38  mLayers << new QgsVectorLayer( layerPath );
39  setLayers( mLayers );
40  setMapTool( new QgsMapToolPan( this ) );
41  setPreviewJobsEnabled( true );
42 }
43 
45 {
46  qDeleteAll( mLayers );
47  delete mPreviewBand;
48  delete mCanvasPreviewBand;
49  delete mCanvasCenterMarker;
50 }
51 
53 {
54  if ( !qgsDoubleNear( rect.area(), 0.0 ) )
55  {
56  QgsGeometry geom;
57  if ( rect.xMinimum() > rect.xMaximum() )
58  {
59  QgsRectangle rect1 = QgsRectangle( -180, rect.yMinimum(), rect.xMaximum(), rect.yMaximum() );
60  QgsRectangle rect2 = QgsRectangle( rect.xMinimum(), rect.yMinimum(), 180, rect.yMaximum() );
61  geom = QgsGeometry::fromRect( rect1 );
62  geom.addPart( QgsGeometry::fromRect( rect2 ) );
63  }
64  else
65  {
66  geom = QgsGeometry::fromRect( rect );
67  }
68  mPreviewBand->setToGeometry( geom, nullptr );
69  mPreviewBand->setColor( QColor( 255, 0, 0, 65 ) );
71  extent.scale( 1.1 );
72  setExtent( extent );
73  refresh();
74  mPreviewBand->show();
75  }
76  else
77  {
78  mPreviewBand->hide();
80  }
81 }
82 
84 {
85  return mCanvasRect;
86 }
87 
89 {
90  mCanvasRect = rect;
91  mCanvasPreviewBand->setToGeometry( QgsGeometry::fromRect( mCanvasRect ), nullptr );
92  mCanvasPreviewBand->show();
93  mCanvasCenterMarker->setCenter( rect.center() );
94  mCanvasCenterMarker->show();
95 }
QgsGeometry::addPart
OperationResult addPart(const QVector< QgsPointXY > &points, QgsWkbTypes::GeometryType geomType=QgsWkbTypes::UnknownGeometry)
Adds a new part to a the geometry.
Definition: qgsgeometry.cpp:699
QgsMapCanvas::extent
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
Definition: qgsmapcanvas.cpp:1056
QgsMapCanvas::refresh
void refresh()
Repaints the canvas map.
Definition: qgsmapcanvas.cpp:525
QgsVertexMarker::setIconType
void setIconType(int iconType)
Definition: qgsvertexmarker.cpp:25
QgsRubberBand
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:50
QgsRectangle::center
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
Definition: qgsrectangle.h:230
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsVertexMarker::ICON_CROSS
@ ICON_CROSS
Definition: qgsvertexmarker.h:53
QgsCoordinateBoundsPreviewMapWidget::~QgsCoordinateBoundsPreviewMapWidget
~QgsCoordinateBoundsPreviewMapWidget() override
Definition: qgscoordinateboundspreviewmapwidget.cpp:44
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsMapCanvas::setPreviewJobsEnabled
void setPreviewJobsEnabled(bool enabled)
Sets whether canvas map preview jobs (low priority render jobs which render portions of the view just...
Definition: qgsmapcanvas.cpp:781
qgsrubberband.h
qgsapplication.h
QgsVertexMarker
A class for marking vertices of features using e.g.
Definition: qgsvertexmarker.h:37
QgsRectangle::xMaximum
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
QgsRectangle::scale
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:235
qgsmaptoolpan.h
QgsRectangle::area
double area() const SIP_HOLDGIL
Returns the area of the rectangle.
Definition: qgsrectangle.h:218
QgsCoordinateBoundsPreviewMapWidget::QgsCoordinateBoundsPreviewMapWidget
QgsCoordinateBoundsPreviewMapWidget(QWidget *parent=nullptr)
Constructor for QgsCoordinateBoundsPreviewMapWidget.
Definition: qgscoordinateboundspreviewmapwidget.cpp:18
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
qgsvertexmarker.h
QgsCoordinateBoundsPreviewMapWidget::setPreviewRect
void setPreviewRect(const QgsRectangle &rect)
Sets the "preview" rectangle for the bounds overview map.
Definition: qgscoordinateboundspreviewmapwidget.cpp:52
QgsVertexMarker::setPenWidth
void setPenWidth(int width)
Definition: qgsvertexmarker.cpp:54
QgsRubberBand::setColor
void setColor(const QColor &color)
Sets the color for the rubberband.
Definition: qgsrubberband.cpp:46
QgsVertexMarker::setCenter
void setCenter(const QgsPointXY &point)
Definition: qgsvertexmarker.cpp:35
QgsVertexMarker::setColor
void setColor(const QColor &color)
Sets the stroke color for the marker.
Definition: qgsvertexmarker.cpp:42
QgsMapCanvas::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
sets destination coordinate reference system
Definition: qgsmapcanvas.cpp:396
QgsMapCanvas::zoomToFullExtent
void zoomToFullExtent()
Zoom to the full extent of all layers.
Definition: qgsmapcanvas.cpp:1200
QgsMapToolPan
A map tool for panning the map.
Definition: qgsmaptoolpan.h:33
QgsApplication::pkgDataPath
static QString pkgDataPath()
Returns the common root path of all application data directories.
Definition: qgsapplication.cpp:578
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
QgsMapCanvas::setExtent
void setExtent(const QgsRectangle &r, bool magnified=false)
Sets the extent of the map canvas to the specified rectangle.
Definition: qgsmapcanvas.cpp:1067
QgsMapCanvas::setMapTool
void setMapTool(QgsMapTool *mapTool, bool clean=false)
Sets the map tool currently being used on the canvas.
Definition: qgsmapcanvas.cpp:2006
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
qgsvectorlayer.h
QgsRubberBand::setWidth
void setWidth(int width)
Sets the width of the line.
Definition: qgsrubberband.cpp:70
QgsRectangle::yMaximum
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsGeometry::fromRect
static QgsGeometry fromRect(const QgsRectangle &rect) SIP_HOLDGIL
Creates a new geometry from a QgsRectangle.
Definition: qgsgeometry.cpp:229
qgscoordinateboundspreviewmapwidget.h
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:996
QgsCoordinateBoundsPreviewMapWidget::canvasRect
QgsRectangle canvasRect() const
Returns the current canvas bounds rectangle shown in the map.
Definition: qgscoordinateboundspreviewmapwidget.cpp:83
QgsRubberBand::setToGeometry
void setToGeometry(const QgsGeometry &geom, QgsVectorLayer *layer)
Sets this rubber band to geom.
Definition: qgsrubberband.cpp:255
QgsMapCanvas::setLayers
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers that should be shown in the canvas.
Definition: qgsmapcanvas.cpp:340
QgsCoordinateBoundsPreviewMapWidget::setCanvasRect
void setCanvasRect(const QgsRectangle &rect)
Sets the canvas bounds rectangle for the bounds overview map.
Definition: qgscoordinateboundspreviewmapwidget.cpp:88