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   QMap<QString, Qt::CheckState>::iterator i = 
mLayersChecked.begin();
 
  119   emit dataChanged( 
index( 0, 0 ), 
index( 
rowCount() - 1, 0 ), QVector<int>() << Qt::CheckStateRole );
 
  124   int r = 
mLayers.indexOf( layer );
 
  125   if ( r >= 0 && mAllowEmpty )
 
  127   return index( r, 0 );
 
  137   if ( items == mAdditionalItems )
 
  147   if ( !mAdditionalItems.isEmpty() )
 
  149     beginRemoveRows( QModelIndex(), offset, offset + mAdditionalItems.count() - 1 );
 
  150     mAdditionalItems.clear();
 
  155   beginInsertRows( QModelIndex(), offset, offset + items.count() - 1 );
 
  156   mAdditionalItems = items;
 
  166   for ( 
const QString &layerId : layerIds )
 
  168     QModelIndex startIndex = 
index( 0, 0 );
 
  169     QModelIndexList list = match( startIndex, 
LayerIdRole, layerId, 1 );
 
  170     if ( !list.isEmpty() )
 
  172       QModelIndex 
index = list[0];
 
  173       beginRemoveRows( QModelIndex(), 
index.row(), 
index.row() );
 
  183   if ( !layers.empty( ) )
 
  189     beginInsertRows( QModelIndex(), 
mLayers.count() + offset, 
mLayers.count() + layers.count() - 1  + offset );
 
  190     const auto constLayers = layers;
 
  206   if ( hasIndex( row, column, 
parent ) )
 
  209     if ( row - offset >= 0 && row - offset < 
mLayers.count() )
 
  210       layer = 
mLayers.at( row - offset );
 
  212     return createIndex( row, column, layer );
 
  215   return QModelIndex();
 
  222   return QModelIndex();
 
  231   return ( mAllowEmpty ? 1 : 0 ) + 
mLayers.length() + mAdditionalItems.count();
 
  243   if ( !
index.isValid() )
 
  246   bool isEmpty = 
index.row() == 0 && mAllowEmpty;
 
  247   int additionalIndex = 
index.row() - ( mAllowEmpty ? 1 : 0 ) - 
mLayers.count();
 
  251     case Qt::DisplayRole:
 
  254       if ( 
index.row() == 0 && mAllowEmpty )
 
  257       if ( additionalIndex >= 0 )
 
  258         return mAdditionalItems.at( additionalIndex );
 
  264       if ( !mShowCrs || !layer->
isSpatial() || role == Qt::EditRole )
 
  266         return layer->
name();
 
  270         return tr( 
"%1 [%2]" ).arg( layer->
name(), layer->
crs().
authid() );
 
  276       if ( isEmpty || additionalIndex >= 0 )
 
  280       return layer ? layer->
id() : QVariant();
 
  285       if ( isEmpty || additionalIndex >= 0 )
 
  288       return QVariant::fromValue<QgsMapLayer *>( 
mLayers.value( 
index.row() - ( mAllowEmpty ? 1 : 0 ) ) );
 
  295       return additionalIndex >= 0;
 
  297     case Qt::CheckStateRole:
 
  301         if ( isEmpty || additionalIndex >= 0 )
 
  311     case Qt::ToolTipRole:
 
  318         if ( title.isEmpty() )
 
  319           title = layer->
name();
 
  320         title = 
"<b>" + title + 
"</b>";
 
  323           if ( 
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
 
  326             title = tr( 
"%1 (%2) " ).arg( title, layer->
crs().
authid() );
 
  331           parts << 
"<br/>" + layer->
abstract().replace( QLatin1String( 
"\n" ), QLatin1String( 
"<br/>" ) );
 
  333         return parts.join( QLatin1String( 
"<br/>" ) );
 
  338     case Qt::DecorationRole:
 
  340       if ( isEmpty || additionalIndex >= 0 )
 
  356   QHash<int, QByteArray> roles  = QAbstractItemModel::roleNames();
 
  365   if ( !
index.isValid() )
 
  368       return Qt::ItemIsDropEnabled;
 
  370       return Qt::ItemFlags();
 
  373   bool isEmpty = 
index.row() == 0 && mAllowEmpty;
 
  374   int additionalIndex = 
index.row() - ( mAllowEmpty ? 1 : 0 ) - 
mLayers.count();
 
  376   Qt::ItemFlags 
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
  378   if ( 
mCanReorder && !isEmpty && additionalIndex < 0 )
 
  380     flags |= Qt::ItemIsDragEnabled;
 
  385     flags |= Qt::ItemIsUserCheckable;
 
  399   beginInsertRows( 
parent, row, row + count - 1 );
 
  400   for ( 
int i = row; i < row + count; ++i )
 
  401     mLayers.insert( i - offset, 
nullptr );
 
  409   if ( 
parent.isValid() || row < 0 )
 
  421   if ( row - offset > 
mLayers.count() - 1 )
 
  426   beginRemoveRows( 
parent, row, row + count - 1 );
 
  427   for ( 
int i = 0; i != count; ++i )
 
  428     mLayers.removeAt( row - offset );
 
  437   types << QStringLiteral( 
"application/qgis.layermodeldata" );
 
  443   if ( !
mCanReorder || action != Qt::MoveAction || !
data->hasFormat( QStringLiteral( 
"application/qgis.layermodeldata" ) ) )
 
  450   std::unique_ptr< QMimeData > 
mimeData = qgis::make_unique< QMimeData >();
 
  452   QByteArray encodedData;
 
  453   QDataStream stream( &encodedData, QIODevice::WriteOnly );
 
  454   QSet< QString > addedLayers;
 
  456   for ( 
const QModelIndex &i : indexes )
 
  461       if ( !addedLayers.contains( 
id ) )
 
  463         addedLayers.insert( 
id );
 
  468   mimeData->setData( QStringLiteral( 
"application/qgis.layermodeldata" ), encodedData );
 
  477   if ( action == Qt::IgnoreAction )
 
  479   else if ( action != Qt::MoveAction )
 
  482   QByteArray encodedData = 
data->data( QStringLiteral( 
"application/qgis.layermodeldata" ) );
 
  483   QDataStream stream( &encodedData, QIODevice::ReadOnly );
 
  484   QStringList newItems;
 
  487   while ( !stream.atEnd() )
 
  496   for ( 
const QString &text : qgis::as_const( newItems ) )
 
  498     QModelIndex idx = 
index( row, 0, QModelIndex() );
 
  508   return Qt::MoveAction;
 
  513   switch ( layer->
type() )
 
  572   if ( !
index.isValid() )
 
  575   bool isEmpty = 
index.row() == 0 && mAllowEmpty;
 
  576   int additionalIndex = 
index.row() - ( mAllowEmpty ? 1 : 0 ) - 
mLayers.count();
 
  580     case Qt::CheckStateRole:
 
  582       if ( !isEmpty && additionalIndex < 0 )
 
  586         emit dataChanged( 
index, 
index, QVector< int >() << Qt::CheckStateRole );
 
  593       if ( !isEmpty && additionalIndex < 0 )