Skip to content

Commit

Permalink
[FEATURE] add zoom to related feature in forms
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Apr 2, 2018
1 parent 409679b commit 7d48179
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 36 deletions.
4 changes: 4 additions & 0 deletions python/gui/qgsattributeeditorcontext.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@




class QgsAttributeEditorContext
{
%Docstring
Expand Down Expand Up @@ -50,6 +51,9 @@ Constructor for QgsAttributeEditorContext

const QgsDistanceArea &distanceArea() const;

void setMapCanvas( QgsMapCanvas *mapCanvas );
QgsMapCanvas *mapCanvas() const;

void setVectorLayerTools( QgsVectorLayerTools *vlTools );
const QgsVectorLayerTools *vectorLayerTools() const;

Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8198,6 +8198,7 @@ void QgisApp::selectByForm()
QgsAttributeEditorContext context;
context.setDistanceArea( myDa );
context.setVectorLayerTools( mVectorLayerTools );
context.setMapCanvas( mMapCanvas );

QgsSelectByFormDialog *dlg = new QgsSelectByFormDialog( vlayer, context, this );
dlg->setMessageBar( messageBar() );
Expand Down
1 change: 1 addition & 0 deletions src/app/qgisappinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ QgsAttributeDialog *QgisAppInterface::getFeatureForm( QgsVectorLayer *l, QgsFeat
QgsAttributeEditorContext context;
context.setDistanceArea( myDa );
context.setVectorLayerTools( qgis->vectorLayerTools() );
context.setMapCanvas( qgis->mapCanvas() );
QgsAttributeDialog *dialog = new QgsAttributeDialog( l, &feature, false, qgis, true, context );
if ( !feature.isValid() )
{
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttr

mEditorContext.setDistanceArea( *myDa );
mEditorContext.setVectorLayerTools( QgisApp::instance()->vectorLayerTools() );
mEditorContext.setMapCanvas( QgisApp::instance()->mapCanvas() );

QgsFeatureRequest r;
bool needsGeom = false;
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsfeatureaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )

context.setDistanceArea( myDa );
context.setVectorLayerTools( QgisApp::instance()->vectorLayerTools() );
context.setMapCanvas( QgisApp::instance()->mapCanvas() );
context.setFormMode( QgsAttributeEditorContext::StandaloneDialog );

QgsAttributeDialog *dialog = new QgsAttributeDialog( mLayer, f, cloneFeature, parentWidget(), true, context );
Expand Down
14 changes: 6 additions & 8 deletions src/gui/attributetable/qgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ QgsDualView::QgsDualView( QWidget *parent )

void QgsDualView::init( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const QgsFeatureRequest &request, const QgsAttributeEditorContext &context, bool loadFeatures )
{
mMapCanvas = mapCanvas;

if ( !layer )
return;

Expand Down Expand Up @@ -196,7 +194,7 @@ void QgsDualView::setFilterMode( QgsAttributeTableFilterModel::FilterMode filter
switch ( mFilterModel->filterMode() )
{
case QgsAttributeTableFilterModel::ShowVisible:
disconnect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, &QgsDualView::extentChanged );
disconnect( mFilterModel->mapCanvas(), &QgsMapCanvas::extentsChanged, this, &QgsDualView::extentChanged );
break;

case QgsAttributeTableFilterModel::ShowAll:
Expand Down Expand Up @@ -228,10 +226,10 @@ void QgsDualView::setFilterMode( QgsAttributeTableFilterModel::FilterMode filter
switch ( filterMode )
{
case QgsAttributeTableFilterModel::ShowVisible:
connect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, &QgsDualView::extentChanged );
if ( mMapCanvas )
connect( mFilterModel->mapCanvas(), &QgsMapCanvas::extentsChanged, this, &QgsDualView::extentChanged );
if ( mFilterModel->mapCanvas() )
{
QgsRectangle rect = mMapCanvas->mapSettings().mapToLayerCoordinates( mLayer, mMapCanvas->extent() );
QgsRectangle rect = mFilterModel->mapCanvas()->mapSettings().mapToLayerCoordinates( mLayer, mFilterModel->mapCanvas()->extent() );
r.setFilterRect( rect );
}
break;
Expand Down Expand Up @@ -874,9 +872,9 @@ void QgsDualView::updateSelectedFeatures()
void QgsDualView::extentChanged()
{
QgsFeatureRequest r = mMasterModel->request();
if ( mMapCanvas && ( r.filterType() != QgsFeatureRequest::FilterNone || !r.filterRect().isNull() ) )
if ( mFilterModel->mapCanvas() && ( r.filterType() != QgsFeatureRequest::FilterNone || !r.filterRect().isNull() ) )
{
QgsRectangle rect = mMapCanvas->mapSettings().mapToLayerCoordinates( mLayer, mMapCanvas->extent() );
QgsRectangle rect = mFilterModel->mapCanvas()->mapSettings().mapToLayerCoordinates( mLayer, mFilterModel->mapCanvas()->extent() );
r.setFilterRect( rect );
mMasterModel->setRequest( r );
mMasterModel->loadLayer();
Expand Down
1 change: 0 additions & 1 deletion src/gui/attributetable/qgsdualview.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
QString mDisplayExpression;
QgsAttributeTableConfig mConfig;
QgsScrollArea *mAttributeEditorScrollArea = nullptr;
QgsMapCanvas *mMapCanvas = nullptr;
// If the current feature is set, while the form is still not initialized
// we will temporarily save it in here and set it on init
QgsFeature mTempAttributeFormFeature;
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsattributeeditorcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "qgis_gui.h"
#include "qgsproject.h"

class QgsMapCanvas;

/**
* \ingroup gui
* This class contains context information for attribute editor widgets.
Expand Down Expand Up @@ -59,6 +61,7 @@ class GUI_EXPORT QgsAttributeEditorContext
QgsAttributeEditorContext( const QgsAttributeEditorContext &parentContext, FormMode formMode )
: mParentContext( &parentContext )
, mVectorLayerTools( parentContext.mVectorLayerTools )
, mMapCanvas( parentContext.mMapCanvas )
, mDistanceArea( parentContext.mDistanceArea )
, mFormMode( formMode )
{
Expand All @@ -68,6 +71,7 @@ class GUI_EXPORT QgsAttributeEditorContext
QgsAttributeEditorContext( const QgsAttributeEditorContext &parentContext, const QgsRelation &relation, RelationMode relationMode, FormMode widgetMode )
: mParentContext( &parentContext )
, mVectorLayerTools( parentContext.mVectorLayerTools )
, mMapCanvas( parentContext.mMapCanvas )
, mDistanceArea( parentContext.mDistanceArea )
, mRelation( relation )
, mRelationMode( relationMode )
Expand All @@ -87,6 +91,9 @@ class GUI_EXPORT QgsAttributeEditorContext

inline const QgsDistanceArea &distanceArea() const { return mDistanceArea; }

inline void setMapCanvas( QgsMapCanvas *mapCanvas ) { mMapCanvas = mapCanvas; }
inline QgsMapCanvas *mapCanvas() const { return mMapCanvas; }

inline void setVectorLayerTools( QgsVectorLayerTools *vlTools ) { mVectorLayerTools = vlTools; }
inline const QgsVectorLayerTools *vectorLayerTools() const { return mVectorLayerTools; }

Expand Down Expand Up @@ -130,6 +137,7 @@ class GUI_EXPORT QgsAttributeEditorContext
const QgsAttributeEditorContext *mParentContext = nullptr;
QgsVectorLayer *mLayer = nullptr;
QgsVectorLayerTools *mVectorLayerTools = nullptr;
QgsMapCanvas *mMapCanvas = nullptr;
QgsDistanceArea mDistanceArea;
QgsRelation mRelation;
RelationMode mRelationMode = Undefined;
Expand Down
4 changes: 1 addition & 3 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,6 @@ void QgsMapCanvas::zoomToFullExtent()
} // zoomToFullExtent



void QgsMapCanvas::zoomToPreviousExtent()
{
if ( mLastExtentIndex > 0 )
Expand Down Expand Up @@ -2281,7 +2280,6 @@ void QgsMapCanvas::schedulePreviewJob( int number )
mPreviewTimerConnection = connect( &mPreviewTimer, &QTimer::timeout, this, [ = ]()
{
startPreviewJob( number );
}
);
} );
mPreviewTimer.start();
}
69 changes: 45 additions & 24 deletions src/gui/qgsrelationeditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "qgstransactiongroup.h"
#include "qgslogger.h"
#include "qgsvectorlayerutils.h"
#include "qgsmapcanvas.h"

#include <QHBoxLayout>
#include <QLabel>
Expand Down Expand Up @@ -94,6 +95,13 @@ QgsRelationEditorWidget::QgsRelationEditorWidget( QWidget *parent )
mUnlinkFeatureButton->setToolTip( tr( "Unlink child feature" ) );
mUnlinkFeatureButton->setObjectName( QStringLiteral( "mUnlinkFeatureButton" ) );
buttonLayout->addWidget( mUnlinkFeatureButton );
// zoom to linked feature
mZoomToFeatureButton = new QToolButton( this );
mZoomToFeatureButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomToSelected.svg" ) ) );
mZoomToFeatureButton->setText( tr( "Zoom To Feature" ) );
mZoomToFeatureButton->setToolTip( tr( "Zoom to child feature" ) );
mZoomToFeatureButton->setObjectName( QStringLiteral( "mZoomToFeatureButton" ) );
buttonLayout->addWidget( mZoomToFeatureButton );
// spacer
buttonLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding ) );
// form view
Expand Down Expand Up @@ -141,6 +149,7 @@ QgsRelationEditorWidget::QgsRelationEditorWidget( QWidget *parent )
connect( mDeleteFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::deleteSelectedFeatures );
connect( mLinkFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::linkFeature );
connect( mUnlinkFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::unlinkSelectedFeatures );
connect( mZoomToFeatureButton, &QAbstractButton::clicked, this, &QgsRelationEditorWidget::zoomToSelectedFeatures );
connect( mFeatureSelectionMgr, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsRelationEditorWidget::updateButtons );

connect( mDualView, &QgsDualView::showContextMenuExternally, this, &QgsRelationEditorWidget::showContextMenu );
Expand Down Expand Up @@ -188,7 +197,6 @@ void QgsRelationEditorWidget::setRelationFeature( const QgsRelation &relation, c
if ( mVisible )
{
QgsFeatureRequest myRequest = mRelation.getRelatedFeaturesRequest( mFeature );

mDualView->init( mRelation.referencingLayer(), nullptr, myRequest, mEditorContext );
}
}
Expand Down Expand Up @@ -270,7 +278,6 @@ void QgsRelationEditorWidget::setViewMode( QgsDualView::ViewMode mode )
mViewMode = mode;
}


void QgsRelationEditorWidget::setFeature( const QgsFeature &feature )
{
mFeature = feature;
Expand Down Expand Up @@ -300,6 +307,25 @@ void QgsRelationEditorWidget::updateButtons()
mLinkFeatureButton->setEnabled( linkable );
mDeleteFeatureButton->setEnabled( editable && selectionNotEmpty );
mUnlinkFeatureButton->setEnabled( linkable && selectionNotEmpty );

mZoomToFeatureButton->setVisible(
mEditorContext.mapCanvas() && (
(
mNmRelation.isValid() &&
mNmRelation.referencedLayer()->geometryType() != QgsWkbTypes::NullGeometry &&
mNmRelation.referencedLayer()->geometryType() != QgsWkbTypes::UnknownGeometry
)
||
(
mRelation.isValid() &&
mRelation.referencedLayer()->geometryType() != QgsWkbTypes::NullGeometry &&
mRelation.referencedLayer()->geometryType() != QgsWkbTypes::UnknownGeometry
)
)
);

mZoomToFeatureButton->setEnabled( selectionNotEmpty );

mToggleEditingButton->setChecked( editable );
mSaveEditsButton->setEnabled( editable );
}
Expand Down Expand Up @@ -451,46 +477,42 @@ void QgsRelationEditorWidget::duplicateFeature()

void QgsRelationEditorWidget::deleteFeature( const QgsFeatureId featureid )
{
QgsFeatureIds featureids;

featureids << featureid;

deleteFeatures( featureids );

deleteFeatures( QgsFeatureIds() << featureid );
}

void QgsRelationEditorWidget::deleteSelectedFeatures( )
void QgsRelationEditorWidget::deleteSelectedFeatures()
{
deleteFeatures( mFeatureSelectionMgr->selectedFeatureIds() );
}

void QgsRelationEditorWidget::deleteFeatures( const QgsFeatureIds &featureids )
{
QgsVectorLayer *layer = nullptr;
if ( mNmRelation.isValid() )
layer = mNmRelation.referencedLayer();
else
layer = mRelation.referencingLayer();

QgsDebugMsg( QString( "Delete %1" ).arg( featureids.size() ) );
QgsVectorLayer *layer = mNmRelation.isValid() ? mNmRelation.referencedLayer() : mRelation.referencingLayer();
layer->deleteFeatures( featureids );
}


void QgsRelationEditorWidget::unlinkFeature( const QgsFeatureId featureid )
{
QgsFeatureIds featureids;

featureids << featureid;

unlinkFeatures( featureids );
unlinkFeatures( QgsFeatureIds() << featureid );
}

void QgsRelationEditorWidget::unlinkSelectedFeatures( )
void QgsRelationEditorWidget::unlinkSelectedFeatures()
{
unlinkFeatures( mFeatureSelectionMgr->selectedFeatureIds() );
}

void QgsRelationEditorWidget::zoomToSelectedFeatures()
{
QgsMapCanvas *c = mEditorContext.mapCanvas();
if ( !c )
return;

c->zoomToFeatureIds(
mNmRelation.isValid() ? mNmRelation.referencedLayer() : mRelation.referencingLayer(),
mFeatureSelectionMgr->selectedFeatureIds()
);
}

void QgsRelationEditorWidget::unlinkFeatures( const QgsFeatureIds &featureids )
{
if ( mNmRelation.isValid() )
Expand Down Expand Up @@ -581,7 +603,6 @@ void QgsRelationEditorWidget::saveEdits()

void QgsRelationEditorWidget::onCollapsedStateChanged( bool collapsed )
{

if ( !collapsed )
{
mVisible = true;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsrelationeditorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsCollapsibleGroupBox
void deleteSelectedFeatures();
void unlinkFeature( const QgsFeatureId featureid = QgsFeatureId() );
void unlinkSelectedFeatures();
void zoomToSelectedFeatures();
void saveEdits();
void toggleEditing( bool state );
void onCollapsedStateChanged( bool collapsed );
Expand All @@ -175,6 +176,7 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsCollapsibleGroupBox
QToolButton *mDeleteFeatureButton = nullptr;
QToolButton *mLinkFeatureButton = nullptr;
QToolButton *mUnlinkFeatureButton = nullptr;
QToolButton *mZoomToFeatureButton = nullptr;
QToolButton *mFormViewButton = nullptr;
QToolButton *mTableViewButton = nullptr;
QGridLayout *mRelationLayout = nullptr;
Expand Down

0 comments on commit 7d48179

Please sign in to comment.