Skip to content
Permalink
Browse files
Unfinished attempt to use query result widget for SQL layer updates
  • Loading branch information
elpaso authored and nyalldawson committed Jul 22, 2021
1 parent e7a7bf3 commit f5946a184b7c59cefb5b4c253ded89a0aa421f60
@@ -27,6 +27,14 @@ Contains utility functions for working with map layers.
Returns the combined extent of a list of ``layers``.

The ``crs`` argument specifies the desired coordinate reference system for the combined extent.
%End

static QgsAbstractDatabaseProviderConnection *databaseConnection( const QgsMapLayer *layer ) /Factory/;
%Docstring
Creates and returns the (possibly NULL) database connection for a ``layer``.
Ownership is transferred to the caller.

.. versionadded:: 3.22
%End

};
@@ -94,6 +94,7 @@
#include "qgsfixattributedialog.h"
#include "qgsprojecttimesettings.h"
#include "qgsmaplayertemporalproperties.h"
#include "qgsmaplayerutils.h"
#include "qgsmeshlayertemporalproperties.h"
#include "qgsvectorlayersavestyledialog.h"
#include "maptools/qgsappmaptools.h"
@@ -2236,7 +2237,8 @@ void QgisApp::resolveVectorLayerDependencies( QgsVectorLayer *vl, QgsMapLayer::S
if ( providerMetadata )
{
// Retrieve the DB connection (if any)
std::unique_ptr< QgsAbstractDatabaseProviderConnection > conn { static_cast<QgsAbstractDatabaseProviderConnection *>( providerMetadata->createConnection( vl->dataProvider()->uri().uri(), {} ) ) };

std::unique_ptr< QgsAbstractDatabaseProviderConnection > conn { QgsMapLayerUtils::databaseConnection( vl ) };
if ( conn )
{
QString tableSchema;
@@ -13,42 +13,44 @@
* *
***************************************************************************/
#include <QClipboard>

#include "qgsapplayertreeviewmenuprovider.h"
#include <QMessageBox>

#include "qgisapp.h"
#include "qgsapplayertreeviewmenuprovider.h"
#include "qgsapplication.h"
#include "qgsclipboard.h"
#include "qgscolorwidgets.h"
#include "qgscolorschemeregistry.h"
#include "qgscolorswatchgrid.h"
#include "qgscolorwidgets.h"
#include "qgsdialog.h"
#include "qgsgui.h"
#include "qgslayernotesmanager.h"
#include "qgslayernotesutils.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreemodellegendnode.h"
#include "qgslayertreeregistrybridge.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerstylecategoriesmodel.h"
#include "qgsmaplayerstyleguiutils.h"
#include "qgsmaplayerutils.h"
#include "qgsmessagebar.h"
#include "qgspointcloudlayer.h"
#include "qgsproject.h"
#include "qgsqueryresultwidget.h"
#include "qgsrasterlayer.h"
#include "qgsrenderer.h"
#include "qgssymbol.h"
#include "qgssinglesymbolrenderer.h"
#include "qgsstyle.h"
#include "qgssymbol.h"
#include "qgssymbollayerutils.h"
#include "qgssymbolselectordialog.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgslayertreeregistrybridge.h"
#include "qgssymbolselectordialog.h"
#include "qgssinglesymbolrenderer.h"
#include "qgsmaplayerstylecategoriesmodel.h"
#include "qgssymbollayerutils.h"
#include "qgsxmlutils.h"
#include "qgsmessagebar.h"
#include "qgspointcloudlayer.h"
#include "qgsvectorlayerlabeling.h"
#include "qgslayernotesmanager.h"
#include "qgslayernotesutils.h"
#include "qgsxmlutils.h"

#include <QMessageBox>

QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView *view, QgsMapCanvas *canvas )
: mView( view )
@@ -217,6 +219,32 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
}
}

