Skip to content

Commit e44d620

Browse files
committedNov 20, 2018
[feature][needs-docs] Allow gpkg vector rename from browser
1 parent ba81a2e commit e44d620

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed
 

‎src/providers/ogr/qgsgeopackagedataitems.cpp

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ QList<QAction *> QgsGeoPackageAbstractLayerItem::actions( QWidget * )
8181
QAction *actionDeleteLayer = new QAction( tr( "Delete Layer '%1'…" ).arg( mName ), this );
8282
connect( actionDeleteLayer, &QAction::triggered, this, &QgsGeoPackageAbstractLayerItem::deleteLayer );
8383
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+
}
8491
return lst;
8592
}
8693

@@ -515,7 +522,7 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
515522
return;
516523
}
517524

518-
if ( layersList.isEmpty() )
525+
if ( ! layersList.isEmpty() )
519526
{
520527
QgsProject::instance()->removeMapLayers( layersList );
521528
}
@@ -533,6 +540,54 @@ void QgsGeoPackageAbstractLayerItem::deleteLayer()
533540
mParent->refreshConnections();
534541
}
535542

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+
536591
}
537592
#endif
538593

@@ -718,6 +773,13 @@ bool QgsGeoPackageAbstractLayerItem::executeDeleteLayer( QString &errCause )
718773
return false;
719774
}
720775

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+
721783

722784
QgsGeoPackageVectorLayerItem::QgsGeoPackageVectorLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType )
723785
: QgsGeoPackageAbstractLayerItem( parent, name, path, uri, layerType, QStringLiteral( "ogr" ) )
@@ -737,9 +799,39 @@ bool QgsGeoPackageRasterLayerItem::executeDeleteLayer( QString &errCause )
737799
return QgsGeoPackageCollectionItem::deleteGeoPackageRasterLayer( mUri, errCause );
738800
}
739801

740-
741802
bool QgsGeoPackageVectorLayerItem::executeDeleteLayer( QString &errCause )
742803
{
743804
return ::deleteLayer( mUri, errCause );
744805
}
745806

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+

‎src/providers/ogr/qgsgeopackagedataitems.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ class QgsGeoPackageAbstractLayerItem : public QgsLayerItem
3636
* the real deletion implementation
3737
*/
3838
virtual bool executeDeleteLayer( QString &errCause );
39+
virtual bool executeRenameLayer( QString &newName, QString &errCause );
3940
#ifdef HAVE_GUI
4041
QList<QAction *> actions( QWidget *menu ) override;
4142
public slots:
4243
virtual void deleteLayer();
44+
void renameLayer();
4345
#endif
4446
};
4547

@@ -63,6 +65,7 @@ class QgsGeoPackageVectorLayerItem : public QgsGeoPackageAbstractLayerItem
6365
QgsGeoPackageVectorLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType );
6466
protected:
6567
bool executeDeleteLayer( QString &errCause ) override;
68+
bool executeRenameLayer( QString &newName, QString &errCause ) override;
6669
};
6770

6871

0 commit comments

Comments
 (0)
Please sign in to comment.