Skip to content
Permalink
Browse files

Merge pull request #39006 from elpaso/bugfix-gh39001-pg-browser-item-…

…issues

Fix some PG browser item UX inconsistencies
  • Loading branch information
elpaso committed Sep 24, 2020
2 parents 5691357 + 3a9eacb commit 6bd03c3ed232637f5b28c582a16575cd2d1e240c
@@ -528,8 +528,8 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
selectedDeletableItemPaths.append( qobject_cast<QgsLayerItem *>( selectedItem )->uri() );
}

const QString deleteText = selectedDeletableItemPaths.count() == 1 ? tr( "Delete Layer" )
: tr( "Delete Selected Layers" );
const QString deleteText = selectedDeletableItemPaths.count() == 1 ? tr( "Delete Layer" )
: tr( "Delete Selected Layers" );
QAction *deleteAction = new QAction( deleteText, menu );
connect( deleteAction, &QAction::triggered, this, [ = ]
{
@@ -27,7 +27,7 @@
#include <QMessageBox>


void QgsPostgresDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
void QgsPostgresDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext context )
{
if ( QgsPGRootItem *rootItem = qobject_cast< QgsPGRootItem * >( item ) )
{
@@ -63,7 +63,7 @@ void QgsPostgresDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
menu->addSeparator();

QAction *actionCreateSchema = new QAction( tr( "New Schema…" ), this );
connect( actionCreateSchema, &QAction::triggered, this, [connItem] { createSchema( connItem ); } );
connect( actionCreateSchema, &QAction::triggered, this, [connItem, context] { createSchema( connItem, context ); } );
menu->addAction( actionCreateSchema );

}
@@ -77,11 +77,11 @@ void QgsPostgresDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
menu->addSeparator();

QAction *actionRename = new QAction( tr( "Rename Schema…" ), this );
connect( actionRename, &QAction::triggered, this, [schemaItem] { renameSchema( schemaItem ); } );
connect( actionRename, &QAction::triggered, this, [schemaItem, context] { renameSchema( schemaItem, context ); } );
menu->addAction( actionRename );

QAction *actionDelete = new QAction( tr( "Delete Schema" ), this );
connect( actionDelete, &QAction::triggered, this, [schemaItem] { deleteSchema( schemaItem ); } );
QAction *actionDelete = new QAction( tr( "Delete Schema" ), this );
connect( actionDelete, &QAction::triggered, this, [schemaItem, context] { deleteSchema( schemaItem, context ); } );
menu->addAction( actionDelete );
}

@@ -91,20 +91,20 @@ void QgsPostgresDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
QString typeName = layerInfo.isView ? tr( "View" ) : tr( "Table" );

QAction *actionRenameLayer = new QAction( tr( "Rename %1…" ).arg( typeName ), this );
connect( actionRenameLayer, &QAction::triggered, this, [layerItem] { renameLayer( layerItem ); } );
connect( actionRenameLayer, &QAction::triggered, this, [layerItem, context] { renameLayer( layerItem, context ); } );
menu->addAction( actionRenameLayer );

if ( !layerInfo.isView )
{
QAction *actionTruncateLayer = new QAction( tr( "Truncate %1" ).arg( typeName ), this );
connect( actionTruncateLayer, &QAction::triggered, this, [layerItem] { truncateTable( layerItem ); } );
QAction *actionTruncateLayer = new QAction( tr( "Truncate %1" ).arg( typeName ), this );
connect( actionTruncateLayer, &QAction::triggered, this, [layerItem, context] { truncateTable( layerItem, context ); } );
menu->addAction( actionTruncateLayer );
}

if ( layerInfo.isMaterializedView )
{
QAction *actionRefreshMaterializedView = new QAction( tr( "Refresh Materialized View" ), this );
connect( actionRefreshMaterializedView, &QAction::triggered, this, [layerItem] { refreshMaterializedView( layerItem ); } );
QAction *actionRefreshMaterializedView = new QAction( tr( "Refresh Materialized View" ), this );
connect( actionRefreshMaterializedView, &QAction::triggered, this, [layerItem, context] { refreshMaterializedView( layerItem, context ); } );
menu->addAction( actionRefreshMaterializedView );
}
}
@@ -116,23 +116,23 @@ bool QgsPostgresDataItemGuiProvider::deleteLayer( QgsLayerItem *item, QgsDataIte
if ( QgsPGLayerItem *layerItem = qobject_cast< QgsPGLayerItem * >( item ) )
{
const QgsPostgresLayerProperty &layerInfo = layerItem->layerInfo();
QString typeName = layerInfo.isView ? tr( "View" ) : tr( "Table" );
const QString typeName = layerInfo.isView ? tr( "View" ) : tr( "Table" );

if ( QMessageBox::question( nullptr, tr( "Delete %1" ).arg( typeName ),
QObject::tr( "Are you sure you want to delete %1.%2?" ).arg( layerInfo.schemaName, layerInfo.tableName ),
QObject::tr( "Are you sure you want to delete %1 '%2.%3'?" ).arg( typeName.toLower(), layerInfo.schemaName, layerInfo.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return false;

QString errCause;
bool res = QgsPostgresUtils::deleteLayer( layerItem->uri(), errCause );
const bool res = QgsPostgresUtils::deleteLayer( layerItem->uri(), errCause );
if ( !res )
{
notify( tr( "Delete %1" ).arg( typeName ), errCause, context, Qgis::MessageLevel::Warning );
return false;
}
else
{
notify( tr( "Delete %1" ).arg( typeName ), tr( "%1 deleted successfully." ).arg( typeName ), context, Qgis::MessageLevel::Success );
notify( tr( "Delete %1" ).arg( typeName ), tr( "%1 '%2' deleted successfully." ).arg( typeName, layerInfo.tableName ), context, Qgis::MessageLevel::Success );
if ( layerItem->parent() )
layerItem->parent()->refresh();
return true;
@@ -226,7 +226,7 @@ void QgsPostgresDataItemGuiProvider::refreshConnection( QgsDataItem *item )
item->parent()->refreshConnections();
}

void QgsPostgresDataItemGuiProvider::createSchema( QgsDataItem *item )
void QgsPostgresDataItemGuiProvider::createSchema( QgsDataItem *item, QgsDataItemGuiContext context )
{
QString schemaName = QInputDialog::getText( nullptr, tr( "Create Schema" ), tr( "Schema name:" ) );
if ( schemaName.isEmpty() )
@@ -236,7 +236,7 @@ void QgsPostgresDataItemGuiProvider::createSchema( QgsDataItem *item )
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "New Schema" ), tr( "Unable to create schema." ) );
notify( tr( "New Schema" ), tr( "Unable to create schema." ), context, Qgis::MessageLevel::Warning );
return;
}

@@ -246,37 +246,39 @@ void QgsPostgresDataItemGuiProvider::createSchema( QgsDataItem *item )
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( nullptr, tr( "New Schema" ), tr( "Unable to create schema %1\n%2" ).arg( schemaName,
result.PQresultErrorMessage() ) );
notify( tr( "New Schema" ), tr( "Unable to create schema '%1'\n%2" ).arg( schemaName,
result.PQresultErrorMessage() ), context, Qgis::MessageLevel::Warning );
conn->unref();
return;
}

conn->unref();

notify( tr( "New Schema" ), tr( "Schema '%1' created successfully." ).arg( schemaName ), context, Qgis::MessageLevel::Success );

item->refresh();
// the parent should be updated
if ( item->parent() )
item->parent()->refreshConnections();
}


void QgsPostgresDataItemGuiProvider::deleteSchema( QgsPGSchemaItem *schemaItem )
void QgsPostgresDataItemGuiProvider::deleteSchema( QgsPGSchemaItem *schemaItem, QgsDataItemGuiContext context )
{
// check if schema contains tables/views
QgsDataSourceUri uri = QgsPostgresConn::connUri( schemaItem->connectionName() );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Delete Schema" ), tr( "Unable to delete schema." ) );
notify( tr( "Delete Schema" ), tr( "Unable to delete schema." ), context, Qgis::MessageLevel::Warning );
return;
}

QString sql = QStringLiteral( "SELECT table_name FROM information_schema.tables WHERE table_schema='%1'" ).arg( schemaItem->name() );
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
QMessageBox::warning( nullptr, tr( "Delete Schema" ), tr( "Unable to delete schema." ) );
notify( tr( "Delete Schema" ), tr( "Unable to delete schema." ), context, Qgis::MessageLevel::Warning );
conn->unref();
return;
}
@@ -310,7 +312,7 @@ void QgsPostgresDataItemGuiProvider::deleteSchema( QgsPGSchemaItem *schemaItem )
else
{
if ( QMessageBox::question( nullptr, QObject::tr( "Delete Schema" ),
QObject::tr( "Are you sure you want to delete the schema '%1'?" ).arg( schemaItem->name() ),
QObject::tr( "Are you sure you want to delete schema '%1'?" ).arg( schemaItem->name() ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;
}
@@ -319,17 +321,17 @@ void QgsPostgresDataItemGuiProvider::deleteSchema( QgsPGSchemaItem *schemaItem )
bool res = QgsPostgresUtils::deleteSchema( schemaItem->name(), uri, errCause, count > 0 );
if ( !res )
{
QMessageBox::warning( nullptr, tr( "Delete Schema" ), errCause );
notify( tr( "Delete Schema" ), tr( "Unable to delete schema: '%1'." ).arg( errCause ), context, Qgis::MessageLevel::Warning );
}
else
{
QMessageBox::information( nullptr, tr( "Delete Schema" ), tr( "Schema deleted successfully." ) );
notify( tr( "Delete Schema" ), tr( "Schema '%1' deleted successfully." ).arg( schemaItem->name() ), context, Qgis::MessageLevel::Success );
if ( schemaItem->parent() )
schemaItem->parent()->refreshConnections();
schemaItem->parent()->refresh();
}
}

void QgsPostgresDataItemGuiProvider::renameSchema( QgsPGSchemaItem *schemaItem )
void QgsPostgresDataItemGuiProvider::renameSchema( QgsPGSchemaItem *schemaItem, QgsDataItemGuiContext context )
{
QgsNewNameDialog dlg( tr( "schema '%1'" ).arg( schemaItem->name() ), schemaItem->name() );
dlg.setWindowTitle( tr( "Rename Schema" ) );
@@ -341,7 +343,7 @@ void QgsPostgresDataItemGuiProvider::renameSchema( QgsPGSchemaItem *schemaItem )
QgsPostgresConn *conn = QgsPostgresConn::connectDb( uri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Rename Schema" ), tr( "Unable to rename schema." ) );
notify( tr( "Rename Schema" ), tr( "Unable to rename schema." ), context, Qgis::MessageLevel::Warning );
return;
}

@@ -352,19 +354,21 @@ void QgsPostgresDataItemGuiProvider::renameSchema( QgsPGSchemaItem *schemaItem )
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( nullptr, tr( "Rename Schema" ), tr( "Unable to rename schema %1\n%2" ).arg( schemaName,
result.PQresultErrorMessage() ) );
notify( tr( "Rename Schema" ), tr( "Unable to rename schema '%1'\n%2" ).arg( schemaItem->name(),
result.PQresultErrorMessage() ), context, Qgis::MessageLevel::Warning );
conn->unref();
return;
}

notify( tr( "Rename Schema" ), tr( "Schema '%1' renamed correctly to '%2'." ).arg( schemaItem->name(),
dlg.name() ), context, Qgis::MessageLevel::Success );

conn->unref();
QMessageBox::information( nullptr, tr( "Rename Schema" ), tr( "Schema renamed successfully." ) );
if ( schemaItem->parent() )
schemaItem->parent()->refreshConnections();
schemaItem->parent()->refresh();
}

void QgsPostgresDataItemGuiProvider::renameLayer( QgsPGLayerItem *layerItem )
void QgsPostgresDataItemGuiProvider::renameLayer( QgsPGLayerItem *layerItem, QgsDataItemGuiContext context )
{
const QgsPostgresLayerProperty &layerInfo = layerItem->layerInfo();
QString typeName = layerInfo.isView ? tr( "View" ) : tr( "Table" );
@@ -389,7 +393,7 @@ void QgsPostgresDataItemGuiProvider::renameLayer( QgsPGLayerItem *layerItem )
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Rename %1" ).arg( typeName ), tr( "Unable to rename %1." ).arg( lowerTypeName ) );
notify( tr( "Rename %1" ).arg( typeName ), tr( "Unable to rename '%1'." ).arg( lowerTypeName ), context, Qgis::MessageLevel::Warning );
return;
}

@@ -408,30 +412,34 @@ void QgsPostgresDataItemGuiProvider::renameLayer( QgsPGLayerItem *layerItem )
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( nullptr, tr( "Rename %1" ).arg( typeName ), tr( "Unable to rename %1 %2\n%3" ).arg( lowerTypeName, layerItem->name(),
result.PQresultErrorMessage() ) );
notify( tr( "Rename %1" ).arg( typeName ), tr( "Unable to rename '%1' %2\n%3" ).arg( lowerTypeName, layerItem->name(),
result.PQresultErrorMessage() ), context, Qgis::MessageLevel::Warning );
conn->unref();
return;
}

