Skip to content

Commit 42edad5

Browse files
committed
[FEATURE] new navigation tool Pan Map to Selection (fix #4031)
1 parent 138d4a2 commit 42edad5

13 files changed

+96
-32
lines changed

images/images.qrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<file>themes/default/mActionOpenTable.png</file>
112112
<file>themes/default/mActionOptions.png</file>
113113
<file>themes/default/mActionPan.png</file>
114+
<file>themes/default/mActionPanToSelected.png</file>
114115
<file>themes/default/mAction.png</file>
115116
<file>themes/default/mActionProjectProperties.png</file>
116117
<file>themes/default/mActionPropertyItem.png</file>
@@ -295,6 +296,7 @@
295296
<file>themes/gis/mActionNodeTool.png</file>
296297
<file>themes/gis/mActionOpenTable.png</file>
297298
<file>themes/gis/mActionPan.png</file>
299+
<file>themes/gis/mActionPanToSelected.png</file>
298300
<file>themes/gis/mActionRaiseItems.png</file>
299301
<file>themes/gis/mActionRedo.png</file>
300302
<file>themes/gis/mActionRemoveLayer.png</file>
1.04 KB
Loading
1.04 KB
Loading

python/gui/qgisinterface.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class QgisInterface : QObject
289289

290290
//! View menu actions
291291
virtual QAction *actionPan() = 0;
292+
virtual QAction *actionPanToSelected() = 0;
292293
virtual QAction *actionZoomIn() = 0;
293294
virtual QAction *actionZoomOut() = 0;
294295
virtual QAction *actionSelect() = 0;

python/gui/qgsmapcanvas.sip

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ class QgsMapCanvasLayer
1212

1313
public:
1414
QgsMapCanvasLayer(QgsMapLayer* layer, bool visible = TRUE, bool isInOverview = FALSE);
15-
15+
1616
void setVisible(bool visible);
1717
void setInOverview(bool isInOverview);
18-
18+
1919
bool isVisible() const;
2020
bool isInOverview() const;
21-
21+
2222
QgsMapLayer* layer();
2323
//const QgsMapLayer* layer() const;
24-
24+
2525
};
2626

2727

@@ -53,17 +53,17 @@ class QgsMapCanvas : QGraphicsView
5353
~QgsMapCanvas();
5454

5555
void setLayerSet(QList<QgsMapCanvasLayer>& layers);
56-
56+
5757
void setCurrentLayer(QgsMapLayer* layer);
58-
58+
5959
void updateOverview();
60-
60+
6161
void enableOverviewMode(QgsMapOverviewCanvas* overview);
62-
62+
6363
QgsMapCanvasMap* map();
64-
64+
6565
QgsMapRenderer* mapRenderer();
66-
66+
6767
//! Accessor for the canvas pixmap
6868
// @deprecated use canvasPaintDevice()
6969
QPixmap& canvasPixmap() /Deprecated/;
@@ -99,24 +99,28 @@ class QgsMapCanvas : QGraphicsView
9999

100100
// ! Clears the list of extents and sets current extent as first item
101101
void clearExtentHistory();
102-
102+
103103
/** Zoom to the extent of the selected features of current (vector) layer.
104104
Added in version 1.2: optionally specify different than current layer */
105105
void zoomToSelected(QgsVectorLayer* layer = NULL);
106106

107+
/** Pan to the selected features of current (vector) layer keeping same extent.
108+
@note added in 2.0 */
109+
void panToSelected( QgsVectorLayer* layer = NULL );
110+
107111
/** \brief Sets the map tool currently being used on the canvas */
108112
void setMapTool(QgsMapTool* mapTool);
109-
110-
/** \brief Unset the current mapset tool or last non zoom tool if
111-
* it the same as passed map tool pointer. The tool is not
112-
* referenced/used any more, but the instance is not deleted
113+
114+
/** \brief Unset the current mapset tool or last non zoom tool if
115+
* it the same as passed map tool pointer. The tool is not
116+
* referenced/used any more, but the instance is not deleted
113117
* by this method.
114118
*/
115119
void unsetMapTool(QgsMapTool* mapTool);
116120

117121
/**Returns the currently active tool*/
118122
QgsMapTool* mapTool();
119-
123+
120124
/** Write property of QColor bgColor. */
121125
virtual void setCanvasColor(const QColor & _newVal);
122126

@@ -128,7 +132,7 @@ class QgsMapCanvas : QGraphicsView
128132

129133
//! return the map layer at position index in the layer stack
130134
QgsMapLayer *layer(int index);
131-
135+
132136
//! return number of layers on the map
133137
int layerCount() const;
134138

@@ -162,10 +166,10 @@ class QgsMapCanvas : QGraphicsView
162166

