25 : QAbstractItemModel( parent )
26 , mProject( project ? project :
QgsProject::instance() )
33 : QAbstractItemModel( parent )
34 , mProject( project ? project :
QgsProject::instance() )
58 QMap<QString, Qt::CheckState>::iterator i =
mLayersChecked.begin();
68 if ( allowEmpty == mAllowEmpty )
73 beginInsertRows( QModelIndex(), 0, 0 );
79 beginRemoveRows( QModelIndex(), 0, 0 );
91 emit dataChanged(
index( 0, 0 ),
index(
rowCount() - 1, 0 ), QVector<int>() << Qt::DisplayRole );
96 QList<QgsMapLayer *> layers;
97 const auto constMLayers =
mLayers;
102 layers.append( layer );
110 QMap<QString, Qt::CheckState>::iterator i =
mLayersChecked.begin();
119 emit dataChanged(
index( 0, 0 ),
index(
rowCount() - 1, 0 ), QVector<int>() << Qt::CheckStateRole );
124 int r =
mLayers.indexOf( layer );
125 if ( r >= 0 && mAllowEmpty )
127 return index( r, 0 );
137 if ( items == mAdditionalItems )
147 if ( !mAdditionalItems.isEmpty() )
149 beginRemoveRows( QModelIndex(), offset, offset + mAdditionalItems.count() - 1 );
150 mAdditionalItems.clear();
155 beginInsertRows( QModelIndex(), offset, offset + items.count() - 1 );
156 mAdditionalItems = items;
166 for (
const QString &layerId : layerIds )
168 QModelIndex startIndex =
index( 0, 0 );
169 QModelIndexList list = match( startIndex,
LayerIdRole, layerId, 1 );
170 if ( !list.isEmpty() )
172 QModelIndex
index = list[0];
173 beginRemoveRows( QModelIndex(),
index.row(),
index.row() );
183 if ( !layers.empty( ) )
189 beginInsertRows( QModelIndex(),
mLayers.count() + offset,
mLayers.count() + layers.count() - 1 + offset );
190 const auto constLayers = layers;
206 if ( hasIndex( row, column,
parent ) )
209 if ( row - offset >= 0 && row - offset <
mLayers.count() )
210 layer =
mLayers.at( row - offset );
212 return createIndex( row, column, layer );
215 return QModelIndex();
222 return QModelIndex();
231 return ( mAllowEmpty ? 1 : 0 ) +
mLayers.length() + mAdditionalItems.count();
243 if ( !
index.isValid() )
246 bool isEmpty =
index.row() == 0 && mAllowEmpty;
247 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
251 case Qt::DisplayRole:
254 if (
index.row() == 0 && mAllowEmpty )
257 if ( additionalIndex >= 0 )
258 return mAdditionalItems.at( additionalIndex );
264 if ( !mShowCrs || !layer->
isSpatial() || role == Qt::EditRole )
266 return layer->
name();
270 return tr(
"%1 [%2]" ).arg( layer->
name(), layer->
crs().
authid() );
276 if ( isEmpty || additionalIndex >= 0 )
280 return layer ? layer->
id() : QVariant();
285 if ( isEmpty || additionalIndex >= 0 )
288 return QVariant::fromValue<QgsMapLayer *>(
mLayers.value(
index.row() - ( mAllowEmpty ? 1 : 0 ) ) );
295 return additionalIndex >= 0;
297 case Qt::CheckStateRole:
301 if ( isEmpty || additionalIndex >= 0 )
311 case Qt::ToolTipRole:
318 if ( title.isEmpty() )
319 title = layer->
name();
320 title =
"<b>" + title +
"</b>";
323 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
326 title = tr(
"%1 (%2) " ).arg( title, layer->
crs().
authid() );
331 parts <<
"<br/>" + layer->
abstract().replace( QLatin1String(
"\n" ), QLatin1String(
"<br/>" ) );
333 return parts.join( QLatin1String(
"<br/>" ) );
338 case Qt::DecorationRole:
340 if ( isEmpty || additionalIndex >= 0 )
356 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
365 if ( !
index.isValid() )
368 return Qt::ItemIsDropEnabled;
370 return Qt::ItemFlags();
373 bool isEmpty =
index.row() == 0 && mAllowEmpty;
374 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
376 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
378 if (
mCanReorder && !isEmpty && additionalIndex < 0 )
380 flags |= Qt::ItemIsDragEnabled;
385 flags |= Qt::ItemIsUserCheckable;
399 beginInsertRows(
parent, row, row + count - 1 );
400 for (
int i = row; i < row + count; ++i )
401 mLayers.insert( i - offset,
nullptr );
409 if (
parent.isValid() || row < 0 )
421 if ( row - offset >
mLayers.count() - 1 )
426 beginRemoveRows(
parent, row, row + count - 1 );
427 for (
int i = 0; i != count; ++i )
428 mLayers.removeAt( row - offset );
437 types << QStringLiteral(
"application/qgis.layermodeldata" );
443 if ( !
mCanReorder || action != Qt::MoveAction || !
data->hasFormat( QStringLiteral(
"application/qgis.layermodeldata" ) ) )
450 std::unique_ptr< QMimeData >
mimeData = qgis::make_unique< QMimeData >();
452 QByteArray encodedData;
453 QDataStream stream( &encodedData, QIODevice::WriteOnly );
454 QSet< QString > addedLayers;
456 for (
const QModelIndex &i : indexes )
461 if ( !addedLayers.contains(
id ) )
463 addedLayers.insert(
id );
468 mimeData->setData( QStringLiteral(
"application/qgis.layermodeldata" ), encodedData );
477 if ( action == Qt::IgnoreAction )
479 else if ( action != Qt::MoveAction )
482 QByteArray encodedData =
data->data( QStringLiteral(
"application/qgis.layermodeldata" ) );
483 QDataStream stream( &encodedData, QIODevice::ReadOnly );
484 QStringList newItems;
487 while ( !stream.atEnd() )
496 for (
const QString &text : qgis::as_const( newItems ) )
498 QModelIndex idx =
index( row, 0, QModelIndex() );
508 return Qt::MoveAction;
513 switch ( layer->
type() )
578 if ( !
index.isValid() )
581 bool isEmpty =
index.row() == 0 && mAllowEmpty;
582 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
586 case Qt::CheckStateRole:
588 if ( !isEmpty && additionalIndex < 0 )
592 emit dataChanged(
index,
index, QVector< int >() << Qt::CheckStateRole );
599 if ( !isEmpty && additionalIndex < 0 )
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString authid() const
Returns the authority identifier for the CRS.
static QIcon iconRaster()
static QIcon iconMesh()
Returns icon for mesh layer type.
static QIcon iconPolygon()
static QIcon iconPointCloud()
Returns icon for point cloud layer.
static QIcon iconVectorTile()
Returns icon for vector tile layer.
void setShowCrs(bool showCrs)
Sets whether the CRS of layers is also included in the model's display role.
void setItemsCanBeReordered(bool allow)
Sets whether items in the model can be reordered via drag and drop.
QHash< int, QByteArray > roleNames() const override
Returns strings for all roles supported by this model.
Qt::ItemFlags flags(const QModelIndex &index) const override
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QList< QgsMapLayer * > layersChecked(Qt::CheckState checkState=Qt::Checked)
layersChecked returns the list of layers which are checked (or unchecked)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QModelIndex parent(const QModelIndex &child) const override
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
void setAllowEmptyLayer(bool allowEmpty)
Sets whether an optional empty layer ("not set") option is present in the model.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
@ EmptyRole
True if index corresponds to the empty (not set) value.
@ LayerIdRole
Stores the map layer ID.
@ LayerRole
Stores pointer to the map layer itself.
@ AdditionalRole
True if index corresponds to an additional (non map layer) item.
QModelIndex indexFromLayer(QgsMapLayer *layer) const
indexFromLayer returns the model index for a given layer
void setAdditionalItems(const QStringList &items)
Sets a list of additional (non map layer) items to include at the end of the model.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void setItemsCheckable(bool checkable)
setItemsCheckable defines if layers should be selectable in the widget
static QIcon iconForLayer(QgsMapLayer *layer)
Returns the icon corresponding to a specified map layer.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QgsMapLayer * layerFromIndex(const QModelIndex &index) const
Returns the map layer corresponding to the specified index.
Qt::DropActions supportedDropActions() const override
void setLayersChecked(const QList< QgsMapLayer * > &layers)
Sets which layers are checked in the model.
void checkAll(Qt::CheckState checkState)
checkAll changes the checkstate for all the layers
void removeLayers(const QStringList &layerIds)
QStringList mimeTypes() const override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
bool itemsCanBeReordered() const
Returns true if items in the model can be reordered via drag and drop.
QMap< QString, Qt::CheckState > mLayersChecked
QgsMapLayerModel(QObject *parent=nullptr, QgsProject *project=nullptr)
QgsMapLayerModel creates a model to display layers in widgets.
QList< QgsMapLayer * > mLayers
void addLayers(const QList< QgsMapLayer * > &layers)
QMimeData * mimeData(const QModelIndexList &indexes) const override
Base class for all map layer types.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QString publicSource() const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
QgsCoordinateReferenceSystem crs
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QString abstract() const
Returns the abstract of the layer used by QGIS Server in GetCapabilities request.
QString shortName() const
Returns the short name of the layer used by QGIS Server to identify the layer.
QString title() const
Returns the title of the layer used by QGIS Server in GetCapabilities request.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the registry.
void layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the registry.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
static QString displayString(Type type) SIP_HOLDGIL
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
@ PointCloudLayer
Added in 3.18.
@ VectorTileLayer
Added in 3.14.