QGIS API Documentation 4.3.0-Master (0cfde48c85b)
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
122 QgsProject *project();
123
130 void setItemsCanBeReordered( bool allow );
131
138 bool itemsCanBeReordered() const;
139
143 void checkAll( Qt::CheckState checkState );
144
152 void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );
153
158 bool allowEmptyLayer() const { return mAllowEmpty; }
159
164 void setShowCrs( bool showCrs );
165
170 bool showCrs() const { return mShowCrs; }
171
175 QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
176
180 void setLayersChecked( const QList< QgsMapLayer * > &layers );
181
183 bool itemsCheckable() const { return mItemCheckable; }
184
189 QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
190
195 QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
196
203 void setAdditionalItems( const QStringList &items );
204
209 QStringList additionalItems() const { return mAdditionalItems; }
210
220 void setAdditionalLayers( const QList<QgsMapLayer *> &layers );
221
228 QList< QgsMapLayer * > additionalLayers() const;
229
230 // QAbstractItemModel interface
231 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
232 QModelIndex parent( const QModelIndex &child ) const override;
233 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
234 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
235 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
236 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
237 Qt::ItemFlags flags( const QModelIndex &index ) const override;
238 bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
239 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
240 QStringList mimeTypes() const override;
241 bool canDropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) const override;
242 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
243 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
244 Qt::DropActions supportedDropActions() const override;
245
251 QHash<int, QByteArray> roleNames() const override SIP_SKIP;
252
256 static QIcon iconForLayer( QgsMapLayer *layer );
257
258 protected slots:
259 void removeLayers( const QStringList &layerIds );
260 void addLayers( const QList<QgsMapLayer *> &layers );
261
262 protected:
264 QList< QPointer<QgsMapLayer> > mAdditionalLayers;
265 QMap<QString, Qt::CheckState> mLayersChecked;
266 bool mItemCheckable = false;
267 bool mCanReorder = false;
268
270
271 private:
272 bool mAllowEmpty = false;
273 QString mEmptyText;
274 QIcon mEmptyIcon;
275 bool mShowCrs = false;
276 QStringList mAdditionalItems;
277};
278
279#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:138
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition qgis_sip.h:270
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition qgis_sip.h:272