QGIS API Documentation  2.12.0-Lyon
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 
25 const int QgsMapLayerModel::LayerIdRole = Qt::UserRole + 1;
26 
28  : QAbstractItemModel( parent )
29  , mLayersChecked( QMap<QString, Qt::CheckState>() )
30  , mItemCheckable( false )
31 {
32  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
33  addLayers( layers );
34 }
35 
37  : QAbstractItemModel( parent )
38  , mLayersChecked( QMap<QString, Qt::CheckState>() )
39  , mItemCheckable( false )
40 {
41  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
42  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
43  addLayers( QgsMapLayerRegistry::instance()->mapLayers().values() );
44 }
45 
47 {
48  mItemCheckable = checkable;
49 }
50 
51 void QgsMapLayerModel::checkAll( Qt::CheckState checkState )
52 {
53  Q_FOREACH ( const QString& key, mLayersChecked.keys() )
54  {
55  mLayersChecked[key] = checkState;
56  }
57  emit dataChanged( index( 0, 0 ), index( mLayers.length() - 1, 0 ) );
58 }
59 
61 {
62  QList<QgsMapLayer *> layers;
63  Q_FOREACH ( QgsMapLayer* layer, mLayers )
64  {
65  if ( mLayersChecked[layer->id()] == checkState )
66  {
67  layers.append( layer );
68  }
69  }
70  return layers;
71 }
72 
74 {
75  int r = mLayers.indexOf( layer );
76  return index( r, 0 );
77 }
78 
80 {
81  Q_FOREACH ( const QString& layerId, layerIds )
82  {
83  QModelIndex startIndex = index( 0, 0 );
84  QModelIndexList list = match( startIndex, LayerIdRole, layerId, 1 );
85  if ( list.count() )
86  {
87  QModelIndex index = list[0];
88  beginRemoveRows( QModelIndex(), index.row(), index.row() );
89  mLayersChecked.remove( layerId );
90  mLayers.removeAt( index.row() );
91  endRemoveRows();
92  }
93  }
94 }
95 
97 {
98  beginInsertRows( QModelIndex(), mLayers.count(), mLayers.count() + layers.count() - 1 );
99  Q_FOREACH ( QgsMapLayer* layer, layers )
100  {
101  mLayers.append( layer );
102  mLayersChecked.insert( layer->id(), Qt::Unchecked );
103  }
104  endInsertRows();
105 }
106 
107 QModelIndex QgsMapLayerModel::index( int row, int column, const QModelIndex &parent ) const
108 {
109  if ( hasIndex( row, column, parent ) )
110  {
111  return createIndex( row, column, mLayers[row] );
112  }
113 
114  return QModelIndex();
115 
116 }
117 
119 {
120  Q_UNUSED( child );
121  return QModelIndex();
122 }
123 
124 
125 int QgsMapLayerModel::rowCount( const QModelIndex &parent ) const
126 {
127  return parent.isValid() ? 0 : mLayers.length();
128 }
129 
130 int QgsMapLayerModel::columnCount( const QModelIndex &parent ) const
131 {
132  Q_UNUSED( parent );
133  return 1;
134 }
135 
136 
138 {
139  if ( !index.isValid() || !index.internalPointer() )
140  return QVariant();
141 
142  if ( role == Qt::DisplayRole )
143  {
144  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
145  return layer->name();
146  }
147 
148  if ( role == LayerIdRole )
149  {
150  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
151  return layer->id();
152  }
153 
154  if ( role == Qt::CheckStateRole && mItemCheckable )
155  {
156  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
157  return mLayersChecked[layer->id()];
158  }
159 
160  if ( role == Qt::DecorationRole )
161  {
162  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
163  QgsMapLayer::LayerType type = layer->type();
164  if ( role == Qt::DecorationRole )
165  {
166  switch ( type )
167  {
169  {
170  return QgsLayerItem::iconRaster();
171  }
172 
174  {
175  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
176  if ( !vl )
177  {
178  return QIcon();
179  }
180  QGis::GeometryType geomType = vl->geometryType();
181  switch ( geomType )
182  {
183  case QGis::Point:
184  {
185  return QgsLayerItem::iconPoint();
186  }
187  case QGis::Polygon :
188  {
189  return QgsLayerItem::iconPolygon();
190  }
191  case QGis::Line :
192  {
193  return QgsLayerItem::iconLine();
194  }
195  case QGis::NoGeometry :
196  {
197  return QgsLayerItem::iconTable();
198  }
199  default:
200  {
201  return QIcon();
202  }
203  }
204  }
205  default:
206  {
207  return QIcon();
208  }
209  }
210  }
211  }
212 
213  return QVariant();
214 }
215 
216 
218 {
219  if ( !index.isValid() )
220  {
221  return 0;
222  }
223 
224  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
225  if ( mItemCheckable )
226  {
227  flags |= Qt::ItemIsUserCheckable;
228  }
229  return flags;
230 }
231 
232 
233 bool QgsMapLayerModel::setData( const QModelIndex &index, const QVariant &value, int role )
234 {
235  if ( role == Qt::CheckStateRole )
236  {
237  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
238  mLayersChecked[layer->id()] = ( Qt::CheckState )value.toInt();
239  emit dataChanged( index, index );
240  return true;
241  }
242 
243  return false;
244 }
bool hasIndex(int row, int column, const QModelIndex &parent) const
QVariant data(const QModelIndex &index, int role) const override
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:94
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:55
static const QIcon & iconPoint()
Definition: qgsdataitem.cpp:98
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
void removeAt(int i)
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QModelIndex indexFromLayer(QgsMapLayer *layer) const
indexFromLayer returns the model index for a given layer
GeometryType
Definition: qgis.h:104
static const QIcon & iconPolygon()
void setItemsCheckable(bool checkable)
setItemsCheckable defines if layers should be selectable in the widget
QgsMapLayerModel(QObject *parent=0)
QgsMapLayerModel creates a model to display layers in widgets.
int indexOf(const T &value, int from) const
const QString & name() const
Get the display name of the layer.
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)
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()
void addLayers(const QList< QgsMapLayer * > &layers)
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
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
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 int LayerIdRole
static const QIcon & iconLine()
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