17 #include <QHBoxLayout> 32 , mExpressionDialogTitle( tr(
"Expression Dialog" ) )
36 QHBoxLayout *layout =
new QHBoxLayout(
this );
37 layout->setContentsMargins( 0, 0, 0, 0 );
39 mCombo =
new QComboBox(
this );
40 mCombo->setEditable(
true );
41 mCombo->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
42 int width = mCombo->minimumSizeHint().width();
43 mCombo->setMinimumWidth( width );
47 mCombo->setModel( mFieldProxyModel );
49 mButton =
new QToolButton(
this );
50 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
53 layout->addWidget( mCombo );
54 layout->addWidget( mButton );
59 setFocusProxy( mCombo );
65 connect( mFieldProxyModel, &QAbstractItemModel::modelAboutToBeReset,
this, &QgsFieldExpressionWidget::beforeResetModel );
66 connect( mFieldProxyModel, &QAbstractItemModel::modelReset,
this, &QgsFieldExpressionWidget::afterResetModel );
72 mCombo->installEventFilter(
this );
77 mExpressionDialogTitle = title;
87 mCombo->lineEdit()->setClearButtonEnabled( allowEmpty );
98 QHBoxLayout *layout =
dynamic_cast<QHBoxLayout *
>( this->layout() );
104 QLayoutItem *item = layout->takeAt( 1 );
105 layout->insertWidget( 0, item->widget() );
108 layout->addWidget( mCombo );
113 mDa = std::shared_ptr<const QgsDistanceArea>(
new QgsDistanceArea( da ) );
118 return mCombo->currentText();
153 *isExpression = valueIsExpression;
165 mExpressionContextGenerator = generator;
188 if ( fieldName.isEmpty() )
197 if ( !idx.isValid() )
200 QString simpleFieldName = fieldName.trimmed();
201 if ( simpleFieldName.startsWith(
'"' ) && simpleFieldName.endsWith(
'"' ) )
203 simpleFieldName.remove( 0, 1 ).chop( 1 );
207 if ( !idx.isValid() )
214 QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
215 mCombo->setCurrentIndex( proxyIndex.row() );
236 dlg.setWindowTitle( mExpressionDialogTitle );
254 const QString
expression = mCombo->lineEdit()->text();
257 QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
258 mCombo->setCurrentIndex( proxyIndex.row() );
264 if ( event->type() == QEvent::EnabledChange )
270 void QgsFieldExpressionWidget::reloadLayer()
275 void QgsFieldExpressionWidget::beforeResetModel()
278 mBackupExpression = mCombo->currentText();
281 void QgsFieldExpressionWidget::afterResetModel()
284 mCombo->lineEdit()->setText( mBackupExpression );
289 if ( watched == mCombo && event->type() == QEvent::KeyPress )
291 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
292 if ( keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return )
298 return QObject::eventFilter( watched, event );
303 return mAllowEvalErrors;
308 if ( allowEvalErrors == mAllowEvalErrors )
320 QString fieldName =
currentField( &isExpression, &isValid );
323 QFontMetrics metrics( mCombo->lineEdit()->font() );
324 if ( metrics.width( fieldName ) > mCombo->lineEdit()->width() )
326 mCombo->setToolTip( fieldName );
330 mCombo->setToolTip( QString() );
342 stylesheet = QStringLiteral(
"QLineEdit { color: %1; }" ).arg( QColor( Qt::gray ).name() );
347 if ( !expression.isEmpty() )
356 QFont font = mCombo->lineEdit()->font();
357 font.setItalic( isExpression );
358 mCombo->lineEdit()->setFont( font );
360 if ( isExpression && !isValid )
362 stylesheet = QStringLiteral(
"QLineEdit { color: %1; }" ).arg( QColor( Qt::red ).name() );
365 mCombo->lineEdit()->setStyleSheet( stylesheet );
371 expression.
prepare( &mExpressionContext );
Class for parsing and evaluation of expressions (formerly called "search strings").
Base class for all map layer types.
bool isField(const QString &expression) const
Returns true if a string represents a field reference, or false if it is an expression consisting of ...
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
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.
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.