Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix saving of the collapsed state for relations
In the attribute form, the collapsed state of RelationReference
was not loaded correctly and was interfering with the collapsed
state of the relation editor.

Plus, the state was saved globally for a reference. Meaning that
if a reference was used (through other references) from other
layers, it was sharing the same state.
  • Loading branch information
pvalsecc authored and Patrick Valsecchi committed Nov 2, 2016
1 parent d3c7a04 commit 28457ed
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -86,6 +86,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
, mRubberBand( nullptr )
, mCurrentSearchWidgetWrapper( nullptr )
{
setObjectName( QStringLiteral( "QgsAttributeTableDialog/" ) + theLayer->id() );
setupUi( this );

Q_FOREACH ( const QgsField& field, mLayer->fields() )
Expand Down
1 change: 1 addition & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -195,6 +195,7 @@ void QgsRelationReferenceWidget::setRelation( const QgsRelation& relation, bool
mReferencedLayer = relation.referencedLayer();
mReferencedFieldIdx = mReferencedLayer->fields().lookupField( relation.fieldPairs().at( 0 ).second );
mReferencingFieldIdx = mReferencingLayer->fields().lookupField( relation.fieldPairs().at( 0 ).first );
mAttributeEditorFrame->setObjectName( QStringLiteral( "referencing/" ) + relation.name() );

QgsAttributeEditorContext context( mEditorContext, relation, QgsAttributeEditorContext::Single, QgsAttributeEditorContext::Embed );

Expand Down
Expand Up @@ -76,6 +76,7 @@ void QgsRelationReferenceWidgetWrapper::initWidget( QWidget* editor )
mWidget->setEmbedForm( false );
mWidget->setReadOnlySelector( false );
mWidget->setAllowMapIdentification( false );
break;
}
ctx = ctx->parentContext();
}
Expand Down
29 changes: 19 additions & 10 deletions src/gui/qgscollapsiblegroupbox.cpp
Expand Up @@ -522,6 +522,8 @@ void QgsCollapsibleGroupBox::init()
// in multiple places or used as options for different parent objects
mSaveCheckedState = false;
mSettingGroup = QLatin1String( "" ); // if not set, use window object name

connect( this, &QObject::objectNameChanged, this, &QgsCollapsibleGroupBox::loadState );
}

void QgsCollapsibleGroupBox::showEvent( QShowEvent * event )
Expand All @@ -547,6 +549,9 @@ void QgsCollapsibleGroupBox::showEvent( QShowEvent * event )

QString QgsCollapsibleGroupBox::saveKey() const
{
if ( objectName().isEmpty() || ( mSettingGroup.isEmpty() && window()->objectName().isEmpty() ) )
return QString(); // cannot get a valid key

// save key for load/save state
// currently QgsCollapsibleGroupBox/window()/object
QString saveKey = '/' + objectName();
Expand All @@ -558,9 +563,9 @@ QString QgsCollapsibleGroupBox::saveKey() const
// }
// if ( parent() )
// saveKey = "/" + parent()->objectName() + saveKey;
QString setgrp = mSettingGroup.isEmpty() ? window()->objectName() : mSettingGroup;
const QString setgrp = mSettingGroup.isEmpty() ? window()->objectName() : mSettingGroup;
saveKey = '/' + setgrp + saveKey;
saveKey = "QgsCollapsibleGroupBox" + saveKey;
saveKey = QStringLiteral( "QgsCollapsibleGroupBox" ) + saveKey;
return saveKey;
}

Expand All @@ -572,19 +577,21 @@ void QgsCollapsibleGroupBox::loadState()
if ( !isEnabled() || ( !mSaveCollapsedState && !mSaveCheckedState ) )
return;

const QString key = saveKey();
if ( key.isEmpty() )
return;

setUpdatesEnabled( false );

QString key = saveKey();
QVariant val;
if ( mSaveCheckedState )
{
val = mSettings->value( key + "/checked" );
QVariant val = mSettings->value( key + "/checked" );
if ( ! val.isNull() )
setChecked( val.toBool() );
}
if ( mSaveCollapsedState )
{
val = mSettings->value( key + "/collapsed" );
QVariant val = mSettings->value( key + "/collapsed" );
if ( ! val.isNull() )
setCollapsed( val.toBool() );
}
Expand All @@ -597,14 +604,16 @@ void QgsCollapsibleGroupBox::saveState() const
if ( !mSettings )
return;

if ( !isEnabled() || ( !mSaveCollapsedState && !mSaveCheckedState ) )
if ( !mShown || !isEnabled() || ( !mSaveCollapsedState && !mSaveCheckedState ) )
return;

QString key = saveKey();
const QString key = saveKey();
if ( key.isEmpty() )
return;

if ( mSaveCheckedState )
mSettings->setValue( key + "/checked", isChecked() );
mSettings->setValue( key + QStringLiteral( "/checked" ), isChecked() );
if ( mSaveCollapsedState )
mSettings->setValue( key + "/collapsed", isCollapsed() );
mSettings->setValue( key + QStringLiteral( "/collapsed" ), isCollapsed() );
}

6 changes: 2 additions & 4 deletions src/gui/qgsrelationeditorwidget.cpp
Expand Up @@ -170,8 +170,7 @@ void QgsRelationEditorWidget::setRelationFeature( const QgsRelation& relation, c
mToggleEditingButton->setEnabled( false );
}

setObjectName( mRelation.name() );
loadState();
setObjectName( QStringLiteral( "referenced/" ) + mRelation.name() );

// If not yet initialized, it is not (yet) visible, so we don't load it to be faster (lazy loading)
// If it is already initialized, it has been set visible before and the currently shown feature is changing
Expand Down Expand Up @@ -240,8 +239,7 @@ void QgsRelationEditorWidget::setRelations( const QgsRelation& relation, const Q
mToggleEditingButton->setEnabled( false );
}

setObjectName( mRelation.name() );
loadState();
setObjectName( QStringLiteral( "referenced/" ) + mRelation.name() );

updateUi();
}
Expand Down

0 comments on commit 28457ed

Please sign in to comment.