26 #include <QTextStream>
36 , mExpressionValid( false )
40 mValueGroupBox->hide();
41 mLoadGroupBox->hide();
48 expressionTree->setModel( mProxyModel );
49 expressionTree->setSortingEnabled(
true );
50 expressionTree->sortByColumn( 0, Qt::AscendingOrder );
52 expressionTree->setContextMenuPolicy( Qt::CustomContextMenu );
66 txtSearchEdit->setPlaceholderText(
tr(
"Search" ) );
71 mValuesListView->setModel( mProxyValues );
72 txtSearchEditValues->setPlaceholderText(
tr(
"Search" ) );
75 splitter->restoreState( settings.
value(
"/windows/QgsExpressionBuilderWidget/splitter" ).
toByteArray() );
76 functionsplit->restoreState( settings.
value(
"/windows/QgsExpressionBuilderWidget/functionsplitter" ).
toByteArray() );
78 txtExpressionString->setFoldingVisible(
false );
92 tab_2->setEnabled(
false );
98 expressionTree->setCurrentIndex( firstItem );
105 settings.
setValue(
"/windows/QgsExpressionBuilderWidget/splitter", splitter->saveState() );
106 settings.
setValue(
"/windows/QgsExpressionBuilderWidget/functionsplitter", functionsplit->saveState() );
126 txtSearchEditValues->setText(
QString(
"" ) );
144 QString help = loadFunctionHelp( item );
145 txtHelpText->setText( help );
151 runPythonCode( txtPython->text() );
154 void QgsExpressionBuilderWidget::runPythonCode(
const QString& code )
161 updateFunctionTree();
168 QDir myDir( mFunctionsPath );
171 myDir.
mkpath( mFunctionsPath );
180 QFile myFile( fileName );
181 if ( myFile.
open( QIODevice::WriteOnly ) )
184 myFileStream << txtPython->text() << endl;
191 mFunctionsPath = path;
195 cmbFileNames->
clear();
199 if ( info.
baseName() ==
"__init__" )
continue;
200 cmbFileNames->addItem( info.
baseName() );
208 txtPython->setText( templatetxt );
209 int index = cmbFileNames->findText( fileName );
211 cmbFileNames->setEditText( fileName );
213 cmbFileNames->setCurrentIndex( index );
235 txtPython->loadScript( path );
240 txtPython->setText( code );
247 int index = cmbFileNames->findText( name );
250 cmbFileNames->addItem( name );
251 cmbFileNames->setCurrentIndex( cmbFileNames->count() - 1 );
268 txtExpressionString->setFocus();
289 for (
int i = 0; i < fields.
count(); ++i )
291 QString fieldName = fields[i].name();
292 fieldNames << fieldName;
301 Q_FOREACH (
const QString& fieldName, fieldValues.
keys() )
306 mFieldValues = fieldValues;
309 void QgsExpressionBuilderWidget::fillFieldValues(
const QString& fieldName,
int countLimit )
320 if ( fieldIndex < 0 )
326 Q_FOREACH (
const QVariant& value, values )
331 else if ( value.
type() == QVariant::Int || value.
type() == QVariant::Double || value.
type() == QVariant::LongLong )
335 strValues.
append( strValue );
338 mFieldValues[fieldName] = strValues;
348 item->
setData( label, Qt::UserRole );
352 if ( mExpressionGroups.
contains( group ) )
361 newgroupNode->
setData( group, Qt::UserRole );
367 mExpressionGroups.
insert( group, newgroupNode );
370 if ( highlightedItem )
374 topLevelItem->
setData( label, Qt::UserRole );
386 return mExpressionValid;
398 while ( expressions.
count() > 20 )
403 settings.
setValue( location, expressions );
411 if ( mExpressionGroups.
contains( name ) )
421 Q_FOREACH (
const QString& expression, expressions )
428 void QgsExpressionBuilderWidget::updateFunctionTree()
431 mExpressionGroups.
clear();
454 QString casestring =
"CASE WHEN condition THEN result END";
461 for (
int i = 0; i < count; i++ )
467 if ( func->
group() ==
"deprecated" )
475 if ( func->
params() != 0 )
483 for (
int i = 0; i < specials.
size(); ++i )
485 QString name = specials[i]->name();
486 registerItem( specials[i]->group(), name,
" " + name +
" " );
489 loadExpressionContext();
499 return txtExpressionString->text();
504 txtExpressionString->setText( expression );
509 mExpressionContext = context;
511 loadExpressionContext();
522 lblPreview->setText(
"" );
523 lblPreview->setStyleSheet(
"" );
524 txtExpressionString->setToolTip(
"" );
525 lblPreview->setToolTip(
"" );
547 lblPreview->setText( formatPreviewString( value.
toString() ) );
553 lblPreview->setText(
"" );
562 lblPreview->setText( formatPreviewString( value.
toString() ) );
572 lblPreview->setText(
tr(
"Expression is invalid <a href=""more"">(more info)</a>" ) );
573 lblPreview->setStyleSheet(
"color: rgba(255, 6, 10, 255);" );
574 txtExpressionString->setToolTip( tooltip );
575 lblPreview->setToolTip( tooltip );
581 lblPreview->setStyleSheet(
"" );
582 txtExpressionString->setToolTip(
"" );
583 lblPreview->setToolTip(
"" );
588 QString QgsExpressionBuilderWidget::formatPreviewString(
const QString& previewString )
const
590 if ( previewString.
length() > 63 )
596 return previewString;
600 void QgsExpressionBuilderWidget::loadExpressionContext()
603 Q_FOREACH (
const QString& variable, variableNames )
605 registerItem(
"Variables", variable,
" @" + variable +
" ",
613 Q_FOREACH (
const QString& functionName, contextFunctions )
619 if ( func->
params() != 0 )
628 if ( txtSearchEdit->text().isEmpty() )
629 expressionTree->collapseAll();
631 expressionTree->expandAll();
652 txtExpressionString->insertText(
" " + index.
data( Qt::DisplayRole ).
toString() +
" " );
653 txtExpressionString->setFocus();
661 txtExpressionString->insertText(
" " + button->
text() +
" " );
678 menu->
popup( expressionTree->mapToGlobal( pt ) );
688 if ( !mLayer || !item )
691 mValueGroupBox->show();
692 fillFieldValues( item->
text(), 10 );
701 if ( !mLayer || !item )
704 mValueGroupBox->show();
705 fillFieldValues( item->
text(), -1 );
708 void QgsExpressionBuilderWidget::setExpressionState(
bool state )
710 mExpressionValid = state;
713 QString QgsExpressionBuilderWidget::helpStylesheet()
const
719 style +=
" .functionname {color: #0a6099; font-weight: bold;} "
720 " .argument {font-family: monospace; color: #bf0c0c; font-style: italic; } "
721 " td.argument { padding-right: 10px; }";
728 if ( !expressionItem )
744 return "<head><style>" + helpStylesheet() +
"</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.
bool isHighlightedVariable(const QString &name) const
Returns true if the specified variable name is intended to be highlighted to the user.
QByteArray toByteArray() const
QString getExpressionText() const
QString & append(QChar ch)
void setFilterCaseSensitivity(Qt::CaseSensitivity cs)
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)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
QString getHelpText() const
Get the help text that is associated with this expression item.
bool isValid() const
Returns the validity of this feature.
static QString group(const QString &group)
Returns the translated name for a function group.
static const int CustomSortRole
Custom sort order role.
A abstract base class for defining QgsExpression functions.
virtual void setSourceModel(QAbstractItemModel *sourceModel)
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.
void setBackground(const QBrush &brush)
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)
Returns the help text for a specified function.
Container of fields for a vector layer.
static QString reportStyleSheet()
get a standard css style sheet for reports.
QString tr(const char *sourceText, const char *disambiguation, int n)
QVariant variable(const QString &name) const
Fetches a matching variable from the context.
virtual void setData(const QVariant &value, int role)
void setBold(bool enable)
static const QList< Function * > & Functions()
QList< T > findChildren(const QString &name) const
QList< Key > keys() 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
void append(const T &value)
Search proxy used to filter the QgsExpressionBuilderWidget tree.
void appendRow(const QList< QStandardItem * > &items)
bool isContextual() const
Returns whether the function is only available if provided by a QgsExpressionContext object...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
int removeAll(const T &value)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
void setDynamicSortFilter(bool enable)
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
int count() const
Return number of items.
QStringList functionNames() const
Retrieves a list of function names contained in the context.
void removeRows(int row, int count)
Encapsulate a field in an attribute table or data source.
QString name()
The name of the function.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
void setFont(const QFont &font)
static bool eval(const QString &command, QString &result)
Eval a python statement.
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
QVariant data(int role) const
QString group()
The group the function belongs to.
static bool run(const QString &command, const QString &messageOnError=QString())
Execute a python statement.
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.
QStringList filteredVariableNames() const
Returns a filtered list of variables names set by all scopes in the context.
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)
static QString variableHelpText(const QString &variableName, bool showValue=true, const QVariant &value=QVariant())
Returns the help text for a specified variable.
iterator insert(const Key &key, const T &value)
QgsExpression::Function * function(const QString &name) const
Fetches a matching function from the context.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
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 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)
void setStringList(const QStringList &strings)
virtual QVariant data(int role) const
bool mkpath(const QString &dirPath) const
QgsExpressionItem::ItemType getItemType() const
Get the type of expression item eg header, field, ExpressionNode.
const T value(const Key &key) const