Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to create an empty GeoPackage from the context menu of the top-level GeoPackage node (fix #25116) #57876

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/gui/providers/ogr/qgsgeopackageitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QMenu>
#include <QString>
#include <QMessageBox>
#include <QFileDialog>

#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
Expand All @@ -40,6 +41,7 @@
#include "qgsmessagebar.h"
#include "qgsprovidermetadata.h"
#include "qgsogrproviderutils.h"
#include "qgsfileutils.h"

void QgsGeoPackageItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems,
Expand Down Expand Up @@ -74,13 +76,22 @@ void QgsGeoPackageItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu
connect( actionNew, &QAction::triggered, rootItem, &QgsGeoPackageRootItem::newConnection );
menu->addAction( actionNew );

QAction *actionCreateDatabase = new QAction( tr( "Create Database…" ), menu );
const QPointer< QgsGeoPackageRootItem > rootItemPointer( rootItem );

QAction *actionCreateDatabase = new QAction( tr( "Create Database…" ), menu );
connect( actionCreateDatabase, &QAction::triggered, this, [this, rootItemPointer ]
{
createDatabase( rootItemPointer );
} );
menu->addAction( actionCreateDatabase );

QAction *actionCreateDatabaseAndLayer = new QAction( tr( "Create Database and Layer…" ), menu );
connect( actionCreateDatabaseAndLayer, &QAction::triggered, this, [this, rootItemPointer ]
{
createDatabaseAndLayer( rootItemPointer );
} );
menu->addAction( actionCreateDatabaseAndLayer );

}

if ( QgsGeoPackageCollectionItem *collectionItem = qobject_cast< QgsGeoPackageCollectionItem * >( item ) )
Expand Down Expand Up @@ -344,11 +355,50 @@ void QgsGeoPackageItemGuiProvider::vacuumGeoPackageDbAction( const QString &path
}

void QgsGeoPackageItemGuiProvider::createDatabase( const QPointer< QgsGeoPackageRootItem > &item )
{
if ( item )
{
QgsSettings settings;
const QString lastUsedDir = settings.value( QStringLiteral( "UI/lastGeoPackageDir" ), QDir::homePath() ).toString();

QString filename = QFileDialog::getSaveFileName( nullptr, tr( "New GeoPackage" ),
lastUsedDir,
tr( "GeoPackage" ) + " (*.gpkg *.GPKG)" );
if ( filename.isEmpty() )
{
return;
}

filename = QgsFileUtils::ensureFileNameHasExtension( filename, QStringList() << QStringLiteral( "gpkg" ) );

const QFileInfo fileInfo( filename );
settings.setValue( QStringLiteral( "UI/lastGeoPackageDir" ), fileInfo.absoluteDir().absolutePath() );

if ( QgsProviderMetadata *ogrMetadata = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "ogr" ) ) )
{
QString error;
if ( ! ogrMetadata->createDatabase( filename, error ) )
{
QMessageBox::critical( nullptr, tr( "New GeoPackage" ), error );
return;
}

// Call QFileInfo to normalize paths, see: https://github.com/qgis/QGIS/issues/36832
if ( QgsOgrProviderUtils::saveConnection( fileInfo.filePath(), QStringLiteral( "GPKG" ) ) )
{
item->refreshConnections();
}
}
}
}

void QgsGeoPackageItemGuiProvider::createDatabaseAndLayer( const QPointer< QgsGeoPackageRootItem > &item )
{
if ( item )
{
QgsNewGeoPackageLayerDialog dialog( nullptr );
dialog.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );

if ( dialog.exec() == QDialog::Accepted )
{
// Call QFileInfo to normalize paths, see: https://github.com/qgis/QGIS/issues/36832
Expand Down
1 change: 1 addition & 0 deletions src/gui/providers/ogr/qgsgeopackageitemguiprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class QgsGeoPackageItemGuiProvider : public QObject, public QgsDataItemGuiProvid
//! Compacts (VACUUM) a geopackage database
void vacuumGeoPackageDbAction( const QString &path, const QString &name, QgsDataItemGuiContext context );
void createDatabase( const QPointer< QgsGeoPackageRootItem > &item );
void createDatabaseAndLayer( const QPointer< QgsGeoPackageRootItem > &item );

protected slots:
void renameVectorLayer( const QString &uri, const QString &key, const QStringList &tableNames,
Expand Down
Loading