Skip to content

Commit 48da1de

Browse files
committedJul 14, 2016
[FEATURE]: pan to current feature in attribute table
1 parent b61641d commit 48da1de

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed
 

‎src/gui/attributetable/qgsdualview.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ void QgsDualView::viewWillShowContextMenu( QMenu* menu, const QModelIndex& atInd
430430
if ( canvas && vl && vl->geometryType() != QGis::NoGeometry )
431431
{
432432
menu->addAction( tr( "Zoom to feature" ), this, SLOT( zoomToCurrentFeature() ) );
433+
menu->addAction( tr( "Pan to feature" ), this, SLOT( panToCurrentFeature() ) );
433434
}
434435

435436
//add user-defined actions to context menu
@@ -651,6 +652,23 @@ void QgsDualView::zoomToCurrentFeature()
651652
}
652653
}
653654

655+
void QgsDualView::panToCurrentFeature()
656+
{
657+
QModelIndex currentIndex = mTableView->currentIndex();
658+
if ( !currentIndex.isValid() )
659+
{
660+
return;
661+
}
662+
663+
QgsFeatureIds ids;
664+
ids.insert( mFilterModel->rowToId( currentIndex ) );
665+
QgsMapCanvas* canvas = mFilterModel->mapCanvas();
666+
if ( canvas )
667+
{
668+
canvas->panToFeatureIds( mLayerCache->layer(), ids );
669+
}
670+
}
671+
654672
void QgsDualView::previewExpressionChanged( const QString& expression )
655673
{
656674
mLayerCache->layer()->setDisplayExpression( expression );

‎src/gui/attributetable/qgsdualview.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
317317

318318
/** Zooms to the active feature*/
319319
void zoomToCurrentFeature();
320+
/** Pans to the active feature*/
321+
void panToCurrentFeature();
320322

321323
private:
322324
void initLayerCache( QgsVectorLayer *layer, bool cacheGeometry );

‎src/gui/qgsmapcanvas.cpp

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,39 +1162,74 @@ void QgsMapCanvas::zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds&
11621162
return;
11631163
}
11641164

1165+
QgsRectangle bbox;
1166+
QString errorMsg;
1167+
if ( boundingBoxOfFeatureIds( ids, layer, bbox, errorMsg ) )
1168+
{
1169+
zoomToFeatureExtent( bbox );
1170+
}
1171+
else
1172+
{
1173+
emit messageEmitted( tr( "Zoom to feature id failed" ), errorMsg, QgsMessageBar::WARNING );
1174+
}
1175+
1176+
}
1177+
1178+
void QgsMapCanvas::panToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids )
1179+
{
1180+
if ( !layer )
1181+
{
1182+
return;
1183+
}
1184+
1185+
QgsRectangle bbox;
1186+
QString errorMsg;
1187+
if ( boundingBoxOfFeatureIds( ids, layer, bbox, errorMsg ) )
1188+
{
1189+
setCenter( bbox.center() );
1190+
refresh();
1191+
}
1192+
else
1193+
{
1194+
emit messageEmitted( tr( "Pan to feature id failed" ), errorMsg, QgsMessageBar::WARNING );
1195+
}
1196+
}
1197+
1198+
bool QgsMapCanvas::boundingBoxOfFeatureIds( const QgsFeatureIds& ids, QgsVectorLayer* layer, QgsRectangle& bbox, QString& errorMsg ) const
1199+
{
11651200
QgsFeatureIterator it = layer->getFeatures( QgsFeatureRequest().setFilterFids( ids ).setSubsetOfAttributes( QgsAttributeList() ) );
1166-
QgsRectangle rect;
1167-
rect.setMinimal();
1201+
bbox.setMinimal();
11681202
QgsFeature fet;
11691203
int featureCount = 0;
1204+
errorMsg.clear();
1205+
11701206
while ( it.nextFeature( fet ) )
11711207
{
11721208
const QgsGeometry* geom = fet.constGeometry();
1173-
QString errorMessage;
11741209
if ( !geom || geom->isEmpty() )
11751210
{
1176-
errorMessage = tr( "Feature does not have a geometry" );
1211+
errorMsg = tr( "Feature does not have a geometry" );
11771212
}
11781213
else if ( geom->geometry()->isEmpty() )
11791214
{
1180-
errorMessage = tr( "Feature geometry is empty" );
1215+
errorMsg = tr( "Feature geometry is empty" );
11811216
}
1182-
if ( !errorMessage.isEmpty() )
1217+
if ( !errorMsg.isEmpty() )
11831218
{
1184-
emit messageEmitted( tr( "Zoom to feature id failed" ), errorMessage, QgsMessageBar::WARNING );
1185-
return;
1219+
return false;
11861220
}
11871221
QgsRectangle r = mapSettings().layerExtentToOutputExtent( layer, geom->boundingBox() );
1188-
rect.combineExtentWith( r );
1222+
bbox.combineExtentWith( r );
11891223
featureCount++;
11901224
}
11911225

11921226
if ( featureCount != ids.count() )
11931227
{
1194-
return;
1228+
errorMsg = tr( "Feature not found" );
1229+
return false;
11951230
}
11961231

1197-
zoomToFeatureExtent( rect );
1232+
return true;
11981233
}
11991234

12001235
void QgsMapCanvas::panToSelected( QgsVectorLayer* layer )

‎src/gui/qgsmapcanvas.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
253253
@param ids the feature ids*/
254254
void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
255255

256+
/** Centers canvas extent to feature ids
257+
@param layer the vector layer
258+
@param ids the feature ids*/
259+
void panToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
260+
256261
/** Pan to the selected features of current (vector) layer keeping same extent. */
257262
void panToSelected( QgsVectorLayer* layer = nullptr );
258263

@@ -813,6 +818,13 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
813818
*/
814819
void endZoomRect( QPoint pos );
815820

821+
/** Returns bounding box of feature list (in canvas coordinates)
822+
@param ids feature id list
823+
@param bbox out: bounding box
824+
@param errorMsg error message in case of error
825+
@return true in case of success*/
826+
bool boundingBoxOfFeatureIds( const QgsFeatureIds& ids, QgsVectorLayer* layer, QgsRectangle& bbox, QString& errorMsg ) const;
827+
816828
friend class TestQgsMapCanvas;
817829

818830
}; // class QgsMapCanvas

0 commit comments

Comments
 (0)
Please sign in to comment.