Skip to content

Commit

Permalink
fix relation id confusion
Browse files Browse the repository at this point in the history
if inserting a relation the id is used as name because it's used as identificator
  • Loading branch information
signedav committed Mar 29, 2018
1 parent 14c057a commit 901327c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
13 changes: 7 additions & 6 deletions src/app/qgsattributesformproperties.cpp
Expand Up @@ -115,7 +115,7 @@ void QgsAttributesFormProperties::initAvailableWidgetsTree()

for ( const QgsRelation &relation : relations )
{
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Relation, QStringLiteral( "%1" ).arg( relation.id() ) ); //relation.name() );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Relation, QStringLiteral( "%1" ).arg( relation.id() ) );
itemData.setShowLabel( true );

RelationConfig cfg( mLayer, relation.id() );
Expand Down Expand Up @@ -374,13 +374,14 @@ void QgsAttributesFormProperties::storeAttributeRelationEdit()
}
}

QgsAttributesFormProperties::RelationConfig QgsAttributesFormProperties::configForRelation( const QString &relationName )
QgsAttributesFormProperties::RelationConfig QgsAttributesFormProperties::configForRelation( const QString &relationId )
{
QTreeWidgetItemIterator itemIt( mAvailableWidgetsTree );
while ( *itemIt )
{
QTreeWidgetItem *item = *itemIt;
if ( item->data( 0, FieldNameRole ).toString() == relationName )

if ( item->data( 0, FieldNameRole ).toString() == relationId )
return item->data( 0, RelationConfigRole ).value<RelationConfig>();
++itemIt;
}
Expand All @@ -407,13 +408,12 @@ QTreeWidgetItem *QgsAttributesFormProperties::loadAttributeEditorTreeItem( QgsAt
case QgsAttributeEditorElement::AeTypeRelation:
{
const QgsAttributeEditorRelation *relationEditor = static_cast<const QgsAttributeEditorRelation *>( widgetDef );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Relation, widgetDef->name() );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Relation, relationEditor->relation().id());
itemData.setShowLabel( widgetDef->showLabel() );
RelationEditorConfiguration relEdConfig;
relEdConfig.showLinkButton = relationEditor->showLinkButton();
relEdConfig.showUnlinkButton = relationEditor->showUnlinkButton();
itemData.setRelationEditorConfiguration( relEdConfig );

newWidget = tree->addItem( parent, itemData );
break;
}
Expand Down Expand Up @@ -531,7 +531,7 @@ QgsAttributeEditorElement *QgsAttributesFormProperties::createAttributeEditorWid
case DnDTreeItemData::Relation:
{
QgsRelation relation = QgsProject::instance()->relationManager()->relation( itemData.name() );
QgsAttributeEditorRelation *relDef = new QgsAttributeEditorRelation( itemData.name(), relation, parent );
QgsAttributeEditorRelation *relDef = new QgsAttributeEditorRelation( relation, parent );
relDef->setShowLinkButton( itemData.relationEditorConfiguration().showLinkButton );
relDef->setShowUnlinkButton( itemData.relationEditorConfiguration().showUnlinkButton );
widgetDef = relDef;
Expand Down Expand Up @@ -811,6 +811,7 @@ QTreeWidgetItem *DnDTree::addItem( QTreeWidgetItem *parent, QgsAttributesFormPro
}
}
newItem->setData( 0, QgsAttributesFormProperties::DnDTreeRole, data );

if ( index < 0 )
parent->addChild( newItem );
else
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsattributeeditorelement.cpp
Expand Up @@ -82,7 +82,7 @@ bool QgsAttributeEditorRelation::init( QgsRelationManager *relationManager )

QgsAttributeEditorElement *QgsAttributeEditorRelation::clone( QgsAttributeEditorElement *parent ) const
{
QgsAttributeEditorRelation *element = new QgsAttributeEditorRelation( name(), mRelationId, parent );
QgsAttributeEditorRelation *element = new QgsAttributeEditorRelation( mRelationId, parent );
element->mRelation = mRelation;
element->mShowLinkButton = mShowLinkButton;
element->mShowUnlinkButton = mShowUnlinkButton;
Expand Down
31 changes: 25 additions & 6 deletions src/core/qgsattributeeditorelement.h
Expand Up @@ -316,15 +316,35 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement
{
public:

/**
* \deprecated since QGIS 3.0.2. The name parameter is not used for anything and overwritten by the relationId internally.
*/
Q_DECL_DEPRECATED QgsAttributeEditorRelation(const QString &name, const QString &relationId, QgsAttributeEditorElement *parent)
: QgsAttributeEditorElement( AeTypeRelation, name, parent )
, mRelationId( relationId )
, mShowLinkButton( true )
, mShowUnlinkButton( true )
{}

/**
* \deprecated since QGIS 3.0.2. The name parameter is not used for anything and overwritten by the relationId internally.
*/
Q_DECL_DEPRECATED QgsAttributeEditorRelation(const QString &name, const QgsRelation &relation, QgsAttributeEditorElement *parent)
: QgsAttributeEditorElement( AeTypeRelation, name, parent )
, mRelationId( relation.id() )
, mRelation( relation )
, mShowLinkButton( true )
, mShowUnlinkButton( true )
{}

/**
* Creates a new element which embeds a relation.
*
* \param name The name of this element
* \param relationId The id of the relation to embed
* \param parent The parent (used as container)
*/
QgsAttributeEditorRelation( const QString &name, const QString &relationId, QgsAttributeEditorElement *parent )
: QgsAttributeEditorElement( AeTypeRelation, name, parent )
QgsAttributeEditorRelation( const QString &relationId, QgsAttributeEditorElement *parent)
: QgsAttributeEditorElement( AeTypeRelation, relationId, parent )
, mRelationId( relationId )
, mShowLinkButton( true )
, mShowUnlinkButton( true )
Expand All @@ -333,12 +353,11 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement
/**
* Creates a new element which embeds a relation.
*
* \param name The name of this element
* \param relation The relation to embed
* \param parent The parent (used as container)
*/
QgsAttributeEditorRelation( const QString &name, const QgsRelation &relation, QgsAttributeEditorElement *parent )
: QgsAttributeEditorElement( AeTypeRelation, name, parent )
QgsAttributeEditorRelation( const QgsRelation &relation, QgsAttributeEditorElement *parent)
: QgsAttributeEditorElement( AeTypeRelation, relation.id(), parent )
, mRelationId( relation.id() )
, mRelation( relation )
, mShowLinkButton( true )
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -550,8 +550,7 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
{
// At this time, the relations are not loaded
// So we only grab the id and delegate the rest to onRelationsLoaded()
QString name = elem.attribute( QStringLiteral( "name" ) );
QgsAttributeEditorRelation *relElement = new QgsAttributeEditorRelation( name, elem.attribute( QStringLiteral( "relation" ), QStringLiteral( "[None]" ) ), parent );
QgsAttributeEditorRelation *relElement = new QgsAttributeEditorRelation( elem.attribute( QStringLiteral( "relation" ), QStringLiteral( "[None]" ) ), parent );
relElement->setShowLinkButton( elem.attribute( QStringLiteral( "showLinkButton" ), QStringLiteral( "1" ) ).toInt() );
relElement->setShowUnlinkButton( elem.attribute( QStringLiteral( "showUnlinkButton" ), QStringLiteral( "1" ) ).toInt() );
newElement = relElement;
Expand Down

0 comments on commit 901327c

Please sign in to comment.