QGIS API Documentation  2.8.2-Wien
 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 "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 
27 QgsMapLayerModel::QgsMapLayerModel( QList<QgsMapLayer *> layers, QObject *parent )
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  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 
60 QList<QgsMapLayer *> QgsMapLayerModel::layersChecked( Qt::CheckState checkState )
61 {
62  QList<QgsMapLayer *> layers;
63  foreach ( QgsMapLayer* layer, mLayers )
64  {
65  if ( mLayersChecked[layer->id()] == checkState )
66  {
67  layers.append( layer );
68  }
69  }
70  return layers;
71 }
72 
73 QModelIndex QgsMapLayerModel::indexFromLayer( QgsMapLayer *layer ) const
74 {
75  int r = mLayers.indexOf( layer );
76  return index( r, 0 );
77 }
78 
79 void QgsMapLayerModel::removeLayers( const QStringList layerIds )
80 {
81  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 
96 void QgsMapLayerModel::addLayers( QList<QgsMapLayer *> layers )
97 {
98  beginInsertRows( QModelIndex(), mLayers.count(), mLayers.count() + layers.count() - 1 );
99  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 
118 QModelIndex QgsMapLayerModel::parent( const QModelIndex &child ) const
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 
137 QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
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 
217 Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
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 }