Skip to content
Permalink
Browse files

New notify on gui dataitems

  • Loading branch information
elpaso committed Jul 16, 2020
1 parent 1f739a9 commit ecb5f4b31bc50b5e1f5a69802acfb1e5ef655b83
@@ -156,6 +156,15 @@ Caller takes responsibility of deleting created.
The function is replacement of :py:func:`QgsDataItem.paramWidget()`

.. versionadded:: 3.10
%End

static void notify( const QString &title, const QString &message, QgsDataItemGuiContext context, Qgis::MessageLevel level = Qgis::Info );
%Docstring
Notify the user showing a ``message`` with ``title`` and ``level``
If the context has a message bar the message will be shown in the message bar
else a message dialog will be used.

.. versionadded:: 3.16
%End
};

@@ -45,9 +45,9 @@
#include <QFileInfo>
#include <QMenu>
#include <QInputDialog>
#include <QMessageBox>
#include <QDesktopServices>
#include <QFileDialog>
#include <QMessageBox>

QString QgsAppDirectoryItemGuiProvider::name()
{
@@ -76,11 +76,11 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
QDir dir( directoryItem->dirPath() );
if ( QFileInfo::exists( dir.absoluteFilePath( name ) ) )
{
QMessageBox::critical( QgisApp::instance(), tr( "Create Directory" ), tr( "The path “%1” already exists." ).arg( QDir::toNativeSeparators( dir.absoluteFilePath( name ) ) ) );
notify( tr( "Create Directory" ), tr( "The path “%1” already exists." ).arg( QDir::toNativeSeparators( dir.absoluteFilePath( name ) ) ), context, Qgis::MessageLevel::Warning );
}
else if ( !dir.mkdir( name ) )
{
QMessageBox::critical( QgisApp::instance(), tr( "Create Directory" ), tr( "Could not create directory “%1”." ).arg( QDir::toNativeSeparators( dir.absoluteFilePath( name ) ) ) );
notify( tr( "Create Directory" ), tr( "Could not create directory “%1”." ).arg( QDir::toNativeSeparators( dir.absoluteFilePath( name ) ) ), context, Qgis::MessageLevel::Critical );
}
else
{
@@ -598,7 +598,7 @@ void QgsLayerItemGuiProvider::deleteLayers( const QStringList &itemPaths, QgsDat
Q_NOWARN_DEPRECATED_POP

if ( !res )
QMessageBox::information( QgisApp::instance(), tr( "Delete Layer" ), tr( "Item Layer %1 cannot be deleted." ).arg( item->name() ) );
notify( tr( "Delete Layer" ), tr( "Item Layer %1 cannot be deleted." ).arg( item->name() ), context, Qgis::MessageLevel::Warning );
}
}
}
@@ -736,28 +736,13 @@ void QgsFieldsItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *me
}
catch ( const QgsProviderConnectionException &ex )
{
if ( context.messageBar() )
{
context.messageBar()->pushCritical( tr( "New Field" ), tr( "Failed to add the new field to '%1': %2" ).arg( fieldsItem->tableName(), ex.what() ) );
}
else
{
QMessageBox::critical( menu, tr( "New Field" ), tr( "Failed to a add new field to '%1': %2" ).arg( fieldsItem->tableName(), ex.what() ) );
}
notify( tr( "New Field" ), tr( "Failed to add the new field to '%1': %2" ).arg( fieldsItem->tableName(), ex.what() ), context, Qgis::MessageLevel::Critical );
}
}
}
else
{
const QString message { tr( "Failed to create layer '%1'. Check application logs and user permissions." ).arg( fieldsItem->tableName() ) };
if ( context.messageBar() )
{
context.messageBar()->pushCritical( tr( "Add Field" ), message );
}
else
{
QMessageBox::critical( menu, tr( "Add Field" ), message );
}
notify( tr( "New Field" ), tr( "Failed to load layer'%1'. Check application logs and user permissions." ).arg( fieldsItem->tableName() ), context, Qgis::MessageLevel::Critical );
}
} );
menu->addAction( addColumnAction );
@@ -795,7 +780,7 @@ void QgsFieldItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
const bool supportsCascade { conn->capabilities().testFlag( QgsAbstractDatabaseProviderConnection::Capability::DeleteFieldCascade ) };
const QString itemName { item->name() };

