25 #include <QTextStream>
33 mValueGroupBox->hide();
34 mLoadGroupBox->hide();
37 mModel =
new QStandardItemModel( );
42 expressionTree->setContextMenuPolicy( Qt::CustomContextMenu );
44 connect( expressionTree, SIGNAL( customContextMenuRequested(
const QPoint & ) ),
this, SLOT(
showContextMenu(
const QPoint & ) ) );
45 connect( expressionTree->selectionModel(), SIGNAL(
currentChanged(
const QModelIndex &,
const QModelIndex & ) ),
46 this, SLOT(
currentChanged(
const QModelIndex &,
const QModelIndex & ) ) );
48 connect( btnLoadAll, SIGNAL( pressed() ),
this, SLOT(
loadAllValues() ) );
49 connect( btnLoadSample, SIGNAL( pressed() ),
this, SLOT(
loadSampleValues() ) );
51 foreach ( QPushButton* button, mOperatorsGroupBox->findChildren<QPushButton *>() )
57 registerItem(
"Operators",
"+",
" + ",
tr(
"Addition operator" ) );
58 registerItem(
"Operators",
"-",
" -" ,
tr(
"Subtraction operator" ) );
59 registerItem(
"Operators",
"*",
" * ",
tr(
"Multiplication operator" ) );
60 registerItem(
"Operators",
"/",
" / ",
tr(
"Division operator" ) );
64 registerItem(
"Operators",
">",
" > ",
tr(
"Greater as operator" ) );
65 registerItem(
"Operators",
"<",
" < ",
tr(
"Less than operator" ) );
66 registerItem(
"Operators",
"<>",
" <> ",
tr(
"Unequal operator" ) );
67 registerItem(
"Operators",
"<=",
" <= ",
tr(
"Less or equal operator" ) );
68 registerItem(
"Operators",
">=",
" >= ",
tr(
"Greater or equal operator" ) );
70 QString(
"<b>|| %1</b><br><i>%2</i><br><i>%3:</i>%4" )
71 .arg(
tr(
"(String Concatenation)" ) )
72 .arg(
tr(
"Joins two values together into a string" ) )
74 .arg(
tr(
"'Dia' || Diameter" ) ) );
82 QString casestring =
"CASE WHEN condition THEN result END";
83 QString caseelsestring =
"CASE WHEN condition THEN result ELSE result END";
85 registerItem(
"Conditionals",
"CASE ELSE", caseelsestring );
89 for (
int i = 0; i < count; i++ )
92 QString name = func->
name();
93 if ( name.startsWith(
"_" ) )
101 for (
int i = 0; i < specials.size(); ++i )
103 QString name = specials[i]->name();
104 registerItem( specials[i]->group(), name,
" " + name +
" " );
107 txtSearchEdit->setPlaceholderText(
tr(
"Search" ) );
110 splitter->restoreState( settings.value(
"/windows/QgsExpressionBuilderWidget/splitter" ).toByteArray() );
111 splitter_2->restoreState( settings.value(
"/windows/QgsExpressionBuilderWidget/splitter2" ).toByteArray() );
118 settings.setValue(
"/windows/QgsExpressionBuilderWidget/splitter", splitter->saveState() );
119 settings.setValue(
"/windows/QgsExpressionBuilderWidget/splitter2", splitter_2->saveState() );
130 QModelIndex idx =
mProxyModel->mapToSource( index );
137 mValueListWidget->clear();
145 txtHelpText->setText( help );
146 txtHelpText->setToolTip( txtHelpText->toPlainText() );
151 QModelIndex idx =
mProxyModel->mapToSource( index );
162 txtExpressionString->setFocus();
180 QStringList fieldNames;
182 for (
int i = 0; i < fields.
count(); ++i )
184 QString fieldName = fields[i].name();
185 fieldNames << fieldName;
199 mValueListWidget->clear();
200 mValueListWidget->setUpdatesEnabled(
false );
201 mValueListWidget->blockSignals(
true );
203 QList<QVariant> values;
205 foreach ( QVariant value, values )
207 if ( value.isNull() )
208 mValueListWidget->addItem(
"NULL" );
209 else if ( value.type() == QVariant::Int || value.type() == QVariant::Double || value.type() == QVariant::LongLong )
210 mValueListWidget->addItem( value.toString() );
212 mValueListWidget->addItem(
"'" + value.toString().replace(
"'",
"''" ) +
"'" );
215 mValueListWidget->setUpdatesEnabled(
true );
216 mValueListWidget->blockSignals(
false );
221 QString expressionText,
226 item->setData( label, Qt::UserRole );
231 groupNode->appendRow( item );
237 newgroupNode->setData( group, Qt::UserRole );
238 newgroupNode->appendRow( item );
239 mModel->appendRow( newgroupNode );
252 QString location = QString(
"/expressions/recent/%1" ).arg( key );
253 QStringList expressions = settings.value( location ).toStringList();
258 while ( expressions.count() > 20 )
260 expressions.pop_back();
263 settings.setValue( location, expressions );
269 QString name =
tr(
"Recent (%1)" ).arg( key );
273 node->removeRows( 0, node->rowCount() );
277 QString location = QString(
"/expressions/recent/%1" ).arg( key );
278 QStringList expressions = settings.value( location ).toStringList();
279 foreach ( QString expression, expressions )
281 this->
registerItem( name, expression, expression, expression );
292 return txtExpressionString->toPlainText();
297 txtExpressionString->setPlainText( expression );
302 QString text = txtExpressionString->toPlainText();
306 if ( text.isEmpty() )
308 lblPreview->setText(
"" );
309 lblPreview->setStyleSheet(
"" );
310 txtExpressionString->setToolTip(
"" );
311 lblPreview->setToolTip(
"" );
334 lblPreview->setText( value.toString() );
340 lblPreview->setText(
"" );
349 lblPreview->setText( value.toString() );
355 QString tooltip = QString(
"<b>%1:</b><br>%2" ).arg(
tr(
"Parser Error" ) ).arg( exp.
parserErrorString() );
357 tooltip += QString(
"<br><br><b>%1:</b><br>%2" ).arg(
tr(
"Eval Error" ) ).arg( exp.
evalErrorString() );
359 lblPreview->setText(
tr(
"Expression is invalid <a href=""more"">(more info)</a>" ) );
360 lblPreview->setStyleSheet(
"color: rgba(255, 6, 10, 255);" );
361 txtExpressionString->setToolTip( tooltip );
362 lblPreview->setToolTip( tooltip );
368 lblPreview->setStyleSheet(
"" );
369 txtExpressionString->setToolTip(
"" );
370 lblPreview->setToolTip(
"" );
377 mProxyModel->setFilterWildcard( txtSearchEdit->text() );
378 if ( txtSearchEdit->text().isEmpty() )
379 expressionTree->collapseAll();
381 expressionTree->expandAll();
388 mv->setWindowTitle(
tr(
"More info on expression error" ) );
395 txtExpressionString->insertPlainText(
" " + item->text() +
" " );
396 txtExpressionString->setFocus();
401 QPushButton* button =
dynamic_cast<QPushButton*
>( sender() );
402 txtExpressionString->insertPlainText(
" " + button->text() +
" " );
403 txtExpressionString->setFocus();
408 QModelIndex idx = expressionTree->indexAt( pt );
416 QMenu* menu =
new QMenu(
this );
417 menu->addAction(
tr(
"Load top 10 unique values" ),
this, SLOT(
loadSampleValues() ) );
418 menu->addAction(
tr(
"Load all unique values" ),
this, SLOT(
loadAllValues() ) );
419 menu->popup( expressionTree->mapToGlobal( pt ) );
425 QModelIndex idx =
mProxyModel->mapToSource( expressionTree->currentIndex() );
432 mValueGroupBox->show();
439 QModelIndex idx =
mProxyModel->mapToSource( expressionTree->currentIndex() );
446 mValueGroupBox->show();
458 if ( !expressionItem )
461 QString helpContents = expressionItem->
getHelpText();
464 if ( helpContents.isEmpty() )
466 QString name = expressionItem->data( Qt::UserRole ).toString();
475 return "<head><style>" + myStyle +
"</style></head><body>" + helpContents +
"</body>";
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 hasParserError() const
Returns true if an error occurred when parsing the input expression.
bool isValid() const
Return 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.
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)
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.
static const QList< Function * > & Functions()
void setMessageAsHtml(const QString &msg)
void addFields(QStringList fieldList)
static int functionCount()
Returns the number of functions defined in the parser.
Search proxy used to filter the QgsExpressionBuilderWidget tree.
int count() const
Return number of items.
QString helptext()
The help text for the function.
QString name()
The name of the function.
QgsExpressionItem::ItemType getItemType()
Get the type of expression item eg header, field, ExpressionNode.
General purpose distance and area calculator.
An expression item that can be used in the QgsExpressionBuilderWidget tree.
QString group()
The group the function belongs to.
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.
void setGeomCalculator(const QgsDistanceArea &calc)
Sets the geometry calculator used in evaluation of expressions,.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
bool nextFeature(QgsFeature &f)
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 getHelpText()
Get the help text that is associated with this expression item.
QString evalErrorString() const
Returns evaluation error.