23#include "moc_qgsfieldexpressionwidget.cpp"
35 , mExpressionDialogTitle( tr(
"Expression Builder" ) )
36 , mDistanceArea( nullptr )
39 QHBoxLayout *layout =
new QHBoxLayout(
this );
40 layout->setContentsMargins( 0, 0, 0, 0 );
42 mCombo =
new QComboBox(
this );
43 mCombo->setEditable(
true );
44 mCombo->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
45 const int width = mCombo->minimumSizeHint().width();
46 mCombo->setMinimumWidth( width );
50 mCombo->setModel( mFieldProxyModel );
52 mButton =
new QToolButton(
this );
53 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
56 layout->addWidget( mCombo );
57 layout->addWidget( mButton );
63 connect( mFieldProxyModel, &QAbstractItemModel::modelAboutToBeReset,
this, &QgsFieldExpressionWidget::beforeResetModel );
64 connect( mFieldProxyModel, &QAbstractItemModel::modelReset,
this, &QgsFieldExpressionWidget::afterResetModel );
70 mCombo->installEventFilter(
this );
75 mExpressionDialogTitle = title;
85 mCombo->lineEdit()->setClearButtonEnabled( allowEmpty );
96 QHBoxLayout *layout =
dynamic_cast<QHBoxLayout *
>( this->layout() );
102 QLayoutItem *item = layout->takeAt( 1 );
103 layout->insertWidget( 0, item->widget() );
106 layout->addWidget( mCombo );
111 mDistanceArea = std::shared_ptr<const QgsDistanceArea>(
new QgsDistanceArea( da ) );
116 return mCombo->currentText();
163 mExpressionContextGenerator = generator;
168 mCustomPreviewLabel = label;
169 mCustomChoices = choices;
170 mPreviewContextGenerator = previewContextGenerator;
193 if ( fieldName.isEmpty() )
201 if ( fieldName.size() > mCombo->lineEdit()->maxLength() )
203 mCombo->lineEdit()->setMaxLength( fieldName.size() );
207 if ( !idx.isValid() )
210 QString simpleFieldName = fieldName.trimmed();
211 if ( simpleFieldName.startsWith(
'"' ) && simpleFieldName.endsWith(
'"' ) )
213 simpleFieldName.remove( 0, 1 ).chop( 1 );
217 if ( !idx.isValid() )
224 const QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
225 mCombo->setCurrentIndex( proxyIndex.row() );
251 dlg.setWindowTitle( mExpressionDialogTitle );
254 if ( !mCustomChoices.isEmpty() )
277 const QString
expression = mCombo->lineEdit()->text();
280 const QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
281 mCombo->setCurrentIndex( proxyIndex.row() );
287 if ( event->type() == QEvent::EnabledChange )
293void QgsFieldExpressionWidget::reloadLayer()
298void QgsFieldExpressionWidget::beforeResetModel()
301 mBackupExpression = mCombo->currentText();
304void QgsFieldExpressionWidget::afterResetModel()
307 mCombo->lineEdit()->setText( mBackupExpression );
312 if ( watched == mCombo && event->type() == QEvent::KeyPress )
314 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
315 if ( keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return )
321 return QObject::eventFilter( watched, event );
326 return mAllowEvalErrors;
341 return mButton->isVisibleTo(
this );
349 mButton->setVisible( visible );
361 const QFontMetrics metrics( mCombo->lineEdit()->font() );
362 if ( metrics.boundingRect( fieldName ).width() > mCombo->lineEdit()->width() )
364 mCombo->setToolTip( fieldName );
368 mCombo->setToolTip( QString() );
380 stylesheet = QStringLiteral(
"QLineEdit { color: %1; }" ).arg( QColor( Qt::gray ).name() );
394 QFont font = mCombo->lineEdit()->font();
396 font.setItalic(
false );
397 mCombo->lineEdit()->setFont( font );
401 stylesheet = QStringLiteral(
"QLineEdit { color: %1; }" ).arg( QColor( Qt::red ).name() );
404 mCombo->lineEdit()->setStyleSheet( stylesheet );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QFont getMonospaceFont()
Returns the monospaced font to use for code editors.
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.