QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsmaplayerlistutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayerlistutils.h
3  ---------------------
4  begin : December 2016
5  copyright : (C) 2016 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSMAPLAYERLISTUTILS_H
16 #define QGSMAPLAYERLISTUTILS_H
17 
18 //
19 // W A R N I N G
20 // -------------
21 //
22 // This file is not part of the QGIS API. It exists purely as an
23 // implementation detail. This header file may change from version to
24 // version without notice, or even be removed.
25 //
26 
27 #include <QPointer>
28 
29 #include "qgsmaplayer.h"
30 #include "qgsmaplayerref.h"
31 
33 
34 inline QList<QgsMapLayer *> _qgis_listQPointerToRaw( const QgsWeakMapLayerPointerList &layers )
35 {
36  QList<QgsMapLayer *> lst;
37  lst.reserve( layers.count() );
38  for ( const QgsWeakMapLayerPointer &layerPtr : layers )
39  {
40  if ( layerPtr )
41  lst.append( layerPtr.data() );
42  }
43  return lst;
44 }
45 
46 inline QgsWeakMapLayerPointerList _qgis_listRawToQPointer( const QList<QgsMapLayer *> &layers )
47 {
49  lst.reserve( layers.count() );
50  for ( QgsMapLayer *layer : layers )
51  {
52  lst.append( layer );
53  }
54  return lst;
55 }
56 
57 inline QList<QgsMapLayer *> _qgis_listRefToRaw( const QList< QgsMapLayerRef > &layers )
58 {
59  QList<QgsMapLayer *> lst;
60  lst.reserve( layers.count() );
61  for ( const QgsMapLayerRef &layer : layers )
62  {
63  if ( layer )
64  lst.append( layer.get() );
65  }
66  return lst;
67 }
68 
69 inline QList< QgsMapLayerRef > _qgis_listRawToRef( const QList<QgsMapLayer *> &layers )
70 {
71  QList< QgsMapLayerRef > lst;
72  lst.reserve( layers.count() );
73  for ( QgsMapLayer *layer : layers )
74  {
75  lst.append( QgsMapLayerRef( layer ) );
76  }
77  return lst;
78 }
79 
80 inline void _qgis_removeLayers( QList< QgsMapLayerRef > &list, const QList< QgsMapLayer *> &layersToRemove )
81 {
82  QMutableListIterator<QgsMapLayerRef> it( list );
83  while ( it.hasNext() )
84  {
85  QgsMapLayerRef &ref = it.next();
86  if ( layersToRemove.contains( ref.get() ) )
87  it.remove();
88  }
89 }
90 
91 inline QStringList _qgis_listQPointerToIDs( const QgsWeakMapLayerPointerList &layers )
92 {
93  QStringList lst;
94  lst.reserve( layers.count() );
95  for ( const QgsWeakMapLayerPointer &layerPtr : layers )
96  {
97  if ( layerPtr )
98  lst << layerPtr->id();
99  }
100  return lst;
101 }
102 
103 inline static QgsMapLayer *_qgis_findLayer( const QList< QgsMapLayer *> &layers, const QString &identifier )
104 {
105  QgsMapLayer *matchId = nullptr;
106  QgsMapLayer *matchName = nullptr;
107  QgsMapLayer *matchNameInsensitive = nullptr;
108 
109  // Look for match against layer IDs
110  for ( QgsMapLayer *layer : layers )
111  {
112  if ( !matchId && layer->id() == identifier )
113  {
114  matchId = layer;
115  break;
116  }
117  if ( !matchName && layer->name() == identifier )
118  {
119  matchName = layer;
120  }
121  if ( !matchNameInsensitive && QString::compare( layer->name(), identifier, Qt::CaseInsensitive ) == 0 )
122  {
123  matchNameInsensitive = layer;
124  }
125  }
126 
127  if ( matchId )
128  {
129  return matchId;
130  }
131  else if ( matchName )
132  {
133  return matchName;
134  }
135  else if ( matchNameInsensitive )
136  {
137  return matchNameInsensitive;
138  }
139  else
140  {
141  return nullptr;
142  }
143 }
144 
145 inline uint qHash( const QgsWeakMapLayerPointer &key )
146 {
147  return qHash( key ? key->id() : QString() );
148 }
149 
151 
152 #endif // QGSMAPLAYERLISTUTILS_H
Base class for all map layer types.
Definition: qgsmaplayer.h:79
_LayerRef< QgsMapLayer > QgsMapLayerRef
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1632
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition: qgis.cpp:252
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:1639
QString name
Definition: qgsmaplayer.h:83
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer...