QGIS API Documentation  2.99.0-Master (c42dad3)
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 class QgsMapLayer;
24 
25 
32 class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
33 {
34  Q_OBJECT
35 
36  Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
37  Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
38  Q_PROPERTY( bool itemsCheckable READ itemsCheckable WRITE setItemsCheckable )
39  Q_PROPERTY( QStringList additionalItems READ additionalItems WRITE setAdditionalItems )
40 
41  public:
42 
45  {
46  LayerIdRole = Qt::UserRole + 1,
50  };
51 
55  explicit QgsMapLayerModel( QObject *parent = nullptr );
56 
60  explicit QgsMapLayerModel( const QList<QgsMapLayer*>& layers, QObject *parent = nullptr );
61 
65  void setItemsCheckable( bool checkable );
66 
70  void checkAll( Qt::CheckState checkState );
71 
77  void setAllowEmptyLayer( bool allowEmpty );
78 
84  bool allowEmptyLayer() const { return mAllowEmpty; }
85 
91  void setShowCrs( bool showCrs );
92 
98  bool showCrs() const { return mShowCrs; }
99 
103  QList<QgsMapLayer*> layersChecked( Qt::CheckState checkState = Qt::Checked );
105  bool itemsCheckable() const { return mItemCheckable; }
106 
110  QModelIndex indexFromLayer( QgsMapLayer* layer ) const;
111 
119  void setAdditionalItems( const QStringList& items );
120 
126  QStringList additionalItems() const { return mAdditionalItems; }
127 
128  protected slots:
129  void removeLayers( const QStringList& layerIds );
130  void addLayers( const QList<QgsMapLayer*>& layers );
131 
132  protected:
133  QList<QgsMapLayer*> mLayers;
134  QMap<QString, Qt::CheckState> mLayersChecked;
136 
137  // QAbstractItemModel interface
138  public:
139  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
140  QModelIndex parent( const QModelIndex &child ) const override;
141  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
142  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
143  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
144 
150  QHash<int, QByteArray> roleNames() const override;
151 
152  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
153  Qt::ItemFlags flags( const QModelIndex &index ) const override;
154 
155  private:
156 
157  bool mAllowEmpty;
158  bool mShowCrs;
159  QStringList mAdditionalItems;
160 };
161 
162 #endif // QGSMAPLAYERMODEL_H
static unsigned index
Base class for all map layer types.
Definition: qgsmaplayer.h:49
ItemDataRole
Item data roles.
QList< QgsMapLayer * > mLayers
Stores pointer to the map layer itself.
bool allowEmptyLayer() const
Returns true if the model allows the empty layer ("not set") choice.
True if index corresponds to an additional (non map layer) item.
QStringList additionalItems() const
Return the list of additional (non map layer) items included at the end of the model.
The QgsMapLayerModel class is a model to display layers in widgets.
QMap< QString, Qt::CheckState > mLayersChecked
bool showCrs() const
Returns true if the model includes layer&#39;s CRS in the display role.
bool itemsCheckable() const
returns if the items can be checked or not
True if index corresponds to the empty (not set) value.