-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsmapcanvas.h
410 lines (293 loc) · 11.9 KB
/
qgsmapcanvas.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/***************************************************************************
qgsmapcanvas.h - description
-------------------
begin : Sun Jun 30 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman at mrcc.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. *
* *
***************************************************************************/
/* $Id: qgsmapcanvas.h 5341 2006-04-22 12:11:36Z wonder $ */
#ifndef QGSMAPCANVAS_H
#define QGSMAPCANVAS_H
#include <list>
#include <memory>
#include <deque>
#include "qgsrect.h"
#include "qgspoint.h"
#include "qgis.h"
#include <QDomDocument>
#include <QGraphicsView>
class QWheelEvent;
class QPixmap;
class QPaintEvent;
class QKeyEvent;
class ResizeEvent;
class QColor;
class QDomDocument;
class QPaintDevice;
class QMouseEvent;
class QRubberBand;
class QGraphicsScene;
class QgsMapToPixel;
class QgsMapLayer;
class QgsMapLayerInterface;
class QgsLegend;
class QgsLegendView;
class QgsRubberBand;
class QgsMapRender;
class QgsMapCanvasMap;
class QgsMapOverviewCanvas;
class QgsMapTool;
/** \class QgsMapCanvasLayer
\brief class that stores additional layer's flags together with pointer to the layer
*/
class GUI_EXPORT QgsMapCanvasLayer
{
public:
QgsMapCanvasLayer(QgsMapLayer* layer, bool visible = TRUE, bool inOverview = FALSE)
: mLayer(layer), mVisible(visible), mInOverview(inOverview) {}
void setVisible(bool visible) { mVisible = visible; }
void setInOverview(bool inOverview) { mInOverview = inOverview; }
bool visible() const { return mVisible; }
bool inOverview() const { return mInOverview; }
QgsMapLayer* layer() { return mLayer; }
const QgsMapLayer* layer() const { return mLayer; }
private:
QgsMapLayer* mLayer;
/** Flag whether layer is visible */
bool mVisible;
/** Flag whether layer is shown in overview */
bool mInOverview;
};
/*! \class QgsMapCanvas
* \brief Map canvas class for displaying all GIS data types.
*/
class GUI_EXPORT QgsMapCanvas : public QGraphicsView
{
Q_OBJECT;
public:
enum WheelAction { WheelZoom, WheelZoomAndRecenter, WheelZoomToMouseCursor, WheelNothing };
//! Constructor
QgsMapCanvas(QWidget * parent = 0, const char *name = 0);
//! Destructor
~QgsMapCanvas();
void setLayerSet(QList<QgsMapCanvasLayer>& layers);
void setCurrentLayer(QgsMapLayer* layer);
void updateOverview();
void setOverview(QgsMapOverviewCanvas* overview);
QgsMapCanvasMap* map();
QgsMapRender* mapRender();
//! Accessor for the canvas pixmap
QPixmap& canvasPixmap();
//! Get the last reported scale of the canvas
double getScale();
//! Clear the map canvas
void clear();
//! Returns the mupp (map units per pixel) for the canvas
double mupp() const;
//! Returns the current zoom exent of the map canvas
QgsRect extent() const;
//! Returns the combined exent for all layers on the map canvas
QgsRect fullExtent() const;
//! Set the extent of the map canvas
void setExtent(QgsRect const & r);
//! Zoom to the full extent of all layers
void zoomFullExtent();
//! Zoom to the previous extent (view)
void zoomPreviousExtent();
/**Zooms to the extend of the selected features*/
void zoomToSelected();
/** \brief Sets the map tool currently being used on the canvas */
void setMapTool(QgsMapTool* mapTool);
/** \brief Unset the current map tool or last non zoom tool
*
* This is called from destructor of map tools to make sure
* that this map tool won't be used any more.
* You don't have to call it manualy, QgsMapTool takes care of it.
*/
void unsetMapTool(QgsMapTool* mapTool);
/**Returns the currently active tool*/
QgsMapTool* mapTool();
/** Write property of QColor bgColor. */
virtual void setCanvasColor(const QColor & _newVal);
/** Read property of QColor bgColor. */
virtual QColor canvasColor() const;
/** Emits signal scalChanged to update scale in main window */
void updateScale();
/** Updates the full extent */
void updateFullExtent();
//! return the map layer at postion index in the layer stack
QgsMapLayer *getZpos(int index);
//! return number of layers on the map
int layerCount() const;
/*! Freeze/thaw the map canvas. This is used to prevent the canvas from
* responding to events while layers are being added/removed etc.
* @param frz Boolean specifying if the canvas should be frozen (true) or
* thawed (false). Default is true.
*/
void freeze(bool frz = true);
/*! Accessor for frozen status of canvas */
bool isFrozen();
//! Flag the canvas as dirty and needed a refresh
void setDirty(bool _dirty);
//! Return the state of the canvas (dirty or not)
bool isDirty() const;
//! Set map units (needed by project properties dialog)
void setMapUnits(QGis::units mapUnits);
//! Get the current canvas map units
QGis::units mapUnits() const;
//! Get the current coordinate transform
QgsMapToPixel * getCoordinateTransform();
//! true if canvas currently drawing
bool isDrawing();
//! returns current layer (set by legend widget)
QgsMapLayer* currentLayer();
//! set wheel action and zoom factor (should be greater than 1)
void setWheelAction(WheelAction action, double factor = 2);
//! Zooms in/out preserving
void zoom(bool zoomIn);
//! Zooms in/out with a given center
void zoomWithCenter(int x, int y, bool zoomIn);
//! used to determine if anti-aliasing is enabled or not
void enableAntiAliasing(bool theFlag);
//! Select which Qt class to render with
void useQImageToRender(bool theFlag);
// following 2 methods should be moved elsewhere or changed to private
// currently used by pan map tool
//! Ends pan action and redraws the canvas.
void panActionEnd(QPoint releasePoint);
//! Called when mouse is moving and pan is activated
void panAction(QMouseEvent * event);
//! returns last position of mouse cursor
QPoint mouseLastXY();
//! zooms with the factor supplied. Factor > 1 zooms in
void zoom(double scaleFactor);
public slots:
/**Repaints the canvas map*/
void refresh();
//! Save the convtents of the map canvas to disk as an image
void saveAsImage(QString theFileName,QPixmap * QPixmap=0, QString="PNG" );
//! This slot is connected to the visibility change of one or more layers
void layerStateChange();
//! Whether to suppress rendering or not
void setRenderFlag(bool theFlag);
//! State of render suppression flag
bool renderFlag() {return mRenderFlag;};
/** A simple helper method to find out if on the fly projections are enabled or not */
bool projectionsEnabled();
/** The map units may have changed, so cope with that */
void mapUnitsChanged();
/** updates pixmap on render progress */
void updateMap();
//! show whatever error is exposed by the QgsMapLayer.
void showError(QgsMapLayer * mapLayer);
//! called to read map canvas settings from project
void readProject(const QDomDocument &);
//! called to write map canvas settings to project
void writeProject(QDomDocument &);
signals:
/** Let the owner know how far we are with render operations */
void setProgress(int,int);
/** emits current mouse position */
void xyCoordinates(QgsPoint & p);
//! Emitted when the scale of the map changes
void scaleChanged(double);
//! Emitted when the extents of the map change
void extentsChanged();
/** Emitted when the canvas has rendered.
Passes a pointer to the painter on which the map was drawn. This is
useful for plugins that wish to draw on the map after it has been
rendered. Passing the painter allows plugins to work when the map is
being rendered onto a pixmap other than the mapCanvas own pixmap member.
*/
void renderComplete(QPainter *);
//! Emitted when a new set of layers has been received
void layersChanged();
//! Emit key press event
void keyPressed(QKeyEvent * e);
//! Emit key release event
void keyReleased(QKeyEvent * e);
protected:
//! Overridden key press event
void keyPressEvent(QKeyEvent * e);
//! Overridden key release event
void keyReleaseEvent(QKeyEvent * e);
//! Overridden mouse move event
void mouseMoveEvent(QMouseEvent * e);
//! Overridden mouse press event
void mousePressEvent(QMouseEvent * e);
//! Overridden mouse release event
void mouseReleaseEvent(QMouseEvent * e);
//! Overridden mouse wheel event
void wheelEvent(QWheelEvent * e);
//! Overridden resize event
void resizeEvent(QResizeEvent * e);
//! called when panning is in action, reset indicates end of panning
void moveCanvasContents(bool reset = FALSE);
//! called on resize or changed extent to notify canvas items to change their rectangle
void updateCanvasItemsPositions();
/// implementation struct
class CanvasProperties;
/// Handle pattern for implementation object
std::auto_ptr<CanvasProperties> mCanvasProperties;
private:
/// this class is non-copyable
/**
@note
Otherwise std::auto_ptr would pass the object responsiblity on to the
copy like a hot potato leaving the copyer in a weird state.
*/
QgsMapCanvas( QgsMapCanvas const & );
//! all map rendering is done in this class
QgsMapRender* mMapRender;
//! owns pixmap with rendered map and controls rendering
QgsMapCanvasMap* mMap;
//! map overview widget - it's controlled by QgsMapCanvas
QgsMapOverviewCanvas* mMapOverview;
//! Flag indicating a map refresh is in progress
bool mDrawing;
//! Flag indicating if the map canvas is frozen.
bool mFrozen;
/*! \brief Flag to track the state of the Map canvas.
*
* The canvas is
* flagged as dirty by any operation that changes the state of
* the layers or the view extent. If the canvas is not dirty, paint
* events are handled by bit-blitting the stored canvas bitmap to
* the canvas. This improves performance by not reading the data source
* when no real change has occurred
*/
bool mDirty;
//! determines whether user has requested to suppress rendering
bool mRenderFlag;
/**Resize events that have been ignored because the canvas is busy with
rendering may put their sizes into this list. The canvas then picks up
the last entry in case a lot of resize events arrive in short time*/
QList< QPair<int, int> > mResizeQueue;
/** debugging member
invoked when a connect() is made to this object
*/
void connectNotify( const char * signal );
//! current layer in legend
QgsMapLayer* mCurrentLayer;
//! graphics scene manages canvas items
QGraphicsScene* mScene;
//! pointer to current map tool
QgsMapTool* mMapTool;
//! previous tool if current is for zooming/panning
QgsMapTool* mLastNonZoomMapTool;
//! recently used extent
QgsRect mLastExtent;
//! Scale factor multiple for default zoom in/out
double mWheelZoomFactor;
//! Mouse wheel action
WheelAction mWheelAction;
}; // class QgsMapCanvas
#endif