QGIS API Documentation 3.99.0-Master (8e76e220402)
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
30#include <QString>
31
32using namespace Qt::StringLiterals;
33
34QDomElement QgsAttributeEditorElement::toDomElement( QDomDocument &doc ) const
35{
36 QDomElement elem = doc.createElement( typeIdentifier() );
37 elem.setAttribute( u"name"_s, mName );
38 elem.setAttribute( u"showLabel"_s, mShowLabel );
39 elem.setAttribute( u"horizontalStretch"_s, mHorizontalStretch );
40 elem.setAttribute( u"verticalStretch"_s, mVerticalStretch );
41 elem.appendChild( mLabelStyle.writeXml( doc ) );
42 saveConfiguration( elem, doc );
43 return elem;
44}
45
47{
48 return mShowLabel;
49}
50
55
60
65
66QgsAttributeEditorElement *QgsAttributeEditorElement::create( const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent )
67{
68 QgsAttributeEditorElement *newElement = nullptr;
69
70 const QString name = element.attribute( u"name"_s );
71
72 if ( element.tagName() == "attributeEditorContainer"_L1 )
73 {
74 newElement = new QgsAttributeEditorContainer( context.projectTranslator()->translate( u"project:layers:%1:formcontainers"_s.arg( layerId ),
75 name ), parent );
76 }
77 else if ( element.tagName() == "attributeEditorField"_L1 )
78 {
79 const int idx = fields.lookupField( name );
80 newElement = new QgsAttributeEditorField( name, idx, parent );
81 }
82 else if ( element.tagName() == "attributeEditorRelation"_L1 )
83 {
84 // At this time, the relations are not loaded
85 // So we only grab the id and delegate the rest to onRelationsLoaded()
86 newElement = new QgsAttributeEditorRelation( element.attribute( u"relation"_s, u"[None]"_s ), parent );
87 }
88 else if ( element.tagName() == "attributeEditorQmlElement"_L1 )
89 {
90 newElement = new QgsAttributeEditorQmlElement( element.attribute( u"name"_s ), parent );
91 }
92 else if ( element.tagName() == "attributeEditorHtmlElement"_L1 )
93 {
94 newElement = new QgsAttributeEditorHtmlElement( element.attribute( u"name"_s ), parent );
95 }
96 else if ( element.tagName() == "attributeEditorTextElement"_L1 )
97 {
98 newElement = new QgsAttributeEditorTextElement( element.attribute( u"name"_s ), parent );
99 }
100 else if ( element.tagName() == "attributeEditorSpacerElement"_L1 )
101 {
102 newElement = new QgsAttributeEditorSpacerElement( element.attribute( u"name"_s ), parent );
103 }
104 else if ( element.tagName() == "attributeEditorAction"_L1 )
105 {
106 newElement = new QgsAttributeEditorAction( QUuid( element.attribute( u"name"_s ) ), parent );
107 }
108
109 if ( newElement )
110 {
111 if ( element.hasAttribute( u"showLabel"_s ) )
112 newElement->setShowLabel( element.attribute( u"showLabel"_s ).toInt() );
113 else
114 newElement->setShowLabel( true );
115
116 newElement->setHorizontalStretch( element.attribute( u"horizontalStretch"_s, u"0"_s ).toInt() );
117 newElement->setVerticalStretch( element.attribute( u"verticalStretch"_s, u"0"_s ).toInt() );
118
119 // Label font and color
120 LabelStyle style;
121 style.readXml( element );
122 newElement->setLabelStyle( style );
123
124 newElement->loadConfiguration( element, layerId, context, fields );
125 }
126
127 return newElement;
128}
129
130
132{
133 QDomElement element { node.firstChildElement( u"labelStyle"_s ) };
134
135 if ( ! element.isNull() )
136 {
137
138 // Label font and color
139 if ( element.hasAttribute( u"labelColor"_s ) )
140 {
141 color = QgsColorUtils::colorFromString( element.attribute( u"labelColor"_s ) );
142 }
143
144 QFont newFont;
145 QgsFontUtils::setFromXmlChildNode( newFont, element, u"labelFont"_s );
146
147 font = newFont;
148
149 if ( element.hasAttribute( u"overrideLabelColor"_s ) )
150 {
151 overrideColor = element.attribute( u"overrideLabelColor"_s ) == QChar( '1' );
152 }
153
154 if ( element.hasAttribute( u"overrideLabelFont"_s ) )
155 {
156 overrideFont = element.attribute( u"overrideLabelFont"_s ) == QChar( '1' );
157 }
158 }
159}
160
161QDomElement QgsAttributeEditorElement::LabelStyle::writeXml( QDomDocument &document ) const
162{
163 QDomElement elem { document.createElement( u"labelStyle"_s ) };
164 elem.setAttribute( u"labelColor"_s, QgsColorUtils::colorToString( color ) );
165 elem.appendChild( QgsFontUtils::toXmlElement( font, document, u"labelFont"_s ) );
166 elem.setAttribute( u"overrideLabelColor"_s, overrideColor ? QChar( '1' ) : QChar( '0' ) );
167 elem.setAttribute( u"overrideLabelFont"_s, overrideFont ? QChar( '1' ) : QChar( '0' ) );
168 return elem;
169}
170
172{
173 return overrideColor == other.overrideColor && overrideFont == other.overrideFont && color == other.color && font == other.font;
174}
This element will load a layer action onto the form.
A container for attribute editors, used to group them visually in the attribute form if it is set to ...
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.
QgsAttributeEditorElement(Qgis::AttributeEditorType type, const QString &name, QgsAttributeEditorElement *parent=nullptr)
Constructor.
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
Translates a string using the Qt QTranslator mechanism.
A container for the context for various read/write operations on 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.