Skip to content

Commit

Permalink
Address PR comments and add ENUM for widget mode
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Jul 22, 2021
1 parent 387c403 commit 276d198
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 33 deletions.
Expand Up @@ -555,7 +555,7 @@ Creates and returns a (possibly invalid) vector layer based on the ``sql`` state
.. versionadded:: 3.22
%End

virtual SqlVectorLayerOptions sqlOptions( const QString &layerSource );
virtual SqlVectorLayerOptions sqlOptions( const QString &layerSource ) throw( QgsProviderConnectionException );
%Docstring
Returns the SQL layer options from a ``layerSource``.

Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsmaplayerutils.sip.in
Expand Up @@ -31,7 +31,7 @@ The ``crs`` argument specifies the desired coordinate reference system for the c

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

.. versionadded:: 3.22
Expand Down
18 changes: 11 additions & 7 deletions python/gui/auto_generated/qgsqueryresultwidget.sip.in
Expand Up @@ -20,9 +20,8 @@ DB connection (an instance of :py:class:`QgsAbstractDatabaseProviderConnection`)
Query results are displayed in a table view.
Query execution and result fetching can be interrupted by pressing the "Stop" push button.

The widget supports an optional SQL layer update mode where the GUI is optimized for the update of
an existing SQL (query) layer (i.e. buttons are labeled differently and the group box for
query layers is expanded).
The widget supports a few QueryWidgetMode modes that pre-configure the widget appearance to
be used in different contexts like when updating the SQL of an existing query layer.

.. note::

Expand All @@ -36,6 +35,13 @@ query layers is expanded).
%End
public:


enum class QueryWidgetMode
{
SqlQueryMode,
QueryLayerUpdateMode,
};

QgsQueryResultWidget( QWidget *parent = 0, QgsAbstractDatabaseProviderConnection *connection /Transfer/ = 0 );
%Docstring
Creates a QgsQueryResultWidget with the given ``connection``, ownership is transferred to the widget.
Expand All @@ -48,11 +54,9 @@ Creates a QgsQueryResultWidget with the given ``connection``, ownership is trans
Initializes the widget from ``options``.
%End

void setUpdateSqlLayerMode( bool updateMode );
void setWidgetMode( QueryWidgetMode widgetMode );
%Docstring
Sets the update SQL layer mode flag to ``updateMode`` (default is ``False``).
When the widget is in update mode the create SQL layer button is renamed to "update" and the
SQL layer creation group box is expanded.
Sets the widget mode to ``widgetMode``, default is SqlQueryMode.
%End