163167
//! true if canvas currently drawing
164168
bool isDrawing();
165-
169+
166170
//! returns current layer (set by legend widget)
167171
QgsMapLayer* currentLayer();
168-
172+
169173
//! set wheel action and zoom factor (should be greater than 1)
170174
void setWheelAction(WheelAction action, double factor = 2);
171175

@@ -178,7 +182,7 @@ class QgsMapCanvas : QGraphicsView
178182
//! Zoom to a specific scale
179183
// added in 1.5
180184
void zoomScale( double scale );
181-
185+
182186
//! Zoom with the factor supplied. Factor > 1 zooms in
183187
void zoomByFactor( double scaleFactor );
184188

@@ -187,7 +191,7 @@ class QgsMapCanvas : QGraphicsView
187191

188192
//! used to determine if anti-aliasing is enabled or not
189193
void enableAntiAliasing(bool theFlag);
190-
194+
191195
//! Select which Qt class to render with
192196
void useImageToRender(bool theFlag);
193197

@@ -197,10 +201,10 @@ class QgsMapCanvas : QGraphicsView
197201
void panActionEnd(QPoint releasePoint);
198202
//! Called when mouse is moving and pan is activated
199203
void panAction(QMouseEvent * event);
200-
204+
201205
//! returns last position of mouse cursor
202206
QPoint mouseLastXY();
203-
207+
204208
public slots:
205209

206210
/**Sets dirty=true and calls render()*/
@@ -222,19 +226,19 @@ class QgsMapCanvas : QGraphicsView
222226

223227
/** The map units may have changed, so cope with that */
224228
void mapUnitsChanged();
225-
229+
226230
/** updates pixmap on render progress */
227231
void updateMap();
228-
232+
229233
//! show whatever error is exposed by the QgsMapLayer.
230234
void showError(QgsMapLayer * mapLayer);
231-
235+
232236
//! called to read map canvas settings from project
233237
void readProject(const QDomDocument &);
234-
238+
235239
//! called to write map canvas settings to project
236240
void writeProject(QDomDocument &);
237-
241+
238242
signals:
239243
/** Let the owner know how far we are with render operations */
240244
void setProgress(int,int);
@@ -271,7 +275,7 @@ class QgsMapCanvas : QGraphicsView
271275

272276
//! Emit key release event
273277
void keyReleased(QKeyEvent * e);
274-
278+
275279
//! Emit map tool changed event
276280
void mapToolSet(QgsMapTool *tool);
277281

@@ -315,7 +319,6 @@ class QgsMapCanvas : QGraphicsView
315319
//! called on resize or changed extent to notify canvas items to change their rectangle
316320
void updateCanvasItemPositions();
317321

318-
319322
}; // class QgsMapCanvas
320323

321324

src/app/qgisapp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ void QgisApp::createActions()
808808
// View Menu Items
809809

810810
connect( mActionPan, SIGNAL( triggered() ), this, SLOT( pan() ) );
811+
connect( mActionPanToSelected, SIGNAL( triggered() ), this, SLOT( panToSelected() ) );
811812
connect( mActionZoomIn, SIGNAL( triggered() ), this, SLOT( zoomIn() ) );
812813
connect( mActionZoomOut, SIGNAL( triggered() ), this, SLOT( zoomOut() ) );
813814
connect( mActionSelect, SIGNAL( triggered() ), this, SLOT( select() ) );
@@ -3266,6 +3267,11 @@ void QgisApp::zoomToSelected()
32663267
mMapCanvas->zoomToSelected();
32673268
}
32683269

