QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
37 class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
38 {
39  Q_OBJECT
40 
41  Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
42  Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
43  Q_PROPERTY( bool itemsCheckable READ itemsCheckable WRITE setItemsCheckable )
44  Q_PROPERTY( QStringList additionalItems READ additionalItems WRITE setAdditionalItems )
45 
46  public:
47 
50  {
51  LayerIdRole = Qt::UserRole + 1,
55  };
56  Q_ENUM( ItemDataRole )
57 
58 
64  explicit QgsMapLayerModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProject *project = nullptr );
65 
72  explicit QgsMapLayerModel( const QList<QgsMapLayer *> &layers, QObject *parent = nullptr, QgsProject *project = nullptr );
73 
77  void setItemsCheckable( bool checkable );
78 
84  void setProject( QgsProject *project );
85 
92  void setItemsCanBeReordered( bool allow );
93 
100  bool itemsCanBeReordered() const;
101 
105  void checkAll( Qt::CheckState checkState );
106 
115  void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );
116 
122  bool allowEmptyLayer() const { return mAllowEmpty; }
123 
129  void setShowCrs( bool showCrs );
130 
136  bool showCrs() const { return mShowCrs; }
137 
141  QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
142 
146  void setLayersChecked( const QList< QgsMapLayer * > &layers );
147 
149  bool itemsCheckable() const { return mItemCheckable; }
150 
155  QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
156 
162  QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
163 
171  void setAdditionalItems( const QStringList &items );
172 
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 
226  static QIcon iconForLayer( QgsMapLayer *layer );
227 
228  protected slots:
229  void removeLayers( const QStringList &layerIds );
230  void addLayers( const QList<QgsMapLayer *> &layers );
231 
232  protected:
233  QList<QgsMapLayer *> mLayers;
234  QList< QPointer<QgsMapLayer> > mAdditionalLayers;
235  QMap<QString, Qt::CheckState> mLayersChecked;
236  bool mItemCheckable = false;
237  bool mCanReorder = false;
238 
239  QgsProject *mProject = nullptr;
240 
241  private:
242 
243  bool mAllowEmpty = false;
244  QString mEmptyText;
245  QIcon mEmptyIcon;
246  bool mShowCrs = false;
247  QStringList mAdditionalItems;
248 };
249 
250 #endif // QGSMAPLAYERMODEL_H
QgsMapLayerModel::showCrs
bool showCrs() const
Returns true if the model includes layer's CRS in the display role.
Definition: qgsmaplayermodel.h:136
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
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:149
QgsMapLayerModel::EmptyRole
@ EmptyRole
True if index corresponds to the empty (not set) value.
Definition: qgsmaplayermodel.h:53
qgis_sip.h
QgsMapLayerModel::allowEmptyLayer
bool allowEmptyLayer() const
Returns true if the model allows the empty layer ("not set") choice.
Definition: qgsmaplayermodel.h:122
QgsMapLayerModel::ItemDataRole
ItemDataRole
Item data roles.
Definition: qgsmaplayermodel.h:49
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsMapLayerModel::LayerRole
@ LayerRole
Stores pointer to the map layer itself.
Definition: qgsmaplayermodel.h:52
QgsMapLayerModel::AdditionalRole
@ AdditionalRole
True if index corresponds to an additional (non map layer) item.
Definition: qgsmaplayermodel.h:54
QgsMapLayerModel::additionalItems
QStringList additionalItems() const
Returns the list of additional (non map layer) items included at the end of the model.
Definition: qgsmaplayermodel.h:178
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:37