Skip to content

Commit

Permalink
[FEATURE]: Zoom to feature with right-click in attribute table
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Dec 28, 2015
1 parent 416c004 commit f815ebc
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
10 changes: 10 additions & 0 deletions python/gui/attributetable/qgsdualview.sip
Expand Up @@ -98,6 +98,16 @@ class QgsDualView : QStackedWidget
* @return The master model
*/
QgsAttributeTableModel* masterModel() const;
/**
Returns the filter model
@return the filter model
*/
QgsAttributeTableFilterModel* filterModel() const;
/**
Returns the table view
@return the table view
*/
const QgsAttributeTableView* tableView() const;

void setRequest( const QgsFeatureRequest& request );

Expand Down
5 changes: 5 additions & 0 deletions python/gui/qgsmapcanvas.sip
Expand Up @@ -168,6 +168,11 @@ class QgsMapCanvas : QGraphicsView
@param layer optionally specify different than current layer */
void zoomToSelected( QgsVectorLayer* layer = NULL );

/** Set canvas extent to the bounding box of a feature
@param layer the vector layer
@param id the feature id*/
void zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id );

/** Pan to the selected features of current (vector) layer keeping same extent. */
void panToSelected( QgsVectorLayer* layer = NULL );

Expand Down
22 changes: 22 additions & 0 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -240,6 +240,8 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
mMainView->setView( QgsDualView::AttributeTable );

editingToggled();

QObject::connect( mMainView->tableView(), SIGNAL( willShowContextMenu( QMenu*, QModelIndex ) ), this, SLOT( viewWillShowContextMenu( QMenu*, QModelIndex ) ) );
}

QgsAttributeTableDialog::~QgsAttributeTableDialog()
Expand Down Expand Up @@ -351,6 +353,26 @@ void QgsAttributeTableDialog::updateFieldFromExpressionSelected()
runFieldCalculation( mLayer, mFieldCombo->currentText(), mUpdateExpressionText->currentField(), filteredIds );
}

void QgsAttributeTableDialog::viewWillShowContextMenu( QMenu* menu, QModelIndex atIndex )
{
if ( menu )
{
menu->addAction( tr( "Zoom to feature" ), this, SLOT( zoomToFeature() ) );
}
}

void QgsAttributeTableDialog::zoomToFeature()
{
QModelIndex currentIndex = mMainView->tableView()->currentIndex();
if ( !currentIndex.isValid() )
{
return;
}

QgsFeatureId id = mMainView->filterModel()->rowToId( currentIndex );
QgisApp::instance()->mapCanvas()->zoomToFeatureId( mLayer, id );
}

void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, const QString& fieldName, const QString& expression, const QgsFeatureIds& filteredIds )
{
QApplication::setOverrideCursor( Qt::WaitCursor );
Expand Down
7 changes: 7 additions & 0 deletions src/app/qgsattributetabledialog.h
Expand Up @@ -203,6 +203,13 @@ class APP_EXPORT QgsAttributeTableDialog : public QDialog, private Ui::QgsAttrib
void updateFieldFromExpression();
void updateFieldFromExpressionSelected();

/** Add items to the rightclick menu
@menu the context menu
@atIndex the current model index*/
void viewWillShowContextMenu( QMenu* menu, QModelIndex atIndex );
/** Zooms to the active feature*/
void zoomToFeature();

private:
QMenu* mMenuActions;
QAction* mActionToggleEditing;
Expand Down
10 changes: 10 additions & 0 deletions src/gui/attributetable/qgsdualview.h
Expand Up @@ -136,6 +136,16 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
* @return The master model
*/
QgsAttributeTableModel* masterModel() const { return mMasterModel; }
/**
Returns the filter model
@return the filter model
*/
QgsAttributeTableFilterModel* filterModel() const { return mFilterModel; }
/**
Returns the table view
@return the table view
*/
const QgsAttributeTableView* tableView() const { return mTableView; }

void setRequest( const QgsFeatureRequest& request );

Expand Down
30 changes: 29 additions & 1 deletion src/gui/qgsmapcanvas.cpp
Expand Up @@ -1042,7 +1042,11 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer* layer )
return;

QgsRectangle rect = mapSettings().layerExtentToOutputExtent( layer, layer->boundingBoxOfSelected() );
zoomToFeatureExtent( rect );
} // zoomToSelected

void QgsMapCanvas::zoomToFeatureExtent( QgsRectangle& rect )
{
// no selected features, only one selected point feature
//or two point features with the same x- or y-coordinates
if ( rect.isEmpty() )
Expand All @@ -1063,7 +1067,31 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer* layer )

setExtent( rect );
refresh();
} // zoomToSelected
}

void QgsMapCanvas::zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id )
{
if ( !layer )
{
return;
}

QgsFeatureIterator it = layer->getFeatures( QgsFeatureRequest().setFilterFid( id ).setSubsetOfAttributes( QgsAttributeList() ) );
QgsFeature fet;
if ( !it.nextFeature( fet ) )
{
return;
}

QgsGeometry* geom = fet.geometry();
if ( !geom )
{
return;
}

QgsRectangle rect = mapSettings().layerExtentToOutputExtent( layer, geom->boundingBox() );
zoomToFeatureExtent( rect );
}

void QgsMapCanvas::panToSelected( QgsVectorLayer* layer )
{
Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -21,6 +21,7 @@
#include "qgsconfig.h"

#include "qgsexpressioncontext.h"
#include "qgsfeature.h"
#include "qgsrectangle.h"
#include "qgspoint.h"
#include "qgis.h"
Expand Down Expand Up @@ -236,6 +237,11 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
@param layer optionally specify different than current layer */
void zoomToSelected( QgsVectorLayer* layer = nullptr );

/** Set canvas extent to the bounding box of a feature
@param layer the vector layer
@param id the feature id*/
void zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id );

/** Pan to the selected features of current (vector) layer keeping same extent. */
void panToSelected( QgsVectorLayer* layer = nullptr );

Expand Down Expand Up @@ -619,6 +625,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! called when panning is in action, reset indicates end of panning
void moveCanvasContents( bool reset = false );

//! Zooms to feature extent. Adds a small margin around the extent
//! and does a pan if rect is empty (point extent)
void zoomToFeatureExtent( QgsRectangle& rect );

//! called on resize or changed extent to notify canvas items to change their rectangle
void updateCanvasItemPositions();

Expand Down

0 comments on commit f815ebc

Please sign in to comment.