Skip to content
Permalink
Browse files

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 28457ed49c764a21559e9269db7d9ab9b6a8060b
@@ -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() )
@@ -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 );

@@ -76,6 +76,7 @@ void QgsRelationReferenceWidgetWrapper::initWidget( QWidget* editor )
mWidget->setEmbedForm( false );
mWidget->setReadOnlySelector( false );
mWidget->setAllowMapIdentification( false );
break;
}
ctx = ctx->parentContext();
}
@@ -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 )
@@ -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();
@@ -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;
}

@@ -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() );
}
@@ -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() );
}

@@ -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
@@ -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();
}

0 comments on commit 28457ed

Please sign in to comment.
You can’t perform that action at this time.