35void QgsDataItemGuiProviderUtils::deleteConnectionsPrivate(
const QStringList &connectionNames,
const std::function<
void(
const QString & )> &deleteConnection, QPointer<QgsDataItem> firstParent )
37 if ( connectionNames.size() > 1 )
39 if ( QMessageBox::question(
nullptr, QObject::tr(
"Remove Connections" ), QObject::tr(
"Are you sure you want to remove all %1 selected connections?" ).arg( connectionNames.size() ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
44 if ( QMessageBox::question(
nullptr, QObject::tr(
"Remove Connection" ), QObject::tr(
"Are you sure you want to remove the connection to “%1”?" ).arg( connectionNames.at( 0 ) ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
48 for (
const QString &connectionName : std::as_const( connectionNames ) )
50 deleteConnection( connectionName );
54 firstParent->refreshConnections();
60 QString newConnectionName( name );
61 while ( connectionNames.contains( newConnectionName ) )
64 newConnectionName = QObject::tr(
"%1 (copy %2)" ).arg( name ).arg( i );
67 return newConnectionName;
70bool QgsDataItemGuiProviderUtils::handleDropUriForConnection( std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection,
const QgsMimeDataUtils::Uri &sourceUri,
const QString &destinationSchema,
QgsDataItemGuiContext context,
const QString &shortTitle,
const QString &longTitle,
const QVariantMap &destinationProviderOptions,
const std::function<
void()> &onSuccessfulCompletion,
const std::function<
void(
Qgis::VectorExportResult,
const QString & )> &onError, QObject *connectionContext )
75 const QString connectionUri = connection->uri();
76 const QString connectionProvider = connection->providerKey();
85 std::unique_ptr< QgsVectorLayerExporterTask > exportTask = dialog.
createExporterTask( destinationProviderOptions );
89 const QString destSchema = dialog.
schema();
90 const QString destTableName = dialog.
tableName();
93 auto pushError = [shortTitle, longTitle, context](
const QString &error ) {
95 QPushButton *detailsButton =
new QPushButton( QObject::tr(
"Details…" ) );
96 QObject::connect( detailsButton, &QPushButton::clicked, detailsButton, [longTitle, error] {
102 item->layout()->addWidget( detailsButton );
107 QObject::connect( exportTask.get(), &
QgsVectorLayerExporterTask::exportComplete, connectionContext, [onSuccessfulCompletion, connectionUri, longTitle, pushError, connectionProvider, destSchema, destTableName, tableComment, shortTitle, context]() {
108 if ( !tableComment.isEmpty() )
110 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection;
113 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( connectionProvider );
114 connection.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionUri, {} ) ) );
116 catch ( QgsProviderConnectionException &e )
118 pushError( QObject::tr(
"Could not retrieve connection details:\n\n%1" ).arg( e.what() ) );
124 connection->setTableComment( destSchema, destTableName, tableComment );
126 catch ( QgsProviderConnectionException &e )
128 pushError( QObject::tr(
"Failed to set new table comment!\n\n" ) + e.what() );
134 onSuccessfulCompletion();
139 if ( error != Qgis::VectorExportResult::UserCanceled )
141 pushError( QObject::tr(
"Failed to import layer!\n\n" ) + errorMessage );
144 onError( error, errorMessage );
156 const QString connectionUri = connection->uri();
157 const QString connectionProvider = connection->providerKey();
162 if ( !dialog.exec() )
165 std::unique_ptr< QgsVectorLayerExporterTask > exportTask = dialog.
createExporterTask( destinationProviderOptions );
169 const QString destSchema = dialog.
schema();
170 const QString destTableName = dialog.
tableName();
173 auto pushError = [shortTitle, longTitle, context](
const QString &error ) {
175 QPushButton *detailsButton =
new QPushButton( QObject::tr(
"Details…" ) );
176 QObject::connect( detailsButton, &QPushButton::clicked, detailsButton, [longTitle, error] {
182 item->layout()->addWidget( detailsButton );
187 QObject::connect( exportTask.get(), &
QgsVectorLayerExporterTask::exportComplete, connectionContext, [onSuccessfulCompletion, connectionUri, longTitle, pushError, connectionProvider, destSchema, destTableName, tableComment, shortTitle, context]() {
188 if ( !tableComment.isEmpty() )
190 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection;
193 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( connectionProvider );
194 connection.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionUri, {} ) ) );
196 catch ( QgsProviderConnectionException &e )
198 pushError( QObject::tr(
"Could not retrieve connection details:\n\n%1" ).arg( e.what() ) );
204 connection->setTableComment( destSchema, destTableName, tableComment );
206 catch ( QgsProviderConnectionException &e )
208 pushError( QObject::tr(
"Failed to set new table comment!\n\n" ) + e.what() );
214 onSuccessfulCompletion();
219 if ( error != Qgis::VectorExportResult::UserCanceled )
221 pushError( QObject::tr(
"Failed to import layer!\n\n" ) + errorMessage );
224 onError( error, errorMessage );
233 bool foundExistingManageMenu =
false;
234 const QList<QAction *> actions = mainMenu->actions();
235 for ( QAction *action : actions )
237 if ( action->text() == subMenuName )
239 action->menu()->addAction( actionToAdd );
240 foundExistingManageMenu =
true;
244 if ( !foundExistingManageMenu )
246 QMenu *addSubMenu =
new QMenu( subMenuName, mainMenu );
247 addSubMenu->addAction( actionToAdd );
248 mainMenu->addMenu( addSubMenu );
254 const QVector<QgsDataItem *> constChildren { item->
children() };
257 if (
c->name() == name )
@ NotPopulated
Children not yet created.
VectorExportResult
Vector layer export result codes.
@ Warning
Warning message.
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
Encapsulates the context in which a QgsDataItem is shown within the application GUI.
QgsMessageBar * messageBar() const
Returns the associated message bar.
QgsMapCanvas * mapCanvas() const
Returns the map canvas associated with the item.
static void refreshChildWithName(QgsDataItem *item, const QString &name)
Refresh child of item that has give name.
static void handleImportVectorLayerForConnection(std::unique_ptr< QgsAbstractDatabaseProviderConnection > connection, const QString &destinationSchema, QgsDataItemGuiContext context, const QString &shortTitle, const QString &longTitle, const QVariantMap &destinationProviderOptions, const std::function< void()> &onSuccessfulCompletion, const std::function< void(Qgis::VectorExportResult error, const QString &errorMessage)> &onError, QObject *connectionContext)
Handles importing a vector layer for connection items.
static const QString uniqueName(const QString &name, const QStringList &connectionNames)
Check if connection with name exists in connectionNames list and then try to append a number to it to...
static bool handleDropUriForConnection(std::unique_ptr< QgsAbstractDatabaseProviderConnection > connection, const QgsMimeDataUtils::Uri &sourceUri, const QString &destinationSchema, QgsDataItemGuiContext context, const QString &shortTitle, const QString &longTitle, const QVariantMap &destinationProviderOptions, const std::function< void()> &onSuccessfulCompletion, const std::function< void(Qgis::VectorExportResult error, const QString &errorMessage)> &onError, QObject *connectionContext)
Handles dropping a vector layer for connection items.
static void addToSubMenu(QMenu *mainMenu, QAction *actionToAdd, const QString &subMenuName)
Add an actionToAdd to the sub menu with subMenuName in mainMenu.
Base class for all items in the model.
QVector< QgsDataItem * > children() const
A generic dialog for customising vector layer import options for database connections.
void setDestinationSchema(const QString &schema)
Sets the destination schema for the new table.
std::unique_ptr< QgsVectorLayerExporterTask > createExporterTask(const QVariantMap &extraProviderOptions=QVariantMap())
Creates a new exporter task to match the settings defined in the dialog.
void setMapCanvas(QgsMapCanvas *canvas)
Sets a map canvas to associate with the dialog.
void setSourceUri(const QgsMimeDataUtils::Uri &uri)
Sets the source table uri.
QString schema() const
Returns the destination schema.
QString tableName() const
Returns the destination table name.
QString tableComment() const
Returns the optional comment to use for the new table.
Represents an item shown within a QgsMessageBar widget.
QgsMessageBarItem * pushWidget(QWidget *widget, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=0)
Display a widget as a message on the bar, after hiding the currently visible one and putting it in a ...
void pushSuccess(const QString &title, const QString &message)
Pushes a success message with default timeout to the message bar.
Interface for showing messages from QGIS in GUI independent way.
virtual void showMessage(bool blocking=true)=0
display the message to the user and deletes itself
static QgsMessageOutput * createMessageOutput()
function that returns new class derived from QgsMessageOutput (don't forget to delete it then if show...
virtual void setMessage(const QString &message, MessageType msgType)=0
Sets message, it won't be displayed until.
virtual void setTitle(const QString &title)=0
Sets title for the messages.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
void exportComplete()
Emitted when exporting the layer is successfully completed.
void errorOccurred(Qgis::VectorExportResult error, const QString &errorMessage)
Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c