QGIS API Documentation  2.99.0-Master (3450a9f)
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 
35 class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
36 {
37  Q_OBJECT
38 
39  Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
40  Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
41  Q_PROPERTY( bool itemsCheckable READ itemsCheckable WRITE setItemsCheckable )
42  Q_PROPERTY( QStringList additionalItems READ additionalItems WRITE setAdditionalItems )
43 
44  public:
45 
48  {
49  LayerIdRole = Qt::UserRole + 1,
53  };
54 
58  explicit QgsMapLayerModel( QObject *parent SIP_TRANSFERTHIS = nullptr );
59 
63  explicit QgsMapLayerModel( const QList<QgsMapLayer *> &layers, QObject *parent = nullptr );
64 
68  void setItemsCheckable( bool checkable );
69 
73  void checkAll( Qt::CheckState checkState );
74 
80  void setAllowEmptyLayer( bool allowEmpty );
81 
87  bool allowEmptyLayer() const { return mAllowEmpty; }
88 
94  void setShowCrs( bool showCrs );
95 
101  bool showCrs() const { return mShowCrs; }
102 
106  QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
108  bool itemsCheckable() const { return mItemCheckable; }
109 
113  QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
114 
122  void setAdditionalItems( const QStringList &items );
123 
129  QStringList additionalItems() const { return mAdditionalItems; }
130 
131  // QAbstractItemModel interface
132  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
133  QModelIndex parent( const QModelIndex &child ) const override;
134  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
135  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
136  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
137 
143  QHash<int, QByteArray> roleNames() const override SIP_SKIP;
144 
145  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
146  Qt::ItemFlags flags( const QModelIndex &index ) const override;
147 
148  protected slots:
149  void removeLayers( const QStringList &layerIds );
150  void addLayers( const QList<QgsMapLayer *> &layers );
151 
152  protected:
153  QList<QgsMapLayer *> mLayers;
154  QMap<QString, Qt::CheckState> mLayersChecked;
156 
157  private:
158 
159  bool mAllowEmpty;
160  bool mShowCrs;
161  QStringList mAdditionalItems;
162 };
163 
164 #endif // QGSMAPLAYERMODEL_H
Base class for all map layer types.
Definition: qgsmaplayer.h:53
ItemDataRole
Item data roles.
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
QList< QgsMapLayer * > mLayers
bool showCrs() const
Returns true if the model includes layer&#39;s CRS in the display role.
#define SIP_SKIP
Definition: qgis.h:457
bool itemsCheckable() const
returns if the items can be checked or not
#define SIP_TRANSFERTHIS
Definition: qgis.h:409
True if index corresponds to the empty (not set) value.