QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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#include "moc_qgsattributeeditorrelation.cpp"
18#include "qgsrelationmanager.h"
19#include "qgsxmlutils.h"
20
22{
23 mRelation = relationManager->relation( mRelationId );
24 return mRelation.isValid();
25}
26
28{
30 element->mRelation = mRelation;
31 element->mButtons = mButtons;
32 element->mForceSuppressFormPopup = mForceSuppressFormPopup;
33 element->mNmRelationId = mNmRelationId;
34 element->mLabel = mLabel;
35 element->mRelationEditorConfig = mRelationEditorConfig;
36 element->mRelationWidgetTypeId = mRelationWidgetTypeId;
37 element->mLabelStyle = mLabelStyle;
38
39 return element;
40}
41
42void QgsAttributeEditorRelation::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
43{
44 elem.setAttribute( QStringLiteral( "relation" ), mRelationId );
45 elem.setAttribute( QStringLiteral( "forceSuppressFormPopup" ), mForceSuppressFormPopup );
46 elem.setAttribute( QStringLiteral( "nmRelationId" ), mNmRelationId.toString() );
47 elem.setAttribute( QStringLiteral( "label" ), mLabel );
48 elem.setAttribute( QStringLiteral( "relationWidgetTypeId" ), mRelationWidgetTypeId );
49
50 QDomElement elemConfig = QgsXmlUtils::writeVariant( mRelationEditorConfig, doc );
51 elemConfig.setTagName( QStringLiteral( "editor_configuration" ) );
52 elem.appendChild( elemConfig );
53}
54
55void QgsAttributeEditorRelation::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
56{
57 Q_UNUSED( layerId )
58 Q_UNUSED( context )
59 Q_UNUSED( fields )
60
61 QVariantMap config = QgsXmlUtils::readVariant( element.firstChildElement( "editor_configuration" ) ).toMap();
62
63 // load defaults
64 if ( config.isEmpty() )
66
67 // pre QGIS 3.18 compatibility
68 if ( ! config.contains( QStringLiteral( "buttons" ) ) )
69 {
70 if ( element.hasAttribute( "buttons" ) )
71 {
73 // QgsAttributeEditorRelation::Button has been deprecated in favor of QgsRelationEditorWidget::Button
74 // we cannot use it here since the new flags are in gui, while the current code is in core
75 // TODO: remove this compatibility code in QGIS 4
76 // or make the enum private if we really want to keep the backward compatibility (but not worth it!)
77 const QString buttonString = element.attribute( QStringLiteral( "buttons" ), qgsFlagValueToKeys( QgsAttributeEditorRelation::Button::AllButtons ) );
78 config.insert( "buttons", qgsFlagValueToKeys( qgsFlagKeysToValue( buttonString, QgsAttributeEditorRelation::Button::AllButtons ) ) );
80 }
81 else
82 {
83 // pre QGIS 3.16 compatibility
86 buttons.setFlag( QgsAttributeEditorRelation::Button::Link, element.attribute( QStringLiteral( "showLinkButton" ), QStringLiteral( "1" ) ).toInt() );
87 buttons.setFlag( QgsAttributeEditorRelation::Button::Unlink, element.attribute( QStringLiteral( "showUnlinkButton" ), QStringLiteral( "1" ) ).toInt() );
88 buttons.setFlag( QgsAttributeEditorRelation::Button::SaveChildEdits, element.attribute( QStringLiteral( "showSaveChildEditsButton" ), QStringLiteral( "1" ) ).toInt() );
90 config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
91 }
92 }
93
95
96 setForceSuppressFormPopup( element.attribute( QStringLiteral( "forceSuppressFormPopup" ), 0 ).toInt() );
97
98 if ( element.hasAttribute( QStringLiteral( "nmRelationId" ) ) )
99 {
100 setNmRelationId( element.attribute( QStringLiteral( "nmRelationId" ) ) );
101 }
102
103 if ( element.hasAttribute( "label" ) )
104 {
105 const QString label = element.attribute( QStringLiteral( "label" ) );
106 setLabel( label );
107 }
108 if ( element.hasAttribute( "relationWidgetTypeId" ) )
109 {
110 const QString relationWidgetTypeId = element.attribute( QStringLiteral( "relationWidgetTypeId" ) );
112 }
113}
114
115QString QgsAttributeEditorRelation::typeIdentifier() const
116{
117 return QStringLiteral( "attributeEditorRelation" );
118}
119
121{
122 mForceSuppressFormPopup = forceSuppressFormPopup;
123}
124
126{
127 return mForceSuppressFormPopup;
128}
129
130void QgsAttributeEditorRelation::setNmRelationId( const QVariant &nmRelationId )
131{
132 mNmRelationId = nmRelationId;
133}
134
136{
137 return mNmRelationId;
138}
139
140void QgsAttributeEditorRelation::setLabel( const QString &label )
141{
142 mLabel = label;
143}
144
146{
147 return mLabel;
148}
149
151{
152 return mRelationWidgetTypeId;
153}
154
155void QgsAttributeEditorRelation::setRelationWidgetTypeId( const QString &relationWidgetTypeId )
156{
157 mRelationWidgetTypeId = relationWidgetTypeId;
158}
159
161{
162 return mRelationEditorConfig;
163}
164
166{
167 mRelationEditorConfig = config;
168}
This is an abstract base class for any elements of a drag and drop form.
QgsAttributeEditorElement * parent() const
Gets the parent of this element.
This element will load a relation editor onto the form.
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.
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
The class is used as a container of context for various read/write operations on other objects.
This class 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:6494
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:6166
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:6188
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493