QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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_sip.h"
25 
26 class QgsMapLayer;
27 class QgsProject;
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  Q_ENUM( ItemDataRole )
56 
57 
63  explicit QgsMapLayerModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProject *project = nullptr );
64 
71  explicit QgsMapLayerModel( const QList<QgsMapLayer *> &layers, QObject *parent = nullptr, QgsProject *project = nullptr );
72 
76  void setItemsCheckable( bool checkable );
77 
84  void setItemsCanBeReordered( bool allow );
85 
92  bool itemsCanBeReordered() const;
93 
97  void checkAll( Qt::CheckState checkState );
98 
104  void setAllowEmptyLayer( bool allowEmpty );
105 
111  bool allowEmptyLayer() const { return mAllowEmpty; }
112 
118  void setShowCrs( bool showCrs );
119 
125  bool showCrs() const { return mShowCrs; }
126 
130  QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
131 
135  void setLayersChecked( const QList< QgsMapLayer * > &layers );
136 
138  bool itemsCheckable() const { return mItemCheckable; }
139 
144  QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
145 
151  QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
152 
160  void setAdditionalItems( const QStringList &items );
161 
167  QStringList additionalItems() const { return mAdditionalItems; }
168 
169  // QAbstractItemModel interface
170  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
171  QModelIndex parent( const QModelIndex &child ) const override;
172  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
173  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
174  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
175  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
176  Qt::ItemFlags flags( const QModelIndex &index ) const override;
177  bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
178  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
179  QStringList mimeTypes() const override;
180  bool canDropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) const override;
181  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
182  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
183  Qt::DropActions supportedDropActions() const override;
184 
190  QHash<int, QByteArray> roleNames() const override SIP_SKIP;
191 
196  static QIcon iconForLayer( QgsMapLayer *layer );
197 
198  protected slots:
199  void removeLayers( const QStringList &layerIds );
200  void addLayers( const QList<QgsMapLayer *> &layers );
201 
202  protected:
203  QList<QgsMapLayer *> mLayers;
204  QMap<QString, Qt::CheckState> mLayersChecked;
205  bool mItemCheckable = false;
206  bool mCanReorder = false;
207 
208  QgsProject *mProject = nullptr;
209 
210  private:
211 
212  bool mAllowEmpty = false;
213  bool mShowCrs = false;
214  QStringList mAdditionalItems;
215 };
216 
217 #endif // QGSMAPLAYERMODEL_H
The QgsMapLayerModel class is a model to display layers in widgets.
bool itemsCheckable() const
returns if the items can be checked or not
ItemDataRole
Item data roles.
@ EmptyRole
True if index corresponds to the empty (not set) value.
@ LayerRole
Stores pointer to the map layer itself.
@ AdditionalRole
True if index corresponds to an additional (non map layer) item.
bool showCrs() const
Returns true if the model includes layer's CRS in the display role.
QStringList additionalItems() const
Returns the list of additional (non map layer) items included at the end of the model.
bool allowEmptyLayer() const
Returns true if the model allows the empty layer ("not set") choice.
Base class for all map layer types.
Definition: qgsmaplayer.h:85
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126