26   : QAbstractItemModel( parent )
 
   27   , mProject( project ? project : 
QgsProject::instance() )
 
   34   : QAbstractItemModel( parent )
 
   35   , mProject( project ? project : 
QgsProject::instance() )
 
   59   QMap<QString, Qt::CheckState>::iterator i = 
mLayersChecked.begin();
 
   71   if ( allowEmpty == mAllowEmpty )
 
   76     beginInsertRows( QModelIndex(), 0, 0 );
 
   82     beginRemoveRows( QModelIndex(), 0, 0 );
 
   94   emit dataChanged( 
index( 0, 0 ), 
index( 
rowCount() - 1, 0 ), QVector<int>() << Qt::DisplayRole );
 
   99   QList<QgsMapLayer *> layers;
 
  100   const auto constMLayers = 
mLayers;
 
  105       layers.append( layer );
 
  113   QMap<QString, Qt::CheckState>::iterator i = 
mLayersChecked.begin();
 
  122   emit dataChanged( 
index( 0, 0 ), 
index( 
rowCount() - 1, 0 ), QVector<int>() << Qt::CheckStateRole );
 
  127   int r = 
mLayers.indexOf( layer );
 
  128   if ( r >= 0 && mAllowEmpty )
 
  130   return index( r, 0 );
 
  140   if ( items == mAdditionalItems )
 
  150   if ( !mAdditionalItems.isEmpty() )
 
  152     beginRemoveRows( QModelIndex(), offset, offset + mAdditionalItems.count() - 1 );
 
  153     mAdditionalItems.clear();
 
  158   beginInsertRows( QModelIndex(), offset, offset + items.count() - 1 );
 
  159   mAdditionalItems = items;
 
  169   for ( 
const QString &layerId : layerIds )
 
  171     QModelIndex startIndex = 
index( 0, 0 );
 
  172     QModelIndexList list = match( startIndex, 
LayerIdRole, layerId, 1 );
 
  173     if ( !list.isEmpty() )
 
  175       QModelIndex 
index = list[0];
 
  176       beginRemoveRows( QModelIndex(), 
index.row(), 
index.row() );
 
  186   if ( !layers.empty( ) )
 
  192     beginInsertRows( QModelIndex(), 
mLayers.count() + offset, 
mLayers.count() + layers.count() - 1  + offset );
 
  193     const auto constLayers = layers;
 
  209   if ( hasIndex( row, column, 
parent ) )
 
  212     if ( row - offset >= 0 && row - offset < 
mLayers.count() )
 
  213       layer = 
mLayers.at( row - offset );
 
  215     return createIndex( row, column, layer );
 
  218   return QModelIndex();
 
  225   return QModelIndex();
 
  234   return ( mAllowEmpty ? 1 : 0 ) + 
mLayers.length() + mAdditionalItems.count();
 
  246   if ( !
index.isValid() )
 
  249   bool isEmpty = 
index.row() == 0 && mAllowEmpty;
 
  250   int additionalIndex = 
index.row() - ( mAllowEmpty ? 1 : 0 ) - 
mLayers.count();
 
  254     case Qt::DisplayRole:
 
  257       if ( 
index.row() == 0 && mAllowEmpty )
 
  260       if ( additionalIndex >= 0 )
 
  261         return mAdditionalItems.at( additionalIndex );
 
  267       if ( !mShowCrs || !layer->
isSpatial() || role == Qt::EditRole )
 
  269         return layer->
name();
 
  273         return tr( 
"%1 [%2]" ).arg( layer->
name(), layer->
crs().
authid() );
 
  279       if ( isEmpty || additionalIndex >= 0 )
 
  283       return layer ? layer->
id() : QVariant();
 
  288       if ( isEmpty || additionalIndex >= 0 )
 
  291       return QVariant::fromValue<QgsMapLayer *>( 
mLayers.value( 
index.row() - ( mAllowEmpty ? 1 : 0 ) ) );
 
  298       return additionalIndex >= 0;
 
  300     case Qt::CheckStateRole:
 
  304         if ( isEmpty || additionalIndex >= 0 )
 
  314     case Qt::ToolTipRole:
 
  321         if ( title.isEmpty() )
 
  322           title = layer->
