QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
qgsattributeeditorelement.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributeeditorelement.cpp - QgsAttributeEditorElement
3
4 ---------------------
5 begin : 18.8.2016
6 copyright : (C) 2016 by Matthias Kuhn
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 ***************************************************************************/
16
18
27#include "qgscolorutils.h"
28#include "qgsfontutils.h"
29
30QDomElement QgsAttributeEditorElement::toDomElement( QDomDocument &doc ) const
31{
32 QDomElement elem = doc.createElement( typeIdentifier() );
33 elem.setAttribute( QStringLiteral( "name" ), mName );
34 elem.setAttribute( QStringLiteral( "showLabel" ), mShowLabel );
35 elem.setAttribute( QStringLiteral( "horizontalStretch" ), mHorizontalStretch );
36 elem.setAttribute( QStringLiteral( "verticalStretch" ), mVerticalStretch );
37 elem.appendChild( mLabelStyle.writeXml( doc ) );
38 saveConfiguration( elem, doc );
39 return elem;
40}
41
43{
44 return mShowLabel;
45}
46
51
56
61
62QgsAttributeEditorElement *QgsAttributeEditorElement::create( const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent )
63{
64 QgsAttributeEditorElement *newElement = nullptr;
65
66 const QString name = element.attribute( QStringLiteral( "name" ) );
67
68 if ( element.tagName() == QLatin1String( "attributeEditorContainer" ) )
69 {
70 newElement = new QgsAttributeEditorContainer( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ),
71 name ), parent );
72 }
73 else if ( element.tagName() == QLatin1String( "attributeEditorField" ) )
74 {
75 const int idx = fields.lookupField( name );
76 newElement = new QgsAttributeEditorField( name, idx, parent );
77 }
78 else if ( element.tagName() == QLatin1String( "attributeEditorRelation" ) )
79 {
80 // At this time, the relations are not loaded
81 // So we only grab the id and delegate the rest to onRelationsLoaded()
82 newElement = new QgsAttributeEditorRelation( element.attribute( QStringLiteral( "relation" ), QStringLiteral( "[None]" ) ), parent );
83 }
84 else if ( element.tagName() == QLatin1String( "attributeEditorQmlElement" ) )
85 {
86 newElement = new QgsAttributeEditorQmlElement( element.attribute( QStringLiteral( "name" ) ), parent );
87 }
88 else if ( element.tagName() == QLatin1String( "attributeEditorHtmlElement" ) )
89 {
90 newElement = new QgsAttributeEditorHtmlElement( element.attribute( QStringLiteral( "name" ) ), parent );
91 }
92 else if ( element.tagName() == QLatin1String( "attributeEditorTextElement" ) )
93 {
94 newElement = new QgsAttributeEditorTextElement( element.attribute( QStringLiteral( "name" ) ), parent );
95 }
96 else if ( element.tagName() == QLatin1String( "attributeEditorSpacerElement" ) )
97 {
98 newElement = new QgsAttributeEditorSpacerElement( element.attribute( QStringLiteral( "name" ) ), parent );
99 }
100 else if ( element.tagName() == QLatin1String( "attributeEditorAction" ) )
101 {
102 newElement = new QgsAttributeEditorAction( QUuid( element.attribute( QStringLiteral( "name" ) ) ), parent );
103 }
104
105 if ( newElement )
106 {
107 if ( element.hasAttribute( QStringLiteral( "showLabel" ) ) )
108 newElement->setShowLabel( element.attribute( QStringLiteral( "showLabel" ) ).toInt() );
109 else
110 newElement->setShowLabel( true );
111
112 newElement->setHorizontalStretch( element.attribute( QStringLiteral( "horizontalStretch" ), QStringLiteral( "0" ) ).toInt() );
113 newElement->setVerticalStretch( element.attribute( QStringLiteral( "verticalStretch" ), QStringLiteral( "0" ) ).toInt() );
114
115 // Label font and color
116 LabelStyle style;
117 style.readXml( element );
118 newElement->setLabelStyle( style );
119
120 newElement->loadConfiguration( element, layerId, context, fields );
121 }
122
123 return newElement;
124}
125
126
128{
129 QDomElement element { node.firstChildElement( QStringLiteral( "labelStyle" ) ) };
130
131 if ( ! element.isNull() )
132 {
133
134 // Label font and color
135 if ( element.hasAttribute( QStringLiteral( "labelColor" ) ) )
136 {
137 color = QgsColorUtils::colorFromString( element.attribute( QStringLiteral( "labelColor" ) ) );
138 }
139
140 QFont newFont;
141 QgsFontUtils::setFromXmlChildNode( newFont, element, QStringLiteral( "labelFont" ) );
142
143 font = newFont;
144
145 if ( element.hasAttribute( QStringLiteral( "overrideLabelColor" ) ) )
146 {
147 overrideColor = element.attribute( QStringLiteral( "overrideLabelColor" ) ) == QChar( '1' );
148 }
149
150 if ( element.hasAttribute( QStringLiteral( "overrideLabelFont" ) ) )
151 {
152 overrideFont = element.attribute( QStringLiteral( "overrideLabelFont" ) ) == QChar( '1' );
153 }
154 }
155}
156
157QDomElement QgsAttributeEditorElement::LabelStyle::writeXml( QDomDocument &document ) const
158{
159 QDomElement elem { document.createElement( QStringLiteral( "labelStyle" ) ) };
160 elem.setAttribute( QStringLiteral( "labelColor" ), QgsColorUtils::colorToString( color ) );
161 elem.appendChild( QgsFontUtils::toXmlElement( font, document, QStringLiteral( "labelFont" ) ) );
162 elem.setAttribute( QStringLiteral( "overrideLabelColor" ), overrideColor ? QChar( '1' ) : QChar( '0' ) );
163 elem.setAttribute( QStringLiteral( "overrideLabelFont" ), overrideFont ? QChar( '1' ) : QChar( '0' ) );
164 return elem;
165}
166
168{
169 return overrideColor == other.overrideColor && overrideFont == other.overrideFont && color == other.color && font == other.font;
170}
This element will load a layer action onto the form.
This is a container for attribute editors, used to group them visually in the attribute form if it is...
This is an abstract base class for any elements of a drag and drop form.
void setHorizontalStretch(int stretch)
Sets the horizontal stretch factor for the element.
QDomElement toDomElement(QDomDocument &doc) const
Gets the XML Dom element to save this element.
QgsAttributeEditorElement * parent() const
Gets the parent of this element.
LabelStyle labelStyle() const
Returns the label style.
void setLabelStyle(const LabelStyle &labelStyle)
Sets the labelStyle.
bool showLabel() const
Controls if this element should be labeled with a title (field, relation or groupname).
QString name() const
Returns the name of this element.
static QgsAttributeEditorElement * create(const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent=nullptr)
Constructs the editor element from the given element.
void setVerticalStretch(int stretch)
Sets the vertical stretch factor for the element.
void setShowLabel(bool showLabel)
Controls if this element should be labeled with a title (field, relation or groupname).
This element will load a field's widget onto the form.
An attribute editor widget that will represent arbitrary HTML code.
An attribute editor widget that will represent arbitrary QML code.
This element will load a relation editor onto the form.
An attribute editor widget that will represent a spacer.
An attribute editor widget that will represent arbitrary text code.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Container of fields for a vector layer.
Definition qgsfields.h:46
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
The derived translate() translates with QTranslator and qm file the sourceText.
The class is used as a container of context for various read/write operations on other objects.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.
void readXml(const QDomNode &node)
Reads configuration from node.
QDomElement writeXml(QDomDocument &document) const
Creates the XML configuration from document.
bool operator==(LabelStyle const &other) const
Returns true if the style is equal to other.