QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
22
23#include "moc_qgsattributeeditorrelation.cpp"
24
25using namespace Qt::StringLiterals;
26
28{
29 mRelation = relationManager->relation( mRelationId );
30 return mRelation.isValid();
31}
32
34{
36 element->mRelation = mRelation;
37 element->mButtons = mButtons;
38 element->mForceSuppressFormPopup = mForceSuppressFormPopup;
39 element->mNmRelationId = mNmRelationId;
40 element->mLabel = mLabel;
41 element->mRelationEditorConfig = mRelationEditorConfig;
42 element->mRelationWidgetTypeId = mRelationWidgetTypeId;
43 element->mLabelStyle = mLabelStyle;
44
45 return element;
46}
47
48void QgsAttributeEditorRelation::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
49{
50 elem.setAttribute( u"relation"_s, mRelationId );
51 elem.setAttribute( u"forceSuppressFormPopup"_s, mForceSuppressFormPopup );
52 elem.setAttribute( u"nmRelationId"_s, mNmRelationId.toString() );
53 elem.setAttribute( u"label"_s, mLabel );
54 elem.setAttribute( u"relationWidgetTypeId"_s, mRelationWidgetTypeId );
55
56 QDomElement elemConfig = QgsXmlUtils::writeVariant( mRelationEditorConfig, doc );
57 elemConfig.setTagName( u"editor_configuration"_s );
58 elem.appendChild( elemConfig );
59}
60
61void QgsAttributeEditorRelation::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
62{
63 Q_UNUSED( layerId )
64 Q_UNUSED( context )
65 Q_UNUSED( fields )
66
67 QVariantMap config = QgsXmlUtils::readVariant( element.firstChildElement( "editor_configuration" ) ).toMap();
68
69 // load defaults
70 if ( config.isEmpty() )
72
73 // pre QGIS 3.18 compatibility
74 if ( ! config.contains( u"buttons"_s ) )
75 {
76 if ( element.hasAttribute( "buttons" ) )
77 {
79 // QgsAttributeEditorRelation::Button has been deprecated in favor of QgsRelationEditorWidget::Button
80 // we cannot use it here since the new flags are in gui, while the current code is in core
81 // TODO: remove this compatibility code in QGIS 5
82 // or make the enum private if we really want to keep the backward compatibility (but not worth it!)
83 const QString buttonString = element.attribute( u"buttons"_s, qgsFlagValueToKeys( QgsAttributeEditorRelation::Button::AllButtons ) );
84 config.insert( "buttons", qgsFlagValueToKeys( qgsFlagKeysToValue( buttonString, QgsAttributeEditorRelation::Button::AllButtons ) ) );
86 }
87 else
88 {
89 // pre QGIS 3.16 compatibility
92 buttons.setFlag( QgsAttributeEditorRelation::Button::Link, element.attribute( u"showLinkButton"_s, u"1"_s ).toInt() );
93 buttons.setFlag( QgsAttributeEditorRelation::Button::Unlink, element.attribute( u"showUnlinkButton"_s, u"1"_s ).toInt() );
94 buttons.setFlag( QgsAttributeEditorRelation::Button::SaveChildEdits, element.attribute( u"showSaveChildEditsButton"_s, u"1"_s ).toInt() );
96 config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
97 }
98 }
99
101
102 setForceSuppressFormPopup( element.attribute( u"forceSuppressFormPopup"_s, 0 ).toInt() );
103
104 if ( element.hasAttribute( u"nmRelationId"_s ) )
105 {
106 setNmRelationId( element.attribute( u"nmRelationId"_s ) );
107 }
108
109 if ( element.hasAttribute( "label" ) )
110 {
111 const QString label = element.attribute( u"label"_s );
112 setLabel( label );
113 }
114 if ( element.hasAttribute( "relationWidgetTypeId" ) )
115 {
116 const QString relationWidgetTypeId = element.attribute( u"relationWidgetTypeId"_s );
118 }
119}
120
121QString QgsAttributeEditorRelation::typeIdentifier() const
122{
123 return u"attributeEditorRelation"_s;
124}
125
130
132{
133 return mForceSuppressFormPopup;
134}
135
137{
138 mNmRelationId = nmRelationId;
139}
140
142{
143 return mNmRelationId;
144}
145
147{
148 mLabel = label;
149}
150
152{
153 return mLabel;
154}
155
157{
158 return mRelationWidgetTypeId;
159}
160
162{
163 mRelationWidgetTypeId = relationWidgetTypeId;
164}
165
167{
168 return mRelationEditorConfig;
169}
170
172{
173 mRelationEditorConfig = config;
174}
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:7451
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:7149
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:7171
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7450