QGIS API Documentation 4.3.0-Master (767b36bf018)
Loading...
Searching...
No Matches
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 : denis.rouzaud@gmail.com
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 "qgis_core.h"
20#include "qgis_sip.h"
21
22#include <QAbstractItemModel>
23#include <QIcon>
24#include <QSortFilterProxyModel>
25#include <QStringList>
26
27class QgsMapLayer;
28class QgsProject;
29
36class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
37{
38 Q_OBJECT
39
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 // *INDENT-OFF*
47
55 {
56 LayerId SIP_MONKEYPATCH_COMPAT_NAME( LayerIdRole ) = Qt::UserRole + 1,
57 Layer SIP_MONKEYPATCH_COMPAT_NAME( LayerRole ),
58 Empty SIP_MONKEYPATCH_COMPAT_NAME( EmptyRole ),
59 Additional SIP_MONKEYPATCH_COMPAT_NAME( AdditionalRole ),
60 };
61 Q_ENUM( CustomRole )
62 // *INDENT-ON*
63
64 // TODO QGIS 5.0 -- remove deprecated constructors
65
66
74 explicit QgsMapLayerModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProject *project = nullptr ) SIP_DEPRECATED;
75
85 Q_DECL_DEPRECATED explicit QgsMapLayerModel( const QList<QgsMapLayer *> &layers, QObject *parent = nullptr, QgsProject *project = nullptr ) SIP_DEPRECATED;
86
87 // TODO QGIS 5.0 -- drop SIP_SKIP from the following constructors, so that they are available in Python
88
96 explicit QgsMapLayerModel( QgsProject *project, QObject *parent SIP_TRANSFERTHIS = nullptr ) SIP_SKIP;
97
103 explicit QgsMapLayerModel( QgsProject *project, const QList<QgsMapLayer *> &layers, QObject *parent SIP_TRANSFERTHIS = nullptr ) SIP_SKIP;
104
108 void setItemsCheckable( bool checkable );
109
115 void setProject( QgsProject *project );
116
123 void setItemsCanBeReordered( bool allow );
124
131 bool itemsCanBeReordered() const;
132
136 void checkAll( Qt::CheckState checkState );
137
145 void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );
146
151 bool allowEmptyLayer() const { return mAllowEmpty; }
152
157 void setShowCrs( bool showCrs );
158
163 bool showCrs() const { return mShowCrs; }
164
168 QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
169
173 void setLayersChecked( const QList< QgsMapLayer * > &layers );
174
176 bool itemsCheckable() const { return mItemCheckable; }
177
182 QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
183
188 QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
189
196 void setAdditionalItems( const QStringList &items );
197
202 QStringList additionalItems() const { return mAdditionalItems; }
203
213 void setAdditionalLayers( const QList<QgsMapLayer *> &layers );
214
221 QList< QgsMapLayer * > additionalLayers() const;
222
223 // QAbstractItemModel interface
224 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
225 QModelIndex parent( const QModelIndex &child ) const override;
226 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
227 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
228 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
229 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
230 Qt::ItemFlags flags( const QModelIndex &index ) const override;
231 bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
232 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
233 QStringList mimeTypes() const override;
234 bool canDropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) const override;
235 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
236 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
237 Qt::DropActions supportedDropActions() const override;
238
244 QHash<int, QByteArray> roleNames() const override SIP_SKIP;
245
249 static QIcon iconForLayer( QgsMapLayer *layer );
250
251 protected slots:
252 void removeLayers( const QStringList &layerIds );
253 void addLayers( const QList<QgsMapLayer *> &layers );
254
255 protected:
257 QList< QPointer<QgsMapLayer> > mAdditionalLayers;
258 QMap<QString, Qt::CheckState> mLayersChecked;
259 bool mItemCheckable = false;
260 bool mCanReorder = false;
261
263
264 private:
265 bool mAllowEmpty = false;
266 QString mEmptyText;
267 QIcon mEmptyIcon;
268 bool mShowCrs = false;
269 QStringList mAdditionalItems;
270};
271
272#endif // QGSMAPLAYERMODEL_H
A model for display of map layers in widgets.
QList< QPointer< QgsMapLayer > > mAdditionalLayers
void setShowCrs(bool showCrs)
Sets whether the CRS of layers is also included in the model's display role.
bool itemsCheckable() const
Returns whether the items can be checked or not.
QgsProject * mProject
void setAllowEmptyLayer(bool allowEmpty, const QString &text=QString(), const QIcon &icon=QIcon())
Sets whether an optional empty layer ("not set") option is present in the model.
void setAdditionalItems(const QStringList &items)
Sets a list of additional (non map layer) items to include at the end of the model.
bool showCrs() const
Returns true if the model includes layer's CRS in the display role.
void setItemsCheckable(bool checkable)
Defines if layers should be selectable in the widget.
QStringList additionalItems
CustomRole
Custom model roles.
QMap< QString, Qt::CheckState > mLayersChecked
QStringList additionalItems() const
Returns the list of additional (non map layer) items included at the end of the model.
QgsMapLayerModel(QObject *parent=nullptr, QgsProject *project=nullptr)
QgsMapLayerModel creates a model to display layers in widgets.
QList< QgsMapLayer * > mLayers
Base class for all map layer types.
Definition qgsmaplayer.h:83
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:114
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:265
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition qgis_sip.h:267