26 : QAbstractItemModel( parent )
27 , mProject( project ? project :
QgsProject::instance() )
34 : QAbstractItemModel( parent )
35 , mProject( project ? project :
QgsProject::instance() )
78 QMap<QString, Qt::CheckState>::iterator i =
mLayersChecked.begin();
90 if ( allowEmpty == mAllowEmpty )
95 beginInsertRows( QModelIndex(), 0, 0 );
101 beginRemoveRows( QModelIndex(), 0, 0 );
113 emit dataChanged(
index( 0, 0 ),
index(
rowCount() - 1, 0 ), QVector<int>() << Qt::DisplayRole );
118 QList<QgsMapLayer *> layers;
119 const auto constMLayers =
mLayers;
124 layers.append( layer );
132 QMap<QString, Qt::CheckState>::iterator i =
mLayersChecked.begin();
141 emit dataChanged(
index( 0, 0 ),
index(
rowCount() - 1, 0 ), QVector<int>() << Qt::CheckStateRole );
146 int r =
mLayers.indexOf( layer );
147 if ( r >= 0 && mAllowEmpty )
149 return index( r, 0 );
159 if ( items == mAdditionalItems )
169 if ( !mAdditionalItems.isEmpty() )
171 beginRemoveRows( QModelIndex(), offset, offset + mAdditionalItems.count() - 1 );
172 mAdditionalItems.clear();
177 beginInsertRows( QModelIndex(), offset, offset + items.count() - 1 );
178 mAdditionalItems = items;
187 QStringList layerIdsToRemove;
191 layerIdsToRemove << layer->id();
200 const QString layerId = layer->id();
219 for (
const QString &layerId : layerIds )
221 QModelIndex startIndex =
index( 0, 0 );
222 QModelIndexList list = match( startIndex,
static_cast< int >(
CustomRole::LayerId ), layerId, 1 );
223 if ( !list.isEmpty() )
225 QModelIndex
index = list[0];
226 beginRemoveRows( QModelIndex(),
index.row(),
index.row() );
236 if ( !layers.empty( ) )
242 beginInsertRows( QModelIndex(),
mLayers.count() + offset,
mLayers.count() + layers.count() - 1 + offset );
243 const auto constLayers = layers;
259 if ( hasIndex( row, column,
parent ) )
262 if ( row - offset >= 0 && row - offset <
mLayers.count() )
263 layer =
mLayers.at( row - offset );
265 return createIndex( row, column, layer );
268 return QModelIndex();
275 return QModelIndex();
284 return ( mAllowEmpty ? 1 : 0 ) +
mLayers.length() + mAdditionalItems.count();
296 if ( !
index.isValid() )
299 bool isEmpty =
index.row() == 0 && mAllowEmpty;
300 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
304 case Qt::DisplayRole:
307 if (
index.row() == 0 && mAllowEmpty )
310 if ( additionalIndex >= 0 )
311 return mAdditionalItems.at( additionalIndex );
317 if ( !mShowCrs || !layer->
isSpatial() || role == Qt::EditRole )
319 return layer->
name();
323 return tr(
"%1 [%2]" ).arg( layer->
name(), layer->
crs().
authid() );
329 if ( isEmpty || additionalIndex >= 0 )
333 return layer ? layer->
id() : QVariant();
338 if ( isEmpty || additionalIndex >= 0 )
341 return QVariant::fromValue<QgsMapLayer *>(
mLayers.value(
index.row() - ( mAllowEmpty ? 1 : 0 ) ) );
348 return additionalIndex >= 0;
350 case Qt::CheckStateRole:
354 if ( isEmpty || additionalIndex >= 0 )
364 case Qt::ToolTipRole:
371 if ( title.isEmpty() )
372 title = layer->
name();
373 title =
"<b>" + title +
"</b>";
376 QString layerCrs = layer->
crs().
authid();
381 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
384 title = tr(
"%1 (%2)" ).arg( title, layerCrs );
389 if ( !abstract.isEmpty() )
390 parts <<
"<br/>" + abstract.replace( QLatin1String(
"\n" ), QLatin1String(
"<br/>" ) );
392 return parts.join( QLatin1String(
"<br/>" ) );
397 case Qt::DecorationRole:
400 return mEmptyIcon.isNull() ? QVariant() : mEmptyIcon;
402 if ( additionalIndex >= 0 )
421 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
430 if ( !
index.isValid() )
433 return Qt::ItemIsDropEnabled;
435 return Qt::ItemFlags();
438 bool isEmpty =
index.row() == 0 && mAllowEmpty;
439 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
441 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
443 if (
mCanReorder && !isEmpty && additionalIndex < 0 )
445 flags |= Qt::ItemIsDragEnabled;
450 flags |= Qt::ItemIsUserCheckable;
464 beginInsertRows(
parent, row, row + count - 1 );
465 for (
int i = row; i < row + count; ++i )
466 mLayers.insert( i - offset,
nullptr );
474 if (
parent.isValid() || row < 0 )
486 if ( row - offset >
mLayers.count() - 1 )
491 beginRemoveRows(
parent, row, row + count - 1 );
492 for (
int i = 0; i != count; ++i )
493 mLayers.removeAt( row - offset );
502 types << QStringLiteral(
"application/qgis.layermodeldata" );
508 if ( !
mCanReorder || action != Qt::MoveAction || !
data->hasFormat( QStringLiteral(
"application/qgis.layermodeldata" ) ) )
515 std::unique_ptr< QMimeData >
mimeData = std::make_unique< QMimeData >();
517 QByteArray encodedData;
518 QDataStream stream( &encodedData, QIODevice::WriteOnly );
519 QSet< QString > addedLayers;
521 for (
const QModelIndex &i : indexes )
526 if ( !addedLayers.contains(
id ) )
528 addedLayers.insert(
id );
533 mimeData->setData( QStringLiteral(
"application/qgis.layermodeldata" ), encodedData );
542 if ( action == Qt::IgnoreAction )
544 else if ( action != Qt::MoveAction )
547 QByteArray encodedData =
data->data( QStringLiteral(
"application/qgis.layermodeldata" ) );
548 QDataStream stream( &encodedData, QIODevice::ReadOnly );
549 QStringList newItems;
552 while ( !stream.atEnd() )
561 for (
const QString &text : std::as_const( newItems ) )
563 QModelIndex idx =
index( row, 0, QModelIndex() );
573 return Qt::MoveAction;
583 if ( !
index.isValid() )
586 bool isEmpty =
index.row() == 0 && mAllowEmpty;
587 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
591 case Qt::CheckStateRole:
593 if ( !isEmpty && additionalIndex < 0 )
597 emit dataChanged(
index,
index, QVector< int >() << Qt::CheckStateRole );
604 if ( !isEmpty && additionalIndex < 0 )
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
double coordinateEpoch() const
Returns the coordinate epoch, as a decimal year.
static QIcon iconForLayer(const QgsMapLayer *layer)
Returns the icon corresponding to a specified map layer.
QList< QPointer< QgsMapLayer > > mAdditionalLayers
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
void setProject(QgsProject *project)
Sets the QgsProject from which map layers are shown.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QModelIndex indexFromLayer(QgsMapLayer *layer) const
indexFromLayer returns the model index for a given layer
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.
QList< QgsMapLayer * > additionalLayers() const
Returns the list of additional layers added to 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
@ Additional
True if index corresponds to an additional (non map layer) item.
@ Layer
Stores pointer to the map layer itself.
@ LayerId
Stores the map layer ID.
@ Empty
True if index corresponds to the empty (not set) value.
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 setAdditionalLayers(const QList< QgsMapLayer * > &layers)
Sets a list of additional layers to include in the model.
void addLayers(const QList< QgsMapLayer * > &layers)
QMimeData * mimeData(const QModelIndexList &indexes) const override
QString title() const
Returns the title 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 abstract() const
Returns the abstract of the layerused by QGIS Server in GetCapabilities request.
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...
QgsCoordinateReferenceSystem crs
QgsMapLayerServerProperties * serverProperties()
Returns QGIS Server Properties for the map layer.
QgsLayerMetadata metadata
QString publicSource(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static QgsProject * instance()
Returns the QgsProject singleton instance.
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.
static QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.