17 #include <QHBoxLayout> 30 , mExpressionDialogTitle( tr(
"Expression Dialog" ) )
34 QHBoxLayout *layout =
new QHBoxLayout(
this );
35 layout->setContentsMargins( 0, 0, 0, 0 );
37 mCombo =
new QComboBox(
this );
38 mCombo->setEditable(
true );
39 mCombo->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
40 int width = mCombo->minimumSizeHint().width();
41 mCombo->setMinimumWidth( width );
45 mCombo->setModel( mFieldProxyModel );
47 mButton =
new QToolButton(
this );
48 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
51 layout->addWidget( mCombo );
52 layout->addWidget( mButton );
57 setFocusProxy( mCombo );
63 connect( mFieldProxyModel, &QAbstractItemModel::modelAboutToBeReset,
this, &QgsFieldExpressionWidget::beforeResetModel );
64 connect( mFieldProxyModel, &QAbstractItemModel::modelReset,
this, &QgsFieldExpressionWidget::afterResetModel );
75 mExpressionDialogTitle = title;
85 QHBoxLayout *layout =
dynamic_cast<QHBoxLayout *
>( this->layout() );
91 QLayoutItem *item = layout->takeAt( 1 );
92 layout->insertWidget( 0, item->widget() );
95 layout->addWidget( mCombo );
100 mDa = std::shared_ptr<const QgsDistanceArea>(
new QgsDistanceArea( da ) );
105 return mCombo->currentText();
121 return QgsExpression::checkExpression(
currentText(), &mExpressionContext, expressionError ? *expressionError : temp );
140 *isExpression = valueIsExpression;
152 mExpressionContextGenerator = generator;
175 if ( fieldName.isEmpty() )
182 if ( !idx.isValid() )
185 QString simpleFieldName = fieldName.trimmed();
186 if ( simpleFieldName.startsWith(
'"' ) && simpleFieldName.endsWith(
'"' ) )
188 simpleFieldName.remove( 0, 1 ).chop( 1 );
192 if ( !idx.isValid() )
199 QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
200 mCombo->setCurrentIndex( proxyIndex.row() );
221 dlg.setWindowTitle( mExpressionDialogTitle );
239 const QString
expression = mCombo->lineEdit()->text();
242 QModelIndex proxyIndex = mFieldProxyModel->mapFromSource( idx );
243 mCombo->setCurrentIndex( proxyIndex.row() );
249 if ( event->type() == QEvent::EnabledChange )
255 void QgsFieldExpressionWidget::reloadLayer()
260 void QgsFieldExpressionWidget::beforeResetModel()
263 mBackupExpression = mCombo->currentText();
266 void QgsFieldExpressionWidget::afterResetModel()
269 mCombo->lineEdit()->setText( mBackupExpression );
274 return mAllowEvalErrors;
279 if ( allowEvalErrors == mAllowEvalErrors )
291 QString fieldName =
currentField( &isExpression, &isValid );
294 QFontMetrics metrics( mCombo->lineEdit()->font() );
295 if ( metrics.width( fieldName ) > mCombo->lineEdit()->width() )
297 mCombo->setToolTip( fieldName );
301 mCombo->setToolTip( QLatin1String(
"" ) );
313 palette.setColor( QPalette::Text, Qt::gray );
318 if ( !expression.isEmpty() )
327 QFont font = mCombo->lineEdit()->font();
328 font.setItalic( isExpression );
329 mCombo->lineEdit()->setFont( font );
331 if ( isExpression && !isValid )
333 palette.setColor( QPalette::Text, Qt::red );
337 palette.setColor( QPalette::Text, Qt::black );
340 mCombo->lineEdit()->setPalette( palette );
346 expression.prepare( &mExpressionContext );
347 return !expression.hasParserError();
Base class for all map layer types.
QgsFieldModel * sourceFieldModel()
Returns the QgsFieldModel used in this QSortFilterProxyModel.
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.
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...
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.
Abstract interface for generating an expression context.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
bool isField(const QString &expression) const
Returns true if a string represents a field reference, or false if it is an expression consisting of ...
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.