QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsmaplayermodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayermodel.h
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 #ifndef QGSMAPLAYERMODEL_H
17 #define QGSMAPLAYERMODEL_H
18 
19 #include <QAbstractItemModel>
20 #include <QSortFilterProxyModel>
21 #include <QStringList>
22 
23 #include "qgis_core.h"
24 #include "qgis.h"
25 
26 class QgsMapLayer;
27 
28 
36 class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
37 {
38  Q_OBJECT
39 
40  Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
41  Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
42  Q_PROPERTY( bool itemsCheckable READ itemsCheckable WRITE setItemsCheckable )
43  Q_PROPERTY( QStringList additionalItems READ additionalItems WRITE setAdditionalItems )
44 
45  public:
46 
49  {
50  LayerIdRole = Qt::UserRole + 1,
54  };
55 
59  explicit QgsMapLayerModel( QObject *parent SIP_TRANSFERTHIS = nullptr );
60 
64  explicit QgsMapLayerModel( const QList<QgsMapLayer *> &layers, QObject *parent = nullptr );
65 
69  void setItemsCheckable( bool checkable );
70 
74  void checkAll( Qt::CheckState checkState );
75 
81  void setAllowEmptyLayer( bool allowEmpty );
82 
88  bool allowEmptyLayer() const { return mAllowEmpty; }
89 
95  void setShowCrs( bool showCrs );
96 
102  bool showCrs() const { return mShowCrs; }
103 
107  QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
109  bool itemsCheckable() const { return mItemCheckable; }
110 
115  QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
116 
122  QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
123 
131  void setAdditionalItems( const QStringList &items );
132 
138  QStringList additionalItems() const { return mAdditionalItems; }
139 
140  // QAbstractItemModel interface
141  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
142  QModelIndex parent( const QModelIndex &child ) const override;
143  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
144  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
145  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
146 
152  QHash<int, QByteArray> roleNames() const override SIP_SKIP;
153 
154  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
155  Qt::ItemFlags flags( const QModelIndex &index ) const override;
156 
161  static QIcon iconForLayer( QgsMapLayer *layer );
162 
163  protected slots:
164  void removeLayers( const QStringList &layerIds );
165  void addLayers( const QList<QgsMapLayer *> &layers );
166 
167  protected:
168  QList<QgsMapLayer *> mLayers;
169  QMap<QString, Qt::CheckState> mLayersChecked;
170  bool mItemCheckable = false;
171 
172  private:
173 
174  bool mAllowEmpty = false;
175  bool mShowCrs = false;
176  QStringList mAdditionalItems;
177 };
178 
179 #endif // QGSMAPLAYERMODEL_H
Base class for all map layer types.
Definition: qgsmaplayer.h:63
ItemDataRole
Item data roles.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
bool allowEmptyLayer() const
Returns true if the model allows the empty layer ("not set") choice.
Stores pointer to the map layer itself.
True if index corresponds to an additional (non map layer) item.
#define SIP_SKIP
Definition: qgis_sip.h:119
The QgsMapLayerModel class is a model to display layers in widgets.
QMap< QString, Qt::CheckState > mLayersChecked
QList< QgsMapLayer * > mLayers
bool itemsCheckable() const
returns if the items can be checked or not
QStringList additionalItems() const
Returns the list of additional (non map layer) items included at the end of the model.
bool showCrs() const
Returns true if the model includes layer&#39;s CRS in the display role.
True if index corresponds to the empty (not set) value.