connect( deleteFieldAction, &QAction::triggered, fieldsItem, [ md, fieldsItem, itemName, context, supportsCascade, menu ]
connect( deleteFieldAction, &QAction::triggered, fieldsItem, [ md, fieldsItem, itemName, context, supportsCascade ]
{
// Confirmation dialog
QMessageBox msgbox{QMessageBox::Icon::Question, tr( "Delete Field" ), tr( "Delete '%1' permanently?" ).arg( itemName ), QMessageBox::Ok | QMessageBox::Cancel };
@@ -818,14 +803,7 @@ void QgsFieldItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
}
catch ( const QgsProviderConnectionException &ex )
{
if ( context.messageBar() )
{
context.messageBar()->pushCritical( tr( "Delete Field" ), tr( "Failed to delete field '%1': %2" ).arg( itemName, ex.what() ) );
}
else
{
QMessageBox::critical( menu, tr( "Delete Field" ), tr( "Failed to delete field '%1': %2" ).arg( itemName, ex.what() ) );
}
notify( tr( "Delete Field" ), tr( "Failed to delete field '%1': %2" ).arg( itemName, ex.what() ), context, Qgis::MessageLevel::Critical );
}
}
} );
@@ -879,7 +857,9 @@ void QgsDatabaseItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *
const QString schemaName { dlg.schemaName() };
const QString geometryColumn { dlg.geometryColumnName() };
const QgsWkbTypes::Type geometryType { dlg.geometryType() };
const bool createSpatialIndex { dlg.createSpatialIndex() };
const bool createSpatialIndex { dlg.createSpatialIndex() &&
geometryType != QgsWkbTypes::NoGeometry &&
geometryType != QgsWkbTypes::Unknown };
const QgsCoordinateReferenceSystem crs { dlg.crs( ) };
// This flag tells to the provider that field types do not need conversion
QMap<QString, QVariant> options { { QStringLiteral( "skipConvertFields" ), true } };
@@ -889,9 +869,6 @@ void QgsDatabaseItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *
options[ QStringLiteral( "geometryColumn" ) ] = geometryColumn;
}

QString title;
QString message;

try
{
conn2->createVectorTable( schemaName, tableName, fields, geometryType, crs, true, &options );
@@ -919,23 +896,13 @@ void QgsDatabaseItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *
{
collectionItem->refresh( );
}
title = QObject::tr( "New Table Created" );
message = QObject::tr( "Table '%1' was created successfully." ).arg( tableName );
notify( QObject::tr( "New Table Created" ), QObject::tr( "Table '%1' was created successfully." ).arg( tableName ), context, Qgis::MessageLevel::Success );
}
catch ( QgsProviderConnectionException &ex )
{
title = QObject::tr( "New Table Creation Error" );
message = QObject::tr( "Error creating new table '%1': %2" ).arg( tableName, ex.what() );
notify( QObject::tr( "New Table Creation Error" ), QObject::tr( "Error creating new table '%1': %2" ).arg( tableName, ex.what() ), context, Qgis::MessageLevel::Critical );
}

if ( context.messageBar() )
{
context.messageBar()->pushSuccess( title, message );
}
else
{
QMessageBox::information( nullptr, title, message );
}
}
} );
menu->addAction( newTableAction );
@@ -188,8 +188,29 @@ void QgsGeoPackageProviderConnection::createSpatialIndex( const QString &schema,
{
QgsMessageLog::logMessage( QStringLiteral( "Schema is not supported by GPKG, ignoring" ), QStringLiteral( "OGR" ), Qgis::Info );
}

QString geometryColumnName { options.geometryColumnName };
if ( geometryColumnName.isEmpty() )
{
// Can we guess it?
try
{
const auto tp { table( schema, name ) };
geometryColumnName = tp.geometryColumn();
}
catch ( QgsProviderConnectionException & )
{
// pass
}
}

if ( geometryColumnName.isEmpty() )
{
throw QgsProviderConnectionException( QObject::tr( "Geometry column name not specified while creating spatial index" ) );
}

executeGdalSqlPrivate( QStringLiteral( "SELECT CreateSpatialIndex(%1, %2)" ).arg( QgsSqliteUtils::quotedString( name ),
QgsSqliteUtils::quotedString( ( options.geometryColumnName ) ) ) );
QgsSqliteUtils::quotedString( ( geometryColumnName ) ) ) );
}

