QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
37  return element;
38 }
39 
40 void QgsAttributeEditorRelation::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
41 {
42  elem.setAttribute( QStringLiteral( "relation" ), mRelation.id() );
43  elem.setAttribute( QStringLiteral( "forceSuppressFormPopup" ), mForceSuppressFormPopup );
44  elem.setAttribute( QStringLiteral( "nmRelationId" ), mNmRelationId.toString() );
45  elem.setAttribute( QStringLiteral( "label" ), mLabel );
46  elem.setAttribute( QStringLiteral( "relationWidgetTypeId" ), mRelationWidgetTypeId );
47 
48  QDomElement elemConfig = QgsXmlUtils::writeVariant( mRelationEditorConfig, doc );
49  elemConfig.setTagName( QStringLiteral( "editor_configuration" ) );
50  elem.appendChild( elemConfig );
51 }
52 
53 void QgsAttributeEditorRelation::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
54 {
55  Q_UNUSED( layerId )
56  Q_UNUSED( context )
57  Q_UNUSED( fields )
58 
59  QVariantMap config = QgsXmlUtils::readVariant( element.firstChildElement( "editor_configuration" ) ).toMap();
60 
61  // load defaults
62  if ( config.isEmpty() )
63  config = relationEditorConfiguration();
64 
65  // pre QGIS 3.18 compatibility
66  if ( ! config.contains( QStringLiteral( "buttons" ) ) )
67  {
68  if ( element.hasAttribute( "buttons" ) )
69  {
71  // QgsAttributeEditorRelation::Button has been deprecated in favor of QgsRelationEditorWidget::Button
72  // we cannot use it here since the new flags are in gui, while the current code is in core
73  // TODO: remove this compatibility code in QGIS 4
74  // or make the enum private if we really want to keep the backward compatibility (but not worth it!)
75  const QString buttonString = element.attribute( QStringLiteral( "buttons" ), qgsFlagValueToKeys( QgsAttributeEditorRelation::Button::AllButtons ) );
76  config.insert( "buttons", qgsFlagValueToKeys( qgsFlagKeysToValue( buttonString, QgsAttributeEditorRelation::Button::AllButtons ) ) );
78  }
79  else
80  {
81  // pre QGIS 3.16 compatibility
83  QgsAttributeEditorRelation::Buttons buttons = QgsAttributeEditorRelation::Button::AllButtons;
84  buttons.setFlag( QgsAttributeEditorRelation::Button::Link, element.attribute( QStringLiteral( "showLinkButton" ), QStringLiteral( "1" ) ).toInt() );
85  buttons.setFlag( QgsAttributeEditorRelation::Button::Unlink, element.attribute( QStringLiteral( "showUnlinkButton" ), QStringLiteral( "1" ) ).toInt() );
86  buttons.setFlag( QgsAttributeEditorRelation::Button::SaveChildEdits, element.attribute( QStringLiteral( "showSaveChildEditsButton" ), QStringLiteral( "1" ) ).toInt() );
88  config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
89  }
90  }
91 
93 
94  setForceSuppressFormPopup( element.attribute( QStringLiteral( "forceSuppressFormPopup" ), 0 ).toInt() );
95 
96  if ( element.hasAttribute( QStringLiteral( "nmRelationId" ) ) )
97  {
98  setNmRelationId( element.attribute( QStringLiteral( "nmRelationId" ) ) );
99  }
100 
101  if ( element.hasAttribute( "label" ) )
102  {
103  const QString label = element.attribute( QStringLiteral( "label" ) );
104  setLabel( label );
105  }
106  if ( element.hasAttribute( "relationWidgetTypeId" ) )
107  {
108  const QString relationWidgetTypeId = element.attribute( QStringLiteral( "relationWidgetTypeId" ) );
110  }
111 }
112 
113 QString QgsAttributeEditorRelation::typeIdentifier() const
114 {
115  return QStringLiteral( "attributeEditorRelation" );
116 }
117 
118 void QgsAttributeEditorRelation::setForceSuppressFormPopup( bool forceSuppressFormPopup )
119 {
120  mForceSuppressFormPopup = forceSuppressFormPopup;
121 }
122 
124 {
125  return mForceSuppressFormPopup;
126 }
127 
128 void QgsAttributeEditorRelation::setNmRelationId( const QVariant &nmRelationId )
129 {
130  mNmRelationId = nmRelationId;
131 }
132 
134 {
135  return mNmRelationId;
136 }
137 
138 void QgsAttributeEditorRelation::setLabel( const QString &label )
139 {
140  mLabel = label;
141 }
142 
144 {
145  return mLabel;
146 }
147 
149 {
150  return mRelationWidgetTypeId;
151 }
152 
153 void QgsAttributeEditorRelation::setRelationWidgetTypeId( const QString &relationWidgetTypeId )
154 {
155  mRelationWidgetTypeId = relationWidgetTypeId;
156 }
157 
159 {
160  return mRelationEditorConfig;
161 }
162 
164 {
165  mRelationEditorConfig = config;
166 }
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.
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:45
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.
Q_GADGET QString id
Definition: qgsrelation.h:46
bool isValid
Definition: qgsrelation.h:50
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:2065
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:2064
T qgsFlagKeysToValue(const QString &keys, const T &defaultValue)
Returns the value corresponding to the given keys of a flag.
Definition: qgis.h:1792
QString qgsFlagValueToKeys(const T &value)
Returns the value for the given keys of a flag.
Definition: qgis.h:1780