Skip to content

Commit

Permalink
[needs-docs] Add some shortcuts for opening the attribute table
Browse files Browse the repository at this point in the history
Adds:
- shift + f6: show table filtered to selected features
- ctrl + f6: show table filtered to visible features

(These are alongside the existing 'f6' shortcut which opens the
table using the default mode set via the options dialog)
  • Loading branch information
nyalldawson committed Nov 30, 2017
1 parent f6ffb00 commit ded892e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 23 deletions.
29 changes: 26 additions & 3 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,24 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
toggleSnapping->setProperty( "Icon", QgsApplication::getThemeIcon( QStringLiteral( "/mIconSnapping.svg" ) ) );
connect( toggleSnapping, &QShortcut::activated, mSnappingUtils, &QgsSnappingUtils::toggleEnabled );

QShortcut *attributeTableSelected = new QShortcut( QKeySequence( tr( "Shift+F6" ) ), this );
attributeTableSelected->setObjectName( QStringLiteral( "attributeTableSelectedFeatures" ) );
attributeTableSelected->setWhatsThis( tr( "Open Attribute Table (Selected Features)" ) );
attributeTableSelected->setProperty( "Icon", QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) ) );
connect( attributeTableSelected, &QShortcut::activated, this, [ = ]
{
attributeTable( QgsAttributeTableFilterModel::ShowSelected );
} );

QShortcut *attributeTableVisible = new QShortcut( QKeySequence( tr( "Ctrl+F6" ) ), this );
attributeTableVisible->setObjectName( QStringLiteral( "attributeTableVisibleFeatures" ) );
attributeTableVisible->setWhatsThis( tr( "Open Attribute Table (Visible Features)" ) );
attributeTableVisible->setProperty( "Icon", QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) ) );
connect( attributeTableVisible, &QShortcut::activated, this, [ = ]
{
attributeTable( QgsAttributeTableFilterModel::ShowVisible );
} );

if ( ! QTouchDevice::devices().isEmpty() )
{
//add reacting to long click in touch
Expand Down Expand Up @@ -1934,7 +1952,12 @@ void QgisApp::createActions()
connect( mActionAddAmsLayer, &QAction::triggered, this, [ = ] { dataSourceManager( QStringLiteral( "arcgismapserver" ) ); } );
connect( mActionAddDelimitedText, &QAction::triggered, this, [ = ] { dataSourceManager( QStringLiteral( "delimitedtext" ) ); } );
connect( mActionAddVirtualLayer, &QAction::triggered, this, [ = ] { dataSourceManager( QStringLiteral( "virtual" ) ); } );
connect( mActionOpenTable, &QAction::triggered, this, &QgisApp::attributeTable );
connect( mActionOpenTable, &QAction::triggered, this, [ = ]
{
QgsSettings settings;
QgsAttributeTableFilterModel::FilterMode initialMode = static_cast< QgsAttributeTableFilterModel::FilterMode>( settings.value( QStringLiteral( "qgis/attributeTableBehavior" ), QgsAttributeTableFilterModel::ShowAll ).toInt() );
attributeTable( initialMode );
} );
connect( mActionOpenFieldCalc, &QAction::triggered, this, &QgisApp::fieldCalculator );
connect( mActionToggleEditing, &QAction::triggered, this, [ = ] { toggleEditing(); } );
connect( mActionSaveLayerEdits, &QAction::triggered, this, &QgisApp::saveActiveLayerEdits );
Expand Down Expand Up @@ -6688,15 +6711,15 @@ void QgisApp::fieldCalculator()
}
}

void QgisApp::attributeTable()
void QgisApp::attributeTable( QgsAttributeTableFilterModel::FilterMode filter )
{
QgsVectorLayer *myLayer = qobject_cast<QgsVectorLayer *>( activeLayer() );
if ( !myLayer )
{
return;
}

QgsAttributeTableDialog *mDialog = new QgsAttributeTableDialog( myLayer );
QgsAttributeTableDialog *mDialog = new QgsAttributeTableDialog( myLayer, filter );
mDialog->show();
// the dialog will be deleted by itself on close
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class QgsGeoCmsProviderRegistry;
#include "qgsrasterminmaxorigin.h"
#include "qgsmaplayeractionregistry.h"
#include "qgsoptionswidgetfactory.h"

#include "qgsattributetablefiltermodel.h"
#include "ui_qgisapp.h"
#include "qgis_app.h"

Expand Down Expand Up @@ -682,7 +682,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void layerProperties();

//! show the attribute table for the currently selected layer
void attributeTable();
void attributeTable( QgsAttributeTableFilterModel::FilterMode filter = QgsAttributeTableFilterModel::ShowAll );

void fieldCalculator();

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisappinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ QDialog *QgisAppInterface::showAttributeTable( QgsVectorLayer *l, const QString
{
if ( l )
{
QgsAttributeTableDialog *dialog = new QgsAttributeTableDialog( l );
QgsAttributeTableDialog *dialog = new QgsAttributeTableDialog( l, QgsAttributeTableFilterModel::ShowFilteredList );
dialog->setFilterExpression( filterExpression );
dialog->show();
return dialog;
Expand Down
8 changes: 2 additions & 6 deletions src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void QgsAttributeTableDialog::updateMultiEditButtonState()
}
}

QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QWidget *parent, Qt::WindowFlags flags )
QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttributeTableFilterModel::FilterMode initialMode, QWidget *parent, Qt::WindowFlags flags )
: QDialog( parent, flags )
, mLayer( layer )

Expand Down Expand Up @@ -150,7 +150,6 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QWidget

