27 : QAbstractItemModel( parent )
28 , mProject( project ? project :
QgsProject::instance() )
35 : QAbstractItemModel( parent )
36 , mProject( project ? project :
QgsProject::instance() )
79 QMap<QString, Qt::CheckState>::iterator i =
mLayersChecked.begin();
91 if ( allowEmpty == mAllowEmpty )
96 beginInsertRows( QModelIndex(), 0, 0 );
102 beginRemoveRows( QModelIndex(), 0, 0 );
114 emit dataChanged(
index( 0, 0 ),
index(
rowCount() - 1, 0 ), QVector<int>() << Qt::DisplayRole );
119 QList<QgsMapLayer *> layers;
120 const auto constMLayers =
mLayers;
125 layers.append( layer );
133 QMap<QString, Qt::CheckState>::iterator i =
mLayersChecked.begin();
142 emit dataChanged(
index( 0, 0 ),
index(
rowCount() - 1, 0 ), QVector<int>() << Qt::CheckStateRole );
147 int r =
mLayers.indexOf( layer );
148 if ( r >= 0 && mAllowEmpty )
150 return index( r, 0 );
160 if ( items == mAdditionalItems )
170 if ( !mAdditionalItems.isEmpty() )
172 beginRemoveRows( QModelIndex(), offset, offset + mAdditionalItems.count() - 1 );
173 mAdditionalItems.clear();
178 beginInsertRows( QModelIndex(), offset, offset + items.count() - 1 );
179 mAdditionalItems = items;
188 QStringList layerIdsToRemove;
192 layerIdsToRemove << layer->id();
201 const QString layerId = layer->id();
220 for (
const QString &layerId : layerIds )
222 QModelIndex startIndex =
index( 0, 0 );
223 QModelIndexList list = match( startIndex,
LayerIdRole, layerId, 1 );
224 if ( !list.isEmpty() )
226 QModelIndex
index = list[0];
227 beginRemoveRows( QModelIndex(),
index.row(),
index.row() );
237 if ( !layers.empty( ) )
243 beginInsertRows( QModelIndex(),
mLayers.count() + offset,
mLayers.count() + layers.count() - 1 + offset );
244 const auto constLayers = layers;
260 if ( hasIndex( row, column,
parent ) )
263 if ( row - offset >= 0 && row - offset <
mLayers.count() )
264 layer =
mLayers.at( row - offset );
266 return createIndex( row, column, layer );
269 return QModelIndex();
276 return QModelIndex();
285 return ( mAllowEmpty ? 1 : 0 ) +
mLayers.length() + mAdditionalItems.count();
297 if ( !
index.isValid() )
300 bool isEmpty =
index.row() == 0 && mAllowEmpty;
301 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
305 case Qt::DisplayRole:
308 if (
index.row() == 0 && mAllowEmpty )
311 if ( additionalIndex >= 0 )
312 return mAdditionalItems.at( additionalIndex );
318 if ( !mShowCrs || !layer->
isSpatial() || role == Qt::EditRole )
320 return layer->
name();
324 return tr(
"%1 [%2]" ).arg( layer->
name(), layer->
crs().
authid() );
330 if ( isEmpty || additionalIndex >= 0 )
334 return layer ? layer->
id() : QVariant();
339 if ( isEmpty || additionalIndex >= 0 )
342 return QVariant::fromValue<QgsMapLayer *>(
mLayers.value(
index.row() - ( mAllowEmpty ? 1 : 0 ) ) );
349 return additionalIndex >= 0;
351 case Qt::CheckStateRole:
355 if ( isEmpty || additionalIndex >= 0 )
365 case Qt::ToolTipRole:
372 if ( title.isEmpty() )
373 title = layer->
name();
374 title =
"<b>" + title +
"</b>";
377 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
380 title = tr(
"%1 (%2) " ).arg( title, layer->
crs().
authid() );
385 parts <<
"<br/>" + layer->
abstract().replace( QLatin1String(
"\n" ), QLatin1String(
"<br/>" ) );
387 return parts.join( QLatin1String(
"<br/>" ) );
392 case Qt::DecorationRole:
395 return mEmptyIcon.isNull() ? QVariant() : mEmptyIcon;
397 if ( additionalIndex >= 0 )
413 QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
422 if ( !
index.isValid() )
425 return Qt::ItemIsDropEnabled;
427 return Qt::ItemFlags();
430 bool isEmpty =
index.row() == 0 && mAllowEmpty;
431 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
433 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
435 if (
mCanReorder && !isEmpty && additionalIndex < 0 )
437 flags |= Qt::ItemIsDragEnabled;
442 flags |= Qt::ItemIsUserCheckable;
456 beginInsertRows(
parent, row, row + count - 1 );
457 for (
int i = row; i < row + count; ++i )
458 mLayers.insert( i - offset,
nullptr );
466 if (
parent.isValid() || row < 0 )
478 if ( row - offset >
mLayers.count() - 1 )
483 beginRemoveRows(
parent, row, row + count - 1 );
484 for (
int i = 0; i != count; ++i )
485 mLayers.removeAt( row - offset );
494 types << QStringLiteral(
"application/qgis.layermodeldata" );
500 if ( !
mCanReorder || action != Qt::MoveAction || !
data->hasFormat( QStringLiteral(
"application/qgis.layermodeldata" ) ) )
507 std::unique_ptr< QMimeData >
mimeData = std::make_unique< QMimeData >();
509 QByteArray encodedData;
510 QDataStream stream( &encodedData, QIODevice::WriteOnly );
511 QSet< QString > addedLayers;
513 for (
const QModelIndex &i : indexes )
518 if ( !addedLayers.contains(
id ) )
520 addedLayers.insert(
id );
525 mimeData->setData( QStringLiteral(
"application/qgis.layermodeldata" ), encodedData );
534 if ( action == Qt::IgnoreAction )
536 else if ( action != Qt::MoveAction )
539 QByteArray encodedData =
data->data( QStringLiteral(
"application/qgis.layermodeldata" ) );
540 QDataStream stream( &encodedData, QIODevice::ReadOnly );
541 QStringList newItems;
544 while ( !stream.atEnd() )
553 for (
const QString &text : std::as_const( newItems ) )
555 QModelIndex idx =
index( row, 0, QModelIndex() );
565 return Qt::MoveAction;
575 if ( !
index.isValid() )
578 bool isEmpty =
index.row() == 0 && mAllowEmpty;
579 int additionalIndex =
index.row() - ( mAllowEmpty ? 1 : 0 ) -
mLayers.count();
583 case Qt::CheckStateRole:
585 if ( !isEmpty && additionalIndex < 0 )
589 emit dataChanged(
index,
index, QVector< int >() << Qt::CheckStateRole );
596 if ( !isEmpty && additionalIndex < 0 )