Skip to content

Commit

Permalink
Small cleanup to QgsDualView - don't use cache to access layer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 5, 2017
1 parent 11c4444 commit 361399c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
59 changes: 30 additions & 29 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -73,22 +73,24 @@ 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 ) ) );
mTableView->horizontalHeader()->setContextMenuPolicy( Qt::CustomContextMenu );
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();
Expand Down Expand Up @@ -120,12 +122,12 @@ void QgsDualView::init( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const Qg
void QgsDualView::columnBoxInit()
{
// load fields
QList<QgsField> fields = mLayerCache->layer()->fields().toList();
QList<QgsField> fields = mLayer->fields().toList();

QString defaultField;

// default expression: saved value
QString displayExpression = mLayerCache->layer()->displayExpression();
QString displayExpression = mLayer->displayExpression();

if ( displayExpression.isEmpty() )
{
Expand All @@ -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
Expand Down Expand Up @@ -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() ) );
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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() );

Expand Down Expand Up @@ -387,7 +389,7 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atInd
}

//add user-defined actions to context menu
QList<QgsAction> actions = mLayerCache->layer()->actions()->actions( QStringLiteral( "Field" ) );
QList<QgsAction> actions = mLayer->actions()->actions( QStringLiteral( "Field" ) );
if ( !actions.isEmpty() )
{
QAction *a = menu->addAction( tr( "Run layer action" ) );
Expand All @@ -404,7 +406,7 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atInd
}

//add actions from QgsMapLayerActionRegistry to context menu
QList<QgsMapLayerAction *> registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayerCache->layer() );
QList<QgsMapLayerAction *> registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer );
if ( !registeredActions.isEmpty() )
{
//add a separator between user defined and standard actions
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -525,8 +527,7 @@ void QgsDualView::autosizeColumn()

void QgsDualView::modifySort()
{
QgsVectorLayer *layer = mLayerCache->layer();
if ( !layer )
if ( !mLayer )
return;

QgsAttributeTableConfig config = mConfig;
Expand All @@ -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 );

Expand Down Expand Up @@ -593,7 +594,7 @@ void QgsDualView::zoomToCurrentFeature()
QgsMapCanvas *canvas = mFilterModel->mapCanvas();
if ( canvas )
{
canvas->zoomToFeatureIds( mLayerCache->layer(), ids );
canvas->zoomToFeatureIds( mLayer, ids );
}
}

Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/gui/attributetable/qgsdualview.h
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 361399c

Please sign in to comment.