QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 );
132  bool itemsCheckable() const { return mItemCheckable; }
133 
138  QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
139 
145  QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
146 
154  void setAdditionalItems( const QStringList &items );
155 
161  QStringList additionalItems() const { return mAdditionalItems; }
162 
163  // QAbstractItemModel interface
164  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
165  QModelIndex parent( const QModelIndex &child ) const override;
166  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
167  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
168  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
169  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
170  Qt::ItemFlags flags( const QModelIndex &index ) const override;
171  bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
172  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
173  QStringList mimeTypes() const override;
174  bool canDropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) const override;
175  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
176  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
177  Qt::DropActions supportedDropActions() const override;
178 
184  QHash<int, QByteArray> roleNames() const override SIP_SKIP;
185 
190  static QIcon iconForLayer( QgsMapLayer *layer );
191 
192  protected slots:
193  void removeLayers( const QStringList &layerIds );
194  void addLayers( const QList<QgsMapLayer *> &layers );
195 
196  protected:
197  QList<QgsMapLayer *> mLayers;
198  QMap<QString, Qt::CheckState> mLayersChecked;
199  bool mItemCheckable = false;
200  bool mCanReorder = false;
201 
202  QgsProject *mProject = nullptr;
203 
204  private:
205 
206  bool mAllowEmpty = false;
207  bool mShowCrs = false;
208  QStringList mAdditionalItems;
209 };
210 
211 #endif // QGSMAPLAYERMODEL_H
QgsMapLayerModel::showCrs
bool showCrs() const
Returns true if the model includes layer's CRS in the display role.
Definition: qgsmaplayermodel.h:125
QgsProject
Definition: qgsproject.h:92
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsMapLayerModel::itemsCheckable
bool itemsCheckable() const
returns if the items can be checked or not
Definition: qgsmaplayermodel.h:132
QgsMapLayerModel::EmptyRole
@ EmptyRole
True if index corresponds to the empty (not set) value.
Definition: qgsmaplayermodel.h:52
qgis_sip.h
QgsMapLayerModel::allowEmptyLayer
bool allowEmptyLayer() const
Returns true if the model allows the empty layer ("not set") choice.
Definition: qgsmaplayermodel.h:111
QgsMapLayerModel::ItemDataRole
ItemDataRole
Item data roles.
Definition: qgsmaplayermodel.h:48
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsMapLayerModel::LayerRole
@ LayerRole
Stores pointer to the map layer itself.
Definition: qgsmaplayermodel.h:51
QgsMapLayerModel::AdditionalRole
@ AdditionalRole
True if index corresponds to an additional (non map layer) item.
Definition: qgsmaplayermodel.h:53
QgsMapLayerModel::additionalItems
QStringList additionalItems() const
Returns the list of additional (non map layer) items included at the end of the model.
Definition: qgsmaplayermodel.h:161
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsMapLayerModel
The QgsMapLayerModel class is a model to display layers in widgets.
Definition: qgsmaplayermodel.h:36