QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsattributeeditorrelation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributeeditorrelation.cpp - QgsAttributeEditorRelation
3
4 ---------------------
5 begin : 12.01.2021
6 copyright : (C) 2021 by Denis Rouzaud
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17
18#include "qgsrelationmanager.h"
19#include "qgsxmlutils.h"
20
21#include "moc_qgsattributeeditorrelation.cpp"
22
24{
25 mRelation = relationManager->relation( mRelationId );
26 return mRelation.isValid();
27}
28
30{
32 element->mRelation = mRelation;
33 element->mButtons = mButtons;
34 element->mForceSuppressFormPopup = mForceSuppressFormPopup;
35 element->mNmRelationId = mNmRelationId;
36 element->mLabel = mLabel;
37 element->mRelationEditorConfig = mRelationEditorConfig;
38 element->mRelationWidgetTypeId = mRelationWidgetTypeId;
39 element->mLabelStyle = mLabelStyle;
40
41 return element;
42}
43
44void QgsAttributeEditorRelation::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
45{
46 elem.setAttribute( QStringLiteral( "relation" ), mRelationId );
47 elem.setAttribute( QStringLiteral( "forceSuppressFormPopup" ), mForceSuppressFormPopup );
48 elem.setAttribute( QStringLiteral( "nmRelationId" ), mNmRelationId.toString() );
49 elem.setAttribute( QStringLiteral( "label" ), mLabel );
50 elem.setAttribute( QStringLiteral( "relationWidgetTypeId" ), mRelationWidgetTypeId );
51
52 QDomElement elemConfig = QgsXmlUtils::writeVariant( mRelationEditorConfig, doc );
53 elemConfig.setTagName( QStringLiteral( "editor_configuration" ) );
54 elem.appendChild( elemConfig );
55}
56
57void QgsAttributeEditorRelation::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
58{
59 Q_UNUSED( layerId )
60 Q_UNUSED( context )
61 Q_UNUSED( fields )
62
63 QVariantMap config = QgsXmlUtils::readVariant( element.firstChildElement( "editor_configuration" ) ).toMap();
64
65 // load defaults
66 if ( config.isEmpty() )
68
69 // pre QGIS 3.18 compatibility
70 if ( ! config.contains( QStringLiteral( "buttons" ) ) )
71 {
72 if ( element.hasAttribute( "buttons" ) )
73 {
75 // QgsAttributeEditorRelation::Button has been deprecated in favor of QgsRelationEditorWidget::Button
76 // we cannot use it here since the new flags are in gui, while the current code is in core
77 // TODO: remove this compatibility code in QGIS 4
78 // or make the enum private if we really want to keep the backward compatibility (but not worth it!)
79 const QString buttonString = element.attribute( QStringLiteral( "buttons" ), qgsFlagValueToKeys( QgsAttributeEditorRelation::Button::AllButtons ) );
80 config.insert( "buttons", qgsFlagValueToKeys( qgsFlagKeysToValue( buttonString, QgsAttributeEditorRelation::Button::AllButtons ) ) );
82 }
83 else
84 {
85 // pre QGIS 3.16 compatibility
88 buttons.setFlag( QgsAttributeEditorRelation::Button::Link, element.attribute( QStringLiteral( "showLinkButton" ), QStringLiteral( "1" ) ).toInt() );
89 buttons.setFlag( QgsAttributeEditorRelation::Button::Unlink, element.attribute( QStringLiteral( "showUnlinkButton" ), QStringLiteral( "1" ) ).toInt() );
90 buttons.setFlag( QgsAttributeEditorRelation::Button::SaveChildEdits, element.attribute( QStringLiteral( "showSaveChildEditsButton" ), QStringLiteral( "1" ) ).toInt() );
92 config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
93 }
94 }
95
97
98 setForceSuppressFormPopup( element.attribute( QStringLiteral( "forceSuppressFormPopup" ), 0 ).toInt() );
99
100 if ( element.hasAttribute( QStringLiteral( "nmRelationId" ) ) )
101 {
102 setNmRelationId( element.attribute( QStringLiteral( "nmRelationId" ) ) );
103 }
104
105 if ( element.hasAttribute( "label" ) )
106 {
107 const QString label = element.attribute( QStringLiteral( "label" ) );
108 setLabel( label );
109 }
110 if ( element.hasAttribute( "relationWidgetTypeId" ) )
111 {
112 const QString relationWidgetTypeId = element.attribute( QStringLiteral( "relationWidgetTypeId" ) );
114 }
115}
116
117QString QgsAttributeEditorRelation::typeIdentifier() const
118{
119 return QStringLiteral( "attributeEditorRelation" );
120}
121
126
128{
129 return mForceSuppressFormPopup;
130}
131
133{
134 mNmRelationId = nmRelationId;
135}
136
138{
139 return mNmRelationId;
140}
141
143{
144 mLabel = label;
145}
146
148{
149 return mLabel;
150}
151
153{
154 return mRelationWidgetTypeId;
155}
156
158{
159 mRelationWidgetTypeId = relationWidgetTypeId;
160}
161
163{
164 return mRelationEditorConfig;
165}
166
168{
169 mRelationEditorConfig = config;
170}
QgsAttributeEditorElement * parent() const
Gets the parent of this element.
QgsAttributeEditorElement(Qgis::AttributeEditorType type, const QString &name, QgsAttributeEditorElement *parent=nullptr)
Constructor.
void setNmRelationId(const QVariant &nmRelationId=QVariant())
Sets nmRelationId for the relation id of the second relation involved in an N:M relation.
bool init(QgsRelationManager *relManager)
Initializes the relation from the id.
void setRelationWidgetTypeId(const QString &relationWidgetTypeId)
Sets the relation widget type.
QgsAttributeEditorElement * clone(QgsAttributeEditorElement *parent) const override
Returns a clone of this element.
QVariantMap relationEditorConfiguration() const
Returns the relation editor widget configuration.
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status to forceSuppressFormPopup.
@ SaveChildEdits
Save child edits button.
QVariant nmRelationId() const
Determines the relation id of the second relation involved in an N:M relation.
bool forceSuppressFormPopup() const
Determines the force suppress form popup status.
Q_DECL_DEPRECATED QgsAttributeEditorRelation(const QString &name, const QString &relationId, QgsAttributeEditorElement *parent)
QString relationWidgetTypeId() const
Returns the current relation widget type id.
void setRelationEditorConfiguration(const QVariantMap &config)
Sets the relation editor configuration.
void setLabel(const QString &label=QString())
Sets label for this element If it's empty it takes the relation id as label.
QString label() const
Determines the label of this element.
Container of fields for a vector layer.
Definition qgsfields.h:46
A container for the context for various read/write operations on objects.
Manages a set of relations between layers.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7170
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:6856
T qgsFlagKeysToValue(const QString &keys, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given keys of a flag.
Definition qgis.h:6878
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169