Skip to content

Commit

Permalink
Fix some PG browser item UX inconsistencies
Browse files Browse the repository at this point in the history
Fixes #39001
  • Loading branch information
elpaso committed Sep 24, 2020
1 parent e7e08fd commit 3a9eacb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -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, [ = ]
{
Expand Down
104 changes: 56 additions & 48 deletions src/providers/postgres/qgspostgresdataitemguiprovider.cpp
Expand Up @@ -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 ) )
{
Expand Down Expand Up @@ -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 );

}
Expand All @@ -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 );
}

Expand All @@ -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 );
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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() )
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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" ) );
Expand All @@ -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;
}

Expand All @@ -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" );
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions src/providers/postgres/qgspostgresdataitemguiprovider.h
Expand Up @@ -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 );

Expand Down

0 comments on commit 3a9eacb

Please sign in to comment.