QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
17 #include <QHBoxLayout>
34 , mExpressionDialogTitle( tr(
"Expression Dialog" ) )
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 );
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 mDistanceArea = std::shared_ptr<const QgsDistanceArea>(
new QgsDistanceArea( da ) );
120 return mCombo->currentText();
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 const QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
217 mCombo->setCurrentIndex( proxyIndex.row() );
243 dlg.setWindowTitle( mExpressionDialogTitle );
264 const QString
expression = mCombo->lineEdit()->text();
267 const QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
268 mCombo->setCurrentIndex( proxyIndex.row() );
274 if ( event->type() == QEvent::EnabledChange )
280 void QgsFieldExpressionWidget::reloadLayer()
285 void QgsFieldExpressionWidget::beforeResetModel()
288 mBackupExpression = mCombo->currentText();
291 void QgsFieldExpressionWidget::afterResetModel()
294 mCombo->lineEdit()->setText( mBackupExpression );
299 if ( watched == mCombo && event->type() == QEvent::KeyPress )
301 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
302 if ( keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return )
308 return QObject::eventFilter( watched, event );
313 return mAllowEvalErrors;
333 const QFontMetrics metrics( mCombo->lineEdit()->font() );
334 if ( metrics.boundingRect( fieldName ).width() > mCombo->lineEdit()->width() )
336 mCombo->setToolTip( fieldName );
340 mCombo->setToolTip( QString() );
352 stylesheet = QStringLiteral(
"QLineEdit { color: %1; }" ).arg( QColor( Qt::gray ).name() );
366 QFont font = mCombo->lineEdit()->font();
368 mCombo->lineEdit()->setFont( font );
372 stylesheet = QStringLiteral(
"QLineEdit { color: %1; }" ).arg( QColor( Qt::red ).name() );
375 mCombo->lineEdit()->setStyleSheet( stylesheet );
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setAllowEmptyFieldName(bool allowEmpty)
Sets whether an optional empty field ("not set") option is present in the model.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Container of fields for a vector layer.
void setLayer(QgsVectorLayer *layer)
Set the layer from which fields are displayed.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void setFields(const QgsFields &fields)
Manually sets the fields to use for the model.
QgsFieldProxyModel * setFilters(QgsFieldProxyModel::Filters filters)
Set flags that affect how fields are filtered in the model.
static bool checkExpression(const QString &text, const QgsExpressionContext *context, QString &errorMessage)
Tests whether a string is a valid expression.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
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.
The QgsFieldProxyModel class provides an easy to use model to display the list of fields of a layer.
A generic dialog for building expression strings.
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setAllowEvalErrors(bool allowEvalErrors)
Allow accepting expressions with evaluation errors.
Single scope for storing variables and functions for use within a QgsExpressionContext....
QgsFieldModel * sourceFieldModel()
Returns the QgsFieldModel used in this QSortFilterProxyModel.
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Represents a vector layer which manages a vector based data sets.
Base class for all map layer types. This is the base class for all map layer types (vector,...
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsFields fields() const
Returns the fields currently shown in the model.
void updatedFields()
Emitted whenever the fields available from this layer have been changed.
QModelIndex indexFromName(const QString &fieldName)
Returns the index corresponding to a given fieldName.
void setExpression(const QString &expression)
Sets a single expression to be added after the fields at the end of the model.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Class for parsing and evaluation of expressions (formerly called "search strings")....
Abstract interface for generating an expression context.
void loadFieldNames(const QgsFields &fields)
This allows loading fields without specifying a layer.