notify( tr( "Rename %1" ).arg( typeName ), tr( "%1 '%2' renamed correctly to '%3'." ).arg( typeName, oldName, newName ),
context, Qgis::MessageLevel::Success );

conn->unref();

if ( layerItem->parent() )
layerItem->parent()->refreshConnections();
layerItem->parent()->refresh();
}

void QgsPostgresDataItemGuiProvider::truncateTable( QgsPGLayerItem *layerItem )
void QgsPostgresDataItemGuiProvider::truncateTable( QgsPGLayerItem *layerItem, QgsDataItemGuiContext context )
{
const QgsPostgresLayerProperty &layerInfo = layerItem->layerInfo();
if ( QMessageBox::question( nullptr, QObject::tr( "Truncate Table" ),
QObject::tr( "Are you sure you want to truncate %1.%2?\n\nThis will delete all data within the table." ).arg( layerInfo.schemaName, layerInfo.tableName ),
QObject::tr( "Are you sure you want to truncate \"%1.%2\"?\n\nThis will delete all data within the table." ).arg( layerInfo.schemaName, layerInfo.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QgsDataSourceUri dsUri( layerItem->uri() );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Truncate Table" ), tr( "Unable to truncate table." ) );
notify( tr( "Truncate Table" ), tr( "Unable to truncate table." ), context, Qgis::MessageLevel::Warning );
return;
}

@@ -449,29 +457,29 @@ void QgsPostgresDataItemGuiProvider::truncateTable( QgsPGLayerItem *layerItem )
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( nullptr, tr( "Truncate Table" ), tr( "Unable to truncate %1\n%2" ).arg( layerItem->name(),
result.PQresultErrorMessage() ) );
notify( tr( "Truncate Table" ), tr( "Unable to truncate '%1'\n%2" ).arg( tableName,
result.PQresultErrorMessage() ), context, Qgis::MessageLevel::Warning );
conn->unref();
return;
}

conn->unref();
QMessageBox::information( nullptr, tr( "Truncate Table" ), tr( "Table truncated successfully." ) );
notify( tr( "Truncate Table" ), tr( "Table '%1' truncated successfully." ).arg( tableName ), context, Qgis::MessageLevel::Success );
}

void QgsPostgresDataItemGuiProvider::refreshMaterializedView( QgsPGLayerItem *layerItem )
void QgsPostgresDataItemGuiProvider::refreshMaterializedView( QgsPGLayerItem *layerItem, QgsDataItemGuiContext context )
{
const QgsPostgresLayerProperty &layerInfo = layerItem->layerInfo();
if ( QMessageBox::question( nullptr, QObject::tr( "Refresh Materialized View" ),
QObject::tr( "Are you sure you want to refresh the materialized view %1.%2?\n\nThis will update all data within the table." ).arg( layerInfo.schemaName, layerInfo.tableName ),
QObject::tr( "Are you sure you want to refresh the materialized view \"%1.%2\"?\n\nThis will update all data within the table." ).arg( layerInfo.schemaName, layerInfo.tableName ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

QgsDataSourceUri dsUri( layerItem->uri() );
QgsPostgresConn *conn = QgsPostgresConn::connectDb( dsUri.connectionInfo( false ), false );
if ( !conn )
{
QMessageBox::warning( nullptr, tr( "Refresh View" ), tr( "Unable to refresh the view." ) );
notify( tr( "Refresh View" ), tr( "Unable to refresh the view." ), context, Qgis::MessageLevel::Warning );
return;
}

@@ -489,14 +497,14 @@ void QgsPostgresDataItemGuiProvider::refreshMaterializedView( QgsPGLayerItem *la
QgsPostgresResult result( conn->PQexec( sql ) );
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
{
QMessageBox::warning( nullptr, tr( "Refresh View" ), tr( "Unable to refresh view %1\n%2" ).arg( layerItem->name(),
result.PQresultErrorMessage() ) );
notify( tr( "Refresh View" ), tr( "Unable to refresh the view '%1'\n%2" ).arg( tableRef,
result.PQresultErrorMessage() ), context, Qgis::MessageLevel::Warning );
conn->unref();
return;
}

conn->unref();
QMessageBox::information( nullptr, tr( "Refresh View" ), tr( "Materialized view refreshed successfully." ) );
notify( tr( "Refresh View" ), tr( "Materialized view '%1' refreshed successfully." ).arg( tableName ), context, Qgis::MessageLevel::Success );
}

void QgsPostgresDataItemGuiProvider::saveConnections()
@@ -43,12 +43,12 @@ class QgsPostgresDataItemGuiProvider : public QObject, public QgsDataItemGuiProv
static void editConnection( QgsDataItem *item );
static void deleteConnection( QgsDataItem *item );
static void refreshConnection( QgsDataItem *item );
static void createSchema( QgsDataItem *item );
static void deleteSchema( QgsPGSchemaItem *schemaItem );
static void renameSchema( QgsPGSchemaItem *schemaItem );
static void renameLayer( QgsPGLayerItem *layerItem );
static void truncateTable( QgsPGLayerItem *layerItem );
static void refreshMaterializedView( QgsPGLayerItem *layerItem );
static void createSchema( QgsDataItem *item, QgsDataItemGuiContext context );
static void deleteSchema( QgsPGSchemaItem *schemaItem, QgsDataItemGuiContext context );
static void renameSchema( QgsPGSchemaItem *schemaItem, QgsDataItemGuiContext context );
static void renameLayer( QgsPGLayerItem *layerItem, QgsDataItemGuiContext context );
static void truncateTable( QgsPGLayerItem *layerItem, QgsDataItemGuiContext context );
static void refreshMaterializedView( QgsPGLayerItem *layerItem, QgsDataItemGuiContext context );
static void saveConnections();
static void loadConnections( QgsDataItem *item );

0 comments on commit 6bd03c3

Please sign in to comment.
You can’t perform that action at this time.