34 , mExpressionDialogTitle( tr(
"Expression Builder" ) )
35 , mDistanceArea( nullptr )
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 const 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 );
62 connect( mFieldProxyModel, &QAbstractItemModel::modelAboutToBeReset,
this, &QgsFieldExpressionWidget::beforeResetModel );
63 connect( mFieldProxyModel, &QAbstractItemModel::modelReset,
this, &QgsFieldExpressionWidget::afterResetModel );
69 mCombo->installEventFilter(
this );
74 mExpressionDialogTitle = title;
84 mCombo->lineEdit()->setClearButtonEnabled( allowEmpty );
95 QHBoxLayout *layout =
dynamic_cast<QHBoxLayout *
>( this->layout() );
101 QLayoutItem *item = layout->takeAt( 1 );
102 layout->insertWidget( 0, item->widget() );
105 layout->addWidget( mCombo );
110 mDistanceArea = std::shared_ptr<const QgsDistanceArea>(
new QgsDistanceArea( da ) );
115 return mCombo->currentText();
162 mExpressionContextGenerator = generator;
167 mCustomPreviewLabel = label;
168 mCustomChoices = choices;
169 mPreviewContextGenerator = previewContextGenerator;
192 if ( fieldName.isEmpty() )
200 if ( fieldName.size() > mCombo->lineEdit()->maxLength() )
202 mCombo->lineEdit()->setMaxLength( fieldName.size() );
206 if ( !idx.isValid() )
209 QString simpleFieldName = fieldName.trimmed();
210 if ( simpleFieldName.startsWith(
'"' ) && simpleFieldName.endsWith(
'"' ) )
212 simpleFieldName.remove( 0, 1 ).chop( 1 );
216 if ( !idx.isValid() )
223 const QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
224 mCombo->setCurrentIndex( proxyIndex.row() );
250 dlg.setWindowTitle( mExpressionDialogTitle );
253 if ( !mCustomChoices.isEmpty() )
276 const QString
expression = mCombo->lineEdit()->text();
279 const QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
280 mCombo->setCurrentIndex( proxyIndex.row() );
286 if ( event->type() == QEvent::EnabledChange )
292void QgsFieldExpressionWidget::reloadLayer()
297void QgsFieldExpressionWidget::beforeResetModel()
300 mBackupExpression = mCombo->currentText();
303void QgsFieldExpressionWidget::afterResetModel()
306 mCombo->lineEdit()->setText( mBackupExpression );
311 if ( watched == mCombo && event->type() == QEvent::KeyPress )
313 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
314 if ( keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return )
320 return QObject::eventFilter( watched, event );
325 return mAllowEvalErrors;
340 return mButton->isVisibleTo(
this );
348 mButton->setVisible( visible );
360 const QFontMetrics metrics( mCombo->lineEdit()->font() );
361 if ( metrics.boundingRect( fieldName ).width() > mCombo->lineEdit()->width() )
363 mCombo->setToolTip( fieldName );
367 mCombo->setToolTip( QString() );
379 stylesheet = QStringLiteral(
"QLineEdit { color: %1; }" ).arg( QColor( Qt::gray ).name() );
393 QFont font = mCombo->lineEdit()->font();
395 mCombo->lineEdit()->setFont( font );
399 stylesheet = QStringLiteral(
"QLineEdit { color: %1; }" ).arg( QColor( Qt::red ).name() );
402 mCombo->lineEdit()->setStyleSheet( stylesheet );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
A generic dialog for building expression strings.
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
void setAllowEvalErrors(bool allowEvalErrors)
Allow accepting expressions with evaluation errors.
Abstract interface for generating an expression context.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void loadFieldNames(const QgsFields &fields)
This allows loading fields without specifying a layer.
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
static bool checkExpression(const QString &text, const QgsExpressionContext *context, QString &errorMessage)
Tests whether a string is a valid expression.
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 setAllowExpression(bool allowExpression)
Sets whether custom expressions are accepted and displayed in the model.
void setLayer(QgsVectorLayer *layer)
Set the layer from which fields are displayed.
void setExpression(const QString &expression)
Sets a single expression to be added after the fields at the end of the model.
QgsFields fields() const
Returns the fields currently shown in the model.
void setFields(const QgsFields &fields)
Manually sets the fields to use for the model.
QModelIndex indexFromName(const QString &fieldName)
Returns the index corresponding to a given fieldName.
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.
QgsFieldModel * sourceFieldModel()
Returns the QgsFieldModel used in this QSortFilterProxyModel.
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
Container of fields for a vector layer.
Base class for all map layer types.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Represents a vector layer which manages a vector based data sets.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void updatedFields()
Emitted whenever the fields available from this layer have been changed.