From 361399c0e2966de3e0133403b10b1c0b4e19cf43 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 20 Feb 2017 14:35:51 +1000 Subject: [PATCH] Small cleanup to QgsDualView - don't use cache to access layer --- src/gui/attributetable/qgsdualview.cpp | 59 +++++++++++++------------- src/gui/attributetable/qgsdualview.h | 3 +- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/gui/attributetable/qgsdualview.cpp b/src/gui/attributetable/qgsdualview.cpp index 1363d918729a..ce628c981d3f 100644 --- a/src/gui/attributetable/qgsdualview.cpp +++ b/src/gui/attributetable/qgsdualview.cpp @@ -73,6 +73,8 @@ void QgsDualView::init( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const Qg if ( !layer ) return; + mLayer = layer; + mEditorContext = context; connect( mTableView, SIGNAL( willShowContextMenu( QMenu *, QModelIndex ) ), this, SLOT( viewWillShowContextMenu( QMenu *, QModelIndex ) ) ); @@ -80,15 +82,15 @@ void QgsDualView::init( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const Qg connect( mTableView->horizontalHeader(), SIGNAL( customContextMenuRequested( QPoint ) ), this, SLOT( showViewHeaderMenu( QPoint ) ) ); connect( mTableView, SIGNAL( columnResized( int, int ) ), this, SLOT( tableColumnResized( int, int ) ) ); - initLayerCache( layer, !request.filterRect().isNull() ); + initLayerCache( !request.filterRect().isNull() ); initModels( mapCanvas, request ); - mConditionalFormatWidget->setLayer( layer ); + mConditionalFormatWidget->setLayer( mLayer ); mTableView->setModel( mFilterModel ); mFeatureList->setModel( mFeatureListModel ); delete mAttributeForm; - mAttributeForm = new QgsAttributeForm( layer, QgsFeature(), mEditorContext ); + mAttributeForm = new QgsAttributeForm( mLayer, QgsFeature(), mEditorContext ); if ( !context.parentContext() ) { mAttributeEditorScrollArea = new QScrollArea(); @@ -120,12 +122,12 @@ void QgsDualView::init( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const Qg void QgsDualView::columnBoxInit() { // load fields - QList fields = mLayerCache->layer()->fields().toList(); + QList fields = mLayer->fields().toList(); QString defaultField; // default expression: saved value - QString displayExpression = mLayerCache->layer()->displayExpression(); + QString displayExpression = mLayer->displayExpression(); if ( displayExpression.isEmpty() ) { @@ -138,13 +140,13 @@ void QgsDualView::columnBoxInit() Q_FOREACH ( const QgsField &field, fields ) { - int fieldIndex = mLayerCache->layer()->fields().lookupField( field.name() ); + int fieldIndex = mLayer->fields().lookupField( field.name() ); if ( fieldIndex == -1 ) continue; - if ( QgsEditorWidgetRegistry::instance()->findBest( mLayerCache->layer(), field.name() ).type() != QLatin1String( "Hidden" ) ) + if ( QgsEditorWidgetRegistry::instance()->findBest( mLayer, field.name() ).type() != QLatin1String( "Hidden" ) ) { - QIcon icon = mLayerCache->layer()->fields().iconForField( fieldIndex ); + QIcon icon = mLayer->fields().iconForField( fieldIndex ); QString text = field.name(); // Generate action for the preview popup button of the feature list @@ -198,14 +200,14 @@ void QgsDualView::setSelectedOnTop( bool selectedOnTop ) mFilterModel->setSelectedOnTop( selectedOnTop ); } -void QgsDualView::initLayerCache( QgsVectorLayer *layer, bool cacheGeometry ) +void QgsDualView::initLayerCache( bool cacheGeometry ) { // Initialize the cache QgsSettings settings; int cacheSize = settings.value( QStringLiteral( "/qgis/attributeTableRowCache" ), "10000" ).toInt(); - mLayerCache = new QgsVectorLayerCache( layer, cacheSize, this ); + mLayerCache = new QgsVectorLayerCache( mLayer, cacheSize, this ); mLayerCache->setCacheGeometry( cacheGeometry ); - if ( 0 == cacheSize || 0 == ( QgsVectorDataProvider::SelectAtId & mLayerCache->layer()->dataProvider()->capabilities() ) ) + if ( 0 == cacheSize || 0 == ( QgsVectorDataProvider::SelectAtId & mLayer->dataProvider()->capabilities() ) ) { connect( mLayerCache, SIGNAL( progress( int, bool & ) ), this, SLOT( progress( int, bool & ) ) ); connect( mLayerCache, SIGNAL( finished() ), this, SLOT( finished() ) ); @@ -241,13 +243,13 @@ void QgsDualView::initModels( QgsMapCanvas *mapCanvas, const QgsFeatureRequest & void QgsDualView::on_mFeatureList_aboutToChangeEditSelection( bool &ok ) { - if ( mLayerCache->layer()->isEditable() && !mAttributeForm->save() ) + if ( mLayer->isEditable() && !mAttributeForm->save() ) ok = false; } void QgsDualView::on_mFeatureList_currentEditSelectionChanged( const QgsFeature &feat ) { - if ( !mLayerCache->layer()->isEditable() || mAttributeForm->save() ) + if ( !mLayer->isEditable() || mAttributeForm->save() ) { mAttributeForm->setFeature( feat ); setCurrentEditSelection( QgsFeatureIds() << feat.id() ); @@ -299,9 +301,9 @@ void QgsDualView::toggleSearchMode( bool enabled ) void QgsDualView::previewExpressionBuilder() { // Show expression builder - QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayerCache->layer() ) ); + QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) ); - QgsExpressionBuilderDialog dlg( mLayerCache->layer(), mFeatureList->displayExpression(), this, QStringLiteral( "generic" ), context ); + QgsExpressionBuilderDialog dlg( mLayer, mFeatureList->displayExpression(), this, QStringLiteral( "generic" ), context ); dlg.setWindowTitle( tr( "Expression based preview" ) ); dlg.setExpressionText( mFeatureList->displayExpression() ); @@ -387,7 +389,7 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atInd } //add user-defined actions to context menu - QList actions = mLayerCache->layer()->actions()->actions( QStringLiteral( "Field" ) ); + QList actions = mLayer->actions()->actions( QStringLiteral( "Field" ) ); if ( !actions.isEmpty() ) { QAction *a = menu->addAction( tr( "Run layer action" ) ); @@ -404,7 +406,7 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atInd } //add actions from QgsMapLayerActionRegistry to context menu - QList registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayerCache->layer() ); + QList registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer ); if ( !registeredActions.isEmpty() ) { //add a separator between user defined and standard actions @@ -456,12 +458,12 @@ void QgsDualView::showViewHeaderMenu( QPoint point ) void QgsDualView::organizeColumns() { - if ( !mLayerCache->layer() ) + if ( !mLayer ) { return; } - QgsOrganizeTableColumnsDialog dialog( mLayerCache->layer(), this ); + QgsOrganizeTableColumnsDialog dialog( mLayer, this ); if ( dialog.exec() == QDialog::Accepted ) { QgsAttributeTableConfig config = dialog.config(); @@ -525,8 +527,7 @@ void QgsDualView::autosizeColumn() void QgsDualView::modifySort() { - QgsVectorLayer *layer = mLayerCache->layer(); - if ( !layer ) + if ( !mLayer ) return; QgsAttributeTableConfig config = mConfig; @@ -547,12 +548,12 @@ void QgsDualView::modifySort() sortingGroupBox->setLayout( new QGridLayout() ); QgsExpressionBuilderWidget *expressionBuilder = new QgsExpressionBuilderWidget(); - QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) ); + QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) ); expressionBuilder->setExpressionContext( context ); - expressionBuilder->setLayer( layer ); + expressionBuilder->setLayer( mLayer ); expressionBuilder->loadFieldNames(); expressionBuilder->loadRecent( QStringLiteral( "generic" ) ); - expressionBuilder->setExpressionText( sortExpression().isEmpty() ? layer->displayExpression() : sortExpression() ); + expressionBuilder->setExpressionText( sortExpression().isEmpty() ? mLayer->displayExpression() : sortExpression() ); sortingGroupBox->layout()->addWidget( expressionBuilder ); @@ -593,7 +594,7 @@ void QgsDualView::zoomToCurrentFeature() QgsMapCanvas *canvas = mFilterModel->mapCanvas(); if ( canvas ) { - canvas->zoomToFeatureIds( mLayerCache->layer(), ids ); + canvas->zoomToFeatureIds( mLayer, ids ); } } @@ -610,18 +611,18 @@ void QgsDualView::panToCurrentFeature() QgsMapCanvas *canvas = mFilterModel->mapCanvas(); if ( canvas ) { - canvas->panToFeatureIds( mLayerCache->layer(), ids ); + canvas->panToFeatureIds( mLayer, ids ); } } void QgsDualView::previewExpressionChanged( const QString &expression ) { - mLayerCache->layer()->setDisplayExpression( expression ); + mLayer->setDisplayExpression( expression ); } void QgsDualView::onSortColumnChanged() { - QgsAttributeTableConfig cfg = mLayerCache->layer()->attributeTableConfig(); + QgsAttributeTableConfig cfg = mLayer->attributeTableConfig(); cfg.setSortExpression( mFilterModel->sortExpression() ); cfg.setSortOrder( mFilterModel->sortOrder() ); setAttributeTableConfig( cfg ); @@ -665,7 +666,7 @@ void QgsDualView::setFeatureSelectionManager( QgsIFeatureSelectionManager *featu void QgsDualView::setAttributeTableConfig( const QgsAttributeTableConfig &config ) { - mLayerCache->layer()->setAttributeTableConfig( config ); + mLayer->setAttributeTableConfig( config ); mFilterModel->setAttributeTableConfig( config ); mTableView->setAttributeTableConfig( config ); mConfig = config; diff --git a/src/gui/attributetable/qgsdualview.h b/src/gui/attributetable/qgsdualview.h index 4bc4f69c4489..df42bafd0c74 100644 --- a/src/gui/attributetable/qgsdualview.h +++ b/src/gui/attributetable/qgsdualview.h @@ -324,7 +324,7 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas void panToCurrentFeature(); private: - void initLayerCache( QgsVectorLayer *layer, bool cacheGeometry ); + void initLayerCache( bool cacheGeometry ); void initModels( QgsMapCanvas *mapCanvas, const QgsFeatureRequest &request ); QgsAttributeEditorContext mEditorContext; @@ -336,6 +336,7 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas QMenu *mPreviewColumnsMenu = nullptr; QMenu *mHorizontalHeaderMenu = nullptr; QgsVectorLayerCache *mLayerCache = nullptr; + QgsVectorLayer *mLayer = nullptr; QProgressDialog *mProgressDlg = nullptr; QgsIFeatureSelectionManager *mFeatureSelectionManager = nullptr; QgsDistanceArea mDistanceArea;