Skip to content
Permalink
Browse files

Merge pull request #6716 from signedav/fix_embeddedrelation

Use always id as identification on drag&drop form creator for relations
  • Loading branch information
m-kuhn committed Apr 9, 2018
2 parents 7fa9d41 + fe08347 commit e2bf9b9db15e3c8624ef912c31d9b791b1bfa988
@@ -268,24 +268,35 @@ This element will load a relation editor onto the form.
%End
public:

QgsAttributeEditorRelation( const QString &name, const QString &relationId, QgsAttributeEditorElement *parent );
QgsAttributeEditorRelation( const QString &name, const QString &relationId, QgsAttributeEditorElement *parent );
%Docstring

.. deprecated:: since QGIS 3.0.2. The name parameter is not used for anything and overwritten by the relationId internally.
%End

QgsAttributeEditorRelation( const QString &name, const QgsRelation &relation, QgsAttributeEditorElement *parent );
%Docstring

.. deprecated:: since QGIS 3.0.2. The name parameter is not used for anything and overwritten by the relationId internally.
%End

QgsAttributeEditorRelation( const QString &relationId, QgsAttributeEditorElement *parent );
%Docstring
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)
%End

QgsAttributeEditorRelation( const QString &name, const QgsRelation &relation, QgsAttributeEditorElement *parent );
QgsAttributeEditorRelation( const QgsRelation &relation, QgsAttributeEditorElement *parent );
%Docstring
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)
%End


const QgsRelation &relation() const;
%Docstring
Get the id of the relation which shall be embedded
@@ -88,14 +88,14 @@ void QgsAttributesFormProperties::initAvailableWidgetsTree()

//load Fields

DnDTreeItemData catItemData = DnDTreeItemData( DnDTreeItemData::Container, "Fields" );
DnDTreeItemData catItemData = DnDTreeItemData( DnDTreeItemData::Container, "Fields", "Fields" );
QTreeWidgetItem *catitem = mAvailableWidgetsTree->addItem( mAvailableWidgetsTree->invisibleRootItem(), catItemData );

const QgsFields fields = mLayer->fields();
for ( int i = 0; i < fields.size(); ++i )
{
const QgsField field = fields.at( i );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Field, field.name() );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Field, field.name(), field.name() );
itemData.setShowLabel( true );

FieldConfig cfg( mLayer, i );
@@ -108,14 +108,14 @@ void QgsAttributesFormProperties::initAvailableWidgetsTree()
catitem->setExpanded( true );

//load Relations
catItemData = DnDTreeItemData( DnDTreeItemData::Container, "Relations" );
catItemData = DnDTreeItemData( DnDTreeItemData::Container, "Relations", "Relations" );
catitem = mAvailableWidgetsTree->addItem( mAvailableWidgetsTree->invisibleRootItem(), catItemData );

const QList<QgsRelation> relations = QgsProject::instance()->relationManager()->referencedRelations( mLayer );

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() ), QStringLiteral( "%1" ).arg( relation.name() ) );
itemData.setShowLabel( true );

RelationConfig cfg( mLayer, relation.id() );
@@ -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;
}
@@ -398,7 +399,7 @@ QTreeWidgetItem *QgsAttributesFormProperties::loadAttributeEditorTreeItem( QgsAt
{
case QgsAttributeEditorElement::AeTypeField:
{
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Field, widgetDef->name() );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::Field, widgetDef->name(), widgetDef->name() );
itemData.setShowLabel( widgetDef->showLabel() );
newWidget = tree->addItem( parent, itemData );
break;
@@ -407,20 +408,19 @@ 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(), relationEditor->relation().name() );
itemData.setShowLabel( widgetDef->showLabel() );
RelationEditorConfiguration relEdConfig;
relEdConfig.showLinkButton = relationEditor->showLinkButton();
relEdConfig.showUnlinkButton = relationEditor->showUnlinkButton();
itemData.setRelationEditorConfiguration( relEdConfig );

