QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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
7  email : [email protected]
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 "qgsrelationmanager.h"
18 #include "qgsxmlutils.h"
19 
21 {
22  mRelation = relationManager->relation( mRelationId );
23  return mRelation.isValid();
24 }
25 
27 {
28  QgsAttributeEditorRelation *element = new QgsAttributeEditorRelation( mRelationId, parent );
29  element->mRelation = mRelation;
30  element->mButtons = mButtons;
31  element->mForceSuppressFormPopup = mForceSuppressFormPopup;
32  element->mNmRelationId = mNmRelationId;
33  element->mLabel = mLabel;
34  element->mRelationEditorConfig = mRelationEditorConfig;
35  element->mRelationWidgetTypeId = mRelationWidgetTypeId;
36  element->mLabelStyle = mLabelStyle;
37 
38  return element;
39 }
40 
41 void QgsAttributeEditorRelation::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
42 {
43  elem.setAttribute( QStringLiteral( "relation" ), mRelation.id() );
44  elem.setAttribute( QStringLiteral( "forceSuppressFormPopup" ), mForceSuppressFormPopup );
45  elem.setAttribute( QStringLiteral( "nmRelationId" ), mNmRelationId.toString() );
46  elem.setAttribute( QStringLiteral( "label" ), mLabel );
47  elem.setAttribute( QStringLiteral( "relationWidgetTypeId" ), mRelationWidgetTypeId );
48 
49  QDomElement elemConfig = QgsXmlUtils::writeVariant( mRelationEditorConfig, doc );
50  elemConfig.setTagName( QStringLiteral( "editor_configuration" ) );
51  elem.appendChild( elemConfig );
52 }
53 
54 void QgsAttributeEditorRelation::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
55 {
56  Q_UNUSED( layerId )
57  Q_UNUSED( context )
58  Q_UNUSED( fields )
59 
60  QVariantMap config = QgsXmlUtils::readVariant( element.firstChildElement( "editor_configuration" ) ).toMap();
61 
62  // load defaults
63  if ( config.isEmpty() )
64  config = relationEditorConfiguration();
65 
66  // pre QGIS 3.18 compatibility
67  if ( ! config.contains( QStringLiteral( "buttons" ) ) )
68  {
69  if ( element.hasAttribute( "buttons" ) )
70  {
72  // QgsAttributeEditorRelation::Button has been deprecated in favor of QgsRelationEditorWidget::Button
73  // we cannot use it here since the new flags are in gui, while the current code is in core
74  // TODO: remove this compatibility code in QGIS 4
75  // or make the enum private if we really want to keep the backward compatibility (but not worth it!)
76  const QString buttonString = element.attribute( QStringLiteral( "buttons" ), qgsFlagValueToKeys( QgsAttributeEditorRelation::Button::AllButtons ) );
77  config.insert( "buttons", qgsFlagValueToKeys( qgsFlagKeysToValue( buttonString, QgsAttributeEditorRelation::Button::AllButtons ) ) );
79  }
80  else
81  {
82  // pre QGIS 3.16 compatibility
84  QgsAttributeEditorRelation::Buttons buttons = QgsAttributeEditorRelation::Button::AllButtons;
85  buttons.setFlag( QgsAttributeEditorRelation::Button::Link, element.attribute( QStringLiteral( "showLinkButton" ), QStringLiteral( "1" ) ).toInt() );
86  buttons.setFlag( QgsAttributeEditorRelation::Button::Unlink, element.attribute( QStringLiteral( "showUnlinkButton" ), QStringLiteral( "1" ) ).toInt() );
87  buttons.setFlag( QgsAttributeEditorRelation::Button::SaveChildEdits, element.attribute( QStringLiteral( "showSaveChildEditsButton" ), QStringLiteral( "1" ) ).toInt() );
89  config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
90  }
91  }
92 
94 
95  setForceSuppressFormPopup( element.attribute( QStringLiteral( "forceSuppressFormPopup" ), 0 ).toInt() );
96 
97  if ( element.hasAttribute( QStringLiteral( "nmRelationId" ) ) )
98  {
99  setNmRelationId( element.attribute( QStringLiteral( "nmRelationId" ) ) );
100  }
101 
102  if ( element.hasAttribute( "label" ) )
103  {
104  const QString label = element.attribute( QStringLiteral( "label" ) );
105  setLabel( label );
106  }
107  if ( element.hasAttribute( "relationWidgetTypeId" ) )
108  {
109  const QString relationWidgetTypeId = element.attribute( QStringLiteral( "relationWidgetTypeId" ) );
111  }
112 }
113 
114 QString QgsAttributeEditorRelation::typeIdentifier() const
115 {
116  return QStringLiteral( "attributeEditorRelation" );
117 }
118 
119 void QgsAttributeEditorRelation::setForceSuppressFormPopup( bool forceSuppressFormPopup )
120 {
121  mForceSuppressFormPopup = forceSuppressFormPopup;
122 }
123 
125 {
126  return mForceSuppressFormPopup;
127 }
128 
129 void QgsAttributeEditorRelation::setNmRelationId( const QVariant &nmRelationId )
130 {
131  mNmRelationId = nmRelationId;
132 }
133 
135 {
136  return mNmRelationId;
137 }
138 
139 void QgsAttributeEditorRelation::setLabel( const QString &label )
140 {
141  mLabel = label;
142 }
143 
145 {
146  return mLabel;
147 }
148 
150 {
151  return mRelationWidgetTypeId;
152 }
153 
154 void QgsAttributeEditorRelation::setRelationWidgetTypeId( const QString &relationWidgetTypeId )
155 {
156  mRelationWidgetTypeId = relationWidgetTypeId;
157 }
158 
160 {
161  return mRelationEditorConfig;
162 }
163 
165 {
166  mRelationEditorConfig = config;
167 }
QgsAttributeEditorElement
This is an abstract base class for any elements of a drag and drop form.
Definition: qgsattributeeditorelement.h:37
QgsRelationManager
This class manages a set of relations between layers.
Definition: qgsrelationmanager.h:35
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsAttributeEditorRelation::relationEditorConfiguration
QVariantMap relationEditorConfiguration() const
Returns the relation editor widget configuration.
Definition: qgsattributeeditorrelation.cpp:159
qgsattributeeditorrelation.h
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsAttributeEditorRelation::clone
QgsAttributeEditorElement * clone(QgsAttributeEditorElement *parent) const override
Returns a clone of this element.
Definition: qgsattributeeditorrelation.cpp:26
qgsFlagKeysToValue
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:2520
QgsRelationManager::relation
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
Definition: qgsrelationmanager.cpp:95
QgsAttributeEditorRelation::init
bool init(QgsRelationManager *relManager)
Initializes the relation from the id.
Definition: qgsattributeeditorrelation.cpp:20
QgsRelation::id
QString id
Definition: qgsrelation.h:46
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:2820
qgsFlagValueToKeys
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition: qgis.h:2498
QgsAttributeEditorRelation::setNmRelationId
void setNmRelationId(const QVariant &nmRelationId=QVariant())
Sets nmRelationId for the relation id of the second relation involved in an N:M relation.
Definition: qgsattributeeditorrelation.cpp:129
QgsXmlUtils::readVariant
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
Definition: qgsxmlutils.cpp:251
QgsAttributeEditorRelation
This element will load a relation editor onto the form.
Definition: qgsattributeeditorrelation.h:32
QgsAttributeEditorRelation::label
QString label() const
Determines the label of this element.
Definition: qgsattributeeditorrelation.cpp:144
QgsAttributeEditorRelation::setRelationWidgetTypeId
void setRelationWidgetTypeId(const QString &relationWidgetTypeId)
Sets the relation widget type.
Definition: qgsattributeeditorrelation.cpp:154
QgsAttributeEditorRelation::forceSuppressFormPopup
bool forceSuppressFormPopup() const
Determines the force suppress form popup status.
Definition: qgsattributeeditorrelation.cpp:124
QgsAttributeEditorElement::mLabelStyle
LabelStyle mLabelStyle
Definition: qgsattributeeditorelement.h:222
qgsrelationmanager.h
QgsAttributeEditorRelation::nmRelationId
QVariant nmRelationId() const
Determines the relation id of the second relation involved in an N:M relation.
Definition: qgsattributeeditorrelation.cpp:134
qgsxmlutils.h
QgsAttributeEditorRelation::relationWidgetTypeId
QString relationWidgetTypeId() const
Returns the current relation widget type id.
Definition: qgsattributeeditorrelation.cpp:149
QgsAttributeEditorRelation::QgsAttributeEditorRelation
Q_DECL_DEPRECATED QgsAttributeEditorRelation(const QString &name, const QString &relationId, QgsAttributeEditorElement *parent)
Definition: qgsattributeeditorrelation.h:63
QgsAttributeEditorRelation::setForceSuppressFormPopup
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status to forceSuppressFormPopup.
Definition: qgsattributeeditorrelation.cpp:119
QgsRelation::isValid
bool isValid
Definition: qgsrelation.h:50
QgsAttributeEditorRelation::setLabel
void setLabel(const QString &label=QString())
Sets label for this element If it's empty it takes the relation id as label.
Definition: qgsattributeeditorrelation.cpp:139
QgsXmlUtils::writeVariant
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
Definition: qgsxmlutils.cpp:106
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2819
QgsAttributeEditorRelation::setRelationEditorConfiguration
void setRelationEditorConfiguration(const QVariantMap &config)
Sets the relation editor configuration.
Definition: qgsattributeeditorrelation.cpp:164
QgsAttributeEditorElement::parent
QgsAttributeEditorElement * parent() const
Gets the parent of this element.
Definition: qgsattributeeditorelement.h:170