16 #include <QDomDocument> 17 #include <QDomElement> 18 #include <QFileDialog> 20 #include <QInputDialog> 22 #include <QMessageBox> 24 #include <QStandardItem> 25 #include <QTextStream> 44 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
48 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
52 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
57 buttonBox->addButton( pbn, QDialogButtonBox::ActionRole );
62 lblDataUri->setText( layer->
name() );
71 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()
92 lstFields->setModel( mModelFields );
93 lstValues->setModel( mModelValues );
95 lstFields->setViewMode( QListView::ListMode );
96 lstValues->setViewMode( QListView::ListMode );
97 lstFields->setSelectionBehavior( QAbstractItemView::SelectRows );
98 lstValues->setSelectionBehavior( QAbstractItemView::SelectRows );
100 lstFields->setUniformItemSizes(
true );
101 lstValues->setUniformItemSizes(
true );
104 void QgsSearchQueryBuilder::getFieldValues(
int limit )
111 mModelValues->
clear();
114 QString fieldName = mModelFields->
data( lstFields->currentIndex() ).toString();
115 int fieldIndex = mFieldMap[fieldName];
117 bool numeric = ( field.
type() == QVariant::Int || field.
type() == QVariant::Double );
123 attrs.
append( fieldIndex );
127 lstValues->setCursor( Qt::WaitCursor );
130 lstValues->setUpdatesEnabled(
false );
136 ( limit == 0 || mModelValues->
rowCount() != limit ) )
143 value =
'\'' + value.
replace(
'\'',
"''" ) +
'\'';
147 if ( !insertedValues.
contains( value ) )
152 insertedValues.
insert( value );
157 lstValues->setUpdatesEnabled(
true );
159 mModelValues->
sort( 0 );
160 lstValues->setCursor( Qt::ArrowCursor );
165 getFieldValues( 25 );
175 long count = countRecords( txtSQL->text() );
207 if ( !search.
prepare( &context ) )
221 if ( value.
toInt() != 0 )
246 if ( txtSQL->text().trimmed().length() > 0 )
253 long numRecs = countRecords( txtSQL->text() );
258 else if ( numRecs == 0 )
260 QMessageBox::warning(
this,
tr(
"No Records" ),
tr(
"The query you specified results in zero records being returned." ) );
271 txtSQL->insertText(
" = " );
276 txtSQL->insertText(
" < " );
281 txtSQL->insertText(
" > " );
286 txtSQL->insertText(
"%" );
291 txtSQL->insertText(
" IN " );
296 txtSQL->insertText(
" NOT IN " );
301 txtSQL->insertText(
" LIKE " );
306 return txtSQL->text();
311 txtSQL->setText( searchString );
321 txtSQL->insertText( mModelValues->
data( index ).
toString() );
326 txtSQL->insertText(
" <= " );
331 txtSQL->insertText(
" >= " );
336 txtSQL->insertText(
" != " );
341 txtSQL->insertText(
" AND " );
346 txtSQL->insertText(
" NOT " );
351 txtSQL->insertText(
" OR " );
361 txtSQL->insertText(
" ILIKE " );
370 if ( saveFileName.
isNull() )
375 if ( !saveFileName.
endsWith(
".qqf", Qt::CaseInsensitive ) )
377 saveFileName +=
".qqf";
380 QFile saveFile( saveFileName );
381 if ( !saveFile.
open( QIODevice::WriteOnly ) )
394 xmlDoc.
save( fileStream, 2 );
406 if ( queryFileName.
isNull() )
411 QFile queryFile( queryFileName );
412 if ( !queryFile.
open( QIODevice::ReadOnly ) )
446 QStringList attributes = searchTree->referencedColumns();
452 for ( ; fieldIt != mFieldMap.
constEnd(); ++fieldIt )
459 for ( ; attIt != attributes.
constEnd(); ++attIt )
462 if ( !mFieldMap.
contains( *attIt ) )
465 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 ),
466 existingAttributes, 0,
false, &ok );
467 if ( !ok || replaceAttribute.
isEmpty() )
471 attributesToReplace.
insert( *attIt, replaceAttribute );
478 for ( ; columnIt != columnRefList.
end(); ++columnIt )
481 if ( replaceIt != attributesToReplace.
constEnd() )
483 ( *columnIt )->setColumnRef( replaceIt.
value() );
487 if ( attributesToReplace.
size() > 0 )
489 newQueryText = query;
494 txtSQL->insertText( newQueryText );
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
Wrapper for iterator of features from vector data provider or vector layer.
void on_btnEqual_clicked()
bool contains(const Key &key) const
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
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)
Q_DECL_DEPRECATED bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
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
QString evalErrorString() const
Returns evaluation error.
void on_btnGreaterEqual_clicked()
Container of fields for a vector layer.
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
const_iterator insert(const T &value)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
int count() const
Return number of items.
QString parserErrorString() const
Returns parser error.
QString tr(const char *sourceText, const char *disambiguation, int n)
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
QgsFields fields() const
Returns the list of fields of this layer.
void on_btnLessThan_clicked()
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...
QgsSearchQueryBuilder(QgsVectorLayer *layer, QWidget *parent=nullptr, const Qt::WindowFlags &fl=QgisGui::ModalDialogFlags)
Constructor - takes pointer to vector layer as a parameter.
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
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)
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()
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)
QString name
Read property of QString layerName.
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)
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
iterator find(const Key &key)
void setEditable(bool editable)
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
const T value(const Key &key) const
QString searchString()
returns newly created search string