if ( vlayer /* FIXME: no raster support in createSqlVectorLayer || rlayer */ )
{
std::unique_ptr< QgsAbstractDatabaseProviderConnection> conn { QgsMapLayerUtils::databaseConnection( layer ) };
if ( conn )
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/dbmanager.svg" ) ), tr( "Update SQL Layer…" ), menu, [ layer ]
{
std::unique_ptr< QgsAbstractDatabaseProviderConnection> conn { QgsMapLayerUtils::databaseConnection( layer ) };
if ( conn )
{
QgsDialog dialog;
dialog.setObjectName( QStringLiteral( "SQLUpdateDialog" ) );
dialog.setWindowTitle( tr( "%1 — Update SQL" ).arg( layer->name() ) );
QgsGui::enableAutoGeometryRestore( &dialog );
QgsQueryResultWidget *widget { new QgsQueryResultWidget( &dialog, conn.release() ) };
widget->setQuery( layer->dataProvider()->uri().sql() );
widget->layout()->setMargin( 0 );
dialog.layout()->addWidget( widget );

// FIXME! connecting to createSqlVectorLayer won't work because we have no QgsVectorLayer *createSqlVectorLayer UPDATE functionality
// also, we'd need a raster equivalent.. and abstract out the QgsDataSourceUri manipulation part so that it can be used
// for both layer creation and update.

}
} );
}

addCustomLayerActions( menu, layer );
if ( layer && layer->type() == QgsMapLayerType::VectorLayer && static_cast<QgsVectorLayer *>( layer )->providerType() == QLatin1String( "virtual" ) )
{
@@ -19,6 +19,9 @@
#include "qgsrectangle.h"
#include "qgscoordinatereferencesystem.h"
#include "qgscoordinatetransformcontext.h"
#include "qgsabstractdatabaseproviderconnection.h"
#include "qgsprovidermetadata.h"
#include "qgsproviderregistry.h"
#include "qgsreferencedgeometry.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
@@ -84,3 +87,28 @@ QgsRectangle QgsMapLayerUtils::combinedExtent( const QList<QgsMapLayer *> &layer
QgsDebugMsgLevel( "Full extent: " + fullExtent.toString(), 5 );
return fullExtent;
}

QgsAbstractDatabaseProviderConnection *QgsMapLayerUtils::databaseConnection( const QgsMapLayer *layer )
{
if ( ! layer || ! layer->dataProvider() )
{
return nullptr;
}

try
{
QgsProviderMetadata *providerMetadata = QgsProviderRegistry::instance()->providerMetadata( layer->dataProvider()->name() );
if ( ! providerMetadata )
{
return nullptr;
}

std::unique_ptr< QgsAbstractDatabaseProviderConnection > conn { static_cast<QgsAbstractDatabaseProviderConnection *>( providerMetadata->createConnection( layer->dataProvider()->uri().uri(), {} ) ) };
return conn.release();
}
catch ( const QgsProviderConnectionException &ex )
{
QgsDebugMsg( QStringLiteral( "Error retrieving database connection for layer %1: %2" ).arg( layer->name(), ex.what() ) );
return nullptr;
}
}
@@ -25,6 +25,7 @@ class QgsMapLayer;
class QgsRectangle;
class QgsCoordinateReferenceSystem;
class QgsCoordinateTransformContext;
class QgsAbstractDatabaseProviderConnection;

/**
* \ingroup core
@@ -43,6 +44,13 @@ class CORE_EXPORT QgsMapLayerUtils
*/
static QgsRectangle combinedExtent( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext );

/**
* Creates and returns the (possibly NULL) database connection for a \a layer.
* Ownership is transferred to the caller.
* \since QGIS 3.22
*/
static QgsAbstractDatabaseProviderConnection *databaseConnection( const QgsMapLayer *layer ) SIP_FACTORY;

};

#endif // QGSMAPLAYERUTILS_H

0 comments on commit f5946a1

Please sign in to comment.