26 : QAbstractItemModel( parent )
33 : QAbstractItemModel( parent )
47 QMap<QString, Qt::CheckState>::iterator i =
mLayersChecked.begin();
57 if ( allowEmpty == mAllowEmpty )
62 beginInsertRows( QModelIndex(), 0, 0 );
68 beginRemoveRows( QModelIndex(), 0, 0 );
76 if ( mShowCrs == showCrs )
80 emit dataChanged(
index( 0, 0 ),
index(
rowCount() - 1, 0 ), QVector<int>() << Qt::DisplayRole );
85 QList<QgsMapLayer *> layers;
86 const auto constMLayers =
mLayers;
91 layers.append( layer );
99 int r =
mLayers.indexOf( layer );
100 if ( r >= 0 && mAllowEmpty )
102 return index( r, 0 );
107 return static_cast<QgsMapLayer *
>( index.internalPointer() );
112 if ( items == mAdditionalItems )
122 if ( !mAdditionalItems.isEmpty() )
124 beginRemoveRows( QModelIndex(), offset, offset + mAdditionalItems.count() - 1 );
125 mAdditionalItems.clear();
130 beginInsertRows( QModelIndex(), offset, offset + items.count() - 1 );
131 mAdditionalItems = items;
141 const auto constLayerIds = layerIds;
142 for (
const QString &layerId : constLayerIds )
144 QModelIndex startIndex =
index( 0, 0 );
145 QModelIndexList list = match( startIndex,
LayerIdRole, layerId, 1 );
146 if ( !list.isEmpty() )
148 QModelIndex
index = list[0];
149 beginRemoveRows( QModelIndex(), index.row(), index.row() );
151 mLayers.removeAt( index.row() - offset );
159 if ( !layers.empty( ) )
165 beginInsertRows( QModelIndex(),
mLayers.count() + offset,
mLayers.count() + layers.count() - 1 + offset );
166 const auto constLayers = layers;
182 if ( hasIndex( row, column, parent ) )
185 if ( row - offset >= 0 && row - offset <
mLayers.count() )
186 layer =
mLayers.at( row - offset );
188 return createIndex( row, column, layer );
191 return QModelIndex();
198 return QModelIndex();
204 if ( parent.isValid() )
207 return ( mAllowEmpty ? 1 : 0 ) +
mLayers.length() + mAdditionalItems.count();
219 if ( !index.isValid() )
222 bool isEmpty = index.row() == 0 && mAllowEmpty;
223 int additionalIndex = index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
227 case Qt::DisplayRole:
229 if ( index.row() == 0 && mAllowEmpty )
232 if ( additionalIndex >= 0 )
233 return mAdditionalItems.at( additionalIndex );
241 return layer->
name();
245 return tr(
"%1 [%2]" ).arg( layer->
name(), layer->
crs().
authid() );
251 if ( isEmpty || additionalIndex >= 0 )
255 return layer ? layer->
id() : QVariant();
260 if ( isEmpty || additionalIndex >= 0 )
263 return QVariant::fromValue<QgsMapLayer *>(
static_cast<QgsMapLayer *
>( index.internalPointer() ) );
270 return additionalIndex >= 0;
272 case Qt::CheckStateRole:
276 if ( isEmpty || additionalIndex >= 0 )
286 case Qt::ToolTipRole:
293 if ( title.isEmpty() )
294 title = layer->
name();
295 title =
"<b>" + title +
"</b>";
298 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
301 title = tr(
"%1 (%2) " ).arg( title, layer->
crs().
authid() );
306 parts <<
"<br/>" + layer->
abstract().replace( QLatin1String(
"\n" ), QLatin1String(
"<br/>" ) );
308 return parts.join( QStringLiteral(
"<br/>" ) );
313 case Qt::DecorationRole:
315 if ( isEmpty || additionalIndex >= 0 )
331 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
340 if ( !index.isValid() )
345 bool isEmpty = index.row() == 0 && mAllowEmpty;
346 int additionalIndex = index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
348 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
351 flags |= Qt::ItemIsUserCheckable;
358 switch ( layer->
type() )
412 bool isEmpty = index.row() == 0 && mAllowEmpty;
413 int additionalIndex = index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
415 if ( role == Qt::CheckStateRole && !isEmpty && additionalIndex < 0 )
419 emit dataChanged( index, index );
QgsMapLayer * layerFromIndex(const QModelIndex &index) const
Returns the map layer corresponding to the specified index.
static QIcon iconRaster()
QModelIndex indexFromLayer(QgsMapLayer *layer) const
indexFromLayer returns the model index for a given layer
Base class for all map layer types.
void layersAdded(const QList< QgsMapLayer *> &layers)
Emitted when one or more layers were added to the registry.
QgsMapLayerType type() const
Returns the type of the layer.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
void checkAll(Qt::CheckState checkState)
checkAll changes the checkstate for all the layers
QString abstract() const
Returns the abstract of the layer used by QGIS Server in GetCapabilities request. ...
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
Stores pointer to the map layer itself.
QModelIndex parent(const QModelIndex &child) const override
void setItemsCheckable(bool checkable)
setItemsCheckable defines if layers should be selectable in the widget
QgsMapLayerModel(QObject *parent=nullptr)
QgsMapLayerModel creates a model to display layers in widgets.
True if index corresponds to an additional (non map layer) item.
void addLayers(const QList< QgsMapLayer *> &layers)
void removeLayers(const QStringList &layerIds)
void setShowCrs(bool showCrs)
Sets whether the CRS of layers is also included in the model's display role.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Qt::ItemFlags flags(const QModelIndex &index) const override
static QIcon iconPolygon()
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QMap< QString, Qt::CheckState > mLayersChecked
void setAllowEmptyLayer(bool allowEmpty)
Sets whether an optional empty layer ("not set") option is present in the model.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
QString publicSource() const
Gets a version of the internal layer definition that has sensitive bits removed (for example...
QList< QgsMapLayer * > mLayers
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
bool showCrs() const
Returns true if the model includes layer's CRS in the display role.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the registry.
QHash< int, QByteArray > roleNames() const override
Returns strings for all roles supported by this model.
static QgsProject * instance()
Returns the QgsProject singleton instance.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
static QString displayString(Type type)
Returns a display string type for a WKB type, e.g., the geometry name used in WKT geometry representa...
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setAdditionalItems(const QStringList &items)
Sets a list of additional (non map layer) items to include at the end of the model.
static QIcon iconForLayer(QgsMapLayer *layer)
Returns the icon corresponding to a specified map layer.
Represents a vector layer which manages a vector based data sets.
static QIcon iconMesh()
Returns icon for mesh layer type.
QString authid() const
Returns the authority identifier for the CRS.
QgsCoordinateReferenceSystem crs
QList< QgsMapLayer * > layersChecked(Qt::CheckState checkState=Qt::Checked)
layersChecked returns the list of layers which are checked (or unchecked)
True if index corresponds to the empty (not set) value.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.