3270+
void QgisApp::panToSelected()
3271+
{
3272+
mMapCanvas->panToSelected();
3273+
}
3274+
32693275
void QgisApp::pan()
32703276
{
32713277
mMapCanvas->setMapTool( mMapTools.mPan );

src/app/qgisapp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
239239
QAction *actionSnappingOptions() { return mActionSnappingOptions; }
240240

241241
QAction *actionPan() { return mActionPan; }
242+
QAction *actionPanToSelected() { return mActionPanToSelected; }
242243
QAction *actionZoomIn() { return mActionZoomIn; }
243244
QAction *actionZoomOut() { return mActionZoomOut; }
244245
QAction *actionSelect() { return mActionSelect; }
@@ -374,6 +375,9 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
374375
void zoomToNext();
375376
//! Zoom to selected features
376377
void zoomToSelected();
378+
//! Pan map to selected features
379+
//! @note added in 2.0
380+
void panToSelected();
377381

378382
//! open the properties dialog for the currently selected layer
379383
void layerProperties();

src/app/qgisappinterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ QgsRasterLayer* QgisAppInterface::addRasterLayer( const QString& url, const QStr
113113
return qgis->addRasterLayer( url, baseName, providerKey, layers, styles, format, crs );
114114
}
115115

116-
117116
bool QgisAppInterface::addProject( QString theProjectName )
118117
{
119118
return qgis->addProject( theProjectName );
@@ -413,6 +412,7 @@ QAction *QgisAppInterface::actionEditSeparator2() { return 0; }
413412

414413
//! View menu actions
415414
QAction *QgisAppInterface::actionPan() { return qgis->actionPan(); }
415+
QAction *QgisAppInterface::actionPanToSelected() { return qgis->actionPanToSelected(); }
416416
QAction *QgisAppInterface::actionZoomIn() { return qgis->actionZoomIn(); }
417417
QAction *QgisAppInterface::actionZoomOut() { return qgis->actionZoomOut(); }
418418
QAction *QgisAppInterface::actionSelect() { return qgis->actionSelect(); }

src/app/qgisappinterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ class QgisAppInterface : public QgisInterface
261261

262262
//! View menu actions
263263
virtual QAction *actionPan();
264+
virtual QAction *actionPanToSelected();
264265
virtual QAction *actionZoomIn();
265266
virtual QAction *actionZoomOut();
266267
virtual QAction *actionSelect();

src/gui/qgisinterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ class GUI_EXPORT QgisInterface : public QObject
402402

403403
//! View menu actions
404404
virtual QAction *actionPan() = 0;
405+
virtual QAction *actionPanToSelected() = 0;
405406
virtual QAction *actionZoomIn() = 0;
406407
virtual QAction *actionZoomOut() = 0;
407408
virtual QAction *actionSelect() = 0;

src/gui/qgsmapcanvas.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,34 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer* layer )
720720
refresh();
721721
} // zoomToSelected
722722

723+
void QgsMapCanvas::panToSelected( QgsVectorLayer* layer )
724+
{
725+
if ( mDrawing )
726+
{
727+
return;
728+
}
729+
730+
if ( layer == NULL )
731+
{
732+
// use current layer by default
733+
layer = qobject_cast<QgsVectorLayer *>( mCurrentLayer );
734+
}
735+
736+
if ( layer == NULL )
737+
{
738+
return;
739+
}
740+
741+
if ( layer->selectedFeatureCount() == 0 )
742+
{
743+
return;
744+
}
745+
746+
QgsRectangle rect = mMapRenderer->layerExtentToOutputExtent( layer, layer->boundingBoxOfSelected() );
747+
setExtent( QgsRectangle( rect.center(), rect.center() ) );
748+
refresh();
749+
} // panToSelected
750+
723751
void QgsMapCanvas::keyPressEvent( QKeyEvent * e )
724752
{
725753

src/gui/qgsmapcanvas.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
156156
Added in version 1.2: optionally specify different than current layer */
157157
void zoomToSelected( QgsVectorLayer* layer = NULL );
158158

159+
/** Pan to the selected features of current (vector) layer keeping same extent.
160+
@note added in 2.0 */
161+
void panToSelected( QgsVectorLayer* layer = NULL );
162+
159163
/** \brief Sets the map tool currently being used on the canvas */
160164
void setMapTool( QgsMapTool* mapTool );
161165

src/ui/qgisapp.ui

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<x>0</x>
1818
<y>0</y>
1919
<width>1052</width>
20-
<height>25</height>
20+
<height>28</height>
2121
</rect>
2222
</property>
2323
<widget class="QMenu" name="mEditMenu">
@@ -107,6 +107,7 @@
107107
<addaction name="mActionDecorationScaleBar"/>
108108
</widget>
109109
<addaction name="mActionPan"/>
110+
<addaction name="mActionPanToSelected"/>
110111
<addaction name="mActionZoomIn"/>
111112
<addaction name="mActionZoomOut"/>
112113
<addaction name="separator"/>
@@ -307,6 +308,7 @@
307308
<bool>false</bool>
308309
</attribute>
309310
<addaction name="mActionPan"/>
311+
<addaction name="mActionPanToSelected"/>
310312
<addaction name="mActionZoomIn"/>
311313
<addaction name="mActionZoomOut"/>
312314
<addaction name="mActionZoomActualSize"/>
@@ -1590,6 +1592,18 @@
15901592
<string>Run feature action</string>
15911593
</property>
15921594
</action>
1595+
<action name="mActionPanToSelected">
1596+
<property name="icon">
1597+
<iconset resource="../../images/images.qrc">
1598+
<normaloff>:/images/themes/default/mActionPanToSelected.png</normaloff>:/images/themes/default/mActionPanToSelected.png</iconset>
1599+
</property>
1600+
<property name="text">
1601+
<string>Pan Map to Selection</string>
1602+
</property>
1603+
<property name="toolTip">
1604+
<string>Pan Map to Selection</string>
1605+
</property>
1606+
</action>
15931607
</widget>
15941608
<resources>
15951609
<include location="../../images/images.qrc"/>

0 commit comments

Comments
 (0)