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;
90 layers.append( layer );
98 int r =
mLayers.indexOf( layer );
99 if ( r >= 0 && mAllowEmpty )
101 return index( r, 0 );
106 return static_cast<QgsMapLayer *
>( index.internalPointer() );
111 if ( items == mAdditionalItems )
121 if ( !mAdditionalItems.isEmpty() )
123 beginRemoveRows( QModelIndex(), offset, offset + mAdditionalItems.count() - 1 );
124 mAdditionalItems.clear();
129 beginInsertRows( QModelIndex(), offset, offset + items.count() - 1 );
130 mAdditionalItems = items;
140 Q_FOREACH (
const QString &layerId, layerIds )
142 QModelIndex startIndex =
index( 0, 0 );
143 QModelIndexList list = match( startIndex,
LayerIdRole, layerId, 1 );
144 if ( !list.isEmpty() )
146 QModelIndex
index = list[0];
147 beginRemoveRows( QModelIndex(), index.row(), index.row() );
149 mLayers.removeAt( index.row() - offset );
157 if ( !layers.empty( ) )
163 beginInsertRows( QModelIndex(),
mLayers.count() + offset,
mLayers.count() + layers.count() - 1 + offset );
179 if ( hasIndex( row, column, parent ) )
182 if ( row - offset >= 0 && row - offset <
mLayers.count() )
183 layer =
mLayers.at( row - offset );
185 return createIndex( row, column, layer );
188 return QModelIndex();
195 return QModelIndex();
201 if ( parent.isValid() )
204 return ( mAllowEmpty ? 1 : 0 ) +
mLayers.length() + mAdditionalItems.count();
216 if ( !index.isValid() )
219 bool isEmpty = index.row() == 0 && mAllowEmpty;
220 int additionalIndex = index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
224 case Qt::DisplayRole:
226 if ( index.row() == 0 && mAllowEmpty )
229 if ( additionalIndex >= 0 )
230 return mAdditionalItems.at( additionalIndex );
238 return layer->
name();
242 return tr(
"%1 [%2]" ).arg( layer->
name(), layer->
crs().
authid() );
248 if ( isEmpty || additionalIndex >= 0 )
252 return layer ? layer->
id() : QVariant();
257 if ( isEmpty || additionalIndex >= 0 )
260 return QVariant::fromValue<QgsMapLayer *>(
static_cast<QgsMapLayer *
>( index.internalPointer() ) );
267 return additionalIndex >= 0;
269 case Qt::CheckStateRole:
273 if ( isEmpty || additionalIndex >= 0 )
283 case Qt::ToolTipRole:
290 if ( title.isEmpty() )
291 title = layer->
name();
292 title =
"<b>" + title +
"</b>";
295 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
298 title = tr(
"%1 (%2) " ).arg( title, layer->
crs().
authid() );
303 parts <<
"<br/>" + layer->
abstract().replace( QLatin1String(
"\n" ), QLatin1String(
"<br/>" ) );
305 return parts.join( QStringLiteral(
"<br/>" ) );
310 case Qt::DecorationRole:
312 if ( isEmpty || additionalIndex >= 0 )
328 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
337 if ( !index.isValid() )
342 bool isEmpty = index.row() == 0 && mAllowEmpty;
343 int additionalIndex = index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
345 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
348 flags |= Qt::ItemIsUserCheckable;
355 switch ( layer->
type() )
409 bool isEmpty = index.row() == 0 && mAllowEmpty;
410 int additionalIndex = index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
412 if ( role == Qt::CheckStateRole && !isEmpty && additionalIndex < 0 )
416 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.
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.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
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.
Reads and writes project states.
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.