16 #include <QDomDocument>
17 #include <QDomElement>
18 #include <QFileDialog>
20 #include <QInputDialog>
22 #include <QMessageBox>
23 #include <QStandardItem>
24 #include <QTextStream>
40 QWidget *parent, Qt::WindowFlags fl )
41 : QDialog( parent, fl )
45 connect( btnEqual, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnEqual_clicked );
46 connect( btnLessThan, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnLessThan_clicked );
47 connect( btnGreaterThan, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnGreaterThan_clicked );
48 connect( btnLike, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnLike_clicked );
49 connect( btnILike, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnILike_clicked );
50 connect( btnPct, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnPct_clicked );
51 connect( btnIn, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnIn_clicked );
52 connect( btnNotIn, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnNotIn_clicked );
53 connect( lstFields, &QListView::doubleClicked,
this, &QgsSearchQueryBuilder::lstFields_doubleClicked );
54 connect( lstValues, &QListView::doubleClicked,
this, &QgsSearchQueryBuilder::lstValues_doubleClicked );
55 connect( btnLessEqual, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnLessEqual_clicked );
56 connect( btnGreaterEqual, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnGreaterEqual_clicked );
57 connect( btnNotEqual, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnNotEqual_clicked );
58 connect( btnAnd, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnAnd_clicked );
59 connect( btnNot, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnNot_clicked );
60 connect( btnOr, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnOr_clicked );
61 connect( btnGetAllValues, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnGetAllValues_clicked );
62 connect( btnSampleValues, &QPushButton::clicked,
this, &QgsSearchQueryBuilder::btnSampleValues_clicked );
64 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsSearchQueryBuilder::showHelp );
66 setWindowTitle( tr(
"Search Query Builder" ) );
68 QPushButton *pbn =
new QPushButton( tr(
"&Test" ) );
69 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
70 connect( pbn, &QAbstractButton::clicked,
this, &QgsSearchQueryBuilder::btnTest_clicked );
72 pbn =
new QPushButton( tr(
"&Clear" ) );
73 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
74 connect( pbn, &QAbstractButton::clicked,
this, &QgsSearchQueryBuilder::btnClear_clicked );
76 pbn =
new QPushButton( tr(
"&Save…" ) );
77 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
78 pbn->setToolTip( tr(
"Save query to an xml file" ) );
81 pbn =
new QPushButton( tr(
"&Load…" ) );
82 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
83 pbn->setToolTip( tr(
"Load query from xml file" ) );
87 lblDataUri->setText( layer->
name() );
91 void QgsSearchQueryBuilder::populateFields()
97 for (
int idx = 0; idx < fields.
count(); ++idx )
99 const QString fieldName = fields.
at( idx ).
name();
100 mFieldMap[fieldName] = idx;
101 QStandardItem *myItem =
new QStandardItem( fieldName );
102 myItem->setEditable(
false );
103 mModelFields->insertRow( mModelFields->rowCount(), myItem );
107 void QgsSearchQueryBuilder::setupListViews()
110 mModelFields =
new QStandardItemModel();
111 mModelValues =
new QStandardItemModel();
112 lstFields->setModel( mModelFields );
113 lstValues->setModel( mModelValues );
115 lstFields->setViewMode( QListView::ListMode );
116 lstValues->setViewMode( QListView::ListMode );
117 lstFields->setSelectionBehavior( QAbstractItemView::SelectRows );
118 lstValues->setSelectionBehavior( QAbstractItemView::SelectRows );
120 lstFields->setUniformItemSizes(
true );
121 lstValues->setUniformItemSizes(
true );
124 void QgsSearchQueryBuilder::getFieldValues(
int limit )
131 mModelValues->clear();
134 const QString fieldName = mModelFields->data( lstFields->currentIndex() ).toString();
135 const int fieldIndex = mFieldMap[fieldName];
137 const bool numeric = (
field.
type() == QVariant::Int ||
field.
type() == QVariant::Double );
143 attrs.append( fieldIndex );
147 lstValues->setCursor( Qt::WaitCursor );
149 mModelValues->blockSignals(
true );
150 lstValues->setUpdatesEnabled(
false );
153 QSet<QString> insertedValues;
156 ( limit == 0 || mModelValues->rowCount() != limit ) )
158 value = feat.
attribute( fieldIndex ).toString();
163 value =
'\'' + value.replace(
'\'', QLatin1String(
"''" ) ) +
'\'';
167 if ( !insertedValues.contains( value ) )
169 QStandardItem *myItem =
new QStandardItem( value );
170 myItem->setEditable(
false );
171 mModelValues->insertRow( mModelValues->rowCount(), myItem );
172 insertedValues.insert( value );
176 mModelValues->blockSignals(
false );
177 lstValues->setUpdatesEnabled(
true );
179 mModelValues->sort( 0 );
180 lstValues->setCursor( Qt::ArrowCursor );
183 void QgsSearchQueryBuilder::btnSampleValues_clicked()
185 getFieldValues( 25 );
188 void QgsSearchQueryBuilder::btnGetAllValues_clicked()
193 void QgsSearchQueryBuilder::btnTest_clicked()
195 const long count = countRecords( mTxtSql->text() );
201 QMessageBox::information(
this, tr(
"Test Query" ), tr(
"Found %n matching feature(s).",
"test result", count ) );
205 long QgsSearchQueryBuilder::countRecords(
const QString &searchString )
208 if ( search.hasParserError() )
210 QMessageBox::critical(
this, tr(
"Query Result" ), search.parserErrorString() );
217 const bool fetchGeom = search.needsGeometry();
224 if ( !search.prepare( &context ) )
226 QMessageBox::critical(
this, tr(
"Query Result" ), search.evalErrorString() );
230 QApplication::setOverrideCursor( Qt::WaitCursor );
236 context.setFeature( feat );
237 const QVariant value = search.evaluate( &context );
238 if ( value.toInt() != 0 )
244 if ( search.hasEvalError() )
248 QApplication::restoreOverrideCursor();
250 if ( search.hasEvalError() )
252 QMessageBox::critical(
this, tr(
"Query Result" ), search.evalErrorString() );
260 void QgsSearchQueryBuilder::btnOk_clicked()
263 if ( mTxtSql->text().trimmed().length() > 0 )
270 const long numRecs = countRecords( mTxtSql->text() );
275 else if ( numRecs == 0 )
277 QMessageBox::warning(
this, tr(
"Query Result" ), tr(
"The query you specified results in zero records being returned." ) );
286 void QgsSearchQueryBuilder::btnEqual_clicked()
288 mTxtSql->insertText( QStringLiteral(
" = " ) );
291 void QgsSearchQueryBuilder::btnLessThan_clicked()
293 mTxtSql->insertText( QStringLiteral(
" < " ) );
296 void QgsSearchQueryBuilder::btnGreaterThan_clicked()
298 mTxtSql->insertText( QStringLiteral(
" > " ) );
301 void QgsSearchQueryBuilder::btnPct_clicked()
303 mTxtSql->insertText( QStringLiteral(
"%" ) );
306 void QgsSearchQueryBuilder::btnIn_clicked()
308 mTxtSql->insertText( QStringLiteral(
" IN " ) );
311 void QgsSearchQueryBuilder::btnNotIn_clicked()
313 mTxtSql->insertText( QStringLiteral(
" NOT IN " ) );
316 void QgsSearchQueryBuilder::btnLike_clicked()
318 mTxtSql->insertText( QStringLiteral(
" LIKE " ) );
323 return mTxtSql->text();
331 void QgsSearchQueryBuilder::lstFields_doubleClicked(
const QModelIndex &index )
336 void QgsSearchQueryBuilder::lstValues_doubleClicked(
const QModelIndex &index )
338 mTxtSql->insertText( mModelValues->data( index ).toString() );
341 void QgsSearchQueryBuilder::btnLessEqual_clicked()
343 mTxtSql->insertText( QStringLiteral(
" <= " ) );
346 void QgsSearchQueryBuilder::btnGreaterEqual_clicked()
348 mTxtSql->insertText( QStringLiteral(
" >= " ) );
351 void QgsSearchQueryBuilder::btnNotEqual_clicked()
353 mTxtSql->insertText( QStringLiteral(
" != " ) );
356 void QgsSearchQueryBuilder::btnAnd_clicked()
358 mTxtSql->insertText( QStringLiteral(
" AND " ) );
361 void QgsSearchQueryBuilder::btnNot_clicked()
363 mTxtSql->insertText( QStringLiteral(
" NOT " ) );
366 void QgsSearchQueryBuilder::btnOr_clicked()
368 mTxtSql->insertText( QStringLiteral(
" OR " ) );
371 void QgsSearchQueryBuilder::btnClear_clicked()
376 void QgsSearchQueryBuilder::btnILike_clicked()
378 mTxtSql->insertText( QStringLiteral(
" ILIKE " ) );
392 mTxtSql->insertText( query );
396 void QgsSearchQueryBuilder::showHelp()
398 QgsHelp::openHelp( QStringLiteral(
"working_with_vector/vector_properties.html#query-builder" ) );