newWidget = tree->addItem( parent, itemData );
break;
}

case QgsAttributeEditorElement::AeTypeContainer:
{
DnDTreeItemData itemData( DnDTreeItemData::Container, widgetDef->name() );
DnDTreeItemData itemData( DnDTreeItemData::Container, widgetDef->name(), widgetDef->name() );
itemData.setShowLabel( widgetDef->showLabel() );

const QgsAttributeEditorContainer *container = static_cast<const QgsAttributeEditorContainer *>( widgetDef );
@@ -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;
@@ -771,7 +771,7 @@ QTreeWidgetItem *DnDTree::addContainer( QTreeWidgetItem *parent, const QString &
QTreeWidgetItem *newItem = new QTreeWidgetItem( QStringList() << title );
newItem->setBackground( 0, QBrush( Qt::lightGray ) );
newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled );
QgsAttributesFormProperties::DnDTreeItemData itemData( QgsAttributesFormProperties::DnDTreeItemData::Container, title );
QgsAttributesFormProperties::DnDTreeItemData itemData( QgsAttributesFormProperties::DnDTreeItemData::Container, title, title );
itemData.setColumnCount( columnCount );
newItem->setData( 0, QgsAttributesFormProperties::DnDTreeRole, itemData );
parent->addChild( newItem );
@@ -811,6 +811,8 @@ QTreeWidgetItem *DnDTree::addItem( QTreeWidgetItem *parent, QgsAttributesFormPro
}
}
newItem->setData( 0, QgsAttributesFormProperties::DnDTreeRole, data );
newItem->setText( 0, data.displayName() );

if ( index < 0 )
parent->addChild( newItem );
else
@@ -1081,19 +1083,21 @@ void DnDTree::setType( const Type &value )

QDataStream &operator<<( QDataStream &stream, const QgsAttributesFormProperties::DnDTreeItemData &data )
{
stream << ( quint32 )data.type() << data.name();
stream << ( quint32 )data.type() << data.name() << data.displayName();
return stream;
}

QDataStream &operator>>( QDataStream &stream, QgsAttributesFormProperties::DnDTreeItemData &data )
{
QString name;
QString displayName;
quint32 type;

stream >> type >> name;
stream >> type >> name >> displayName;

data.setType( ( QgsAttributesFormProperties::DnDTreeItemData::Type )type );
data.setName( name );
data.setDisplayName( displayName );

return stream;
}
@@ -83,9 +83,10 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
//do we need that
DnDTreeItemData() = default;

DnDTreeItemData( Type type, const QString &name )
DnDTreeItemData( Type type, const QString &name, const QString &displayName )
: mType( type )
, mName( name )
, mDisplayName( displayName )
, mColumnCount( 1 )
, mShowAsGroupBox( false )
, mShowLabel( true )
@@ -94,6 +95,9 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
QString name() const { return mName; }
void setName( const QString &name ) { mName = name; }

QString displayName() const { return mDisplayName; }
void setDisplayName( const QString &displayName ) { mDisplayName = displayName; }

Type type() const { return mType; }
void setType( Type type ) { mType = type; }

@@ -117,6 +121,8 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
private:
Type mType = Field;
QString mName;
QString mDisplayName;

int mColumnCount = 1;
bool mShowAsGroupBox = false;
bool mShowLabel = true;
@@ -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;
@@ -316,35 +316,47 @@ 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 )
{}

/**
* \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 )
{}

/**
* 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 )
{}

/**
* 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 )
, mShowUnlinkButton( true )
{}


/**
* Get the id of the relation which shall be embedded
*
@@ -396,8 +408,8 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement
QString typeIdentifier() const override;
QString mRelationId;
QgsRelation mRelation;
bool mShowLinkButton;
bool mShowUnlinkButton;
bool mShowLinkButton = true;
bool mShowUnlinkButton = true;
};


@@ -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;

0 comments on commit e2bf9b9

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