QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "qgsmaplayermodel.h"
19 #include "qgsmaplayerregistry.h"
20 #include "qgsapplication.h"
21 #include "qgsvectorlayer.h"
22 
23 
24 const int QgsMapLayerModel::LayerIdRole = Qt::UserRole + 1;
25 
26 QgsMapLayerModel::QgsMapLayerModel( QList<QgsMapLayer *> layers, QObject *parent )
27  : QAbstractItemModel( parent )
28  , mLayersChecked( QMap<QString, Qt::CheckState>() )
29  , mItemCheckable( false )
30 {
31  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
32  addLayers( layers );
33 }
34 
36  : QAbstractItemModel( parent )
37  , mLayersChecked( QMap<QString, Qt::CheckState>() )
38  , mItemCheckable( false )
39 {
40  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
41  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
42  addLayers( QgsMapLayerRegistry::instance()->mapLayers().values() );
43 }
44 
46 {
47  mItemCheckable = checkable;
48 }
49 
50 void QgsMapLayerModel::checkAll( Qt::CheckState checkState )
51 {
52  foreach ( const QString key, mLayersChecked.keys() )
53  {
54  mLayersChecked[key] = checkState;
55  }
56  emit dataChanged( index( 0, 0 ), index( mLayers.length() - 1, 0 ) );
57 }
58 
59 QList<QgsMapLayer *> QgsMapLayerModel::layersChecked( Qt::CheckState checkState )
60 {
61  QList<QgsMapLayer *> layers;
62  foreach ( QgsMapLayer* layer, mLayers )
63  {
64  if ( mLayersChecked[layer->id()] == checkState )
65  {
66  layers.append( layer );
67  }
68  }
69  return layers;
70 }
71 
73 {
74  int r = mLayers.indexOf( layer );
75  return index( r, 0 );
76 }
77 
78 void QgsMapLayerModel::removeLayers( const QStringList layerIds )
79 {
80  foreach ( const QString layerId, layerIds )
81  {
82  QModelIndex startIndex = index( 0, 0 );
83  QModelIndexList list = match( startIndex, LayerIdRole, layerId, 1 );
84  if ( list.count() )
85  {
86  QModelIndex index = list[0];
87  beginRemoveRows( QModelIndex(), index.row(), index.row() );
88  mLayersChecked.remove( layerId );
89  mLayers.removeAt( index.row() );
90  endRemoveRows();
91  }
92  }
93 }
94 
95 void QgsMapLayerModel::addLayers( QList<QgsMapLayer *> layers )
96 {
97  beginInsertRows( QModelIndex(), mLayers.count(), mLayers.count() + layers.count() - 1 );
98  foreach ( QgsMapLayer* layer, layers )
99  {
100  mLayers.append( layer );
101  mLayersChecked.insert( layer->id(), Qt::Unchecked );
102  }
103  endInsertRows();
104 }
105 
106 QModelIndex QgsMapLayerModel::index( int row, int column, const QModelIndex &parent ) const
107 {
108  if ( hasIndex( row, column, parent ) )
109  {
110  return createIndex( row, column, mLayers[row] );
111  }
112 
113  return QModelIndex();
114 
115 }
116 
117 QModelIndex QgsMapLayerModel::parent( const QModelIndex &child ) const
118 {
119  Q_UNUSED( child );
120  return QModelIndex();
121 }
122 
123 
124 int QgsMapLayerModel::rowCount( const QModelIndex &parent ) const
125 {
126  return parent.isValid() ? 0 : mLayers.length();
127 }
128 
129 int QgsMapLayerModel::columnCount( const QModelIndex &parent ) const
130 {
131  Q_UNUSED( parent );
132  return 1;
133 }
134 
135 QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
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 == Qt::CheckStateRole && mItemCheckable )
153  {
154  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
155  return mLayersChecked[layer->id()];
156  }
157 
158  if ( role == Qt::DecorationRole )
159  {
160  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
161  QgsMapLayer::LayerType type = layer->type();
162  if ( role == Qt::DecorationRole )
163  {
164  switch ( type )
165  {
167  {
168  return QgsApplication::getThemeIcon( "/mIconRasterLayer.svg" );
169  }
170 
172  {
173  QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
174  if ( !vl )
175  {
176  return QIcon();
177  }
178  QGis::GeometryType geomType = vl->geometryType();
179  switch ( geomType )
180  {
181  case QGis::Point:
182  {
183  return QgsApplication::getThemeIcon( "/mIconPointLayer.svg" );
184  }
185  case QGis::Polygon :
186  {
187  return QgsApplication::getThemeIcon( "/mIconPolygonLayer.svg" );
188  }
189  case QGis::Line :
190  {
191  return QgsApplication::getThemeIcon( "/mIconLineLayer.svg" );
192  }
193  case QGis::NoGeometry :
194  {
195  return QgsApplication::getThemeIcon( "/mIconTableLayer.png" );
196  }
197  default:
198  {
199  return QIcon();
200  }
201  }
202  }
203  default:
204  {
205  return QIcon();
206  }
207  }
208  }
209  }
210 
211  return QVariant();
212 }
213 
214 
215 Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
216 {
217  if ( !index.isValid() )
218  {
219  return 0;
220  }
221 
222  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
223  if ( mItemCheckable )
224  {
225  flags |= Qt::ItemIsUserCheckable;
226  }
227  return flags;
228 }
229 
230 
231 bool QgsMapLayerModel::setData( const QModelIndex &index, const QVariant &value, int role )
232 {
233  if ( role == Qt::CheckStateRole )
234  {
235  QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
236  mLayersChecked[layer->id()] = ( Qt::CheckState )value.toInt();
237  emit dataChanged( index, index );
238  return true;
239  }
240 
241  return false;
242 }
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:47
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:86
GeometryType
Definition: qgis.h:155
QList< QgsMapLayer * > mLayers
bool setData(const QModelIndex &index, const QVariant &value, int role)
void checkAll(Qt::CheckState checkState)
checkAll changes the checkstate for all the layers
int rowCount(const QModelIndex &parent) const
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void setItemsCheckable(bool checkable)
setItemsCheckable defines if layers should be selectable in the widget
void addLayers(QList< QgsMapLayer * > layers)
QgsMapLayerModel(QObject *parent=0)
QgsMapLayerModel creates a model to display layers in widgets.
const QString & name() const
Get the display name of the layer.
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:53
QVariant data(const QModelIndex &index, int role) const
QMap< QString, Qt::CheckState > mLayersChecked
Qt::ItemFlags flags(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:92
int columnCount(const QModelIndex &parent) const
QGis::GeometryType geometryType() const
Returns point, line or polygon.
QModelIndex parent(const QModelIndex &child) const
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
static const int LayerIdRole
QModelIndex indexFromLayer(QgsMapLayer *layer)
indexFromLayer returns the model index for a given layer
void removeLayers(const QStringList layerIds)
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)