void setConnection( QgsAbstractDatabaseProviderConnection *connection /Transfer/ );
Expand Down
10 changes: 1 addition & 9 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -236,7 +236,7 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions options { conn2->sqlOptions( layer->source() ) };
options.layerName = layer->name();
QgsQueryResultWidget *queryResultWidget { new QgsQueryResultWidget( &dialog, conn2.release() ) };
queryResultWidget->setUpdateSqlLayerMode( true );
queryResultWidget->setWidgetMode( QgsQueryResultWidget::QueryWidgetMode::QueryLayerUpdateMode );
queryResultWidget->setSqlVectorLayerOptions( options );
queryResultWidget->executeQuery();
queryResultWidget->layout()->setMargin( 0 );
Expand All @@ -252,15 +252,7 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
std::unique_ptr<QgsMapLayer> sqlLayer { conn3->createSqlVectorLayer( options ) };
if ( sqlLayer->isValid() )
{
// Store layer settings
QgsReadWriteContext context;
context.setPathResolver( QgsProject::instance()->pathResolver() );
context.setProjectTranslator( QgsProject::instance() );
QString errorMsg;
QDomDocument doc;
layer->exportNamedStyle( doc, errorMsg, context );
layer->setDataSource( sqlLayer->source(), sqlLayer->name(), sqlLayer->dataProvider()->name(), QgsDataProvider::ProviderOptions() );
layer->importNamedStyle( doc, errorMsg );
queryResultWidget->notify( QObject::tr( "Layer Update Success" ), QObject::tr( "The SQL layer was updated successfully" ), Qgis::MessageLevel::Success );
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/core/providers/qgsabstractdatabaseproviderconnection.h
Expand Up @@ -684,7 +684,7 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
* \throws QgsProviderConnectionException if any errors are encountered or if SQL layer creation is not supported.
* \since QGIS 3.22
*/
virtual SqlVectorLayerOptions sqlOptions( const QString &layerSource );
virtual SqlVectorLayerOptions sqlOptions( const QString &layerSource ) SIP_THROW( QgsProviderConnectionException );

/**
* Executes raw \a sql and returns the (possibly empty) query results, optionally \a feedback can be provided.
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaplayerutils.h
Expand Up @@ -45,7 +45,7 @@ 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.
* Creates and returns the (possibly NULLPTR) database connection for a \a layer.
* Ownership is transferred to the caller.
* \since QGIS 3.22
*/
Expand Down
20 changes: 15 additions & 5 deletions src/gui/qgsqueryresultwidget.cpp
Expand Up @@ -130,12 +130,22 @@ void QgsQueryResultWidget::setSqlVectorLayerOptions( const QgsAbstractDatabasePr
mLayerNameLineEdit->setText( options.layerName );
}

void QgsQueryResultWidget::setUpdateSqlLayerMode( bool updateMode )
void QgsQueryResultWidget::setWidgetMode( QueryWidgetMode widgetMode )
{
mUpdateSqlLayerMode = updateMode;
mLoadAsNewLayerGroupBox->setTitle( updateMode ? tr( "Update query layer" ) : tr( "Load as new layer" ) );
mLoadLayerPushButton->setText( updateMode ? tr( "Update layer" ) : tr( "Load layer" ) );
mLoadAsNewLayerGroupBox->setCollapsed( false );
mQueryWidgetMode = widgetMode;
switch ( widgetMode )
{
case QueryWidgetMode::SqlQueryMode:
mLoadAsNewLayerGroupBox->setTitle( tr( "Load as New Layer" ) );
mLoadLayerPushButton->setText( tr( "Load Layer" ) );
mLoadAsNewLayerGroupBox->setCollapsed( true );
break;
case QueryWidgetMode::QueryLayerUpdateMode:
mLoadAsNewLayerGroupBox->setTitle( tr( "Update Query Layer" ) );
mLoadLayerPushButton->setText( tr( "Update Layer" ) );
mLoadAsNewLayerGroupBox->setCollapsed( false );
break;
}
}

void QgsQueryResultWidget::executeQuery()
Expand Down
24 changes: 16 additions & 8 deletions src/gui/qgsqueryresultwidget.h
Expand Up @@ -95,9 +95,8 @@ class GUI_EXPORT QgsConnectionsApiFetcher: public QObject
* Query results are displayed in a table view.
* Query execution and result fetching can be interrupted by pressing the "Stop" push button.
*
* The widget supports an optional SQL layer update mode where the GUI is optimized for the update of
* an existing SQL (query) layer (i.e. buttons are labeled differently and the group box for
* query layers is expanded).
* The widget supports a few QueryWidgetMode modes that pre-configure the widget appearance to
* be used in different contexts like when updating the SQL of an existing query layer.
*
* \note the ownership of the connection is transferred to the widget.
*
Expand All @@ -109,6 +108,17 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul

public:


/**
* \brief The QueryWidgetMode enum represents various modes for the widget appearance.
*/
enum class QueryWidgetMode : int
{
SqlQueryMode = 1 << 0, //!< Defaults widget mode for SQL execution and SQL query layer creation.
QueryLayerUpdateMode = 1 << 1, //!< SQL query layer update mode: the create SQL layer button is renamed to 'Update' and the SQL layer creation group box is expanded.
};
Q_ENUM( QueryWidgetMode )

/**
* Creates a QgsQueryResultWidget with the given \a connection, ownership is transferred to the widget.
*/
Expand All @@ -122,11 +132,9 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul
void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );

/**
* Sets the update SQL layer mode flag to \a updateMode (default is FALSE).
* When the widget is in update mode the create SQL layer button is renamed to "update" and the
* SQL layer creation group box is expanded.
* Sets the widget mode to \a widgetMode, default is SqlQueryMode.
*/
void setUpdateSqlLayerMode( bool updateMode );
void setWidgetMode( QueryWidgetMode widgetMode );

/**
* Sets the connection to \a connection, ownership is transferred to the widget.
Expand Down Expand Up @@ -199,7 +207,7 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul
QString mSqlErrorMessage;
long long mActualRowCount = -1;
long long mFetchedRowsBatchCount = 0;
bool mUpdateSqlLayerMode = false;
QueryWidgetMode mQueryWidgetMode = QueryWidgetMode::SqlQueryMode;

/**
* Updates SQL layer columns.
Expand Down

0 comments on commit 276d198

Please sign in to comment.