QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsvectorlayerselectionproperties.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayerselectionproperties.cpp
3 ---------------
4 begin : July 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgscolorutils.h"
21#include "qgssymbollayerutils.h"
22
23#include <QString>
24
25#include "moc_qgsvectorlayerselectionproperties.cpp"
26
27using namespace Qt::StringLiterals;
28
32
34
35
36QDomElement QgsVectorLayerSelectionProperties::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context )
37{
38 QDomElement element = document.createElement( u"selection"_s );
39
40 element.setAttribute( u"mode"_s, qgsEnumValueToKey( mSelectionRenderingMode ) );
41
42 QgsColorUtils::writeXml( mSelectionColor, u"selectionColor"_s, document, element, context );
43
44 if ( mSelectionSymbol )
45 {
46 QDomElement selectionSymbolElement = document.createElement( u"selectionSymbol"_s );
47 selectionSymbolElement.appendChild( QgsSymbolLayerUtils::saveSymbol( QString(), mSelectionSymbol.get(), document, context ) );
48 element.appendChild( selectionSymbolElement );
49 }
50
51 parentElement.appendChild( element );
52 return element;
53}
54
55bool QgsVectorLayerSelectionProperties::readXml( const QDomElement &element, const QgsReadWriteContext &context )
56{
57 const QDomElement selectionElement = element.firstChildElement( u"selection"_s ).toElement();
58 if ( selectionElement.isNull() )
59 return false;
60
61 mSelectionRenderingMode = qgsEnumKeyToValue( selectionElement.attribute( u"mode"_s ), Qgis::SelectionRenderingMode::Default );
62 mSelectionColor = QgsColorUtils::readXml( selectionElement, u"selectionColor"_s, context );
63
64 {
65 const QDomElement selectionSymbolElement = selectionElement.firstChildElement( u"selectionSymbol"_s ).firstChildElement( u"symbol"_s );
66 mSelectionSymbol = QgsSymbolLayerUtils::loadSymbol( selectionSymbolElement, context );
67 }
68 return true;
69}
70
72{
73 auto res = std::make_unique< QgsVectorLayerSelectionProperties >( nullptr );
74 res->mSelectionRenderingMode = mSelectionRenderingMode;
75 res->mSelectionColor = mSelectionColor;
76 res->mSelectionSymbol.reset( mSelectionSymbol ? mSelectionSymbol->clone() : nullptr );
77 return res.release();
78}
79
81{
82 return mSelectionColor;
83}
84
86{
87 if ( mSelectionColor == color )
88 {
89 return;
90 }
91
92 mSelectionColor = color;
94}
95
97{
98 return mSelectionSymbol.get();
99}
100
102{
103 mSelectionSymbol.reset( symbol );
105}
106
111
113{
114 if ( mSelectionRenderingMode == mode )
115 {
116 return;
117 }
118
119 mSelectionRenderingMode = mode;
121}
SelectionRenderingMode
Specifies how a selection should be rendered.
Definition qgis.h:1837
@ Default
Use default symbol and selection colors.
Definition qgis.h:1838
static QColor readXml(const QDomElement &element, const QString &identifier, const QgsReadWriteContext &context)
Reads a color from an XML element, matching the specified identifier string.
static void writeXml(const QColor &color, const QString &identifier, QDomDocument &document, QDomElement &element, const QgsReadWriteContext &context)
Writes a color to an XML element, storing it under the specified identifier.
QgsMapLayerSelectionProperties(QObject *parent)
Constructor for QgsMapLayerSelectionProperties, with the specified parent object.
A container for the context for various read/write operations on objects.
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:227
void setSelectionSymbol(QgsSymbol *symbol)
Sets the symbol used to render selected features in the layer.
QgsVectorLayerSelectionProperties(QObject *parent=nullptr)
Constructor for QgsVectorLayerSelectionProperties, with the specified parent object.
QgsSymbol * selectionSymbol() const
Returns the symbol used to render selected features in the layer.
void selectionSymbolChanged()
Emitted whenever the selection symbol changes.
QgsVectorLayerSelectionProperties * clone() const override
Creates a clone of the properties.
QDomElement writeXml(QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context) override
Writes the properties to a DOM element, to be used later with readXml().
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads temporal properties from a DOM element previously written by writeXml().
void setSelectionColor(const QColor &color)
Sets the color to use for rendering selected features in the layer.
void selectionColorChanged()
Emitted whenever the selection color changes.
void setSelectionRenderingMode(Qgis::SelectionRenderingMode mode)
Sets the selection rendering mode to use for selected features in the layer.
void selectionRenderingModeChanged()
Emitted whenever the selection rendering mode changes.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:7176
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7157