22 #include <QDomElement> 25 static void _readOldLegendGroup(
const QDomElement &groupElem,
QgsLayerTreeGroup *parent );
26 static void _readOldLegendLayer(
const QDomElement &layerElem,
QgsLayerTreeGroup *parent );
30 if ( legendElem.isNull() )
33 QDomNodeList legendChildren = legendElem.childNodes();
35 for (
int i = 0; i < legendChildren.size(); ++i )
37 QDomElement currentChildElem = legendChildren.at( i ).toElement();
38 if ( currentChildElem.tagName() == QLatin1String(
"legendlayer" ) )
40 _readOldLegendLayer( currentChildElem, root );
42 else if ( currentChildElem.tagName() == QLatin1String(
"legendgroup" ) )
44 _readOldLegendGroup( currentChildElem, root );
53 static bool _readOldLegendLayerOrderGroup(
const QDomElement &groupElem, QMap<int, QString> &layerIndexes )
55 QDomNodeList legendChildren = groupElem.childNodes();
57 for (
int i = 0; i < legendChildren.size(); ++i )
59 QDomElement currentChildElem = legendChildren.at( i ).toElement();
60 if ( currentChildElem.tagName() == QLatin1String(
"legendlayer" ) )
62 QDomElement layerFileElem = currentChildElem.firstChildElement( QStringLiteral(
"filegroup" ) ).firstChildElement( QStringLiteral(
"legendlayerfile" ) );
64 int layerIndex = currentChildElem.attribute( QStringLiteral(
"drawingOrder" ) ).toInt();
65 if ( layerIndex == -1 )
67 layerIndexes.insert( layerIndex, layerFileElem.attribute( QStringLiteral(
"layerid" ) ) );
69 else if ( currentChildElem.tagName() == QLatin1String(
"legendgroup" ) )
71 if ( !_readOldLegendLayerOrderGroup( currentChildElem, layerIndexes ) )
82 if ( legendElem.isNull() )
85 hasCustomOrder = legendElem.attribute( QStringLiteral(
"updateDrawingOrder" ) ) == QLatin1String(
"false" );
88 QMap<int, QString> layerIndexes;
91 bool res = _readOldLegendLayerOrderGroup( legendElem, layerIndexes );
93 if ( !res && hasCustomOrder )
96 Q_FOREACH (
const QString &layerId, layerIndexes )
99 order.append( layerId );
106 static QDomElement _writeOldLegendLayer( QDomDocument &doc,
QgsLayerTreeLayer *nodeLayer,
bool hasCustomOrder,
const QList<QgsMapLayer *> &order )
108 int drawingOrder = -1;
109 if ( hasCustomOrder )
110 drawingOrder = order.indexOf( nodeLayer->
layer() );
112 QDomElement layerElem = doc.createElement( QStringLiteral(
"legendlayer" ) );
113 layerElem.setAttribute( QStringLiteral(
"drawingOrder" ), drawingOrder );
114 layerElem.setAttribute( QStringLiteral(
"open" ), nodeLayer->
isExpanded() ? QStringLiteral(
"true" ) : QStringLiteral(
"false" ) );
116 layerElem.setAttribute( QStringLiteral(
"name" ), nodeLayer->
name() );
117 layerElem.setAttribute( QStringLiteral(
"showFeatureCount" ), nodeLayer->
customProperty( QStringLiteral(
"showFeatureCount" ) ).toInt() );
119 QDomElement fileGroupElem = doc.createElement( QStringLiteral(
"filegroup" ) );
120 fileGroupElem.setAttribute( QStringLiteral(
"open" ), nodeLayer->
isExpanded() ?
"true" :
"false" );
121 fileGroupElem.setAttribute( QStringLiteral(
"hidden" ), QStringLiteral(
"false" ) );
123 QDomElement layerFileElem = doc.createElement( QStringLiteral(
"legendlayerfile" ) );
124 layerFileElem.setAttribute( QStringLiteral(
"isInOverview" ), nodeLayer->
customProperty( QStringLiteral(
"overview" ) ).toInt() );
125 layerFileElem.setAttribute( QStringLiteral(
"layerid" ), nodeLayer->
layerId() );
126 layerFileElem.setAttribute( QStringLiteral(
"visible" ), nodeLayer->
isVisible() ? 1 : 0 );
128 layerElem.appendChild( fileGroupElem );
129 fileGroupElem.appendChild( layerFileElem );
134 static void _writeOldLegendGroupChildren( QDomDocument &doc, QDomElement &groupElem,
QgsLayerTreeGroup *nodeGroup,
bool hasCustomOrder,
const QList<QgsMapLayer *> &order );
136 static QDomElement _writeOldLegendGroup( QDomDocument &doc,
QgsLayerTreeGroup *nodeGroup,
bool hasCustomOrder,
const QList<QgsMapLayer *> &order )
138 QDomElement groupElem = doc.createElement( QStringLiteral(
"legendgroup" ) );
139 groupElem.setAttribute( QStringLiteral(
"open" ), nodeGroup->
isExpanded() ?
"true" :
"false" );
140 groupElem.setAttribute( QStringLiteral(
"name" ), nodeGroup->
name() );
143 if ( nodeGroup->
customProperty( QStringLiteral(
"embedded" ) ).toInt() )
145 groupElem.setAttribute( QStringLiteral(
"embedded" ), 1 );
146 groupElem.setAttribute( QStringLiteral(
"project" ), nodeGroup->
customProperty( QStringLiteral(
"embedded_project" ) ).toString() );
149 _writeOldLegendGroupChildren( doc, groupElem, nodeGroup, hasCustomOrder, order );
154 static void _writeOldLegendGroupChildren( QDomDocument &doc, QDomElement &groupElem,
QgsLayerTreeGroup *nodeGroup,
bool hasCustomOrder,
const QList<QgsMapLayer *> &order )
160 groupElem.appendChild( _writeOldLegendGroup( doc,
QgsLayerTree::toGroup( node ), hasCustomOrder, order ) );
164 groupElem.appendChild( _writeOldLegendLayer( doc,
QgsLayerTree::toLayer( node ), hasCustomOrder, order ) );
172 QDomElement legendElem = doc.createElement( QStringLiteral(
"legend" ) );
173 legendElem.setAttribute( QStringLiteral(
"updateDrawingOrder" ), hasCustomOrder ? QStringLiteral(
"false" ) : QStringLiteral(
"true" ) );
175 _writeOldLegendGroupChildren( doc, legendElem, root, hasCustomOrder, order );
186 return QStringLiteral(
"Qt::Unchecked" );
187 case Qt::PartiallyChecked:
188 return QStringLiteral(
"Qt::PartiallyChecked" );
191 return QStringLiteral(
"Qt::Checked" );
197 if ( txt == QLatin1String(
"Qt::Unchecked" ) )
198 return Qt::Unchecked;
199 else if ( txt == QLatin1String(
"Qt::PartiallyChecked" ) )
200 return Qt::PartiallyChecked;
207 static void _readOldLegendGroup(
const QDomElement &groupElem,
QgsLayerTreeGroup *parent )
209 QDomNodeList groupChildren = groupElem.childNodes();
214 groupNode->
setExpanded( groupElem.attribute( QStringLiteral(
"open" ) ) == QLatin1String(
"true" ) );
216 if ( groupElem.attribute( QStringLiteral(
"embedded" ) ) == QLatin1String(
"1" ) )
219 groupNode->
setCustomProperty( QStringLiteral(
"embedded_project" ), groupElem.attribute( QStringLiteral(
"project" ) ) );
222 for (
int i = 0; i < groupChildren.size(); ++i )
224 QDomElement currentChildElem = groupChildren.at( i ).toElement();
225 if ( currentChildElem.tagName() == QLatin1String(
"legendlayer" ) )
227 _readOldLegendLayer( currentChildElem, groupNode );
229 else if ( currentChildElem.tagName() == QLatin1String(
"legendgroup" ) )
231 _readOldLegendGroup( currentChildElem, groupNode );
238 static void _readOldLegendLayer(
const QDomElement &layerElem,
QgsLayerTreeGroup *parent )
240 QDomElement layerFileElem = layerElem.firstChildElement( QStringLiteral(
"filegroup" ) ).firstChildElement( QStringLiteral(
"legendlayerfile" ) );
241 QString layerId = layerFileElem.attribute( QStringLiteral(
"layerid" ) );
245 layerNode->
setExpanded( layerElem.attribute( QStringLiteral(
"open" ) ) == QLatin1String(
"true" ) );
247 if ( layerFileElem.attribute( QStringLiteral(
"isInOverview" ) ) == QLatin1String(
"1" ) )
250 if ( layerElem.attribute( QStringLiteral(
"embedded" ) ) == QLatin1String(
"1" ) )
253 if ( layerElem.attribute( QStringLiteral(
"showFeatureCount" ) ) == QLatin1String(
"1" ) )
293 QList<QgsLayerTreeNode *> nodesToRemove;
301 nodesToRemove << node;
337 if ( child->
customProperty( QStringLiteral(
"embedded" ) ).toInt() )
355 if ( !node->
customProperty( QStringLiteral(
"embedded_project" ) ).toString().isEmpty() )
358 QString newPath = project->
writePath( node->
customProperty( QStringLiteral(
"embedded_project" ) ).toString() );
372 layer.
setCustomProperty( QStringLiteral(
"legend/expressionFilterEnabled" ), enabled );
378 *enabled = layer.
customProperty( QStringLiteral(
"legend/expressionFilterEnabled" ),
"" ).toBool();
379 return layer.
customProperty( QStringLiteral(
"legend/expressionFilter" ),
"" ).toString();
388 if ( exprEnabled && !expr.isEmpty() )
static bool readOldLegendLayerOrder(const QDomElement &legendElem, bool &hasCustomOrder, QStringList &order)
Try to load custom layer order from.
Layer tree group node serves as a container for layers and further groups.
static bool layersEditable(const QList< QgsLayerTreeLayer *> &layerNodes)
Returns true if any of the layers is editable.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Base class for all map layer types.
static void removeInvalidLayers(QgsLayerTreeGroup *group)
Remove layer nodes that refer to invalid layers.
static QString checkStateToXml(Qt::CheckState state)
Convert Qt::CheckState to QString.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
void removeAllChildren()
Remove all child nodes.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
static bool readOldLegend(QgsLayerTreeGroup *root, const QDomElement &legendElem)
Try to load layer tree from.
static bool layersModified(const QList< QgsLayerTreeLayer *> &layerNodes)
Returns true if any of the layers is modified.
void removeChildNode(QgsLayerTreeNode *node)
Remove a child node from this group.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well) ...
bool isExpanded() const
Returns whether the node should be shown as expanded or collapsed in GUI.
bool isEditable() const override
Returns true if the provider is in editing mode.
QString name() const override
Returns the layer's name.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
static QDomElement writeOldLegend(QDomDocument &doc, QgsLayerTreeGroup *root, bool hasCustomOrder, const QList< QgsMapLayer *> &order)
Returns.
QgsLayerTreeLayer * insertLayer(int index, QgsMapLayer *layer)
Insert a new layer node for given map layer at specified position.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is a null pointer, the node is a root node. ...
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file...
static void replaceChildrenOfEmbeddedGroups(QgsLayerTreeGroup *group)
Remove subtree of embedded groups and replaces it with a custom property embedded-visible-layers.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
static Qt::CheckState checkStateFromXml(const QString &txt)
Convert QString to Qt::CheckState.
Reads and writes project states.
static bool hasLegendFilterExpression(const QgsLayerTreeGroup &group)
Test if one of the layers in a group has an expression filter.
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
QgsMapLayer * layer() const
Leaf node pointing to a layer.
static QgsLayerTreeLayer * insertLayerBelow(QgsLayerTreeGroup *group, const QgsMapLayer *refLayer, QgsMapLayer *layerToInsert)
Insert a QgsMapLayer just below another one.
static void setLegendFilterByExpression(QgsLayerTreeLayer &layer, const QString &expr, bool enabled=true)
Sets the expression filter of a legend layer.
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
void addChildNode(QgsLayerTreeNode *node)
Append an existing node.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
QString name() const override
Returns the group's name.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
Represents a vector layer which manages a vector based data sets.
static QStringList invisibleLayerList(QgsLayerTreeNode *node)
Gets invisible layers.
static QString legendFilterByExpression(const QgsLayerTreeLayer &layer, bool *enabled=nullptr)
Returns the expression filter of a legend layer.
static void updateEmbeddedGroupsProjectPath(QgsLayerTreeGroup *group, const QgsProject *project)
Updates an embedded group from a project.
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file...
Layer tree node points to a map layer.