bool QgsGeoPackageProviderConnection::spatialIndexExists( const QString &schema, const QString &name, const QString &geometryColumn ) const
@@ -45,7 +45,7 @@ QString QgsGdalItemGuiProvider::name()
return QStringLiteral( "gdal_items" );
}

void QgsGdalItemGuiProvider::onDeleteLayer()
void QgsGdalItemGuiProvider::onDeleteLayer( QgsDataItemGuiContext context )
{
QAction *s = qobject_cast<QAction *>( sender() );
QVariantMap data = s->data().toMap();
@@ -125,42 +125,41 @@ void QgsGdalItemGuiProvider::onDeleteLayer()

if ( deleted )
{
QMessageBox::information( nullptr, title, tr( "Table deleted successfully." ) );
notify( title, tr( "Table deleted successfully." ), context, Qgis::MessageLevel::Success );
if ( parent )
parent->refresh();
}
else
{
QMessageBox::warning( nullptr, title, errorMessage.isEmpty() ?
tr( "Could not delete table." ) :
tr( "Could not delete table, reason: %1." ).arg( errorMessage ) );
notify( title, errorMessage.isEmpty() ?
tr( "Could not delete table." ) :
tr( "Could not delete table, reason: %1." ).arg( errorMessage ), context, Qgis::MessageLevel::Warning );
}
}
else
{
if ( !QFile::remove( path ) )
{
QMessageBox::warning( nullptr, title, tr( "Could not delete file." ) );
notify( title, tr( "Could not delete file." ), context, Qgis::MessageLevel::Warning );
}
else
{
QMessageBox::information( nullptr, title, tr( "File deleted successfully." ) );
notify( title, tr( "File deleted successfully." ), context, Qgis::MessageLevel::Success );
if ( parent )
parent->refresh();
}
}
}
else
{
QMessageBox::warning( nullptr, title, QObject::tr( "The layer “%1” cannot be deleted because it is in the current project as “%2”,"
" remove it from the project and retry." ).arg( path, projectLayer->name() ) );
notify( title, tr( "The layer “%1” cannot be deleted because it is in the current project as “%2”,"
" remove it from the project and retry." ).arg( path, projectLayer->name() ), context, Qgis::MessageLevel::Warning );
}
}

void QgsGdalItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context )
{
Q_UNUSED( selectedItems );
Q_UNUSED( context );

if ( QgsGdalLayerItem *layerItem = qobject_cast< QgsGdalLayerItem * >( item ) )
{
@@ -175,7 +174,7 @@ void QgsGdalItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu
data.insert( QStringLiteral( "path" ), isPostgresRaster ? layerItem->name() : layerItem->path() );
data.insert( QStringLiteral( "parentItem" ), QVariant::fromValue( QPointer< QgsDataItem >( layerItem->parent() ) ) );
actionDeleteLayer->setData( data );
connect( actionDeleteLayer, &QAction::triggered, this, &QgsGdalItemGuiProvider::onDeleteLayer );
connect( actionDeleteLayer, &QAction::triggered, this, [ = ] { onDeleteLayer( context ); } );
menu->addAction( actionDeleteLayer );
}
}
@@ -46,7 +46,7 @@ class QgsGdalItemGuiProvider : public QObject, public QgsDataItemGuiProvider
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ) override;

protected slots:
void onDeleteLayer();
void onDeleteLayer( QgsDataItemGuiContext context );
};

