35void QgsDataItemGuiProviderUtils::deleteConnectionsPrivate(
const QStringList &connectionNames,
const std::function<
void(
const QString & )> &deleteConnection, QPointer<QgsDataItem> firstParent )
37 if ( connectionNames.size() > 1 )
40 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 )
47 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 )
52 for (
const QString &connectionName : std::as_const( connectionNames ) )
54 deleteConnection( connectionName );
58 firstParent->refreshConnections();
64 QString newConnectionName( name );
65 while ( connectionNames.contains( newConnectionName ) )
68 newConnectionName = QObject::tr(
"%1 (copy %2)" ).arg( name ).arg( i );
71 return newConnectionName;
75 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection,
77 const QString &destinationSchema,
79 const QString &shortTitle,
80 const QString &longTitle,
81 const QVariantMap &destinationProviderOptions,
82 const std::function<
void()> &onSuccessfulCompletion,
84 QObject *connectionContext
90 const QString connectionUri = connection->uri();
91 const QString connectionProvider = connection->providerKey();
100 std::unique_ptr< QgsVectorLayerExporterTask > exportTask = dialog.
createExporterTask( destinationProviderOptions );
104 const QString destSchema = dialog.
schema();
105 const QString destTableName = dialog.
tableName();
108 auto pushError = [shortTitle, longTitle, context](
const QString &error ) {
110 QPushButton *detailsButton =
new QPushButton( QObject::tr(
"Details…" ) );
111 QObject::connect( detailsButton, &QPushButton::clicked, detailsButton, [longTitle, error] {
117 item->layout()->addWidget( detailsButton );
123 connect( exportTask.get(), &
QgsVectorLayerExporterTask::exportComplete, connectionContext, [onSuccessfulCompletion, connectionUri, longTitle, pushError, connectionProvider, destSchema, destTableName, tableComment, shortTitle, context]() {
124 if ( !tableComment.isEmpty() )
126 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection;
129 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( connectionProvider );
130 connection.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionUri, {} ) ) );
132 catch ( QgsProviderConnectionException &e )
134 pushError( QObject::tr(
"Could not retrieve connection details:\n\n%1" ).arg( e.what() ) );
140 connection->setTableComment( destSchema, destTableName, tableComment );
142 catch ( QgsProviderConnectionException &e )
144 pushError( QObject::tr(
"Failed to set new table comment!\n\n" ) + e.what() );
150 onSuccessfulCompletion();
155 if ( error != Qgis::VectorExportResult::UserCanceled )
157 pushError( QObject::tr(
"Failed to import layer!\n\n" ) + errorMessage );
160 onError( error, errorMessage );
168 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection,
169 const QString &destinationSchema,
171 const QString &shortTitle,
172 const QString &longTitle,
173 const QVariantMap &destinationProviderOptions,
174 const std::function<
void()> &onSuccessfulCompletion,
176 QObject *connectionContext
182 const QString connectionUri = connection->uri();
183 const QString connectionProvider = connection->providerKey();
188 if ( !dialog.exec() )
191 std::unique_ptr< QgsVectorLayerExporterTask > exportTask = dialog.
createExporterTask( destinationProviderOptions );
195 const QString destSchema = dialog.
schema();
196 const QString destTableName = dialog.
tableName();
199 auto pushError = [shortTitle, longTitle, context](
const QString &error ) {
201 QPushButton *detailsButton =
new QPushButton( QObject::tr(
"Details…" ) );
202 QObject::connect( detailsButton, &QPushButton::clicked, detailsButton, [longTitle, error] {
208 item->layout()->addWidget( detailsButton );
214 connect( exportTask.get(), &
QgsVectorLayerExporterTask::exportComplete, connectionContext, [onSuccessfulCompletion, connectionUri, longTitle, pushError, connectionProvider, destSchema, destTableName, tableComment, shortTitle, context]() {
215 if ( !tableComment.isEmpty() )
217 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection;
220 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( connectionProvider );
221 connection.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionUri, {} ) ) );
223 catch ( QgsProviderConnectionException &e )
225 pushError( QObject::tr(
"Could not retrieve connection details:\n\n%1" ).arg( e.what() ) );
231 connection->setTableComment( destSchema, destTableName, tableComment );
233 catch ( QgsProviderConnectionException &e )
235 pushError( QObject::tr(
"Failed to set new table comment!\n\n" ) + e.what() );
241 onSuccessfulCompletion();
246 if ( error != Qgis::VectorExportResult::UserCanceled )
248 pushError( QObject::tr(
"Failed to import layer!\n\n" ) + errorMessage );
251 onError( error, errorMessage );
260 bool foundExistingManageMenu =
false;
261 const QList<QAction *> actions = mainMenu->actions();
262 for ( QAction *action : actions )
264 if ( action->text() == subMenuName )
266 action->menu()->addAction( actionToAdd );
267 foundExistingManageMenu =
true;
271 if ( !foundExistingManageMenu )
273 QMenu *addSubMenu =
new QMenu( subMenuName, mainMenu );
274 addSubMenu->addAction( actionToAdd );
275 mainMenu->addMenu( addSubMenu );
281 const QVector<QgsDataItem *> constChildren { item->
children() };
284 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, Qgis::StringFormat format)=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