16#include <QDomDocument>
20#include <QInputDialog>
23#include <QStandardItem>
38 QWidget *parent, Qt::WindowFlags fl )
39 : QDialog( parent, fl )
43 connect( btnEqual, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnEqual_clicked );
44 connect( btnLessThan, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnLessThan_clicked );
45 connect( btnGreaterThan, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnGreaterThan_clicked );
46 connect( btnLike, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnLike_clicked );
47 connect( btnILike, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnILike_clicked );
48 connect( btnPct, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnPct_clicked );
49 connect( btnIn, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnIn_clicked );
50 connect( btnNotIn, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnNotIn_clicked );
51 connect( lstFields, &QListView::doubleClicked,
this, &QgsSearchQueryBuilder::lstFields_doubleClicked );
52 connect( lstValues, &QListView::doubleClicked,
this, &QgsSearchQueryBuilder::lstValues_doubleClicked );
53 connect( btnLessEqual, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnLessEqual_clicked );
54 connect( btnGreaterEqual, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnGreaterEqual_clicked );
55 connect( btnNotEqual, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnNotEqual_clicked );
56 connect( btnAnd, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnAnd_clicked );
57 connect( btnNot, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnNot_clicked );
58 connect( btnOr, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnOr_clicked );
59 connect( btnGetAllValues, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnGetAllValues_clicked );
60 connect( btnSampleValues, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnSampleValues_clicked );
62 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsSearchQueryBuilder::showHelp );
64 setWindowTitle( tr(
"Search Query Builder" ) );
66 QPushButton *pbn =
new QPushButton( tr(
"&Test" ) );
67 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
68 connect( pbn, &QAbstractButton::clicked,
this, &QgsSearchQueryBuilder::btnTest_clicked );
70 pbn =
new QPushButton( tr(
"&Clear" ) );
71 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
72 connect( pbn, &QAbstractButton::clicked,
this, &QgsSearchQueryBuilder::btnClear_clicked );
74 pbn =
new QPushButton( tr(
"&Save…" ) );
75 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
76 pbn->setToolTip( tr(
"Save query to an xml file" ) );
79 pbn =
new QPushButton( tr(
"&Load…" ) );
80 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
81 pbn->setToolTip( tr(
"Load query from xml file" ) );
85 lblDataUri->setText( layer->
name() );
89void QgsSearchQueryBuilder::populateFields()
95 for (
int idx = 0; idx < fields.
count(); ++idx )
97 const QString fieldName = fields.
at( idx ).
name();
98 mFieldMap[fieldName] = idx;
99 QStandardItem *myItem =
new QStandardItem( fieldName );
100 myItem->setEditable(
false );
101 mModelFields->insertRow( mModelFields->rowCount(), myItem );
105void QgsSearchQueryBuilder::setupListViews()
108 mModelFields =
new QStandardItemModel();
109 mModelValues =
new QStandardItemModel();
110 lstFields->setModel( mModelFields );
111 lstValues->setModel( mModelValues );
113 lstFields->setViewMode( QListView::ListMode );
114 lstValues->setViewMode( QListView::ListMode );
115 lstFields->setSelectionBehavior( QAbstractItemView::SelectRows );
116 lstValues->setSelectionBehavior( QAbstractItemView::SelectRows );
118 lstFields->setUniformItemSizes(
true );
119 lstValues->setUniformItemSizes(
true );
122void QgsSearchQueryBuilder::getFieldValues(
int limit )
129 mModelValues->clear();
132 const QString fieldName = mModelFields->data( lstFields->currentIndex() ).toString();
133 const int fieldIndex = mFieldMap[fieldName];
135 const bool numeric = ( field.
type() == QMetaType::Type::Int || field.
type() == QMetaType::Type::Double );
141 attrs.append( fieldIndex );
145 lstValues->setCursor( Qt::WaitCursor );
147 mModelValues->blockSignals(
true );
148 lstValues->setUpdatesEnabled(
false );
151 QSet<QString> insertedValues;
154 ( limit == 0 || mModelValues->rowCount() != limit ) )
156 value = feat.
attribute( fieldIndex ).toString();
161 value =
'\'' + value.replace(
'\'', QLatin1String(
"''" ) ) +
'\'';
165 if ( !insertedValues.contains( value ) )
167 QStandardItem *myItem =
new QStandardItem( value );
168 myItem->setEditable(
false );
169 mModelValues->insertRow( mModelValues->rowCount(), myItem );
170 insertedValues.insert( value );
174 mModelValues->blockSignals(
false );
175 lstValues->setUpdatesEnabled(
true );
177 mModelValues->sort( 0 );
178 lstValues->setCursor( Qt::ArrowCursor );
181void QgsSearchQueryBuilder::btnSampleValues_clicked()
183 getFieldValues( 25 );
186void QgsSearchQueryBuilder::btnGetAllValues_clicked()
191void QgsSearchQueryBuilder::btnTest_clicked()
193 const long count = countRecords( mTxtSql->text() );
199 QMessageBox::information(
this, tr(
"Test Query" ), tr(
"Found %n matching feature(s).",
"test result", count ) );
203long QgsSearchQueryBuilder::countRecords(
const QString &searchString )
206 if ( search.hasParserError() )
208 QMessageBox::critical(
this, tr(
"Query Result" ), search.parserErrorString() );
215 const bool fetchGeom = search.needsGeometry();
222 if ( !search.prepare( &context ) )
224 QMessageBox::critical(
this, tr(
"Query Result" ), search.evalErrorString() );
228 QApplication::setOverrideCursor( Qt::WaitCursor );
234 context.setFeature( feat );
235 const QVariant value = search.evaluate( &context );
236 if ( value.toInt() != 0 )
242 if ( search.hasEvalError() )
246 QApplication::restoreOverrideCursor();
248 if ( search.hasEvalError() )
250 QMessageBox::critical(
this, tr(
"Query Result" ), search.evalErrorString() );
258void QgsSearchQueryBuilder::btnOk_clicked()
261 if ( mTxtSql->text().trimmed().length() > 0 )
268 const long numRecs = countRecords( mTxtSql->text() );
273 else if ( numRecs == 0 )
275 QMessageBox::warning(
this, tr(
"Query Result" ), tr(
"The query you specified results in zero records being returned." ) );
284void QgsSearchQueryBuilder::btnEqual_clicked()
286 mTxtSql->insertText( QStringLiteral(
" = " ) );
289void QgsSearchQueryBuilder::btnLessThan_clicked()
291 mTxtSql->insertText( QStringLiteral(
" < " ) );
294void QgsSearchQueryBuilder::btnGreaterThan_clicked()
296 mTxtSql->insertText( QStringLiteral(
" > " ) );
299void QgsSearchQueryBuilder::btnPct_clicked()
301 mTxtSql->insertText( QStringLiteral(
"%" ) );
304void QgsSearchQueryBuilder::btnIn_clicked()
306 mTxtSql->insertText( QStringLiteral(
" IN " ) );
309void QgsSearchQueryBuilder::btnNotIn_clicked()
311 mTxtSql->insertText( QStringLiteral(
" NOT IN " ) );
314void QgsSearchQueryBuilder::btnLike_clicked()
316 mTxtSql->insertText( QStringLiteral(
" LIKE " ) );
321 return mTxtSql->text();
329void QgsSearchQueryBuilder::lstFields_doubleClicked(
const QModelIndex &index )
334void QgsSearchQueryBuilder::lstValues_doubleClicked(
const QModelIndex &index )
336 mTxtSql->insertText( mModelValues->data( index ).toString() );
339void QgsSearchQueryBuilder::btnLessEqual_clicked()
341 mTxtSql->insertText( QStringLiteral(
" <= " ) );
344void QgsSearchQueryBuilder::btnGreaterEqual_clicked()
346 mTxtSql->insertText( QStringLiteral(
" >= " ) );
349void QgsSearchQueryBuilder::btnNotEqual_clicked()
351 mTxtSql->insertText( QStringLiteral(
" != " ) );
354void QgsSearchQueryBuilder::btnAnd_clicked()
356 mTxtSql->insertText( QStringLiteral(
" AND " ) );
359void QgsSearchQueryBuilder::btnNot_clicked()
361 mTxtSql->insertText( QStringLiteral(
" NOT " ) );
364void QgsSearchQueryBuilder::btnOr_clicked()
366 mTxtSql->insertText( QStringLiteral(
" OR " ) );
369void QgsSearchQueryBuilder::btnClear_clicked()
374void QgsSearchQueryBuilder::btnILike_clicked()
376 mTxtSql->insertText( QStringLiteral(
" ILIKE " ) );
390 mTxtSql->insertText( query );
394void QgsSearchQueryBuilder::showHelp()
396 QgsHelp::openHelp( QStringLiteral(
"working_with_vector/vector_properties.html#query-builder" ) );
The Qgis class provides global constants for use throughout the application.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
@ NoFlags
No flags are set.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
static bool loadQueryFromFile(QString &subset)
Load query from the XML file.
static bool saveQueryToFile(const QString &subset)
Save query to the XML file.
QgsSearchQueryBuilder(QgsVectorLayer *layer, QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor - takes pointer to vector layer as a parameter.
void setSearchString(const QString &searchString)
change search string shown in text field
QString searchString()
returns newly created search string
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QList< int > QgsAttributeList