Skip to content
Permalink
Browse files

[FEATURE] add zoom to related feature in forms

  • Loading branch information
jef-n committed Apr 2, 2018
1 parent 409679b commit 7d48179223e9e619a70295093425ada2f4279b7b
@@ -10,6 +10,7 @@




class QgsAttributeEditorContext
{
%Docstring
@@ -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;

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

@@ -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:
@@ -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;
@@ -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();
@@ -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;
@@ -25,6 +25,8 @@
#include "qgis_gui.h"
#include "qgsproject.h"

class QgsMapCanvas;

/**
* \ingroup gui
* This class contains context information for attribute editor widgets.
@@ -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 )
{
@@ -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 )
@@ -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; }

@@ -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;
@@ -893,7 +893,6 @@ void QgsMapCanvas::zoomToFullExtent()
} // zoomToFullExtent



void QgsMapCanvas::zoomToPreviousExtent()
{
if ( mLastExtentIndex > 0 )
@@ -2281,7 +2280,6 @@ void QgsMapCanvas::schedulePreviewJob( int number )
mPreviewTimerConnection = connect( &mPreviewTimer, &QTimer::timeout, this, [ = ]()
{
startPreviewJob( number );
}
);
} );
mPreviewTimer.start();
}
@@ -29,6 +29,7 @@
#include "qgstransactiongroup.h"
#include "qgslogger.h"
#include "qgsvectorlayerutils.h"
#include "qgsmapcanvas.h"

#include <QHBoxLayout>
#include <QLabel>
@@ -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
@@ -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 );
@@ -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 );
}
}
@@ -270,7 +278,6 @@ void QgsRelationEditorWidget::setViewMode( QgsDualView::ViewMode mode )
mViewMode = mode;
}


void QgsRelationEditorWidget::setFeature( const QgsFeature &feature )
{
mFeature = feature;
@@ -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 );
}
@@ -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() )
@@ -581,7 +603,6 @@ void QgsRelationEditorWidget::saveEdits()

void QgsRelationEditorWidget::onCollapsedStateChanged( bool collapsed )
{

if ( !collapsed )
{
mVisible = true;
@@ -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 );
@@ -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;

0 comments on commit 7d48179

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