1 /*************************************************************************** 2 qgsmaplayerproxymodel.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 QGSMAPLAYERPROXYMODEL_H 17 #define QGSMAPLAYERPROXYMODEL_H 18 19 #include <QSortFilterProxyModel> 20 #include <QStringList> 21 22 #include "qgis_core.h" 23 #include "qgis_sip.h" 24 25 class QgsMapLayerModel; 26 class QgsMapLayer; 27 28 /** 29 * \ingroup core 30 * \brief The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widgets. 31 * \since QGIS 2.3 32 */ 33 class CORE_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel 34 { 35 Q_OBJECT 36 37 Q_PROPERTY( QgsMapLayerProxyModel::Filters filters READ filters WRITE setFilters ) 38 Q_PROPERTY( QList<QgsMapLayer *> exceptedLayerList READ exceptedLayerList WRITE setExceptedLayerList ) 39 Q_PROPERTY( QStringList exceptedLayerIds READ exceptedLayerIds WRITE setExceptedLayerIds ) 40 41 public: 42 enum Filter 43 { 44 RasterLayer = 1, 45 NoGeometry = 2, 46 PointLayer = 4, 47 LineLayer = 8, 48 PolygonLayer = 16, 49 HasGeometry = PointLayer | LineLayer | PolygonLayer, 50 VectorLayer = NoGeometry | HasGeometry, 51 PluginLayer = 32, 52 WritableLayer = 64, 53 MeshLayer = 128, //!< QgsMeshLayer \since QGIS 3.6 54 VectorTileLayer = 256, //!< QgsVectorTileLayer \since QGIS 3.14 55 PointCloudLayer = 512, //!< QgsPointCloudLayer \since QGIS 3.18 56 AnnotationLayer = 1024, //!< QgsAnnotationLayer \since QGIS 3.22 57 All = RasterLayer | VectorLayer | PluginLayer | MeshLayer | VectorTileLayer | PointCloudLayer | AnnotationLayer 58 }; 59 Q_DECLARE_FLAGS( Filters, Filter ) 60 Q_FLAG( Filters ) 61 62 /** 63 * \brief QgsMapLayerProxModel creates a proxy model with a QgsMapLayerModel as source model. 64 * It can be used to filter the layers list in a widget. 65 */ 66 explicit QgsMapLayerProxyModel( QObject *parent SIP_TRANSFERTHIS = nullptr ); 67 68 /** 69 * \brief layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel 70 */ sourceLayerModel()71 QgsMapLayerModel *sourceLayerModel() const { return mModel; } 72 73 /** 74 * Sets \a filter flags which affect how layers are filtered within the model. 75 * 76 * \see filters() 77 * 78 * \since QGIS 2.3 79 */ 80 QgsMapLayerProxyModel *setFilters( QgsMapLayerProxyModel::Filters filters ); 81 82 /** 83 * Returns the filter flags which affect how layers are filtered within the model. 84 * 85 * \see setFilters() 86 * 87 * \since QGIS 2.3 88 */ filters()89 const Filters &filters() const { return mFilters; } 90 91 /** 92 * Returns if the \a layer matches the given \a filters 93 * \since QGIS 3.14 94 */ 95 static bool layerMatchesFilters( const QgsMapLayer *layer, const Filters &filters ); 96 97 /** 98 * Sets an allowlist of \a layers to include within the model. Only layers 99 * from this list will be shown. 100 * 101 * An empty list indicates that no filter by allowlist should be performed. 102 * 103 * \see layerAllowlist() 104 * \see setExceptedLayerList() 105 * 106 * \deprecated use setLayerAllowList() 107 */ 108 Q_DECL_DEPRECATED void setLayerWhitelist( const QList<QgsMapLayer *> &layers ) SIP_DEPRECATED; 109 110 /** 111 * Sets an allowlist of \a layers to include within the model. Only layers 112 * from this list will be shown. 113 * 114 * An empty list indicates that no filter by allowlist should be performed. 115 * 116 * \see layerAllowlist() 117 * \see setExceptedLayerList() 118 * 119 * \since QGIS 3.14 120 */ 121 void setLayerAllowlist( const QList<QgsMapLayer *> &layers ); 122 123 /** 124 * Returns the list of layers which are excluded from the model. 125 * 126 * An empty list indicates that no filtering by allowlist should be performed. 127 * 128 * \see setLayerAllowlist() 129 * \see exceptedLayerList() 130 * 131 * \deprecated use layerAllowlist() instead 132 */ layerWhitelist()133 Q_DECL_DEPRECATED QList<QgsMapLayer *> layerWhitelist() SIP_DEPRECATED {return mLayerAllowlist;} 134 135 /** 136 * Returns the list of layers which are excluded from the model. 137 * 138 * An empty list indicates that no filtering by allowlist should be performed. 139 * 140 * \see setLayerAllowlist() 141 * \see exceptedLayerList() 142 * 143 * \since QGIS 3.14 144 */ layerAllowlist()145 QList<QgsMapLayer *> layerAllowlist() {return mLayerAllowlist;} 146 147 /** 148 * Sets a blocklist of layers to exclude from the model. 149 * \see exceptedLayerList() 150 * \see setExceptedLayerIds() 151 * \see setLayerAllowlist() 152 */ 153 void setExceptedLayerList( const QList<QgsMapLayer *> &exceptList ); 154 155 /** 156 * Returns the blocklist of layers which are excluded from the model. 157 * \see setExceptedLayerList() 158 * \see exceptedLayerIds() 159 * \see layerAllowlist() 160 */ exceptedLayerList()161 QList<QgsMapLayer *> exceptedLayerList() {return mExceptList;} 162 163 /** 164 * Sets a blocklist of layers (by layer ID) to exclude from the model. 165 * \see exceptedLayerIds() 166 * \see setExceptedLayerList() 167 */ 168 void setExceptedLayerIds( const QStringList &ids ); 169 170 /** 171 * Returns the blocklist of layer IDs which are excluded from the model. 172 * \see setExceptedLayerIds() 173 * \see exceptedLayerList() 174 */ 175 QStringList exceptedLayerIds() const; 176 177 /** 178 * Sets a blocklist of data providers which should be excluded from the model. 179 * \see excludedProviders() 180 * \since QGIS 3.0 181 */ 182 void setExcludedProviders( const QStringList &providers ); 183 184 /** 185 * Returns the blocklist of data providers which are excluded from the model. 186 * \see setExcludedProviders() 187 * \since QGIS 3.0 188 */ excludedProviders()189 QStringList excludedProviders() const { return mExcludedProviders; } 190 191 /** 192 * Returns the current filter string, if set. 193 * 194 * \see setFilterString() 195 * \since QGIS 3.4 196 */ filterString()197 QString filterString() const { return mFilterString; } 198 199 /** 200 * Returns TRUE if the proxy model accepts the specified map \a layer. 201 * 202 * \since QGIS 3.8 203 */ 204 bool acceptsLayer( QgsMapLayer *layer ) const; 205 206 bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override; 207 bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override; 208 209 public slots: 210 211 /** 212 * Sets a \a filter string, such that only layers with names matching the 213 * specified string will be shown. 214 * 215 * \see filterString() 216 * \since QGIS 3.4 217 */ 218 void setFilterString( const QString &filter ); 219 220 private: 221 Filters mFilters; 222 QList<QgsMapLayer *> mExceptList; 223 QList<QgsMapLayer *> mLayerAllowlist; 224 QgsMapLayerModel *mModel = nullptr; 225 QStringList mExcludedProviders; 226 QString mFilterString; 227 }; 228 229 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerProxyModel::Filters ) 230 231 #endif // QGSMAPLAYERPROXYMODEL_H 232