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(
const QString& searchString )
189 if ( search.hasParserError() )
198 bool fetchGeom = search.needsGeometry();
208 if ( !search.prepare( &context ) )
221 QVariant value = search.evaluate( &context );
222 if ( value.
toInt() != 0 )
228 if ( search.hasEvalError() )
234 if ( search.hasEvalError() )
247 if ( txtSQL->text().trimmed().length() > 0 )
254 long numRecs = countRecords( txtSQL->text() );
259 else if ( numRecs == 0 )
261 QMessageBox::warning(
this,
tr(
"No Records" ),
tr(
"The query you specified results in zero records being returned." ) );
272 txtSQL->insertText(
" = " );
277 txtSQL->insertText(
" < " );
282 txtSQL->insertText(
" > " );
287 txtSQL->insertText(
"%" );
292 txtSQL->insertText(
" IN " );
297 txtSQL->insertText(
" NOT IN " );
302 txtSQL->insertText(
" LIKE " );
307 return txtSQL->text();
312 txtSQL->setText( searchString );
322 txtSQL->insertText( mModelValues->
data( index ).
toString() );
327 txtSQL->insertText(
" <= " );
332 txtSQL->insertText(
" >= " );
337 txtSQL->insertText(
" != " );
342 txtSQL->insertText(
" AND " );
347 txtSQL->insertText(
" NOT " );
352 txtSQL->insertText(
" OR " );
362 txtSQL->insertText(
" ILIKE " );
371 if ( saveFileName.
isNull() )
376 if ( !saveFileName.
endsWith(
".qqf", Qt::CaseInsensitive ) )
378 saveFileName +=
".qqf";
381 QFile saveFile( saveFileName );
382 if ( !saveFile.
open( QIODevice::WriteOnly ) )
395 xmlDoc.
save( fileStream, 2 );
407 if ( queryFileName.
isNull() )
412 QFile queryFile( queryFileName );
413 if ( !queryFile.
open( QIODevice::ReadOnly ) )
447 QStringList attributes = searchTree->referencedColumns();
453 for ( ; fieldIt != mFieldMap.
constEnd(); ++fieldIt )
460 for ( ; attIt != attributes.
constEnd(); ++attIt )
463 if ( !mFieldMap.
contains( *attIt ) )
466 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 ),
467 existingAttributes, 0,
false, &ok );
468 if ( !ok || replaceAttribute.
isEmpty() )
472 attributesToReplace.
insert( *attIt, replaceAttribute );
479 for ( ; columnIt != columnRefList.
end(); ++columnIt )
482 if ( replaceIt != attributesToReplace.
constEnd() )
484 ( *columnIt )->setColumnRef( replaceIt.
value() );
488 if ( attributesToReplace.
size() > 0 )
490 newQueryText = query;
495 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 on_btnEqual_clicked()
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QgsSearchQueryBuilder(QgsVectorLayer *layer, QWidget *parent=0, const Qt::WindowFlags &fl=QgisGui::ModalDialogFlags)
Constructor - takes pointer to vector layer as a parameter.
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)
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsFields fields() const
Returns the list of fields of this layer.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
void on_btnLessEqual_clicked()
void setSearchString(const QString &searchString)
change search string shown in text field
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)
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
int toInt(bool *ok) const
void on_btnGetAllValues_clicked()
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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()
Test the constructed search string to see if it's correct.
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)
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
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.
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)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
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)
char * toString(const QLatin1String &string)
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