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 int r =
mLayers.indexOf( layer );
111 if ( r >= 0 && mAllowEmpty )
113 return index( r, 0 );
123 if ( items == mAdditionalItems )
133 if ( !mAdditionalItems.isEmpty() )
135 beginRemoveRows( QModelIndex(), offset, offset + mAdditionalItems.count() - 1 );
136 mAdditionalItems.clear();
141 beginInsertRows( QModelIndex(), offset, offset + items.count() - 1 );
142 mAdditionalItems = items;
152 for (
const QString &layerId : layerIds )
154 QModelIndex startIndex =
index( 0, 0 );
155 QModelIndexList list = match( startIndex,
LayerIdRole, layerId, 1 );
156 if ( !list.isEmpty() )
158 QModelIndex
index = list[0];
159 beginRemoveRows( QModelIndex(),
index.row(),
index.row() );
169 if ( !layers.empty( ) )
175 beginInsertRows( QModelIndex(),
mLayers.count() + offset,
mLayers.count() + layers.count() - 1 + offset );
176 const auto constLayers = layers;
192 if ( hasIndex( row, column,
parent ) )
195 if ( row - offset >= 0 && row - offset <
mLayers.count() )
196 layer =
mLayers.at( row - offset );
198 return createIndex( row, column, layer );
201 return QModelIndex();
208 return QModelIndex();
217 return ( mAllowEmpty ? 1 : 0 ) +
mLayers.length() + mAdditionalItems.count();
229 if ( !
index.isValid() )
232 bool isEmpty =
index.row() == 0 && mAllowEmpty;
233 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
237 case Qt::DisplayRole:
240 if (
index.row() == 0 && mAllowEmpty )
243 if ( additionalIndex >= 0 )
244 return mAdditionalItems.at( additionalIndex );
250 if ( !mShowCrs || !layer->
isSpatial() || role == Qt::EditRole )
252 return layer->
name();
256 return tr(
"%1 [%2]" ).arg( layer->
name(), layer->
crs().
authid() );
262 if ( isEmpty || additionalIndex >= 0 )
266 return layer ? layer->
id() : QVariant();
271 if ( isEmpty || additionalIndex >= 0 )
274 return QVariant::fromValue<QgsMapLayer *>(
mLayers.value(
index.row() - ( mAllowEmpty ? 1 : 0 ) ) );
281 return additionalIndex >= 0;
283 case Qt::CheckStateRole:
287 if ( isEmpty || additionalIndex >= 0 )
297 case Qt::ToolTipRole:
304 if ( title.isEmpty() )
305 title = layer->
name();
306 title =
"<b>" + title +
"</b>";
309 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
312 title = tr(
"%1 (%2) " ).arg( title, layer->
crs().
authid() );
317 parts <<
"<br/>" + layer->
abstract().replace( QLatin1String(
"\n" ), QLatin1String(
"<br/>" ) );
319 return parts.join( QStringLiteral(
"<br/>" ) );
324 case Qt::DecorationRole:
326 if ( isEmpty || additionalIndex >= 0 )
342 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
351 if ( !
index.isValid() )
354 return Qt::ItemIsDropEnabled;
359 bool isEmpty =
index.row() == 0 && mAllowEmpty;
360 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
362 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
364 if (
mCanReorder && !isEmpty && additionalIndex < 0 )
366 flags |= Qt::ItemIsDragEnabled;
371 flags |= Qt::ItemIsUserCheckable;
385 beginInsertRows(
parent, row, row + count - 1 );
386 for (
int i = row; i < row + count; ++i )
387 mLayers.insert( i - offset,
nullptr );
395 if (
parent.isValid() || row < 0 )
407 if ( row - offset >
mLayers.count() - 1 )
412 beginRemoveRows(
parent, row, row + count - 1 );
413 for (
int i = 0; i != count; ++i )
414 mLayers.removeAt( row - offset );
423 types << QStringLiteral(
"application/qgis.layermodeldata" );
429 if ( !
mCanReorder || action != Qt::MoveAction || !
data->hasFormat( QStringLiteral(
"application/qgis.layermodeldata" ) ) )
436 std::unique_ptr< QMimeData >
mimeData = qgis::make_unique< QMimeData >();
438 QByteArray encodedData;
439 QDataStream stream( &encodedData, QIODevice::WriteOnly );
440 QSet< QString > addedLayers;
442 for (
const QModelIndex &i : indexes )
447 if ( !addedLayers.contains(
id ) )
449 addedLayers.insert(
id );
454 mimeData->setData( QStringLiteral(
"application/qgis.layermodeldata" ), encodedData );
463 if ( action == Qt::IgnoreAction )
465 else if ( action != Qt::MoveAction )
468 QByteArray encodedData =
data->data( QStringLiteral(
"application/qgis.layermodeldata" ) );
469 QDataStream stream( &encodedData, QIODevice::ReadOnly );
470 QStringList newItems;
473 while ( !stream.atEnd() )
482 for (
const QString &text : qgis::as_const( newItems ) )
484 QModelIndex idx =
index( row, 0, QModelIndex() );
494 return Qt::MoveAction;
499 switch ( layer->
type() )
558 if ( !
index.isValid() )
561 bool isEmpty =
index.row() == 0 && mAllowEmpty;
562 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
566 case Qt::CheckStateRole:
568 if ( !isEmpty && additionalIndex < 0 )
579 if ( !isEmpty && additionalIndex < 0 )