@@ -81,6 +81,13 @@ QList<QAction *> QgsGeoPackageAbstractLayerItem::actions( QWidget * )
81
81
QAction *actionDeleteLayer = new QAction ( tr ( " Delete Layer '%1'…" ).arg ( mName ), this );
82
82
connect ( actionDeleteLayer, &QAction::triggered, this , &QgsGeoPackageAbstractLayerItem::deleteLayer );
83
83
lst.append ( actionDeleteLayer );
84
+ // For now, rename is only available for vectors
85
+ if ( mapLayerType () == QgsMapLayer::LayerType::VectorLayer )
86
+ {
87
+ QAction *actionRenameLayer = new QAction ( tr ( " Rename Layer '%1'…" ).arg ( mName ), this );
88
+ connect ( actionRenameLayer, &QAction::triggered, this , &QgsGeoPackageAbstractLayerItem::renameLayer );
89
+ lst.append ( actionRenameLayer );
90
+ }
84
91
return lst;
85
92
}
86
93
@@ -515,7 +522,7 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
515
522
return ;
516
523
}
517
524
518
- if ( layersList.isEmpty () )
525
+ if ( ! layersList.isEmpty () )
519
526
{
520
527
QgsProject::instance ()->removeMapLayers ( layersList );
521
528
}
@@ -533,6 +540,54 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
533
540
mParent ->refreshConnections ();
534
541
}
535
542
543
+ }
544
+
545
+ void QgsGeoPackageAbstractLayerItem::renameLayer ()
546
+ {
547
+ // Check if the layer(s) are in the registry
548
+ QList<QgsMapLayer *> layersList;
549
+ const auto mapLayers ( QgsProject::instance ()->mapLayers () );
550
+ for ( QgsMapLayer *layer : mapLayers )
551
+ {
552
+ if ( layer->publicSource () == mUri )
553
+ {
554
+ layersList << layer;
555
+ }
556
+ }
557
+
558
+ if ( ! layersList.isEmpty ( ) )
559
+ {
560
+ if ( QMessageBox::question ( nullptr , QObject::tr ( " Rename Layer" ), QObject::tr ( " The layer <b>%1</b> exists in the current project <b>%2</b>,"
561
+ " do you want to remove it from the project and rename it?" ).arg ( mName , layersList.at ( 0 )->name () ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
562
+ {
563
+ return ;
564
+ }
565
+ }
566
+ if ( ! layersList.isEmpty () )
567
+ {
568
+ QgsProject::instance ()->removeMapLayers ( layersList );
569
+ }
570
+
571
+ bool ok;
572
+ QString newName = QInputDialog::getText ( nullptr , QObject::tr ( " Rename %1" ).arg ( mName ),
573
+ QObject::tr ( " New name:" ), QLineEdit::Normal,
574
+ QString (), &ok );
575
+ if ( ! ok || newName.isEmpty () )
576
+ return ;
577
+
578
+ QString errCause;
579
+ bool res = executeRenameLayer ( newName, errCause );
580
+ if ( !res )
581
+ {
582
+ QMessageBox::warning ( nullptr , tr ( " Rename Layer" ), errCause );
583
+ }
584
+ else
585
+ {
586
+ QMessageBox::information ( nullptr , tr ( " Rename Layer" ), tr ( " Layer <b>%1</b> renamed successfully." ).arg ( mName ) );
587
+ if ( mParent )
588
+ mParent ->refreshConnections ();
589
+ }
590
+
536
591
}
537
592
#endif
538
593
@@ -718,6 +773,13 @@ bool QgsGeoPackageAbstractLayerItem::executeDeleteLayer( QString &errCause )
718
773
return false ;
719
774
}
720
775
776
+ bool QgsGeoPackageAbstractLayerItem::executeRenameLayer ( QString &newName, QString &errCause )
777
+ {
778
+ Q_UNUSED ( newName );
779
+ errCause = QObject::tr ( " The layer <b>%1</b> cannot be renamed because this feature is not yet implemented for this kind of layers." ).arg ( mName );
780
+ return false ;
781
+ }
782
+
721
783
722
784
QgsGeoPackageVectorLayerItem::QgsGeoPackageVectorLayerItem ( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType )
723
785
: QgsGeoPackageAbstractLayerItem( parent, name, path, uri, layerType, QStringLiteral( " ogr" ) )
@@ -737,9 +799,39 @@ bool QgsGeoPackageRasterLayerItem::executeDeleteLayer( QString &errCause )
737
799
return QgsGeoPackageCollectionItem::deleteGeoPackageRasterLayer ( mUri , errCause );
738
800
}
739
801
740
-
741
802
bool QgsGeoPackageVectorLayerItem::executeDeleteLayer ( QString &errCause )
742
803
{
743
804
return ::deleteLayer ( mUri , errCause );
744
805
}
745
806
807
+ bool QgsGeoPackageVectorLayerItem::executeRenameLayer ( QString &newName, QString &errCause )
808
+ {
809
+ const QVariantMap parts = QgsProviderRegistry::instance ()->decodeUri ( mProviderKey , mUri );
810
+ if ( parts.empty () || parts.value ( QStringLiteral ( " path" ) ).isNull () || parts.value ( " layerName" ).isNull () )
811
+ {
812
+ errCause = QObject::tr ( " Layer URI %1 is not valid!" ).arg ( mUri );
813
+ }
814
+ else
815
+ {
816
+ QString filePath = parts.value ( QStringLiteral ( " path" ) ).toString ();
817
+ // TODO: maybe an index?
818
+ QString oldName = parts.value ( QStringLiteral ( " layerName" ) ).toString ();
819
+
820
+ GDALDatasetH hDS = GDALOpenEx ( filePath.toUtf8 ().constData (), GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr , nullptr , nullptr );
821
+ if ( hDS )
822
+ {
823
+ QString sql ( QStringLiteral ( " ALTER TABLE \" %1\" RENAME TO \" %2\" " ).arg ( oldName, newName ) );
824
+ OGRLayerH ogrLayer ( GDALDatasetExecuteSQL ( hDS, sql.toUtf8 ().constData (), nullptr , nullptr ) );
825
+ if ( ogrLayer )
826
+ GDALDatasetReleaseResultSet ( hDS, ogrLayer );
827
+ GDALClose ( hDS );
828
+ errCause = CPLGetLastErrorMsg ( );
829
+ }
830
+ else
831
+ {
832
+ errCause = QObject::tr ( " There was an error opening %1!" ).arg ( filePath );
833
+ }
834
+ }
835
+ return errCause.isEmpty ();
836
+ }
837
+
0 commit comments