File: qgslayertreeutils.cpp

package info (click to toggle)
qgis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 374,696 kB
  • ctags: 66,263
  • sloc: cpp: 396,139; ansic: 241,070; python: 130,609; xml: 14,884; perl: 1,290; sh: 1,287; sql: 500; yacc: 268; lex: 242; makefile: 168
file content (338 lines) | stat: -rw-r--r-- 11,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/***************************************************************************
  qgslayertreeutils.cpp
  --------------------------------------
  Date                 : May 2014
  Copyright            : (C) 2014 by Martin Dobias
  Email                : wonder dot sk at gmail dot com
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "qgslayertreeutils.h"

#include "qgslayertree.h"

#include "qgsvectorlayer.h"

#include "qgsproject.h"

#include <QDomElement>


static void _readOldLegendGroup( const QDomElement& groupElem, QgsLayerTreeGroup* parent );
static void _readOldLegendLayer( const QDomElement& layerElem, QgsLayerTreeGroup* parent );

bool QgsLayerTreeUtils::readOldLegend( QgsLayerTreeGroup* root, const QDomElement& legendElem )
{
  if ( legendElem.isNull() )
    return false;

  QDomNodeList legendChildren = legendElem.childNodes();

  for ( int i = 0; i < legendChildren.size(); ++i )
  {
    QDomElement currentChildElem = legendChildren.at( i ).toElement();
    if ( currentChildElem.tagName() == "legendlayer" )
    {
      _readOldLegendLayer( currentChildElem, root );
    }
    else if ( currentChildElem.tagName() == "legendgroup" )
    {
      _readOldLegendGroup( currentChildElem, root );
    }
  }

  return true;
}



static bool _readOldLegendLayerOrderGroup( const QDomElement& groupElem, QMap<int, QString>& layerIndexes )
{
  QDomNodeList legendChildren = groupElem.childNodes();

  for ( int i = 0; i < legendChildren.size(); ++i )
  {
    QDomElement currentChildElem = legendChildren.at( i ).toElement();
    if ( currentChildElem.tagName() == "legendlayer" )
    {
      QDomElement layerFileElem = currentChildElem.firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" );

      int layerIndex = currentChildElem.attribute( "drawingOrder" ).toInt();
      if ( layerIndex == -1 )
        return false; // order undefined
      layerIndexes.insert( layerIndex, layerFileElem.attribute( "layerid" ) );
    }
    else if ( currentChildElem.tagName() == "legendgroup" )
    {
      if ( !_readOldLegendLayerOrderGroup( currentChildElem, layerIndexes ) )
        return false;
    }
  }

  return true;
}


bool QgsLayerTreeUtils::readOldLegendLayerOrder( const QDomElement& legendElem, bool& hasCustomOrder, QStringList& order )
{
  if ( legendElem.isNull() )
    return false;

  hasCustomOrder = legendElem.attribute( "updateDrawingOrder" ) == "false";
  order.clear();

  QMap<int, QString> layerIndexes;

  // try to read the order. may be undefined (order = -1) for some or all items
  bool res = _readOldLegendLayerOrderGroup( legendElem, layerIndexes );

  if ( !res && hasCustomOrder )
    return false; // invalid state

  foreach ( QString layerId, layerIndexes )
  {
    QgsDebugMsg( layerId );
    order.append( layerId );
  }

  return true;
}


static QDomElement _writeOldLegendLayer( QDomDocument& doc, QgsLayerTreeLayer* nodeLayer, bool hasCustomOrder, const QStringList& order )
{
  int drawingOrder = -1;
  if ( hasCustomOrder )
    drawingOrder = order.indexOf( nodeLayer->layerId() );

  QDomElement layerElem = doc.createElement( "legendlayer" );
  layerElem.setAttribute( "drawingOrder", drawingOrder );
  layerElem.setAttribute( "open", nodeLayer->isExpanded() ? "true" : "false" );
  layerElem.setAttribute( "checked", QgsLayerTreeUtils::checkStateToXml( nodeLayer->isVisible() ) );
  layerElem.setAttribute( "name", nodeLayer->layerName() );
  layerElem.setAttribute( "showFeatureCount", nodeLayer->customProperty( "showFeatureCount" ).toInt() );

  QDomElement fileGroupElem = doc.createElement( "filegroup" );
  fileGroupElem.setAttribute( "open", nodeLayer->isExpanded() ? "true" : "false" );
  fileGroupElem.setAttribute( "hidden", "false" );

  QDomElement layerFileElem = doc.createElement( "legendlayerfile" );
  layerFileElem.setAttribute( "isInOverview", nodeLayer->customProperty( "overview" ).toInt() );
  layerFileElem.setAttribute( "layerid", nodeLayer->layerId() );
  layerFileElem.setAttribute( "visible", nodeLayer->isVisible() == Qt::Checked ? 1 : 0 );

  layerElem.appendChild( fileGroupElem );
  fileGroupElem.appendChild( layerFileElem );
  return layerElem;
}

// need forward declaration as write[..]Group and write[..]GroupChildren call each other
static void _writeOldLegendGroupChildren( QDomDocument& doc, QDomElement& groupElem, QgsLayerTreeGroup* nodeGroup, bool hasCustomOrder, const QStringList& order );

static QDomElement _writeOldLegendGroup( QDomDocument& doc, QgsLayerTreeGroup* nodeGroup, bool hasCustomOrder, const QStringList& order )
{
  QDomElement groupElem = doc.createElement( "legendgroup" );
  groupElem.setAttribute( "open", nodeGroup->isExpanded() ? "true" : "false" );
  groupElem.setAttribute( "name", nodeGroup->name() );
  groupElem.setAttribute( "checked", QgsLayerTreeUtils::checkStateToXml( nodeGroup->isVisible() ) );

  if ( nodeGroup->customProperty( "embedded" ).toInt() )
  {
    groupElem.setAttribute( "embedded", 1 );
    groupElem.setAttribute( "project", nodeGroup->customProperty( "embedded_project" ).toString() );
  }

  _writeOldLegendGroupChildren( doc, groupElem, nodeGroup, hasCustomOrder, order );
  return groupElem;
}


static void _writeOldLegendGroupChildren( QDomDocument& doc, QDomElement& groupElem, QgsLayerTreeGroup* nodeGroup, bool hasCustomOrder, const QStringList& order )
{
  foreach ( QgsLayerTreeNode* node, nodeGroup->children() )
  {
    if ( QgsLayerTree::isGroup( node ) )
    {
      groupElem.appendChild( _writeOldLegendGroup( doc, QgsLayerTree::toGroup( node ), hasCustomOrder, order ) );
    }
    else if ( QgsLayerTree::isLayer( node ) )
    {
      groupElem.appendChild( _writeOldLegendLayer( doc, QgsLayerTree::toLayer( node ), hasCustomOrder, order ) );
    }
  }
}


QDomElement QgsLayerTreeUtils::writeOldLegend( QDomDocument& doc, QgsLayerTreeGroup* root, bool hasCustomOrder, const QStringList& order )
{
  QDomElement legendElem = doc.createElement( "legend" );
  legendElem.setAttribute( "updateDrawingOrder", hasCustomOrder ? "false" : "true" );

  _writeOldLegendGroupChildren( doc, legendElem, root, hasCustomOrder, order );

  return legendElem;
}


QString QgsLayerTreeUtils::checkStateToXml( Qt::CheckState state )
{
  switch ( state )
  {
    case Qt::Unchecked:        return "Qt::Unchecked";
    case Qt::PartiallyChecked: return "Qt::PartiallyChecked";
  case Qt::Checked: default: return "Qt::Checked";
  }
}

Qt::CheckState QgsLayerTreeUtils::checkStateFromXml( QString txt )
{
  if ( txt == "Qt::Unchecked" )
    return Qt::Unchecked;
  else if ( txt == "Qt::PartiallyChecked" )
    return Qt::PartiallyChecked;
  else // "Qt::Checked"
    return Qt::Checked;
}



static void _readOldLegendGroup( const QDomElement& groupElem, QgsLayerTreeGroup* parent )
{
  QDomNodeList groupChildren = groupElem.childNodes();

  QgsLayerTreeGroup* groupNode = new QgsLayerTreeGroup( groupElem.attribute( "name" ) );

  groupNode->setVisible( QgsLayerTreeUtils::checkStateFromXml( groupElem.attribute( "checked" ) ) );
  groupNode->setExpanded( groupElem.attribute( "open" ) == "true" );

  if ( groupElem.attribute( "embedded" ) == "1" )
  {
    groupNode->setCustomProperty( "embedded", 1 );
    groupNode->setCustomProperty( "embedded_project", groupElem.attribute( "project" ) );
  }

  for ( int i = 0; i < groupChildren.size(); ++i )
  {
    QDomElement currentChildElem = groupChildren.at( i ).toElement();
    if ( currentChildElem.tagName() == "legendlayer" )
    {
      _readOldLegendLayer( currentChildElem, groupNode );
    }
    else if ( currentChildElem.tagName() == "legendgroup" )
    {
      _readOldLegendGroup( currentChildElem, groupNode );
    }
  }

  parent->addChildNode( groupNode );
}

static void _readOldLegendLayer( const QDomElement& layerElem, QgsLayerTreeGroup* parent )
{
  QDomElement layerFileElem = layerElem.firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" );
  QString layerId = layerFileElem.attribute( "layerid" );
  QgsLayerTreeLayer* layerNode = new QgsLayerTreeLayer( layerId, layerElem.attribute( "name" ) );

  layerNode->setVisible( QgsLayerTreeUtils::checkStateFromXml( layerElem.attribute( "checked" ) ) );
  layerNode->setExpanded( layerElem.attribute( "open" ) == "true" );

  if ( layerFileElem.attribute( "isInOverview" ) == "1" )
    layerNode->setCustomProperty( "overview", 1 );

  if ( layerElem.attribute( "embedded" ) == "1" )
    layerNode->setCustomProperty( "embedded", 1 );

  if ( layerElem.attribute( "showFeatureCount" ) == "1" )
    layerNode->setCustomProperty( "showFeatureCount", 1 );

  // drawing order is handled by readOldLegendLayerOrder()

  parent->addChildNode( layerNode );
}



bool QgsLayerTreeUtils::layersEditable( const QList<QgsLayerTreeLayer*>& layerNodes )
{
  foreach ( QgsLayerTreeLayer* layerNode, layerNodes )
  {
    QgsVectorLayer *vl = qobject_cast<QgsVectorLayer*>( layerNode->layer() );
    if ( !vl )
      continue;

    if ( vl->isEditable() )
      return true;
  }
  return false;
}

bool QgsLayerTreeUtils::layersModified( const QList<QgsLayerTreeLayer*>& layerNodes )
{
  foreach ( QgsLayerTreeLayer* layerNode, layerNodes )
  {
    QgsVectorLayer *vl = qobject_cast<QgsVectorLayer*>( layerNode->layer() );
    if ( !vl )
      continue;

    if ( vl->isEditable() && vl->isModified() )
      return true;
  }
  return false;
}

void QgsLayerTreeUtils::removeInvalidLayers( QgsLayerTreeGroup* group )
{
  QList<QgsLayerTreeNode*> nodesToRemove;
  foreach ( QgsLayerTreeNode* node, group->children() )
  {
    if ( QgsLayerTree::isGroup( node ) )
      removeInvalidLayers( QgsLayerTree::toGroup( node ) );
    else if ( QgsLayerTree::isLayer( node ) )
    {
      if ( !QgsLayerTree::toLayer( node )->layer() )
        nodesToRemove << node;
    }
  }

  foreach ( QgsLayerTreeNode* node, nodesToRemove )
    group->removeChildNode( node );
}

void QgsLayerTreeUtils::removeChildrenOfEmbeddedGroups( QgsLayerTreeGroup* group )
{
  foreach ( QgsLayerTreeNode* child, group->children() )
  {
    if ( QgsLayerTree::isGroup( child ) )
    {
      if ( child->customProperty( "embedded" ).toInt() )
        QgsLayerTree::toGroup( child )->removeAllChildren();
      else
        removeChildrenOfEmbeddedGroups( QgsLayerTree::toGroup( child ) );
    }
  }
}


void QgsLayerTreeUtils::updateEmbeddedGroupsProjectPath( QgsLayerTreeGroup* group )
{
  foreach ( QgsLayerTreeNode* node, group->children() )
  {
    if ( !node->customProperty( "embedded_project" ).toString().isEmpty() )
    {
      // may change from absolute path to relative path
      QString newPath = QgsProject::instance()->writePath( node->customProperty( "embedded_project" ).toString() );
      node->setCustomProperty( "embedded_project", newPath );
    }

    if ( QgsLayerTree::isGroup( node ) )
    {
        updateEmbeddedGroupsProjectPath( QgsLayerTree::toGroup( node ) );
    }
  }
}