Skip to content

Commit

Permalink
New button to open attribute table on filtered features
Browse files Browse the repository at this point in the history
Adds a new button to the attribute form in search mode
to open the attribute table in attribute editor mode
from the filtered features.

Funded by: ARPA Piemonte
  • Loading branch information
elpaso authored and nyalldawson committed Dec 3, 2021
1 parent 1f8fb2e commit 1b9e804
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/gui/auto_generated/qgsattributeform.sip.in
Expand Up @@ -254,6 +254,14 @@ Emitted when the user chooses to flash a filtered set of features.
.. versionadded:: 3.0
%End

void openFilteredFeaturesAttributeTable( const QString &filter );
%Docstring
Emitted when the user chooses to open the attribute table dialog with a filtered set of features.

.. versionadded:: 3.24
%End


public slots:

void changeAttribute( const QString &field, const QVariant &value, const QString &hintText = QString() );
Expand Down
13 changes: 13 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -10623,6 +10623,19 @@ void QgisApp::selectByForm()
dlg->setMessageBar( messageBar() );
dlg->setMapCanvas( mapCanvas() );
dlg->setAttribute( Qt::WA_DeleteOnClose );
connect( dlg, &QgsSelectByFormDialog::showFilteredFeaturesAttributeTable, [ = ]( const QString & filter )
{
if ( !vlayer || !vlayer->dataProvider() )
{
return;
}

QgsAttributeTableDialog *dialog = new QgsAttributeTableDialog( vlayer, QgsAttributeTableFilterModel::FilterMode::ShowFilteredList );
dialog->setFilterExpression( filter );
dialog->setView( QgsDualView::ViewMode::AttributeEditor );
dialog->show();

} );
dlg->show();
}

Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -962,6 +962,11 @@ void QgsAttributeTableDialog::setFilterExpression( const QString &filterString,
mFeatureFilterWidget->setFilterExpression( filterString, type, alwaysShowFilter );
}

void QgsAttributeTableDialog::setView( QgsDualView::ViewMode mode )
{
mMainView->setView( mode );
}

void QgsAttributeTableDialog::deleteFeature( const QgsFeatureId fid )
{
QgsDebugMsg( QStringLiteral( "Delete %1" ).arg( fid ) );
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsattributetabledialog.h
Expand Up @@ -71,6 +71,12 @@ class APP_EXPORT QgsAttributeTableDialog : public QDialog, private Ui::QgsAttrib
QgsAttributeForm::FilterType type = QgsAttributeForm::ReplaceFilter,
bool alwaysShowFilter = false );

/**
* Set the view \a mode (e.g. attribute table or attribute editor).
* \since QGIS 3.24
*/
void setView( QgsDualView::ViewMode mode );

private slots:

/**
Expand Down
21 changes: 21 additions & 0 deletions src/app/qgsselectbyformdialog.cpp
Expand Up @@ -62,6 +62,7 @@ void QgsSelectByFormDialog::setMapCanvas( QgsMapCanvas *canvas )
mMapCanvas = canvas;
connect( mForm, &QgsAttributeForm::zoomToFeatures, this, &QgsSelectByFormDialog::zoomToFeatures );
connect( mForm, &QgsAttributeForm::flashFeatures, this, &QgsSelectByFormDialog::flashFeatures );
connect( mForm, &QgsAttributeForm::openFilteredFeaturesAttributeTable, this, &QgsSelectByFormDialog::showFilteredFeaturesAttributeTable );
}

void QgsSelectByFormDialog::zoomToFeatures( const QString &filter )
Expand Down Expand Up @@ -94,3 +95,23 @@ void QgsSelectByFormDialog::flashFeatures( const QString &filter )
Qgis::MessageLevel::Info );
}
}

void QgsSelectByFormDialog::openFeaturesAttributeTable( const QString &filter )
{
Q_ASSERT( mLayer );
QgsFeatureIterator it = mLayer->getFeatures( filter );
QgsFeature f;
if ( it.isValid() && it.nextFeature( f ) )
{
emit showFilteredFeaturesAttributeTable( filter );
}
else
{
if ( mMessageBar )
{
mMessageBar->pushMessage( QString(),
tr( "No matching features found" ),
Qgis::MessageLevel::Info );
}
}
}
5 changes: 5 additions & 0 deletions src/app/qgsselectbyformdialog.h
Expand Up @@ -66,6 +66,11 @@ class APP_EXPORT QgsSelectByFormDialog : public QDialog

void zoomToFeatures( const QString &filter );
void flashFeatures( const QString &filter );
void openFeaturesAttributeTable( const QString &filter );

signals:

void showFilteredFeaturesAttributeTable( const QString &filter );

private:

Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -1872,6 +1872,16 @@ void QgsAttributeForm::init()
connect( flashButton, &QToolButton::clicked, this, &QgsAttributeForm::searchFlash );
boxLayout->addWidget( flashButton );

QPushButton *openAttributeTableButton = new QPushButton();
openAttributeTableButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
openAttributeTableButton->setText( tr( "&Open Attribute Table" ) );
openAttributeTableButton->setToolTip( tr( "Open attribute table editor with filtered features" ) );
connect( openAttributeTableButton, &QToolButton::clicked, this, [ = ]
{
emit openFilteredFeaturesAttributeTable( createFilterExpression() );
} );
boxLayout->addWidget( openAttributeTableButton );

QPushButton *zoomButton = new QPushButton();
zoomButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
zoomButton->setText( tr( "&Zoom to Features" ) );
Expand Down
7 changes: 7 additions & 0 deletions src/gui/qgsattributeform.h
Expand Up @@ -268,6 +268,13 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
*/
void flashFeatures( const QString &filter );

/**
* Emitted when the user chooses to open the attribute table dialog with a filtered set of features.
* \since QGIS 3.24
*/
void openFilteredFeaturesAttributeTable( const QString &filter );


public slots:

/**
Expand Down

0 comments on commit 1b9e804

Please sign in to comment.