/
qgssearchquerybuilder.cpp
347 lines (285 loc) · 8.94 KB
/
qgssearchquerybuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/***************************************************************************
qgssearchquerybuilder.cpp - Query builder for search strings
----------------------
begin : March 2006
copyright : (C) 2006 by Martin Dobias
email : wonder.sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#include <QListView>
#include <QMessageBox>
#include <QStandardItem>
#include "qgsfeature.h"
#include "qgsfield.h"
#include "qgssearchquerybuilder.h"
#include "qgssearchstring.h"
#include "qgssearchtreenode.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#if QT_VERSION < 0x040300
#define toPlainText() text()
#endif
QgsSearchQueryBuilder::QgsSearchQueryBuilder( QgsVectorLayer* layer,
QWidget *parent, Qt::WFlags fl )
: QDialog( parent, fl ), mLayer( layer )
{
setupUi( this );
setupListViews();
setWindowTitle( tr( "Search query builder" ) );
QPushButton *pbn = new QPushButton( tr( "&Test" ) );
buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
connect( pbn, SIGNAL( clicked() ), this, SLOT( on_btnTest_clicked() ) );
pbn = new QPushButton( tr( "&Clear" ) );
buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
connect( pbn, SIGNAL( clicked() ), this, SLOT( on_btnClear_clicked() ) );
// disable unsupported operators
btnIn->setEnabled( false );
btnNotIn->setEnabled( false );
btnPct->setEnabled( false );
// change to ~
btnILike->setText( "~" );
lblDataUri->setText( layer->name() );
populateFields();
}
QgsSearchQueryBuilder::~QgsSearchQueryBuilder()
{
}
void QgsSearchQueryBuilder::populateFields()
{
QgsDebugMsg( "entering." );
const QgsFieldMap& fields = mLayer->pendingFields();
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
{
QString fieldName = it->name();
mFieldMap[fieldName] = it.key();
QStandardItem *myItem = new QStandardItem( fieldName );
myItem->setEditable( false );
mModelFields->insertRow( mModelFields->rowCount(), myItem );
}
}
void QgsSearchQueryBuilder::setupListViews()
{
QgsDebugMsg( "entering." );
//Models
mModelFields = new QStandardItemModel();
mModelValues = new QStandardItemModel();
lstFields->setModel( mModelFields );
lstValues->setModel( mModelValues );
// Modes
lstFields->setViewMode( QListView::ListMode );
lstValues->setViewMode( QListView::ListMode );
lstFields->setSelectionBehavior( QAbstractItemView::SelectRows );
lstValues->setSelectionBehavior( QAbstractItemView::SelectRows );
// Performance tip since Qt 4.1
lstFields->setUniformItemSizes( true );
lstValues->setUniformItemSizes( true );
}
void QgsSearchQueryBuilder::getFieldValues( int limit )
{
if ( !mLayer )
{
return;
}
// clear the values list
mModelValues->clear();
// determine the field type
QString fieldName = mModelFields->data( lstFields->currentIndex() ).toString();
int fieldIndex = mFieldMap[fieldName];
QgsField field = mLayer->pendingFields()[fieldIndex];//provider->fields()[fieldIndex];
bool numeric = ( field.type() == QVariant::Int || field.type() == QVariant::Double );
QgsFeature feat;
QString value;
QgsAttributeList attrs;
attrs.append( fieldIndex );
mLayer->select( attrs, QgsRectangle(), false );
lstValues->setCursor( Qt::WaitCursor );
// Block for better performance
mModelValues->blockSignals( true );
lstValues->setUpdatesEnabled( false );
/**MH: keep already inserted values in a set. Querying is much faster compared to QStandardItemModel::findItems*/
QSet<QString> insertedValues;
while ( mLayer->nextFeature( feat ) &&
( limit == 0 || mModelValues->rowCount() != limit ) )
{
const QgsAttributeMap& attributes = feat.attributeMap();
value = attributes[fieldIndex].toString();
if ( !numeric )
{
// put string in single quotes
value = "'" + value + "'";
}
// add item only if it's not there already
if ( !insertedValues.contains( value ) )
{
QStandardItem *myItem = new QStandardItem( value );
myItem->setEditable( false );
mModelValues->insertRow( mModelValues->rowCount(), myItem );
insertedValues.insert( value );
}
}
// Unblock for normal use
mModelValues->blockSignals( false );
lstValues->setUpdatesEnabled( true );
// TODO: already sorted, signal emit to refresh model
mModelValues->sort( 0 );
lstValues->setCursor( Qt::ArrowCursor );
}
void QgsSearchQueryBuilder::on_btnSampleValues_clicked()
{
getFieldValues( 25 );
}
void QgsSearchQueryBuilder::on_btnGetAllValues_clicked()
{
getFieldValues( 0 );
}
void QgsSearchQueryBuilder::on_btnTest_clicked()
{
long count = countRecords( txtSQL->toPlainText() );
// error?
if ( count == -1 )
return;
QMessageBox::information( this, tr( "Search results" ), tr( "Found %n matching feature(s).", "test result", count ) );
}
// This method tests the number of records that would be returned
long QgsSearchQueryBuilder::countRecords( QString searchString )
{
QgsSearchString search;
if ( !search.setString( searchString ) )
{
QMessageBox::critical( this, tr( "Search string parsing error" ), search.parserErrorMsg() );
return -1;
}
QgsSearchTreeNode* searchTree = search.tree();
if ( searchTree == NULL )
{
// entered empty search string
return mLayer->featureCount();
}
QApplication::setOverrideCursor( Qt::WaitCursor );
int count = 0;
QgsFeature feat;
QgsVectorDataProvider* provider = mLayer->dataProvider();
const QgsFieldMap& fields = provider->fields();
QgsAttributeList allAttributes = provider->attributeIndexes();
provider->select( allAttributes, QgsRectangle(), false );
while ( provider->nextFeature( feat ) )
{
if ( searchTree->checkAgainst( fields, feat.attributeMap() ) )
{
count++;
}
// check if there were errors during evaulating
if ( searchTree->hasError() )
break;
}
QApplication::restoreOverrideCursor();
return count;
}
void QgsSearchQueryBuilder::on_btnOk_clicked()
{
// if user hits Ok and there is no query, skip the validation
if ( txtSQL->toPlainText().trimmed().length() > 0 )
{
accept();
return;
}
// test the query to see if it will result in a valid layer
long numRecs = countRecords( txtSQL->toPlainText() );
if ( numRecs == -1 )
{
// error shown in countRecords
}
else if ( numRecs == 0 )
{
QMessageBox::warning( this, tr( "No Records" ), tr( "The query you specified results in zero records being returned." ) );
}
else
{
accept();
}
}
void QgsSearchQueryBuilder::on_btnEqual_clicked()
{
txtSQL->insertPlainText( " = " );
}
void QgsSearchQueryBuilder::on_btnLessThan_clicked()
{
txtSQL->insertPlainText( " < " );
}
void QgsSearchQueryBuilder::on_btnGreaterThan_clicked()
{
txtSQL->insertPlainText( " > " );
}
void QgsSearchQueryBuilder::on_btnPct_clicked()
{
txtSQL->insertPlainText( " % " );
}
void QgsSearchQueryBuilder::on_btnIn_clicked()
{
txtSQL->insertPlainText( " IN " );
}
void QgsSearchQueryBuilder::on_btnNotIn_clicked()
{
txtSQL->insertPlainText( " NOT IN " );
}
void QgsSearchQueryBuilder::on_btnLike_clicked()
{
txtSQL->insertPlainText( " LIKE " );
}
QString QgsSearchQueryBuilder::searchString()
{
return txtSQL->toPlainText();
}
void QgsSearchQueryBuilder::setSearchString( QString searchString )
{
txtSQL->setPlainText( searchString );
}
void QgsSearchQueryBuilder::on_lstFields_doubleClicked( const QModelIndex &index )
{
txtSQL->insertPlainText( mModelFields->data( index ).toString() );
}
void QgsSearchQueryBuilder::on_lstValues_doubleClicked( const QModelIndex &index )
{
txtSQL->insertPlainText( mModelValues->data( index ).toString() );
}
void QgsSearchQueryBuilder::on_btnLessEqual_clicked()
{
txtSQL->insertPlainText( " <= " );
}
void QgsSearchQueryBuilder::on_btnGreaterEqual_clicked()
{
txtSQL->insertPlainText( " >= " );
}
void QgsSearchQueryBuilder::on_btnNotEqual_clicked()
{
txtSQL->insertPlainText( " != " );
}
void QgsSearchQueryBuilder::on_btnAnd_clicked()
{
txtSQL->insertPlainText( " AND " );
}
void QgsSearchQueryBuilder::on_btnNot_clicked()
{
txtSQL->insertPlainText( " NOT " );
}
void QgsSearchQueryBuilder::on_btnOr_clicked()
{
txtSQL->insertPlainText( " OR " );
}
void QgsSearchQueryBuilder::on_btnClear_clicked()
{
txtSQL->clear();
}
void QgsSearchQueryBuilder::on_btnILike_clicked()
{
//txtSQL->insertPlainText(" ILIKE ");
txtSQL->insertPlainText( " ~ " );
}