26 #include <QTextStream>
34 , mExpressionValid( false )
38 mValueGroupBox->hide();
39 mLoadGroupBox->hide();
45 expressionTree->setModel( mProxyModel );
47 expressionTree->setContextMenuPolicy( Qt::CustomContextMenu );
61 txtSearchEdit->setPlaceholderText(
tr(
"Search" ) );
64 splitter->restoreState( settings.
value(
"/windows/QgsExpressionBuilderWidget/splitter" ).
toByteArray() );
65 functionsplit->restoreState( settings.
value(
"/windows/QgsExpressionBuilderWidget/functionsplitter" ).
toByteArray() );
67 txtExpressionString->setFoldingVisible(
false );
81 tab_2->setEnabled(
false );
87 expressionTree->setCurrentIndex( firstItem );
94 settings.
setValue(
"/windows/QgsExpressionBuilderWidget/splitter", splitter->saveState() );
95 settings.
setValue(
"/windows/QgsExpressionBuilderWidget/functionsplitter", functionsplit->saveState() );
113 mValueListWidget->clear();
120 QString help = loadFunctionHelp( item );
121 txtHelpText->setText( help );
122 txtHelpText->setToolTip( txtHelpText->toPlainText() );
128 runPythonCode( txtPython->text() );
131 void QgsExpressionBuilderWidget::runPythonCode(
QString code )
138 updateFunctionTree();
145 QDir myDir( mFunctionsPath );
148 myDir.
mkpath( mFunctionsPath );
157 QFile myFile( fileName );
158 if ( myFile.
open( QIODevice::WriteOnly ) )
161 myFileStream << txtPython->text() << endl;
168 mFunctionsPath = path;
172 cmbFileNames->
clear();
176 if ( info.
baseName() ==
"__init__" )
continue;
177 cmbFileNames->addItem( info.
baseName() );
185 txtPython->setText( templatetxt );
186 int index = cmbFileNames->findText( fileName );
188 cmbFileNames->setEditText( fileName );
190 cmbFileNames->setCurrentIndex( index );
212 txtPython->loadScript( path );
217 txtPython->setText( code );
224 int index = cmbFileNames->findText( name );
227 cmbFileNames->addItem( name );
228 cmbFileNames->setCurrentIndex( cmbFileNames->count() - 1 );
245 txtExpressionString->setFocus();
265 for (
int i = 0; i < fields.
count(); ++i )
267 QString fieldName = fields[i].name();
268 fieldNames << fieldName;
274 void QgsExpressionBuilderWidget::fillFieldValues(
int fieldIndex,
int countLimit )
282 mValueListWidget->clear();
284 if ( fieldIndex < 0 )
287 mValueListWidget->setUpdatesEnabled(
false );
288 mValueListWidget->blockSignals(
true );
295 mValueListWidget->addItem(
"NULL" );
296 else if ( value.
type() == QVariant::Int || value.
type() == QVariant::Double || value.
type() == QVariant::LongLong )
297 mValueListWidget->addItem( value.
toString() );
299 mValueListWidget->addItem(
"'" + value.
toString().
replace(
"'",
"''" ) +
"'" );
302 mValueListWidget->setUpdatesEnabled(
true );
303 mValueListWidget->blockSignals(
false );
313 item->
setData( label, Qt::UserRole );
315 if ( mExpressionGroups.
contains( group ) )
324 newgroupNode->
setData( group, Qt::UserRole );
327 mExpressionGroups.
insert( group, newgroupNode );
333 return mExpressionValid;
345 while ( expressions.
count() > 20 )
350 settings.
setValue( location, expressions );
358 if ( mExpressionGroups.
contains( name ) )
367 foreach (
QString expression, expressions )
369 this->
registerItem( name, expression, expression, expression );
373 void QgsExpressionBuilderWidget::updateFunctionTree()
376 mExpressionGroups.
clear();
378 registerItem(
"Operators",
"+",
" + ",
tr(
"Addition operator" ) );
379 registerItem(
"Operators",
"-",
" - ",
tr(
"Subtraction operator" ) );
380 registerItem(
"Operators",
"*",
" * ",
tr(
"Multiplication operator" ) );
381 registerItem(
"Operators",
"/",
" / ",
tr(
"Division operator" ) );
385 registerItem(
"Operators",
">",
" > ",
tr(
"Greater as operator" ) );
386 registerItem(
"Operators",
"<",
" < ",
tr(
"Less than operator" ) );
387 registerItem(
"Operators",
"<>",
" <> ",
tr(
"Unequal operator" ) );
388 registerItem(
"Operators",
"<=",
" <= ",
tr(
"Less or equal operator" ) );
389 registerItem(
"Operators",
">=",
" >= ",
tr(
"Greater or equal operator" ) );
391 QString(
"<b>|| %1</b><br><i>%2</i><br><i>%3:</i>%4" )
392 .arg(
tr(
"(String Concatenation)" ) )
393 .arg(
tr(
"Joins two values together into a string" ) )
394 .arg(
tr(
"Usage" ) )
395 .arg(
tr(
"'Dia' || Diameter" ) ) );
404 QString casestring =
"CASE WHEN condition THEN result END";
405 QString caseelsestring =
"CASE WHEN condition THEN result ELSE result END";
407 registerItem(
"Conditionals",
"CASE ELSE", caseelsestring );
413 for (
int i = 0; i < count; i++ )
419 if ( func->
params() != 0 )
427 for (
int i = 0; i < specials.
size(); ++i )
429 QString name = specials[i]->name();
430 registerItem( specials[i]->group(), name,
" " + name +
" " );
441 return txtExpressionString->text();
446 txtExpressionString->setText( expression );
457 lblPreview->setText(
"" );
458 lblPreview->setStyleSheet(
"" );
459 txtExpressionString->setToolTip(
"" );
460 lblPreview->setToolTip(
"" );
481 lblPreview->setText( formatPreviewString( value.
toString() ) );
487 lblPreview->setText(
"" );
496 lblPreview->setText( formatPreviewString( value.
toString() ) );
506 lblPreview->setText(
tr(
"Expression is invalid <a href=""more"">(more info)</a>" ) );
507 lblPreview->setStyleSheet(
"color: rgba(255, 6, 10, 255);" );
508 txtExpressionString->setToolTip( tooltip );
509 lblPreview->setToolTip( tooltip );
515 lblPreview->setStyleSheet(
"" );
516 txtExpressionString->setToolTip(
"" );
517 lblPreview->setToolTip(
"" );
522 QString QgsExpressionBuilderWidget::formatPreviewString(
const QString& previewString )
const
524 if ( previewString.
length() > 63 )
530 return previewString;
537 if ( txtSearchEdit->text().isEmpty() )
538 expressionTree->collapseAll();
540 expressionTree->expandAll();
555 txtExpressionString->insertText(
" " + item->
text() +
" " );
556 txtExpressionString->setFocus();
564 txtExpressionString->insertText(
" " + button->
text() +
" " );
581 menu->
popup( expressionTree->mapToGlobal( pt ) );
591 if ( !mLayer || !item )
594 mValueGroupBox->show();
597 fillFieldValues( fieldIndex, 10 );
606 if ( !mLayer || !item )
609 mValueGroupBox->show();
611 fillFieldValues( fieldIndex, -1 );
614 void QgsExpressionBuilderWidget::setExpressionState(
bool state )
616 mExpressionValid = state;
621 if ( !expressionItem )
638 return "<head><style>" + myStyle +
"</style></head><body>" + helpContents +
"</body>";
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
QByteArray toByteArray() const
QString & append(QChar ch)
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
bool contains(const Key &key) const
void setNameFilters(const QStringList &nameFilters)
bool isValid() const
Returns the validity of this feature.
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
A abstract base class for defining QgsExpression functions.
virtual void setSourceModel(QAbstractItemModel *sourceModel)
void uniqueValues(int index, QList< QVariant > &uniqueValues, int limit=-1)
Returns unique values for column.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
static QString helptext(QString name)
static bool eval(QString command, QString &result)
Eval a python statement.
Container of fields for a vector layer.
static QString group(QString group)
QString getExpressionText()
static QString reportStyleSheet()
get a standard css style sheet for reports.
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual void setData(const QVariant &value, int role)
static const QList< Function * > & Functions()
QList< T > findChildren(const QString &name) const
void setMessageAsHtml(const QString &msg)
void setValue(const QString &key, const QVariant &value)
static int functionCount()
Returns the number of functions defined in the parser.
const char * name() const
int count(const T &value) const
Search proxy used to filter the QgsExpressionBuilderWidget tree.
static bool run(QString command, QString messageOnError=QString())
execute a python statement
void appendRow(const QList< QStandardItem * > &items)
int removeAll(const T &value)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
int count() const
Return number of items.
void removeRows(int row, int count)
QString name()
The name of the function.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QgsExpressionItem::ItemType getItemType()
Get the type of expression item eg header, field, ExpressionNode.
void setFilterWildcard(const QString &pattern)
General purpose distance and area calculator.
QString & replace(int position, int n, QChar after)
An expression item that can be used in the QgsExpressionBuilderWidget tree.
QVariant value(const QString &key, const QVariant &defaultValue) const
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
QStringList toStringList() const
QString group()
The group the function belongs to.
QStandardItem * itemFromIndex(const QModelIndex &index) const
QStringList entryList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
static QList< Function * > specialColumns()
Returns a list of special Column definitions.
int params()
The number of parameters this function takes.
A generic message view for displaying QGIS messages.
const QString helptext()
The help text for the function.
QString left(int n) const
void setGeomCalculator(const QgsDistanceArea &calc)
Sets the geometry calculator used in evaluation of expressions,.
void prepend(const T &value)
iterator insert(const Key &key, const T &value)
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
bool nextFeature(QgsFeature &f)
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.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
bool isEmpty() const
Check whether the container is empty.
QString parserErrorString() const
Returns parser error.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString getHelpText()
Get the help text that is associated with this expression item.
QString evalErrorString() const
Returns evaluation error.
static bool isValid()
returns true if the runner has an instance (and thus is able to run commands)
void appendRow(const QList< QStandardItem * > &items)
virtual QVariant data(int role) const
bool mkpath(const QString &dirPath) const
const T value(const Key &key) const