-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgscoordinateboundspreviewmapwidget.cpp
95 lines (86 loc) · 3.39 KB
/
qgscoordinateboundspreviewmapwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/***************************************************************************
* qgscoordinateboundspreviewmapwidget.h *
* Copyright (C) 2019 by Nyall Dawson *
* nyall dot dawson at gmail dot com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "qgscoordinateboundspreviewmapwidget.h"
#include "qgsrubberband.h"
#include "qgsvertexmarker.h"
#include "qgsapplication.h"
#include "qgsvectorlayer.h"
#include "qgsmaptoolpan.h"
QgsCoordinateBoundsPreviewMapWidget::QgsCoordinateBoundsPreviewMapWidget( QWidget *parent )
: QgsMapCanvas( parent )
{
mPreviewBand = new QgsRubberBand( this, QgsWkbTypes::PolygonGeometry );
mPreviewBand->setWidth( 4 );
mCanvasPreviewBand = new QgsRubberBand( this, QgsWkbTypes::PolygonGeometry );
mCanvasPreviewBand->setWidth( 4 );
QColor rectColor = QColor( 185, 84, 210, 60 );
mCanvasPreviewBand->setColor( rectColor );
mCanvasCenterMarker = new QgsVertexMarker( this );
mCanvasCenterMarker->setIconType( QgsVertexMarker::ICON_CROSS );
mCanvasCenterMarker->setColor( QColor( 185, 84, 210 ) );
mCanvasCenterMarker->setPenWidth( 3 );
QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:4326" ) );
setDestinationCrs( srs );
QString layerPath = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/world_map.gpkg|layername=countries" );
mLayers << new QgsVectorLayer( layerPath );
setLayers( mLayers );
setMapTool( new QgsMapToolPan( this ) );
setPreviewJobsEnabled( true );
}
QgsCoordinateBoundsPreviewMapWidget::~QgsCoordinateBoundsPreviewMapWidget()
{
qDeleteAll( mLayers );
delete mPreviewBand;
delete mCanvasPreviewBand;
delete mCanvasCenterMarker;
}
void QgsCoordinateBoundsPreviewMapWidget::setPreviewRect( const QgsRectangle &rect )
{
if ( !qgsDoubleNear( rect.area(), 0.0 ) )
{
QgsGeometry geom;
if ( rect.xMinimum() > rect.xMaximum() )
{
QgsRectangle rect1 = QgsRectangle( -180, rect.yMinimum(), rect.xMaximum(), rect.yMaximum() );
QgsRectangle rect2 = QgsRectangle( rect.xMinimum(), rect.yMinimum(), 180, rect.yMaximum() );
geom = QgsGeometry::fromRect( rect1 );
geom.addPart( QgsGeometry::fromRect( rect2 ) );
}
else
{
geom = QgsGeometry::fromRect( rect );
}
mPreviewBand->setToGeometry( geom, nullptr );
mPreviewBand->setColor( QColor( 255, 0, 0, 65 ) );
QgsRectangle extent = geom.boundingBox();
extent.scale( 1.1 );
setExtent( extent );
refresh();
mPreviewBand->show();
}
else
{
mPreviewBand->hide();
zoomToFullExtent();
}
}
QgsRectangle QgsCoordinateBoundsPreviewMapWidget::canvasRect() const
{
return mCanvasRect;
}
void QgsCoordinateBoundsPreviewMapWidget::setCanvasRect( const QgsRectangle &rect )
{
mCanvasRect = rect;
mCanvasPreviewBand->setToGeometry( QgsGeometry::fromRect( mCanvasRect ), nullptr );
mCanvasPreviewBand->show();
mCanvasCenterMarker->setCenter( rect.center() );
mCanvasCenterMarker->show();
}