QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 : [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  ***************************************************************************/
16 
18 
25 #include "qgssymbollayerutils.h"
26 #include "qgsfontutils.h"
27 
28 QDomElement QgsAttributeEditorElement::toDomElement( QDomDocument &doc ) const
29 {
30  QDomElement elem = doc.createElement( typeIdentifier() );
31  elem.setAttribute( QStringLiteral( "name" ), mName );
32  elem.setAttribute( QStringLiteral( "showLabel" ), mShowLabel );
33  elem.appendChild( mLabelStyle.writeXml( doc ) );
34  saveConfiguration( elem, doc );
35  return elem;
36 }
37 
39 {
40  return mShowLabel;
41 }
42 
44 {
46 }
47 
49 {
50  return mLabelStyle;
51 }
52 
54 {
56 }
57 
58 QgsAttributeEditorElement *QgsAttributeEditorElement::create( const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent )
59 {
60  QgsAttributeEditorElement *newElement = nullptr;
61 
62  const QString name = element.attribute( QStringLiteral( "name" ) );
63 
64  if ( element.tagName() == QLatin1String( "attributeEditorContainer" ) )
65  {
66  newElement = new QgsAttributeEditorContainer( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ),
67  name ), parent );
68  }
69  else if ( element.tagName() == QLatin1String( "attributeEditorField" ) )
70  {
71  const int idx = fields.lookupField( name );
72  newElement = new QgsAttributeEditorField( name, idx, parent );
73  }
74  else if ( element.tagName() == QLatin1String( "attributeEditorRelation" ) )
75  {
76  // At this time, the relations are not loaded
77  // So we only grab the id and delegate the rest to onRelationsLoaded()
78  newElement = new QgsAttributeEditorRelation( element.attribute( QStringLiteral( "relation" ), QStringLiteral( "[None]" ) ), parent );
79  }
80  else if ( element.tagName() == QLatin1String( "attributeEditorQmlElement" ) )
81  {
82  newElement = new QgsAttributeEditorQmlElement( element.attribute( QStringLiteral( "name" ) ), parent );
83  }
84  else if ( element.tagName() == QLatin1String( "attributeEditorHtmlElement" ) )
85  {
86  newElement = new QgsAttributeEditorHtmlElement( element.attribute( QStringLiteral( "name" ) ), parent );
87  }
88  else if ( element.tagName() == QLatin1String( "attributeEditorAction" ) )
89  {
90  newElement = new QgsAttributeEditorAction( QUuid( element.attribute( QStringLiteral( "name" ) ) ), parent );
91  }
92 
93  if ( newElement )
94  {
95  if ( element.hasAttribute( QStringLiteral( "showLabel" ) ) )
96  newElement->setShowLabel( element.attribute( QStringLiteral( "showLabel" ) ).toInt() );
97  else
98  newElement->setShowLabel( true );
99 
100  // Label font and color
101  LabelStyle style;
102  style.readXml( element );
103  newElement->setLabelStyle( style );
104 
105  newElement->loadConfiguration( element, layerId, context, fields );
106  }
107 
108  return newElement;
109 }
110 
111 
113 {
114  QDomElement element { node.firstChildElement( QStringLiteral( "labelStyle" ) ) };
115 
116  if ( ! element.isNull() )
117  {
118 
119  // Label font and color
120  if ( element.hasAttribute( QStringLiteral( "labelColor" ) ) )
121  {
122  color = QgsSymbolLayerUtils::decodeColor( element.attribute( QStringLiteral( "labelColor" ) ) );
123  }
124 
125  QFont newFont;
126  QgsFontUtils::setFromXmlChildNode( newFont, element, QStringLiteral( "labelFont" ) );
127 
128  font = newFont;
129 
130  if ( element.hasAttribute( QStringLiteral( "overrideLabelColor" ) ) )
131  {
132  overrideColor = element.attribute( QStringLiteral( "overrideLabelColor" ) ) == QChar( '1' );
133  }
134 
135  if ( element.hasAttribute( QStringLiteral( "overrideLabelFont" ) ) )
136  {
137  overrideFont = element.attribute( QStringLiteral( "overrideLabelFont" ) ) == QChar( '1' );
138  }
139  }
140 }
141 
142 QDomElement QgsAttributeEditorElement::LabelStyle::writeXml( QDomDocument &document ) const
143 {
144  QDomElement elem { document.createElement( QStringLiteral( "labelStyle" ) ) };
145  elem.setAttribute( QStringLiteral( "labelColor" ), QgsSymbolLayerUtils::encodeColor( color ) );
146  elem.appendChild( QgsFontUtils::toXmlElement( font, document, QStringLiteral( "labelFont" ) ) );
147  elem.setAttribute( QStringLiteral( "overrideLabelColor" ), overrideColor ? QChar( '1' ) : QChar( '0' ) );
148  elem.setAttribute( QStringLiteral( "overrideLabelFont" ), overrideFont ? QChar( '1' ) : QChar( '0' ) );
149  return elem;
150 }
151 
153 {
154  return overrideColor == other.overrideColor && overrideFont == other.overrideFont && color == other.color && font == other.font;
155 }
QgsAttributeEditorElement
This is an abstract base class for any elements of a drag and drop form.
Definition: qgsattributeeditorelement.h:37
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:64
QgsAttributeEditorElement::labelStyle
LabelStyle labelStyle() const
Returns the label style.
Definition: qgsattributeeditorelement.cpp:48
QgsAttributeEditorElement::LabelStyle::operator==
bool operator==(LabelStyle const &other) const
Returns true if the style is equal to other.
Definition: qgsattributeeditorelement.cpp:152
QgsAttributeEditorElement::LabelStyle::readXml
void readXml(const QDomNode &node)
Reads configuration from node.
Definition: qgsattributeeditorelement.cpp:112
QgsAttributeEditorElement::create
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.
Definition: qgsattributeeditorelement.cpp:58
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
qgsattributeeditorcontainer.h
QgsAttributeEditorField
This element will load a field's widget onto the form.
Definition: qgsattributeeditorfield.h:27
QgsAttributeEditorElement::LabelStyle
The TabStyle struct defines color and font overrides for form fields, tabs and groups labels.
Definition: qgsattributeeditorelement.h:82
qgsattributeeditorrelation.h
qgsattributeeditorhtmlelement.h
QgsAttributeEditorElement::setShowLabel
void setShowLabel(bool showLabel)
Controls if this element should be labeled with a title (field, relation or groupname).
Definition: qgsattributeeditorelement.cpp:43
qgssymbollayerutils.h
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsAttributeEditorElement::LabelStyle::writeXml
QDomElement writeXml(QDomDocument &document) const
Creates the XML configuration from document.
Definition: qgsattributeeditorelement.cpp:142
qgsfontutils.h
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:69
qgsattributeeditoraction.h
QgsReadWriteContext::projectTranslator
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
Definition: qgsreadwritecontext.h:128
QgsAttributeEditorElement::LabelStyle::font
QFont font
Label font.
Definition: qgsattributeeditorelement.h:103
QgsAttributeEditorQmlElement
An attribute editor widget that will represent arbitrary QML code.
Definition: qgsattributeeditorqmlelement.h:28
QgsAttributeEditorElement::showLabel
bool showLabel() const
Controls if this element should be labeled with a title (field, relation or groupname).
Definition: qgsattributeeditorelement.cpp:38
QgsAttributeEditorElement::LabelStyle::overrideColor
bool overrideColor
Override label color.
Definition: qgsattributeeditorelement.h:106
QgsAttributeEditorRelation
This element will load a relation editor onto the form.
Definition: qgsattributeeditorrelation.h:32
QgsAttributeEditorAction
This element will load a layer action onto the form.
Definition: qgsattributeeditoraction.h:28
qgsattributeeditorqmlelement.h
QgsAttributeEditorElement::mLabelStyle
LabelStyle mLabelStyle
Definition: qgsattributeeditorelement.h:222
QgsAttributeEditorElement::LabelStyle::overrideFont
bool overrideFont
Override label font.
Definition: qgsattributeeditorelement.h:109
QgsAttributeEditorElement::mName
QString mName
Definition: qgsattributeeditorelement.h:219
QgsFontUtils::setFromXmlChildNode
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.
Definition: qgsfontutils.cpp:400
QgsFontUtils::toXmlElement
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
Definition: qgsfontutils.cpp:354
QgsAttributeEditorElement::mShowLabel
bool mShowLabel
Definition: qgsattributeeditorelement.h:221
QgsAttributeEditorContainer
This is a container for attribute editors, used to group them visually in the attribute form if it is...
Definition: qgsattributeeditorcontainer.h:27
QgsAttributeEditorElement::LabelStyle::color
QColor color
Label font.
Definition: qgsattributeeditorelement.h:100
QgsAttributeEditorElement::name
QString name() const
Returns the name of this element.
Definition: qgsattributeeditorelement.h:156
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:349
QgsAttributeEditorElement::toDomElement
QDomElement toDomElement(QDomDocument &doc) const
Gets the XML Dom element to save this element.
Definition: qgsattributeeditorelement.cpp:28
qgsattributeeditorelement.h
qgsattributeeditorfield.h
QgsAttributeEditorElement::setLabelStyle
void setLabelStyle(const LabelStyle &labelStyle)
Sets the labelStyle.
Definition: qgsattributeeditorelement.cpp:53
QgsProjectTranslator::translate
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.
QgsAttributeEditorHtmlElement
An attribute editor widget that will represent arbitrary HTML code.
Definition: qgsattributeeditorhtmlelement.h:30
QgsAttributeEditorElement::parent
QgsAttributeEditorElement * parent() const
Gets the parent of this element.
Definition: qgsattributeeditorelement.h:170