File: qgsmaplayermodel.cpp

package info (click to toggle)
qgis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 374,696 kB
  • ctags: 66,263
  • sloc: cpp: 396,139; ansic: 241,070; python: 130,609; xml: 14,884; perl: 1,290; sh: 1,287; sql: 500; yacc: 268; lex: 242; makefile: 168
file content (244 lines) | stat: -rw-r--r-- 6,670 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***************************************************************************
   qgsmaplayermodel.cpp
    --------------------------------------
   Date                 : 01.04.2014
   Copyright            : (C) 2014 Denis Rouzaud
   Email                : denis.rouzaud@gmail.com
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#include <QIcon>

#include "qgsdataitem.h"
#include "qgsmaplayermodel.h"
#include "qgsmaplayerregistry.h"
#include "qgsapplication.h"
#include "qgsvectorlayer.h"


const int QgsMapLayerModel::LayerIdRole = Qt::UserRole + 1;

QgsMapLayerModel::QgsMapLayerModel( QList<QgsMapLayer *> layers, QObject *parent )
    : QAbstractItemModel( parent )
    , mLayersChecked( QMap<QString, Qt::CheckState>() )
    , mItemCheckable( false )
{
  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
  addLayers( layers );
}

QgsMapLayerModel::QgsMapLayerModel( QObject *parent )
    : QAbstractItemModel( parent )
    , mLayersChecked( QMap<QString, Qt::CheckState>() )
    , mItemCheckable( false )
{
  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
  connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
  addLayers( QgsMapLayerRegistry::instance()->mapLayers().values() );
}

void QgsMapLayerModel::setItemsCheckable( bool checkable )
{
  mItemCheckable = checkable;
}

void QgsMapLayerModel::checkAll( Qt::CheckState checkState )
{
  foreach ( const QString key, mLayersChecked.keys() )
  {
    mLayersChecked[key] = checkState;
  }
  emit dataChanged( index( 0, 0 ), index( mLayers.length() - 1, 0 ) );
}

QList<QgsMapLayer *> QgsMapLayerModel::layersChecked( Qt::CheckState checkState )
{
  QList<QgsMapLayer *> layers;
  foreach ( QgsMapLayer* layer, mLayers )
  {
    if ( mLayersChecked[layer->id()] == checkState )
    {
      layers.append( layer );
    }
  }
  return layers;
}

QModelIndex QgsMapLayerModel::indexFromLayer( QgsMapLayer *layer )
{
  int r = mLayers.indexOf( layer );
  return index( r, 0 );
}

void QgsMapLayerModel::removeLayers( const QStringList layerIds )
{
  foreach ( const QString layerId, layerIds )
  {
    QModelIndex startIndex = index( 0, 0 );
    QModelIndexList list = match( startIndex, LayerIdRole, layerId, 1 );
    if ( list.count() )
    {
      QModelIndex index = list[0];
      beginRemoveRows( QModelIndex(), index.row(), index.row() );
      mLayersChecked.remove( layerId );
      mLayers.removeAt( index.row() );
      endRemoveRows();
    }
  }
}

void QgsMapLayerModel::addLayers( QList<QgsMapLayer *> layers )
{
  beginInsertRows( QModelIndex(), mLayers.count(), mLayers.count() + layers.count() - 1 );
  foreach ( QgsMapLayer* layer, layers )
  {
    mLayers.append( layer );
    mLayersChecked.insert( layer->id(), Qt::Unchecked );
  }
  endInsertRows();
}

QModelIndex QgsMapLayerModel::index( int row, int column, const QModelIndex &parent ) const
{
  if ( hasIndex( row, column, parent ) )
  {
    return createIndex( row, column, mLayers[row] );
  }

  return QModelIndex();

}

QModelIndex QgsMapLayerModel::parent( const QModelIndex &child ) const
{
  Q_UNUSED( child );
  return QModelIndex();
}


int QgsMapLayerModel::rowCount( const QModelIndex &parent ) const
{
  return parent.isValid() ? 0 : mLayers.length();
}

int QgsMapLayerModel::columnCount( const QModelIndex &parent ) const
{
  Q_UNUSED( parent );
  return 1;
}


QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
{
  if ( !index.isValid() || !index.internalPointer() )
    return QVariant();

  if ( role == Qt::DisplayRole )
  {
    QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
    return layer->name();
  }

  if ( role == LayerIdRole )
  {
    QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
    return layer->id();
  }

  if ( role == Qt::CheckStateRole && mItemCheckable )
  {
    QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
    return mLayersChecked[layer->id()];
  }

  if ( role == Qt::DecorationRole )
  {
    QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
    QgsMapLayer::LayerType type = layer->type();
    if ( role == Qt::DecorationRole )
    {
      switch ( type )
      {
        case QgsMapLayer::RasterLayer:
        {
          return QgsLayerItem::iconRaster();
        }

        case QgsMapLayer::VectorLayer:
        {
          QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
          if ( !vl )
          {
            return QIcon();
          }
          QGis::GeometryType geomType = vl->geometryType();
          switch ( geomType )
          {
            case QGis::Point:
            {
              return QgsLayerItem::iconPoint();
            }
            case QGis::Polygon :
            {
              return QgsLayerItem::iconPolygon();
            }
            case QGis::Line :
            {
              return QgsLayerItem::iconLine();
            }
            case QGis::NoGeometry :
            {
              return QgsLayerItem::iconTable();
            }
            default:
            {
              return QIcon();
            }
          }
        }
        default:
        {
          return QIcon();
        }
      }
    }
  }

  return QVariant();
}


Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
{
  if ( !index.isValid() )
  {
    return 0;
  }

  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  if ( mItemCheckable )
  {
    flags |= Qt::ItemIsUserCheckable;
  }
  return flags;
}


bool QgsMapLayerModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
  if ( role == Qt::CheckStateRole )
  {
    QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
    mLayersChecked[layer->id()] = ( Qt::CheckState )value.toInt();
    emit dataChanged( index, index );
    return true;
  }

  return false;
}