QgsFeatureRequest r;
bool needsGeom = false;
QgsAttributeTableFilterModel::FilterMode initialMode = static_cast< QgsAttributeTableFilterModel::FilterMode>( settings.value( QStringLiteral( "qgis/attributeTableBehavior" ), QgsAttributeTableFilterModel::ShowAll ).toInt() );
if ( mLayer->geometryType() != QgsWkbTypes::NullGeometry &&
initialMode == QgsAttributeTableFilterModel::ShowVisible )
{
Expand Down Expand Up @@ -277,10 +276,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QWidget
mMainViewButtonGroup->setId( mTableViewButton, QgsDualView::AttributeTable );
mMainViewButtonGroup->setId( mAttributeViewButton, QgsDualView::AttributeEditor );

// Load default attribute table filter
QgsAttributeTableFilterModel::FilterMode defaultFilterMode = ( QgsAttributeTableFilterModel::FilterMode ) settings.value( QStringLiteral( "qgis/attributeTableBehavior" ), QgsAttributeTableFilterModel::ShowAll ).toInt();

switch ( defaultFilterMode )
switch ( initialMode )
{
case QgsAttributeTableFilterModel::ShowVisible:
filterVisible();
Expand Down
4 changes: 3 additions & 1 deletion src/app/qgsattributetabledialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ class APP_EXPORT QgsAttributeTableDialog : public QDialog, private Ui::QgsAttrib
/**
* Constructor
* \param layer layer pointer
* \param initialMode initial filter for dialog
* \param parent parent object
* \param flags window flags
*/
QgsAttributeTableDialog( QgsVectorLayer *layer, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::Window );
QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttributeTableFilterModel::FilterMode initialMode = QgsAttributeTableFilterModel::ShowAll, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::Window );

~QgsAttributeTableDialog();

QgsExpressionContext createExpressionContext() const override;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolidentifyaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void QgsMapToolIdentifyAction::showAttributeTable( QgsMapLayer *layer, const QLi
}
filter = filter.replace( QRegExp( ",$" ), QStringLiteral( ")" ) );

QgsAttributeTableDialog *tableDialog = new QgsAttributeTableDialog( vl );
QgsAttributeTableDialog *tableDialog = new QgsAttributeTableDialog( vl, QgsAttributeTableFilterModel::ShowFilteredList );
tableDialog->setFilterExpression( filter );
tableDialog->show();
}
Expand Down
13 changes: 4 additions & 9 deletions tests/src/app/testqgsattributetable.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ void TestQgsAttributeTable::initTestCase()
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );

QSettings().setValue( QStringLiteral( "/qgis/attributeTableBehavior" ), QgsAttributeTableFilterModel::ShowAll );
}

//runs after all tests
Expand Down Expand Up @@ -184,16 +182,14 @@ void TestQgsAttributeTable::testNoGeom()
std::unique_ptr< QgsVectorLayer> tempLayer( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=col1:double" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
QVERIFY( tempLayer->isValid() );

s.setValue( QStringLiteral( "/qgis/attributeTableBehavior" ), QgsAttributeTableFilterModel::ShowAll );
std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( tempLayer.get() ) );
std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( tempLayer.get(), QgsAttributeTableFilterModel::ShowAll ) );

QVERIFY( !dlg->mMainView->masterModel()->layerCache()->cacheGeometry() );
QVERIFY( dlg->mMainView->masterModel()->request().flags() & QgsFeatureRequest::NoGeometry );

// but if we are requesting only visible features, then geometry must be fetched...

s.setValue( QStringLiteral( "/qgis/attributeTableBehavior" ), QgsAttributeTableFilterModel::ShowVisible );
dlg.reset( new QgsAttributeTableDialog( tempLayer.get() ) );
dlg.reset( new QgsAttributeTableDialog( tempLayer.get(), QgsAttributeTableFilterModel::ShowVisible ) );
QVERIFY( dlg->mMainView->masterModel()->layerCache()->cacheGeometry() );
QVERIFY( !( dlg->mMainView->masterModel()->request().flags() & QgsFeatureRequest::NoGeometry ) );

Expand Down Expand Up @@ -222,8 +218,7 @@ void TestQgsAttributeTable::testSelected()
QgsFeature f3( tempLayer->dataProvider()->fields(), 3 );
QVERIFY( tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 << f3 ) );

s.setValue( QStringLiteral( "/qgis/attributeTableBehavior" ), QgsAttributeTableFilterModel::ShowSelected );
std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( tempLayer.get() ) );
std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( tempLayer.get(), QgsAttributeTableFilterModel::ShowSelected ) );

QVERIFY( !dlg->mMainView->masterModel()->layerCache()->cacheGeometry() );
//should be nothing - because no selection!
Expand All @@ -236,7 +231,7 @@ void TestQgsAttributeTable::testSelected()
QCOMPARE( dlg->mMainView->masterModel()->request().filterFids(), QgsFeatureIds() << 1 << 3 );

// another test - start with selection when dialog created
dlg.reset( new QgsAttributeTableDialog( tempLayer.get() ) );
dlg.reset( new QgsAttributeTableDialog( tempLayer.get(), QgsAttributeTableFilterModel::ShowSelected ) );
QVERIFY( !dlg->mMainView->masterModel()->layerCache()->cacheGeometry() );
QCOMPARE( dlg->mMainView->masterModel()->request().filterType(), QgsFeatureRequest::FilterFids );
QCOMPARE( dlg->mMainView->masterModel()->request().filterFids(), QgsFeatureIds() << 1 << 3 );
Expand Down

0 comments on commit ded892e

Please sign in to comment.