17 #include <QHBoxLayout> 33 , mExpressionDialogTitle( tr(
"Expression Dialog" ) )
37 QHBoxLayout *layout =
new QHBoxLayout(
this );
38 layout->setContentsMargins( 0, 0, 0, 0 );
40 mCombo =
new QComboBox(
this );
41 mCombo->setEditable(
true );
42 mCombo->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
43 int width = mCombo->minimumSizeHint().width();
44 mCombo->setMinimumWidth( width );
48 mCombo->setModel( mFieldProxyModel );
50 mButton =
new QToolButton(
this );
51 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
54 layout->addWidget( mCombo );
55 layout->addWidget( mButton );
60 setFocusProxy( mCombo );
66 connect( mFieldProxyModel, &QAbstractItemModel::modelAboutToBeReset,
this, &QgsFieldExpressionWidget::beforeResetModel );
67 connect( mFieldProxyModel, &QAbstractItemModel::modelReset,
this, &QgsFieldExpressionWidget::afterResetModel );
73 mCombo->installEventFilter(
this );
78 mExpressionDialogTitle = title;
88 mCombo->lineEdit()->setClearButtonEnabled( allowEmpty );
99 QHBoxLayout *layout =
dynamic_cast<QHBoxLayout *
>( this->layout() );
105 QLayoutItem *item = layout->takeAt( 1 );
106 layout->insertWidget( 0, item->widget() );
109 layout->addWidget( mCombo );
114 mDa = std::shared_ptr<const QgsDistanceArea>(
new QgsDistanceArea( da ) );
119 return mCombo->currentText();
154 *isExpression = valueIsExpression;
166 mExpressionContextGenerator = generator;
189 if ( fieldName.isEmpty() )
198 if ( !idx.isValid() )
201 QString simpleFieldName = fieldName.trimmed();
202 if ( simpleFieldName.startsWith(
'"' ) && simpleFieldName.endsWith(
'"' ) )
204 simpleFieldName.remove( 0, 1 ).chop( 1 );
208 if ( !idx.isValid() )
215 QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
216 mCombo->setCurrentIndex( proxyIndex.row() );
237 dlg.setWindowTitle( mExpressionDialogTitle );
255 const QString
expression = mCombo->lineEdit()->text();
258 QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
259 mCombo->setCurrentIndex( proxyIndex.row() );
265 if ( event->type() == QEvent::EnabledChange )
271 void QgsFieldExpressionWidget::reloadLayer()
276 void QgsFieldExpressionWidget::beforeResetModel()
279 mBackupExpression = mCombo->currentText();
282 void QgsFieldExpressionWidget::afterResetModel()
285 mCombo->lineEdit()->setText( mBackupExpression );
290 if ( watched == mCombo && event->type() == QEvent::KeyPress )
292 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
293 if ( keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return )
299 return QObject::eventFilter( watched, event );
304 return mAllowEvalErrors;
309 if ( allowEvalErrors == mAllowEvalErrors )
321 QString fieldName =
currentField( &isExpression, &isValid );
324 QFontMetrics metrics( mCombo->lineEdit()->font() );
325 if ( metrics.width( fieldName ) > mCombo->lineEdit()->width() )
327 mCombo->setToolTip( fieldName );
331 mCombo->setToolTip( QString() );
340 QPalette palette = mCombo->lineEdit()->palette();
343 palette.setColor( QPalette::Text, Qt::gray );
348 if ( !expression.isEmpty() )
357 QFont font = mCombo->lineEdit()->font();
358 font.setItalic( isExpression );
359 mCombo->lineEdit()->setFont( font );
361 if ( isExpression && !isValid )
363 palette.setColor( QPalette::Text, Qt::red );
367 palette.setColor( QPalette::Text, Qt::black );
370 mCombo->lineEdit()->setPalette( palette );
376 expression.
prepare( &mExpressionContext );
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.
Base class for all map layer types.
QgsFieldModel * sourceFieldModel()
Returns the QgsFieldModel used in this QSortFilterProxyModel.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setExpression(const QString &expression)
Sets a single expression to be added after the fields at the end of the model.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
void setAllowEvalErrors(bool allowEvalErrors)
Allow accepting expressions with evaluation errors.
static bool checkExpression(const QString &text, const QgsExpressionContext *context, QString &errorMessage)
Tests whether a string is a valid expression.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setLayer(QgsVectorLayer *layer)
Set the layer from which fields are displayed.
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
void setAllowExpression(bool allowExpression)
Sets whether custom expressions are accepted and displayed in the model.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Abstract interface for generating an expression context.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
bool isField(const QString &expression) const
Returns true if a string represents a field reference, or false if it is an expression consisting of ...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QModelIndex indexFromName(const QString &fieldName)
Returns the index corresponding to a given fieldName.
Represents a vector layer which manages a vector based data sets.
void updatedFields()
Is emitted, whenever the fields available from this layer have been changed.
A generic dialog for building expression strings.