Skip to content
Permalink
Browse files

QgsQueryBuilder: improvements for easier subclassing

- Make test() method virtual so it can be overriden in a derived class
- Make enabling/disabling of "use unfiltered layer" checkbox automatic
  when layer's subsetString is changed (for example by an overriden
  test() implementation)
- Add a codeEditorWidget() method that returns the sql editor widget,
  so that custom behavior can be added.
  • Loading branch information
rouault committed Nov 14, 2020
1 parent b4811f2 commit b878ac9c85d313fc4917350db4956f2429b5ce22
Showing with 69 additions and 9 deletions.
  1. +22 −2 python/gui/auto_generated/qgsquerybuilder.sip.in
  2. +13 −5 src/gui/qgsquerybuilder.cpp
  3. +34 −2 src/gui/qgsquerybuilder.h
@@ -41,16 +41,36 @@ vector layer properties dialog
QString sql();
void setSql( const QString &sqlStatement );

%If (HAVE_QSCI_SIP)

QgsCodeEditor *codeEditorWidget() const;
%Docstring
Returns the code editor widget for the SQL.

.. versionadded:: 3.18
%End
%End
%If (!HAVE_QSCI_SIP)

QWidget *codeEditorWidget() const;
%Docstring
Returns the code editor widget for the SQL.

.. versionadded:: 3.18
%End
%End

public slots:
virtual void accept();

virtual void reject();

void clear();

void test();
virtual void test();
%Docstring
Test the constructed sql statement to see if the vector layer data provider likes it.
The default implementation tests that the constructed sql statement to
see if the vector layer data provider likes it.
The number of rows that would be returned is displayed in a message box.
The test uses a "select count(*) from ..." query to test the SQL
statement.
@@ -85,8 +85,8 @@ QgsQueryBuilder::QgsQueryBuilder( QgsVectorLayer *layer,
setupGuiViews();

mOrigSubsetString = layer->subsetString();

mUseUnfilteredLayer->setDisabled( mLayer->subsetString().isEmpty() );
connect( layer, &QgsVectorLayer::subsetStringChanged, this, &QgsQueryBuilder::layerSubsetStringChanged );
layerSubsetStringChanged();

lblDataUri->setText( tr( "Set provider filter on %1" ).arg( layer->name() ) );
txtSQL->setText( mOrigSubsetString );
@@ -190,6 +190,7 @@ void QgsQueryBuilder::btnSampleValues_clicked()
QString prevSubsetString = mLayer->subsetString();
if ( mUseUnfilteredLayer->isChecked() && !prevSubsetString.isEmpty() )
{
mIgnoreLayerSubsetStringChangedSignal = true;
mLayer->setSubsetString( QString() );
}

@@ -199,6 +200,7 @@ void QgsQueryBuilder::btnSampleValues_clicked()
if ( prevSubsetString != mLayer->subsetString() )
{
mLayer->setSubsetString( prevSubsetString );
mIgnoreLayerSubsetStringChangedSignal = false;
}

lstValues->setCursor( Qt::ArrowCursor );
@@ -211,6 +213,7 @@ void QgsQueryBuilder::btnGetAllValues_clicked()
QString prevSubsetString = mLayer->subsetString();
if ( mUseUnfilteredLayer->isChecked() && !prevSubsetString.isEmpty() )
{
mIgnoreLayerSubsetStringChangedSignal = true;
mLayer->setSubsetString( QString() );
}

@@ -220,6 +223,7 @@ void QgsQueryBuilder::btnGetAllValues_clicked()
if ( prevSubsetString != mLayer->subsetString() )
{
mLayer->setSubsetString( prevSubsetString );
mIgnoreLayerSubsetStringChangedSignal = false;
}

lstValues->setCursor( Qt::ArrowCursor );
@@ -233,8 +237,6 @@ void QgsQueryBuilder::test()

if ( mLayer->setSubsetString( txtSQL->text() ) )
{
mUseUnfilteredLayer->setDisabled( mLayer->subsetString().isEmpty() );

const long featureCount { mLayer->featureCount() };
// Check for errors
if ( featureCount < 0 )
@@ -434,7 +436,6 @@ void QgsQueryBuilder::clear()
{
txtSQL->clear();
mLayer->setSubsetString( QString() );
mUseUnfilteredLayer->setDisabled( true );
}

void QgsQueryBuilder::btnILike_clicked()
@@ -533,3 +534,10 @@ void QgsQueryBuilder::loadQuery()
txtSQL->clear();
txtSQL->insertText( query );
}

void QgsQueryBuilder::layerSubsetStringChanged()
{
if ( mIgnoreLayerSubsetStringChangedSignal )
return;
mUseUnfilteredLayer->setDisabled( mLayer->subsetString().isEmpty() );
}
@@ -26,6 +26,7 @@
#include "qgis_gui.h"

class QgsVectorLayer;
class QgsCodeEditor;

/**
* \ingroup gui
@@ -59,18 +60,45 @@ class GUI_EXPORT QgsQueryBuilder : public QDialog, private Ui::QgsQueryBuilderBa
QString sql();
void setSql( const QString &sqlStatement );

#ifdef SIP_RUN
SIP_IF_FEATURE( HAVE_QSCI_SIP )

/**
* Returns the code editor widget for the SQL.
* \since QGIS 3.18
*/
QgsCodeEditor *codeEditorWidget() const;
SIP_END
SIP_IF_FEATURE( !HAVE_QSCI_SIP )

/**
* Returns the code editor widget for the SQL.
* \since QGIS 3.18
*/
QWidget *codeEditorWidget() const;
SIP_END
#else

/**
* Returns the code editor widget for the SQL.
* \since QGIS 3.18
*/
QgsCodeEditor *codeEditorWidget() const { return txtSQL; }
#endif

public slots:
void accept() override;
void reject() override;
void clear();

/**
* Test the constructed sql statement to see if the vector layer data provider likes it.
* The default implementation tests that the constructed sql statement to
* see if the vector layer data provider likes it.
* The number of rows that would be returned is displayed in a message box.
* The test uses a "select count(*) from ..." query to test the SQL
* statement.
*/
void test();
virtual void test();

/**
* Save query to the XML file
@@ -105,6 +133,7 @@ class GUI_EXPORT QgsQueryBuilder : public QDialog, private Ui::QgsQueryBuilderBa
void btnNot_clicked();
void btnOr_clicked();
void onTextChanged( const QString &text );
void layerSubsetStringChanged();

/**
* Gets all distinct values for the field. Values are inserted
@@ -150,5 +179,8 @@ class GUI_EXPORT QgsQueryBuilder : public QDialog, private Ui::QgsQueryBuilderBa

//! original subset string
QString mOrigSubsetString;

//! whether to ignore subsetStringChanged() signal from the layer
bool mIgnoreLayerSubsetStringChangedSignal = false;
};
#endif //QGSQUERYBUILDER_H

0 comments on commit b878ac9

Please sign in to comment.
You can’t perform that action at this time.