QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 #include <QIcon>
23 
24 #include "qgis_core.h"
25 #include "qgis_sip.h"
26 
27 class QgsMapLayer;
28 class QgsProject;
29 
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 
47  // *INDENT-OFF*
48 
56  {
57  LayerId SIP_MONKEYPATCH_COMPAT_NAME(LayerIdRole) = Qt::UserRole + 1,
58  Layer SIP_MONKEYPATCH_COMPAT_NAME(LayerRole),
59  Empty SIP_MONKEYPATCH_COMPAT_NAME(EmptyRole),
60  Additional SIP_MONKEYPATCH_COMPAT_NAME(AdditionalRole),
61  };
62  Q_ENUM( CustomRole )
63  // *INDENT-ON*
64 
65 
71  explicit QgsMapLayerModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProject *project = nullptr );
72 
79  explicit QgsMapLayerModel( const QList<QgsMapLayer *> &layers, QObject *parent = nullptr, QgsProject *project = nullptr );
80 
84  void setItemsCheckable( bool checkable );
85 
91  void setProject( QgsProject *project );
92 
99  void setItemsCanBeReordered( bool allow );
100 
107  bool itemsCanBeReordered() const;
108 
112  void checkAll( Qt::CheckState checkState );
113 
121  void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );
122 
127  bool allowEmptyLayer() const { return mAllowEmpty; }
128 
133  void setShowCrs( bool showCrs );
134 
139  bool showCrs() const { return mShowCrs; }
140 
144  QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
145 
149  void setLayersChecked( const QList< QgsMapLayer * > &layers );
150 
152  bool itemsCheckable() const { return mItemCheckable; }
153 
158  QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
159 
164  QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
165 
172  void setAdditionalItems( const QStringList &items );
173 
178  QStringList additionalItems() const { return mAdditionalItems; }
179 
189  void setAdditionalLayers( const QList<QgsMapLayer *> &layers );
190 
197  QList< QgsMapLayer * > additionalLayers() const;
198 
199  // QAbstractItemModel interface
200  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
201  QModelIndex parent( const QModelIndex &child ) const override;
202  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
203  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
204  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
205  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
206  Qt::ItemFlags flags( const QModelIndex &index ) const override;
207  bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
208  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
209  QStringList mimeTypes() const override;
210  bool canDropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) const override;
211  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
212  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
213  Qt::DropActions supportedDropActions() const override;
214 
220  QHash<int, QByteArray> roleNames() const override SIP_SKIP;
221 
225  static QIcon iconForLayer( QgsMapLayer *layer );
226 
227  protected slots:
228  void removeLayers( const QStringList &layerIds );
229  void addLayers( const QList<QgsMapLayer *> &layers );
230 
231  protected:
232  QList<QgsMapLayer *> mLayers;
233  QList< QPointer<QgsMapLayer> > mAdditionalLayers;
234  QMap<QString, Qt::CheckState> mLayersChecked;
235  bool mItemCheckable = false;
236  bool mCanReorder = false;
237 
238  QgsProject *mProject = nullptr;
239 
240  private:
241 
242  bool mAllowEmpty = false;
243  QString mEmptyText;
244  QIcon mEmptyIcon;
245  bool mShowCrs = false;
246  QStringList mAdditionalItems;
247 };
248 
249 #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
bool showCrs() const
Returns true if the model includes layer's CRS in the display role.
CustomRole
Custom model roles.
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:75
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:107
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition: qgis_sip.h:271
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition: qgis_sip.h:273