1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***************************************************************************
qgsmaplayermodel.h
--------------------------------------
Date : 01.04.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSMAPLAYERMODEL_H
#define QGSMAPLAYERMODEL_H
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <QStringList>
#include "qgis_core.h"
#include "qgis_sip.h"
class QgsMapLayer;
/**
* \ingroup core
* \brief The QgsMapLayerModel class is a model to display layers in widgets.
* \see QgsMapLayerProxyModel to sort and/filter the layers
* \see QgsFieldModel to combine in with a field selector.
* \since QGIS 2.3
*/
class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
{
Q_OBJECT
Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
Q_PROPERTY( bool itemsCheckable READ itemsCheckable WRITE setItemsCheckable )
Q_PROPERTY( QStringList additionalItems READ additionalItems WRITE setAdditionalItems )
public:
//! Item data roles
enum ItemDataRole
{
LayerIdRole = Qt::UserRole + 1, //!< Stores the map layer ID
LayerRole, //!< Stores pointer to the map layer itself
EmptyRole, //!< True if index corresponds to the empty (not set) value
AdditionalRole, //!< True if index corresponds to an additional (non map layer) item
};
Q_ENUM( ItemDataRole )
/**
* \brief QgsMapLayerModel creates a model to display layers in widgets.
*/
explicit QgsMapLayerModel( QObject *parent SIP_TRANSFERTHIS = nullptr );
/**
* \brief QgsMapLayerModel creates a model to display a specific list of layers in a widget.
*/
explicit QgsMapLayerModel( const QList<QgsMapLayer *> &layers, QObject *parent = nullptr );
/**
* \brief setItemsCheckable defines if layers should be selectable in the widget
*/
void setItemsCheckable( bool checkable );
/**
* \brief checkAll changes the checkstate for all the layers
*/
void checkAll( Qt::CheckState checkState );
/**
* Sets whether an optional empty layer ("not set") option is present in the model.
* \see allowEmptyLayer()
* \since QGIS 3.0
*/
void setAllowEmptyLayer( bool allowEmpty );
/**
* Returns TRUE if the model allows the empty layer ("not set") choice.
* \see setAllowEmptyLayer()
* \since QGIS 3.0
*/
bool allowEmptyLayer() const { return mAllowEmpty; }
/**
* Sets whether the CRS of layers is also included in the model's display role.
* \see showCrs()
* \since QGIS 3.0
*/
void setShowCrs( bool showCrs );
/**
* Returns TRUE if the model includes layer's CRS in the display role.
* \see setShowCrs()
* \since QGIS 3.0
*/
bool showCrs() const { return mShowCrs; }
/**
* \brief layersChecked returns the list of layers which are checked (or unchecked)
*/
QList<QgsMapLayer *> layersChecked( Qt::CheckState checkState = Qt::Checked );
//! returns if the items can be checked or not
bool itemsCheckable() const { return mItemCheckable; }
/**
* \brief indexFromLayer returns the model index for a given layer
* \see layerFromIndex()
*/
QModelIndex indexFromLayer( QgsMapLayer *layer ) const;
/**
* Returns the map layer corresponding to the specified \a index.
* \see indexFromLayer()
* \since QGIS 3.0
*/
QgsMapLayer *layerFromIndex( const QModelIndex &index ) const;
/**
* Sets a list of additional (non map layer) items to include at the end of the model.
* These may represent additional layers such as layers which are not included in the map
* layer registry, or paths to layers which have not yet been loaded into QGIS.
* \see additionalItems()
* \since QGIS 3.0
*/
void setAdditionalItems( const QStringList &items );
/**
* Returns the list of additional (non map layer) items included at the end of the model.
* \see setAdditionalItems()
* \since QGIS 3.0
*/
QStringList additionalItems() const { return mAdditionalItems; }
// QAbstractItemModel interface
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
QModelIndex parent( const QModelIndex &child ) const override;
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
/**
* Returns strings for all roles supported by this model.
*
* \note Available only with Qt5 (Python and c++)
*/
QHash<int, QByteArray> roleNames() const override SIP_SKIP;
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;
/**
* Returns the icon corresponding to a specified map \a layer.
* \since QGIS 3.0
*/
static QIcon iconForLayer( QgsMapLayer *layer );
protected slots:
void removeLayers( const QStringList &layerIds );
void addLayers( const QList<QgsMapLayer *> &layers );
protected:
QList<QgsMapLayer *> mLayers;
QMap<QString, Qt::CheckState> mLayersChecked;
bool mItemCheckable = false;
private:
bool mAllowEmpty = false;
bool mShowCrs = false;
QStringList mAdditionalItems;
};
#endif // QGSMAPLAYERMODEL_H