38using namespace Qt::StringLiterals;
40void QgsDataItemGuiProviderUtils::deleteConnectionsPrivate(
const QStringList &connectionNames,
const std::function<
void(
const QString & )> &deleteConnection, QPointer<QgsDataItem> firstParent )
42 if ( connectionNames.size() > 1 )
45 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 )
52 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 )
57 for (
const QString &connectionName : std::as_const( connectionNames ) )
59 deleteConnection( connectionName );
63 firstParent->refreshConnections();
69 QString newConnectionName( name );
70 while ( connectionNames.contains( newConnectionName ) )
73 newConnectionName = QObject::tr(
"%1 (copy %2)" ).arg( name ).arg( i );
76 return newConnectionName;
80 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection,
82 const QString &destinationSchema,
84 const QString &shortTitle,
85 const QString &longTitle,
86 const QVariantMap &destinationProviderOptions,
87 const std::function<
void()> &onSuccessfulCompletion,
89 QObject *connectionContext
95 const QString connectionUri = connection->uri();
96 const QString connectionProvider = connection->providerKey();
102 if ( !dialog.exec() )
105 std::unique_ptr< QgsVectorLayerExporterTask > exportTask = dialog.
createExporterTask( destinationProviderOptions );
109 const QString destSchema = dialog.
schema();
110 const QString destTableName = dialog.
tableName();
114 auto pushError = [shortTitle, longTitle, context](
const QString &error ) {
116 QPushButton *detailsButton =
new QPushButton( QObject::tr(
"Details…" ) );
117 QObject::connect( detailsButton, &QPushButton::clicked, detailsButton, [longTitle, error] {
123 item->layout()->addWidget( detailsButton );
134 [onSuccessfulCompletion, connectionUri, longTitle, pushError, connectionProvider, destSchema, destTableName, tableComment, destGeometryColumn, styleSourceLayer, shortTitle, context]() {
135 if ( !tableComment.isEmpty() || styleSourceLayer )
137 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection;
140 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( connectionProvider );
141 connection.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionUri, {} ) ) );
143 catch ( QgsProviderConnectionException &e )
145 pushError( QObject::tr(
"Could not retrieve connection details:\n\n%1" ).arg( e.what() ) );
149 if ( !tableComment.isEmpty() )
153 connection->setTableComment( destSchema, destTableName, tableComment );
155 catch ( QgsProviderConnectionException &e )
157 pushError( QObject::tr(
"Failed to set new table comment!\n\n" ) + e.what() );
162 if ( styleSourceLayer )
167 styleUri = connection->styleStorageUri( destSchema, destTableName, destGeometryColumn, styleSourceLayer->wkbType() );
169 catch ( QgsProviderConnectionException &e )
171 pushError( QObject::tr(
"Failed to save style to database!\n\n" ) + e.what() );
174 const QgsSaveStyleResult styleResult = QgsMapLayerUtils::saveLayerStyleToDatabase( styleSourceLayer, connectionProvider, styleUri, destTableName, QString(), true, QString() );
175 const QString errMsg = styleResult.providerSaveStyleError.isEmpty() ? styleResult.qmlError : styleResult.providerSaveStyleError;
176 if ( !errMsg.isEmpty() )
178 pushError( QObject::tr(
"Failed to save style to database!\n\n" ) + errMsg );
185 onSuccessfulCompletion();
191 if ( error != Qgis::VectorExportResult::UserCanceled )
193 pushError( QObject::tr(
"Failed to import layer!\n\n" ) + errorMessage );
196 onError( error, errorMessage );
204 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection,
205 const QString &destinationSchema,
207 const QString &shortTitle,
208 const QString &longTitle,
209 const QVariantMap &destinationProviderOptions,
210 const std::function<
void()> &onSuccessfulCompletion,
212 QObject *connectionContext
218 const QString connectionUri = connection->uri();
219 const QString connectionProvider = connection->providerKey();
224 if ( !dialog.exec() )
227 std::unique_ptr< QgsVectorLayerExporterTask > exportTask = dialog.
createExporterTask( destinationProviderOptions );
231 const QString destSchema = dialog.
schema();
232 const QString destTableName = dialog.
tableName();
236 auto pushError = [shortTitle, longTitle, context](
const QString &error ) {
238 QPushButton *detailsButton =
new QPushButton( QObject::tr(
"Details…" ) );
239 QObject::connect( detailsButton, &QPushButton::clicked, detailsButton, [longTitle, error] {
245 item->layout()->addWidget( detailsButton );
256 [onSuccessfulCompletion, connectionUri, longTitle, pushError, connectionProvider, destSchema, destTableName, tableComment, destGeometryColumn, styleSourceLayer, shortTitle, context]() {
257 if ( !tableComment.isEmpty() || styleSourceLayer )
259 std::unique_ptr<QgsAbstractDatabaseProviderConnection> connection;
262 QgsProviderMetadata *md = QgsProviderRegistry::instance()->providerMetadata( connectionProvider );
263 connection.reset( static_cast<QgsAbstractDatabaseProviderConnection *>( md->createConnection( connectionUri, {} ) ) );
265 catch ( QgsProviderConnectionException &e )
267 pushError( QObject::tr(
"Could not retrieve connection details:\n\n%1" ).arg( e.what() ) );
271 if ( !tableComment.isEmpty() )
275 connection->setTableComment( destSchema, destTableName, tableComment );
277 catch ( QgsProviderConnectionException &e )
279 pushError( QObject::tr(
"Failed to set new table comment!\n\n" ) + e.what() );
284 if ( styleSourceLayer )
289 styleUri = connection->styleStorageUri( destSchema, destTableName, destGeometryColumn, styleSourceLayer->wkbType() );
291 catch ( QgsProviderConnectionException &e )
293 pushError( QObject::tr(
"Failed to save style to database!\n\n" ) + e.what() );
296 const QgsSaveStyleResult styleResult = QgsMapLayerUtils::saveLayerStyleToDatabase( styleSourceLayer, connectionProvider, styleUri, destTableName, QString(), true, QString() );
297 const QString errMsg = styleResult.providerSaveStyleError.isEmpty() ? styleResult.qmlError : styleResult.providerSaveStyleError;
298 if ( !errMsg.isEmpty() )
300 pushError( QObject::tr(
"Failed to save style to database!\n\n" ) + errMsg );
307 onSuccessfulCompletion();
313 if ( error != Qgis::VectorExportResult::UserCanceled )
315 pushError( QObject::tr(
"Failed to import layer!\n\n" ) + errorMessage );
318 onError( error, errorMessage );
327 bool foundExistingManageMenu =
false;
328 const QList<QAction *> actions = mainMenu->actions();
329 for ( QAction *action : actions )
331 if ( action->text() == subMenuName )
333 action->menu()->addAction( actionToAdd );
334 foundExistingManageMenu =
true;
338 if ( !foundExistingManageMenu )
340 QMenu *addSubMenu =
new QMenu( subMenuName, mainMenu );
341 addSubMenu->addAction( actionToAdd );
342 mainMenu->addMenu( addSubMenu );
348 const QVector<QgsDataItem *> constChildren { item->
children() };
351 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.
QgsVectorLayer * sourceLayer() const
Returns the source layer, or nullptr if no source layer is set.
std::unique_ptr< QgsVectorLayerExporterTask > createExporterTask(const QVariantMap &extraProviderOptions=QVariantMap())
Creates a new exporter task to match the settings defined in the dialog.
bool saveStyleToDatabase() const
Returns true if the style should be saved to the database after import.
void setMapCanvas(QgsMapCanvas *canvas)
Sets a map canvas to associate with the dialog.
QString geometryColumn() const
Returns the geometry column name for the destination table.
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).
Represents a vector layer which manages a vector based dataset.
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