17 #include <QHBoxLayout> 34 , mExpressionDialogTitle( tr(
"Expression Dialog" ) )
38 QHBoxLayout *layout =
new QHBoxLayout(
this );
39 layout->setContentsMargins( 0, 0, 0, 0 );
41 mCombo =
new QComboBox(
this );
42 mCombo->setEditable(
true );
43 mCombo->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
44 int width = mCombo->minimumSizeHint().width();
45 mCombo->setMinimumWidth( width );
49 mCombo->setModel( mFieldProxyModel );
51 mButton =
new QToolButton(
this );
52 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
55 layout->addWidget( mCombo );
56 layout->addWidget( mButton );
61 setFocusProxy( mCombo );
67 connect( mFieldProxyModel, &QAbstractItemModel::modelAboutToBeReset,
this, &QgsFieldExpressionWidget::beforeResetModel );
68 connect( mFieldProxyModel, &QAbstractItemModel::modelReset,
this, &QgsFieldExpressionWidget::afterResetModel );
74 mCombo->installEventFilter(
this );
79 mExpressionDialogTitle = title;
89 mCombo->lineEdit()->setClearButtonEnabled( allowEmpty );
100 QHBoxLayout *layout =
dynamic_cast<QHBoxLayout *
>( this->layout() );
106 QLayoutItem *item = layout->takeAt( 1 );
107 layout->insertWidget( 0, item->widget() );
110 layout->addWidget( mCombo );
115 mDa = std::shared_ptr<const QgsDistanceArea>(
new QgsDistanceArea( da ) );
120 return mCombo->currentText();
155 *isExpression = valueIsExpression;
167 mExpressionContextGenerator = generator;
190 if ( fieldName.isEmpty() )
199 if ( !idx.isValid() )
202 QString simpleFieldName = fieldName.trimmed();
203 if ( simpleFieldName.startsWith(
'"' ) && simpleFieldName.endsWith(
'"' ) )
205 simpleFieldName.remove( 0, 1 ).chop( 1 );
209 if ( !idx.isValid() )
216 QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
217 mCombo->setCurrentIndex( proxyIndex.row() );
238 dlg.setWindowTitle( mExpressionDialogTitle );
256 const QString
expression = mCombo->lineEdit()->text();
259 QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
260 mCombo->setCurrentIndex( proxyIndex.row() );
266 if ( event->type() == QEvent::EnabledChange )
272 void QgsFieldExpressionWidget::reloadLayer()
277 void QgsFieldExpressionWidget::beforeResetModel()
280 mBackupExpression = mCombo->currentText();
283 void QgsFieldExpressionWidget::afterResetModel()
286 mCombo->lineEdit()->setText( mBackupExpression );
291 if ( watched == mCombo && event->type() == QEvent::KeyPress )
293 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
294 if ( keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return )
300 return QObject::eventFilter( watched, event );
305 return mAllowEvalErrors;
310 if ( allowEvalErrors == mAllowEvalErrors )
322 QString fieldName =
currentField( &isExpression, &isValid );
325 QFontMetrics metrics( mCombo->lineEdit()->font() );
326 if ( metrics.width( fieldName ) > mCombo->lineEdit()->width() )
328 mCombo->setToolTip( fieldName );
332 mCombo->setToolTip( QString() );
344 stylesheet = QStringLiteral(
"QLineEdit { color: %1; } Qt::gray );" ).arg( QColor( Qt::gray ).name() );
349 if ( !expression.isEmpty() )
358 QFont font = mCombo->lineEdit()->font();
359 font.setItalic( isExpression );
360 mCombo->lineEdit()->setFont( font );
362 if ( isExpression && !isValid )
364 stylesheet = QStringLiteral(
"QLineEdit { color: %1; } Qt::gray );" ).arg( QColor( Qt::red ).name() );
367 mCombo->lineEdit()->setStyleSheet( stylesheet );
373 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()
Emitted whenever the fields available from this layer have been changed.
A generic dialog for building expression strings.