class QgsGdalGuiProviderMetadata: public QgsProviderGuiMetadata
@@ -32,7 +32,7 @@ void QgsOgrItemGuiProvider::populateContextMenu(
QgsDataItem *item,
QMenu *menu,
const QList<QgsDataItem *> &,
QgsDataItemGuiContext )
QgsDataItemGuiContext context )
{
if ( QgsOgrLayerItem *layerItem = qobject_cast< QgsOgrLayerItem * >( item ) )
{
@@ -45,7 +45,7 @@ void QgsOgrItemGuiProvider::populateContextMenu(
data.insert( QStringLiteral( "name" ), layerItem->name() );
data.insert( QStringLiteral( "parent" ), QVariant::fromValue( QPointer< QgsDataItem >( layerItem->parent() ) ) );
actionDeleteLayer->setData( data );
connect( actionDeleteLayer, &QAction::triggered, this, &QgsOgrItemGuiProvider::onDeleteLayer );
connect( actionDeleteLayer, &QAction::triggered, this, [ = ] { onDeleteLayer( context ); } );
menu->addAction( actionDeleteLayer );
}

@@ -60,12 +60,12 @@ void QgsOgrItemGuiProvider::populateContextMenu(
data.insert( QStringLiteral( "path" ), collectionItem->path() );
data.insert( QStringLiteral( "parent" ), QVariant::fromValue( QPointer< QgsDataItem >( collectionItem->parent() ) ) );
actionDeleteCollection->setData( data );
connect( actionDeleteCollection, &QAction::triggered, this, &QgsOgrItemGuiProvider::deleteCollection );
connect( actionDeleteCollection, &QAction::triggered, this, [ = ] { deleteCollection( context ); } );
menu->addAction( actionDeleteCollection );
}
}

void QgsOgrItemGuiProvider::onDeleteLayer()
void QgsOgrItemGuiProvider::onDeleteLayer( QgsDataItemGuiContext context )
{
QAction *s = qobject_cast<QAction *>( sender() );
QVariantMap data = s->data().toMap();
@@ -106,23 +106,23 @@ void QgsOgrItemGuiProvider::onDeleteLayer()
bool res = QgsOgrProviderUtils::deleteLayer( uri, errCause );
if ( !res )
{
QMessageBox::warning( nullptr, title, errCause );
notify( title, errCause, context, Qgis::MessageLevel::Critical );
}
else
{
QMessageBox::information( nullptr, title, isSubLayer ? tr( "Layer deleted successfully." ) : tr( "File deleted successfully." ) );
notify( title, isSubLayer ? tr( "Layer deleted successfully." ) : tr( "File deleted successfully." ), context, Qgis::MessageLevel::Success );
if ( parent )
parent->refresh();
}
}
else
{
QMessageBox::warning( nullptr, title, QObject::tr( "The layer '%1' cannot be deleted because it is in the current project as '%2',"
" remove it from the project and retry." ).arg( name, projectLayer->name() ) );
notify( title, QObject::tr( "The layer '%1' cannot be deleted because it is in the current project as '%2',"
" remove it from the project and retry." ).arg( name, projectLayer->name() ), context, Qgis::MessageLevel::Warning );
}
}

void QgsOgrItemGuiProvider::deleteCollection()
void QgsOgrItemGuiProvider::deleteCollection( QgsDataItemGuiContext context )
{
QAction *s = qobject_cast<QAction *>( sender() );
QVariantMap data = s->data().toMap();
@@ -166,19 +166,19 @@ void QgsOgrItemGuiProvider::deleteCollection()
}
if ( !res )
{
QMessageBox::warning( nullptr, title, tr( "Could not delete %1." ).arg( type ) );
notify( title, tr( "Could not delete %1." ).arg( type ), context, Qgis::MessageLevel::Warning );
}
else
{
QMessageBox::information( nullptr, title, tr( "%1 deleted successfully." ).arg( typeCaps ) );
notify( title, tr( "%1 deleted successfully." ).arg( typeCaps ), context, Qgis::MessageLevel::Success );
if ( parent )
parent->refresh();
}
}
else
{
QMessageBox::warning( nullptr, title, QObject::tr( "The %1 '%2' cannot be deleted because it is in the current project as '%3',"
" remove it from the project and retry." ).arg( type, path, projectLayer->name() ) );
notify( title, tr( "The %1 '%2' cannot be deleted because it is in the current project as '%3',"
" remove it from the project and retry." ).arg( type, path, projectLayer->name() ), context, Qgis::MessageLevel::Warning );
}
}
///@endcond
@@ -40,8 +40,9 @@ class QgsOgrItemGuiProvider : public QObject, public QgsDataItemGuiProvider
QgsDataItemGuiContext context ) override;

protected slots:
void onDeleteLayer();
void deleteCollection();
void onDeleteLayer( QgsDataItemGuiContext context );
void deleteCollection( QgsDataItemGuiContext context );

};

///@endcond

0 comments on commit ecb5f4b

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