Skip to content
Permalink
Browse files

[FEATURE]: Zoom to feature with right-click in attribute table

  • Loading branch information
mhugent committed Dec 28, 2015
1 parent 416c004 commit f815ebc86bcaf073bb929924a511ab58ffd6cd8e
@@ -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 );

@@ -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 );

@@ -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()
@@ -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 );
@@ -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;
@@ -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 );

@@ -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() )
@@ -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 )
{
@@ -21,6 +21,7 @@
#include "qgsconfig.h"

#include "qgsexpressioncontext.h"
#include "qgsfeature.h"
#include "qgsrectangle.h"
#include "qgspoint.h"
#include "qgis.h"
@@ -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 );

@@ -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();

0 comments on commit f815ebc

Please sign in to comment.
You can’t perform that action at this time.