name();
 
  323         title = 
"<b>" + title + 
"</b>";
 
  326           if ( 
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
 
  329             title = tr( 
"%1 (%2) " ).arg( title, layer->
crs().
authid() );
 
  334           parts << 
"<br/>" + layer->
abstract().replace( QLatin1String( 
"\n" ), QLatin1String( 
"<br/>" ) );
 
  336         return parts.join( QLatin1String( 
"<br/>" ) );
 
  341     case Qt::DecorationRole:
 
  344         return mEmptyIcon.isNull() ? QVariant() : mEmptyIcon;
 
  346       if ( additionalIndex >= 0 )
 
  362   QHash<int, QByteArray> roles  = QAbstractItemModel::roleNames();
 
  371   if ( !
index.isValid() )
 
  374       return Qt::ItemIsDropEnabled;
 
  376       return Qt::ItemFlags();
 
  379   bool isEmpty = 
index.row() == 0 && mAllowEmpty;
 
  380   int additionalIndex = 
index.row() - ( mAllowEmpty ? 1 : 0 ) - 
mLayers.count();
 
  382   Qt::ItemFlags 
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
  384   if ( 
mCanReorder && !isEmpty && additionalIndex < 0 )
 
  386     flags |= Qt::ItemIsDragEnabled;
 
  391     flags |= Qt::ItemIsUserCheckable;
 
  405   beginInsertRows( 
parent, row, row + count - 1 );
 
  406   for ( 
int i = row; i < row + count; ++i )
 
  407     mLayers.insert( i - offset, 
nullptr );
 
  415   if ( 
parent.isValid() || row < 0 )
 
  427   if ( row - offset > 
mLayers.count() - 1 )
 
  432   beginRemoveRows( 
parent, row, row + count - 1 );
 
  433   for ( 
int i = 0; i != count; ++i )
 
  434     mLayers.removeAt( row - offset );
 
  443   types << QStringLiteral( 
"application/qgis.layermodeldata" );
 
  449   if ( !
mCanReorder || action != Qt::MoveAction || !
data->hasFormat( QStringLiteral( 
"application/qgis.layermodeldata" ) ) )
 
  456   std::unique_ptr< QMimeData > 
mimeData = std::make_unique< QMimeData >();
 
  458   QByteArray encodedData;
 
  459   QDataStream stream( &encodedData, QIODevice::WriteOnly );
 
  460   QSet< QString > addedLayers;
 
  462   for ( 
const QModelIndex &i : indexes )
 
  467       if ( !addedLayers.contains( 
id ) )
 
  469         addedLayers.insert( 
id );
 
  474   mimeData->setData( QStringLiteral( 
"application/qgis.layermodeldata" ), encodedData );
 
  483   if ( action == Qt::IgnoreAction )
 
  485   else if ( action != Qt::MoveAction )
 
  488   QByteArray encodedData = 
data->data( QStringLiteral( 
"application/qgis.layermodeldata" ) );
 
  489   QDataStream stream( &encodedData, QIODevice::ReadOnly );
 
  490   QStringList newItems;
 
  493   while ( !stream.atEnd() )
 
  502   for ( 
const QString &text : std::as_const( newItems ) )
 
  504     QModelIndex idx = 
index( row, 0, QModelIndex() );
 
  514   return Qt::MoveAction;
 
  524   if ( !
index.isValid() )
 
  527   bool isEmpty = 
index.row() == 0 && mAllowEmpty;
 
  528   int additionalIndex = 
index.row() - ( mAllowEmpty ? 1 : 0 ) - 
mLayers.count();
 
  532     case Qt::CheckStateRole:
 
  534       if ( !isEmpty && additionalIndex < 0 )
 
  538         emit dataChanged( 
index, 
index, QVector< int >() << Qt::CheckStateRole );
 
  545       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 iconForLayer(const QgsMapLayer *layer)
Returns the icon corresponding to a specified map 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
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 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.
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.
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...