QGIS API Documentation 4.3.0-Master (0cfde48c85b)
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
7 email : matthias@opengis.ch
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
66std::unique_ptr<QgsAttributeEditorElement> QgsAttributeEditorElement::create(
67 const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent
68)
69{
70 std::unique_ptr<QgsAttributeEditorElement> newElement;
71
72 const QString name = element.attribute( u"name"_s );
73
74 if ( element.tagName() == "attributeEditorContainer"_L1 )
75 {
76 newElement = std::make_unique<QgsAttributeEditorContainer>( context.projectTranslator()->translate( u"project:layers:%1:formcontainers"_s.arg( layerId ), name ), parent );
77 }
78 else if ( element.tagName() == "attributeEditorField"_L1 )
79 {
80 const int idx = fields.lookupField( name );
81 newElement = std::make_unique<QgsAttributeEditorField>( name, idx, parent );
82 }
83 else if ( element.tagName() == "attributeEditorRelation"_L1 )
84 {
85 // At this time, the relations are not loaded
86 // So we only grab the id and delegate the rest to onRelationsLoaded()
87 newElement = std::make_unique<QgsAttributeEditorRelation>( element.attribute( u"relation"_s, u"[None]"_s ), parent );
88 }
89 else if ( element.tagName() == "attributeEditorQmlElement"_L1 )
90 {
91 newElement = std::make_unique<QgsAttributeEditorQmlElement>( element.attribute( u"name"_s ), parent );
92 }
93 else if ( element.tagName() == "attributeEditorHtmlElement"_L1 )
94 {
95 newElement = std::make_unique<QgsAttributeEditorHtmlElement>( element.attribute( u"name"_s ), parent );
96 }
97 else if ( element.tagName() == "attributeEditorTextElement"_L1 )
98 {
99 newElement = std::make_unique<QgsAttributeEditorTextElement>( element.attribute( u"name"_s ), parent );
100 }
101 else if ( element.tagName() == "attributeEditorSpacerElement"_L1 )
102 {
103 newElement = std::make_unique<QgsAttributeEditorSpacerElement>( element.attribute( u"name"_s ), parent );
104 }
105 else if ( element.tagName() == "attributeEditorAction"_L1 )
106 {
107 newElement = std::make_unique<QgsAttributeEditorAction>( QUuid( element.attribute( u"name"_s ) ), parent );
108 }
109
110 if ( newElement )
111 {
112 if ( element.hasAttribute( u"showLabel"_s ) )
113 newElement->setShowLabel( element.attribute( u"showLabel"_s ).toInt() );
114 else
115 newElement->setShowLabel( true );
116
117 newElement->setHorizontalStretch( element.attribute( u"horizontalStretch"_s, u"0"_s ).toInt() );
118 newElement->setVerticalStretch( element.attribute( u"verticalStretch"_s, u"0"_s ).toInt() );
119
120 // Label font and color
121 LabelStyle style;
122 style.readXml( element );
123 newElement->setLabelStyle( style );
124
125 newElement->loadConfiguration( element, layerId, context, fields );
126 }
127
128 return newElement;
129}
130
131
133{
134 QDomElement element { node.firstChildElement( u"labelStyle"_s ) };
135
136 if ( !element.isNull() )
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}
static std::unique_ptr< 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.
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.
void setShowLabel(bool showLabel)
Controls if this element should be labeled with a title (field, relation or groupname).
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:45
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.