QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsmaplayermodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayermodel.cpp
3  --------------------------------------
4  Date : 01.04.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
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 
16 #include <QIcon>
17 
18 #include "qgsdataitem.h"
19 #include "qgsmaplayermodel.h"
20 #include "qgsmaplayerregistry.h"
21 #include "qgsapplication.h"
22 #include "qgsvectorlayer.h"
23 
24 
26  : QAbstractItemModel( parent )
27  , mLayersChecked( QMap<QString, Qt::CheckState>() )
28  , mItemCheckable( false )
29 {
30  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
31  addLayers( layers );
32 }
33 
35  : QAbstractItemModel( parent )
36  , mLayersChecked( QMap<QString, Qt::CheckState>() )
37  , mItemCheckable( false )
38 {
39  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
40  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
41  addLayers( QgsMapLayerRegistry::instance()->mapLayers().values() );
42 }
43 
45 {
46  mItemCheckable = checkable;
47 }
48 
49 void QgsMapLayerModel::checkAll( Qt::CheckState checkState )
50 {
51  Q_FOREACH ( const QString& key, mLayersChecked.keys() )
52  {
53  mLayersChecked[key] = checkState;
54  }
55  emit dataChanged( index( 0, 0 ), index( mLayers.length() - 1, 0 ) );
56 }
57 
59 {
60  QList<QgsMapLayer *> layers;
61  Q_FOREACH ( QgsMapLayer* layer, mLayers )
62  {
63  if ( mLayersChecked[layer->id()] == checkState )
64  {
65  layers.append( layer );
66  }
67  }
68  return layers;
69 }
70 
72 {
73  int r = mLayers.indexOf( layer );
74  return index( r, 0 );
75 }
76 
78 {
79  Q_FOREACH ( const QString& layerId, layerIds )
80  {
81  QModelIndex startIndex = index( 0, 0 );
82  QModelIndexList list = match( startIndex, LayerIdRole, layerId, 1 );
83  if ( !list.isEmpty() )
84  {
85  QModelIndex index = list[0];
86  beginRemoveRows( QModelIndex(), index.row(), index.row() );
87  mLayersChecked.remove( layerId );
88  mLayers.removeAt( index.row() );
89  endRemoveRows();
90  }
91  }
92 }
93 
95 {
96  beginInsertRows( QModelIndex(), mLayers.count(), mLayers.count() + layers.count() - 1 );
97  Q_FOREACH ( QgsMapLayer* layer, layers )
98  {
99  mLayers.append( layer );
100  mLayersChecked.insert( layer->id(), Qt::Unchecked );
101  }
102  endInsertRows();
103 }
104 
105 QModelIndex QgsMapLayerModel::index( int row, int column, const QModelIndex &parent ) const
106 {
107  if ( hasIndex( row, column, parent ) )
108  {
109  return createIndex( row, column, mLayers[row] );
110  }
111 
112  return QModelIndex();
113 
114 }
115 
117 {
118  Q_UNUSED( child );
119  return QModelIndex();
120 }
121 
122 
124 {
125  return parent.isValid() ? 0 : mLayers.length();
126 }
127 
129 {
130  Q_UNUSED( parent );
131  return 1;
132 }
133 
134 
136 {
137  if ( !index.isValid() || !index.internalPointer() )
138  return QVariant();
139 
140  if ( role == Qt::DisplayRole )
141  {
142  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
143  return layer->name();
144  }
145 
146  if ( role == LayerIdRole )
147  {
148  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
149  return layer->id();
150  }
151 
152  if ( role == LayerRole )
153  {
154  return QVariant::fromValue<QgsMapLayer*>( static_cast<QgsMapLayer*>( index.internalPointer() ) );
155  }
156 
157  if ( role == Qt::CheckStateRole && mItemCheckable )
158  {
159  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
160  return mLayersChecked[layer->id()];
161  }
162 
163  if ( role == Qt::DecorationRole )
164  {
165  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
166  QgsMapLayer::LayerType type = layer->type();
167  if ( role == Qt::DecorationRole )
168  {
169  switch ( type )
170  {
172  {
173  return QgsLayerItem::iconRaster();
174  }
175 
177  {
178  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
179  if ( !vl )
180  {
181  return QIcon();
182  }
183  QGis::GeometryType geomType = vl->geometryType();
184  switch ( geomType )
185  {
186  case QGis::Point:
187  {
188  return QgsLayerItem::iconPoint();
189  }
190  case QGis::Polygon :
191  {
192  return QgsLayerItem::iconPolygon();
193  }
194  case QGis::Line :
195  {
196  return QgsLayerItem::iconLine();
197  }
198  case QGis::NoGeometry :
199  {
200  return QgsLayerItem::iconTable();
201  }
202  default:
203  {
204  return QIcon();
205  }
206  }
207  }
208  default:
209  {
210  return QIcon();
211  }
212  }
213  }
214  }
215 
216  return QVariant();
217 }
218 
219 #if QT_VERSION >= 0x050000
221 {
223  roles[LayerIdRole] = "layerId";
224  roles[LayerRole] = "layer";
225 
226  return roles;
227 }
228 #endif
229 
231 {
232  if ( !index.isValid() )
233  {
234  return nullptr;
235  }
236 
237  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
238  if ( mItemCheckable )
239  {
240  flags |= Qt::ItemIsUserCheckable;
241  }
242  return flags;
243 }
244 
245 
246 bool QgsMapLayerModel::setData( const QModelIndex &index, const QVariant &value, int role )
247 {
248  if ( role == Qt::CheckStateRole )
249  {
250  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
251  mLayersChecked[layer->id()] = ( Qt::CheckState )value.toInt();
252  emit dataChanged( index, index );
253  return true;
254  }
255 
256  return false;
257 }
bool hasIndex(int row, int column, const QModelIndex &parent) const
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
QVariant data(const QModelIndex &index, int role) const override
QModelIndex indexFromLayer(QgsMapLayer *layer) const
indexFromLayer returns the model index for a given layer
Base class for all map layer types.
Definition: qgsmaplayer.h:49
GeometryType
Definition: qgis.h:115
static const QIcon & iconPoint()
Definition: qgsdataitem.cpp:99
int length() const
QList< QgsMapLayer * > mLayers
int columnCount(const QModelIndex &parent) const override
void checkAll(Qt::CheckState checkState)
checkAll changes the checkstate for all the layers
const QHash< int, QByteArray > & roleNames() const
void removeAt(int i)
bool setData(const QModelIndex &index, const QVariant &value, int role) override
static const QIcon & iconPolygon()
void setItemsCheckable(bool checkable)
setItemsCheckable defines if layers should be selectable in the widget
QgsMapLayerModel(QObject *parent=nullptr)
QgsMapLayerModel creates a model to display layers in widgets.
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:99
void addLayers(const QList< QgsMapLayer *> &layers)
int indexOf(const T &value, int from) const
QList< Key > keys() const
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, QFlags< Qt::MatchFlag > flags) const
void removeLayers(const QStringList &layerIds)
bool isValid() const
int count(const T &value) const
void append(const T &value)
QString id() const
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
int toInt(bool *ok) const
Qt::ItemFlags flags(const QModelIndex &index) const override
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
static const QIcon & iconRaster()
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:57
void beginRemoveRows(const QModelIndex &parent, int first, int last)
int row() const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QMap< QString, Qt::CheckState > mLayersChecked
void * internalPointer() const
QGis::GeometryType geometryType() const
Returns point, line or polygon.
int rowCount(const QModelIndex &parent) const override
QModelIndex createIndex(int row, int column, void *ptr) const
void beginInsertRows(const QModelIndex &parent, int first, int last)
static const QIcon & iconTable()
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
static const QIcon & iconLine()
QString name
Read property of QString layerName.
Definition: qgsmaplayer.h:53
iterator insert(const Key &key, const T &value)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QList< QgsMapLayer * > layersChecked(Qt::CheckState checkState=Qt::Checked)
layersChecked returns the list of layers which are checked (or unchecked)
int remove(const Key &key)
typedef ItemFlags