QGIS API Documentation 4.1.0-Master (60fea48833c)
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
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
70 explicit QgsMapLayerModel( QObject *parent SIP_TRANSFERTHIS = nullptr, QgsProject *project = nullptr );
71
78 explicit QgsMapLayerModel( const QList<QgsMapLayer *> &layers, QObject *parent = nullptr, QgsProject *project = nullptr );
79
83 void setItemsCheckable( bool checkable );
84
90 void setProject( QgsProject *project );
91
98 void setItemsCanBeReordered( bool allow );
99
106 bool itemsCanBeReordered() const;
107
111 void checkAll( Qt::CheckState checkState );
112
120 void setAllowEmptyLayer( bool allowEmpty, const QString &text = QString(), const QIcon &icon = QIcon() );
121
126 bool allowEmptyLayer() const { return mAllowEmpty; }
127
132 void setShowCrs( bool showCrs );
133
138 bool showCrs() const { return mShowCrs; }
139
143 QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
144
148 void setLayersChecked( const QList< QgsMapLayer * > &layers );
149
151 bool itemsCheckable() const { return mItemCheckable; }
152
157 QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
158
163 QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
164
171 void setAdditionalItems( const QStringList &items );
172
177 QStringList additionalItems() const { return mAdditionalItems; }
178
188 void setAdditionalLayers( const QList<QgsMapLayer *> &layers );
189
196 QList< QgsMapLayer * > additionalLayers() const;
197
198 // QAbstractItemModel interface
199 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
200 QModelIndex parent( const QModelIndex &child ) const override;
201 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
202 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
203 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
204 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
205 Qt::ItemFlags flags( const QModelIndex &index ) const override;
206 bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
207 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
208 QStringList mimeTypes() const override;
209 bool canDropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) const override;
210 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
211 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
212 Qt::DropActions supportedDropActions() const override;
213
219 QHash<int, QByteArray> roleNames() const override SIP_SKIP;
220
224 static QIcon iconForLayer( QgsMapLayer *layer );
225
226 protected slots:
227 void removeLayers( const QStringList &layerIds );
228 void addLayers( const QList<QgsMapLayer *> &layers );
229
230 protected:
232 QList< QPointer<QgsMapLayer> > mAdditionalLayers;
233 QMap<QString, Qt::CheckState> mLayersChecked;
234 bool mItemCheckable = false;
235 bool mCanReorder = false;
236
238
239 private:
240 bool mAllowEmpty = false;
241 QString mEmptyText;
242 QIcon mEmptyIcon;
243 bool mShowCrs = false;
244 QStringList mAdditionalItems;
245};
246
247#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
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:83
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.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:267
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition qgis_sip.h:269