35#include "moc_qgsexpressionlineedit.cpp"
37using namespace Qt::StringLiterals;
41 , mExpressionDialogTitle( tr(
"Expression Builder" ) )
43 mButton =
new QToolButton();
44 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
46 connect( mButton, &QAbstractButton::clicked,
this, &QgsExpressionLineEdit::editExpression );
60 mExpressionDialogTitle = title;
67 if ( multiLine && !mCodeEditor )
70 mCodeEditor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
74 QHBoxLayout *newLayout =
new QHBoxLayout();
75 newLayout->setContentsMargins( 0, 0, 0, 0 );
76 newLayout->addWidget( mCodeEditor );
78 QVBoxLayout *vLayout =
new QVBoxLayout();
79 vLayout->addWidget( mButton );
80 vLayout->addStretch();
81 newLayout->addLayout( vLayout );
84 setLayout( newLayout );
86 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
88 setFocusProxy( mCodeEditor );
89 connect( mCodeEditor, &QsciScintilla::textChanged,
this,
static_cast<void (
QgsExpressionLineEdit::* )()
>( &QgsExpressionLineEdit::expressionEdited ) );
93 else if ( !multiLine && !mLineEdit )
96 mCodeEditor =
nullptr;
98 mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
100 QHBoxLayout *newLayout =
new QHBoxLayout();
101 newLayout->setContentsMargins( 0, 0, 0, 0 );
102 newLayout->addWidget( mLineEdit );
103 newLayout->addWidget( mButton );
106 setLayout( newLayout );
108 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
110 setFocusProxy( mLineEdit );
111 connect( mLineEdit, &QLineEdit::textChanged,
this,
static_cast<void (
QgsExpressionLineEdit::* )(
const QString & )
>( &QgsExpressionLineEdit::expressionEdited ) );
119 return mExpectedOutputFormat;
124 mExpectedOutputFormat = expected;
129 mDa = std::make_unique<QgsDistanceArea>( da );
134 if ( !mExpressionContextGenerator || mExpressionContextGenerator == mLayer )
135 mExpressionContextGenerator = layer;
142 return mLineEdit->text();
143 else if ( mCodeEditor )
144 return mCodeEditor->text();
152 const QgsExpressionContext context = mExpressionContextGenerator ? mExpressionContextGenerator->createExpressionContext() : mExpressionContext;
158 mExpressionContextGenerator = generator;
164 mLineEdit->setText( newExpression );
165 else if ( mCodeEditor )
166 mCodeEditor->setText( newExpression );
169void QgsExpressionLineEdit::editExpression()
171 const QString currentExpression =
expression();
176 dlg.setExpectedOutputFormat( mExpectedOutputFormat );
179 dlg.setGeomCalculator( *mDa );
181 dlg.setWindowTitle( mExpressionDialogTitle );
185 const QString newExpression = dlg.expressionText();
190void QgsExpressionLineEdit::expressionEdited()
195void QgsExpressionLineEdit::expressionEdited(
const QString &expression )
203 if ( event->type() == QEvent::EnabledChange )
209void QgsExpressionLineEdit::updateLineEditStyle(
const QString &expression )
214 QPalette appPalette = qApp->palette();
215 QPalette palette = mLineEdit->palette();
218 palette.setColor( QPalette::Text, appPalette.color( QPalette::Disabled, QPalette::Text ) );
229 palette.setColor( QPalette::Text, Qt::red );
233 palette.setColor( QPalette::Text, appPalette.color( QPalette::Text ) );
236 mLineEdit->setPalette( palette );
239bool QgsExpressionLineEdit::isExpressionValid(
const QString &expressionStr )
243 const QgsExpressionContext context = mExpressionContextGenerator ? mExpressionContextGenerator->createExpressionContext() : mExpressionContext;
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A QGIS expression editor based on QScintilla2.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
A generic dialog for building expression strings.
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...
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...
QString expression() const
Returns the current expression shown in the widget.
void setMultiLine(bool multiLine)
Sets whether the widget should show a multiline text editor.
void changeEvent(QEvent *event) override
bool isValidExpression(QString *expressionError=nullptr) const
Determines if the current expression is valid.
~QgsExpressionLineEdit() override
void expressionChanged(const QString &expression)
Emitted when the expression is changed.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the expression builder dialog for the widget.
void setGeomCalculator(const QgsDistanceArea &distanceArea)
Set the geometry calculator used in the expression dialog.
void setExpressionDialogTitle(const QString &title)
Sets the title used in the expression builder dialog.
void setExpression(const QString &expression)
Sets the current expression to show in the widget.
QString expectedOutputFormat() const
Returns the expected format string, which is shown in the expression builder dialog for the widget.
QgsExpressionLineEdit(QWidget *parent=nullptr)
Constructor for QgsExpressionLineEdit.
void setLayer(QgsVectorLayer *layer)
Sets a layer associated with the widget.
static bool checkExpression(const QString &text, const QgsExpressionContext *context, QString &errorMessage)
Tests whether a string is a valid expression.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
static QgsProject * instance()
Returns the QgsProject singleton instance.
Represents a vector layer which manages a vector based dataset.