Skip to content

Commit

Permalink
QgsQueryBuilder: improvements for easier subclassing
Browse files Browse the repository at this point in the history
- 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 b878ac9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
24 changes: 22 additions & 2 deletions python/gui/auto_generated/qgsquerybuilder.sip.in
Expand Up @@ -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.
Expand Down
18 changes: 13 additions & 5 deletions src/gui/qgsquerybuilder.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -190,6 +190,7 @@ void QgsQueryBuilder::btnSampleValues_clicked()
QString prevSubsetString = mLayer->subsetString();
if ( mUseUnfilteredLayer->isChecked() && !prevSubsetString.isEmpty() )
{
mIgnoreLayerSubsetStringChangedSignal = true;
mLayer->setSubsetString( QString() );
}

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

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

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

lstValues->setCursor( Qt::ArrowCursor );
Expand All @@ -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 )
Expand Down Expand Up @@ -434,7 +436,6 @@ void QgsQueryBuilder::clear()
{
txtSQL->clear();
mLayer->setSubsetString( QString() );
mUseUnfilteredLayer->setDisabled( true );
}

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

void QgsQueryBuilder::layerSubsetStringChanged()
{
if ( mIgnoreLayerSubsetStringChangedSignal )
return;
mUseUnfilteredLayer->setDisabled( mLayer->subsetString().isEmpty() );
}
36 changes: 34 additions & 2 deletions src/gui/qgsquerybuilder.h
Expand Up @@ -26,6 +26,7 @@
#include "qgis_gui.h"

class QgsVectorLayer;
class QgsCodeEditor;

/**
* \ingroup gui
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.