16 #include <QDomDocument>
17 #include <QDomElement>
18 #include <QFileDialog>
20 #include <QInputDialog>
22 #include <QMessageBox>
24 #include <QStandardItem>
25 #include <QTextStream>
35 :
QDialog( parent, fl ), mLayer( layer )
43 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
47 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
51 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
56 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
61 lblDataUri->setText( layer->
name() );
70 void QgsSearchQueryBuilder::populateFields()
77 for (
int idx = 0; idx < fields.
count(); ++idx )
79 QString fieldName = fields[idx].name();
80 mFieldMap[fieldName] = idx;
87 void QgsSearchQueryBuilder::setupListViews()
93 lstFields->setModel( mModelFields );
94 lstValues->setModel( mModelValues );
96 lstFields->setViewMode( QListView::ListMode );
97 lstValues->setViewMode( QListView::ListMode );
98 lstFields->setSelectionBehavior( QAbstractItemView::SelectRows );
99 lstValues->setSelectionBehavior( QAbstractItemView::SelectRows );
101 lstFields->setUniformItemSizes(
true );
102 lstValues->setUniformItemSizes(
true );
105 void QgsSearchQueryBuilder::getFieldValues(
int limit )
112 mModelValues->
clear();
116 int fieldIndex = mFieldMap[fieldName];
118 bool numeric = ( field.
type() == QVariant::Int || field.
type() == QVariant::Double );
124 attrs.
append( fieldIndex );
128 lstValues->setCursor( Qt::WaitCursor );
131 lstValues->setUpdatesEnabled(
false );
137 ( limit == 0 || mModelValues->
rowCount() != limit ) )
144 value =
"'" + value.
replace(
"'",
"''" ) +
"'";
148 if ( !insertedValues.
contains( value ) )
153 insertedValues.
insert( value );
158 lstValues->setUpdatesEnabled(
true );
160 mModelValues->
sort( 0 );
161 lstValues->setCursor( Qt::ArrowCursor );
166 getFieldValues( 25 );
176 long count = countRecords( txtSQL->text() );
186 long QgsSearchQueryBuilder::countRecords(
QString searchString )
189 if ( search.hasParserError() )
198 bool fetchGeom = search.needsGeometry();
204 if ( !search.prepare( fields ) )
216 QVariant value = search.evaluate( &feat );
217 if ( value.
toInt() != 0 )
223 if ( search.hasEvalError() )
229 if ( search.hasEvalError() )
242 if ( txtSQL->text().trimmed().length() > 0 )
249 long numRecs = countRecords( txtSQL->text() );
254 else if ( numRecs == 0 )
256 QMessageBox::warning(
this,
tr(
"No Records" ),
tr(
"The query you specified results in zero records being returned." ) );
267 txtSQL->insertText(
" = " );
272 txtSQL->insertText(
" < " );
277 txtSQL->insertText(
" > " );
282 txtSQL->insertText(
"%" );
287 txtSQL->insertText(
" IN " );
292 txtSQL->insertText(
" NOT IN " );
297 txtSQL->insertText(
" LIKE " );
302 return txtSQL->text();
307 txtSQL->setText( searchString );
317 txtSQL->insertText( mModelValues->
data( index ).
toString() );
322 txtSQL->insertText(
" <= " );
327 txtSQL->insertText(
" >= " );
332 txtSQL->insertText(
" != " );
337 txtSQL->insertText(
" AND " );
342 txtSQL->insertText(
" NOT " );
347 txtSQL->insertText(
" OR " );
357 txtSQL->insertText(
" ILIKE " );
366 if ( saveFileName.
isNull() )
371 if ( !saveFileName.
endsWith(
".qqf", Qt::CaseInsensitive ) )
373 saveFileName +=
".qqf";
376 QFile saveFile( saveFileName );
377 if ( !saveFile.
open( QIODevice::WriteOnly ) )
390 xmlDoc.
save( fileStream, 2 );
402 if ( queryFileName.
isNull() )
407 QFile queryFile( queryFileName );
408 if ( !queryFile.
open( QIODevice::ReadOnly ) )
442 QStringList attributes = searchTree->referencedColumns();
448 for ( ; fieldIt != mFieldMap.
constEnd(); ++fieldIt )
455 for ( ; attIt != attributes.
constEnd(); ++attIt )
458 if ( !mFieldMap.
contains( *attIt ) )
461 QString replaceAttribute =
QInputDialog::getItem( 0,
tr(
"Select attribute" ),
tr(
"There is no attribute '%1' in the current vector layer. Please select an existing attribute" ).arg( *attIt ),
462 existingAttributes, 0,
false, &ok );
463 if ( !ok || replaceAttribute.
isEmpty() )
467 attributesToReplace.
insert( *attIt, replaceAttribute );
474 for ( ; columnIt != columnRefList.
end(); ++columnIt )
477 if ( replaceIt != attributesToReplace.
constEnd() )
479 ( *columnIt )->setColumnRef( replaceIt.
value() );
483 if ( attributesToReplace.
size() > 0 )
485 newQueryText = query;
490 txtSQL->insertText( newQueryText );
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
void setSearchString(QString searchString)
change search string shown in text field
void on_btnEqual_clicked()
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
bool contains(const Key &key) const
static QString quotedColumnRef(QString name)
return quoted column reference (in double quotes)
virtual QVariant data(const QModelIndex &index, int role) const
QDomNode appendChild(const QDomNode &newChild)
void push_back(const T &value)
void on_btnNotEqual_clicked()
void on_lstFields_doubleClicked(const QModelIndex &index)
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
void on_btnLessEqual_clicked()
const_iterator constBegin() const
void on_btnGreaterEqual_clicked()
Container of fields for a vector layer.
const_iterator insert(const T &value)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QString tr(const char *sourceText, const char *disambiguation, int n)
void on_btnLessThan_clicked()
const QString & name() const
Get the display name of the layer.
void setValue(const QString &key, const QVariant &value)
void on_btnSampleValues_clicked()
void append(const T &value)
int toInt(bool *ok) const
void on_btnGetAllValues_clicked()
void on_btnClear_clicked()
const_iterator constEnd() const
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void setOverrideCursor(const QCursor &cursor)
void restoreOverrideCursor()
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
int count() const
Return number of items.
void on_btnTest_clicked()
void insertRow(int row, const QList< QStandardItem * > &items)
Encapsulate a field in an attribute table or data source.
void on_btnLike_clicked()
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QDomText createTextNode(const QString &value)
void on_lstValues_doubleClicked(const QModelIndex &index)
bool blockSignals(bool block)
bool contains(const T &value) const
const Key key(const T &value) const
void on_btnILike_clicked()
QString & replace(int position, int n, QChar after)
QVariant value(const QString &key, const QVariant &defaultValue) const
void save(QTextStream &str, int indent) const
void on_btnGreaterThan_clicked()
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
char * toString(const T &value)
void on_btnNotIn_clicked()
QDomElement firstChildElement(const QString &tagName) const
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
virtual int rowCount(const QModelIndex &parent) const
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
iterator insert(const Key &key, const T &value)
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
const_iterator constEnd() const
QDomElement createElement(const QString &tagName)
bool nextFeature(QgsFeature &f)
const_iterator constBegin() const
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QString absolutePath() const
virtual void sort(int column, Qt::SortOrder order)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
QString parserErrorString() const
Returns parser error.
iterator find(const Key &key)
void setEditable(bool editable)
QgsSearchQueryBuilder(QgsVectorLayer *layer, QWidget *parent=0, Qt::WindowFlags fl=QgisGui::ModalDialogFlags)
Constructor - takes pointer to vector layer as a parameter.
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
const T value(const Key &key) const
QString searchString()
returns newly created search string