/
qgshighlight.h
171 lines (148 loc) · 5.2 KB
/
qgshighlight.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/***************************************************************************
qgshighlight.h - widget to highlight geometries
--------------------------------------
Date : 02-Mar-2011
Copyright : (C) 2011 by Juergen E. Fischer, norBIT GmbH
Email : jef at norbit dot de
***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef QGSHIGHLIGHT_H
#define QGSHIGHLIGHT_H
#include "qgsmapcanvasitem.h"
#include "qgsgeometry.h"
#include "qgssymbol.h"
#include <QBrush>
#include <QColor>
#include <QList>
#include <QPen>
#include <QPainter>
#include <QPainterPath>
#include "qgis_gui.h"
class QgsMapLayer;
class QgsVectorLayer;
class QgsSymbol;
/**
* \ingroup gui
* A class for highlight features on the map.
*
* The QgsHighlight class provides a transparent overlay canvas item
* for highlighting features or geometries on a map canvas.
*
* \code{.py}
* color = QColor(Qt.red)
* highlight = QgsHighlight(mapCanvas, feature, layer)
* highlight.setColor(color)
* color.setAlpha(50)
* highlight.setFillColor(color)
* highlight.show()
* \endcode
*/
class GUI_EXPORT QgsHighlight: public QObject, public QgsMapCanvasItem
{
Q_OBJECT
Q_PROPERTY( QColor color READ color WRITE setColor )
Q_PROPERTY( QColor fillColor READ fillColor WRITE setFillColor )
Q_PROPERTY( int width READ width WRITE setWidth )
Q_PROPERTY( int buffer READ buffer WRITE setBuffer )
public:
/**
* Constructor for QgsHighlight
* \param mapCanvas associated map canvas
* \param geom initial geometry of highlight
* \param layer associated map layer
*/
QgsHighlight( QgsMapCanvas *mapCanvas, const QgsGeometry &geom, QgsMapLayer *layer );
/**
* Constructor for highlighting true feature shape using feature attributes
* and renderer.
* \param mapCanvas map canvas
* \param feature
* \param layer vector layer
*/
QgsHighlight( QgsMapCanvas *mapCanvas, const QgsFeature &feature, QgsVectorLayer *layer );
~QgsHighlight() override;
/**
* Return line/stroke color
* \since QGIS 3.4
*/
QColor color( ) const { return mColor; }
/**
* Set line/stroke to color, polygon fill to color with alpha = 63.
* This is legacy function, use setFillColor() after setColor() if different fill color is required. */
void setColor( const QColor &color );
/**
* Return fill color
* \since QGIS 3.4
*/
QColor fillColor( ) const { return mFillColor; }
/**
* Fill color for the highlight.
* Will be used for polygons and points.
*
* \since QGIS 2.4
*/
void setFillColor( const QColor &fillColor );
/**
* Return stroke width
* \since QGIS 3.4
*/
int width( ) const { return mWidth; }
/**
* Set stroke width.
*
* \note Ignored in feature mode.
*/
void setWidth( int width );
/**
* Return buffer
* \since QGIS 3.4
*/
double buffer( ) const { return mBuffer; }
/**
* Set line / stroke buffer in millimeters.
*
* \since QGIS 2.4
*/
void setBuffer( double buffer ) { mBuffer = buffer; }
/**
* Set minimum line / stroke width in millimeters.
*
* \since QGIS 2.4
*/
void setMinWidth( double width ) { mMinWidth = width; }
/**
* Returns the layer for which this highlight has been created.
*/
QgsMapLayer *layer() const { return mLayer; }
void updatePosition() override;
protected:
void paint( QPainter *p ) override;
//! recalculates needed rectangle
void updateRect();
private:
void init();
void setSymbol( QgsSymbol *symbol, const QgsRenderContext &context, const QColor &color, const QColor &fillColor );
double getSymbolWidth( const QgsRenderContext &context, double width, QgsUnitTypes::RenderUnit unit );
//! Gets renderer for current color mode and colors. The renderer should be freed by caller.
std::unique_ptr< QgsFeatureRenderer > createRenderer( QgsRenderContext &context, const QColor &color, const QColor &fillColor );
void paintPoint( QPainter *p, const QgsPointXY &point );
void paintLine( QPainter *p, QgsPolylineXY line );
void paintPolygon( QPainter *p, const QgsPolygonXY &polygon );
int mWidth = 1; // line / stroke width property
QColor mColor; // line / stroke color property
QColor mFillColor; // line / stroke fillColor property
QBrush mBrush;
QPen mPen;
QgsGeometry *mGeometry = nullptr;
QgsMapLayer *mLayer = nullptr;
QgsFeature mFeature;
double mBuffer = 0; // line / stroke buffer in pixels
double mMinWidth = 0; // line / stroke minimum width in pixels
};
#endif