17#include "moc_qgsexpressionlineedit.cpp"
35 , mExpressionDialogTitle( tr(
"Expression Builder" ) )
37 mButton =
new QToolButton();
38 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
40 connect( mButton, &QAbstractButton::clicked,
this, &QgsExpressionLineEdit::editExpression );
54 mExpressionDialogTitle = title;
61 if ( multiLine && !mCodeEditor )
64 mCodeEditor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
68 QHBoxLayout *newLayout =
new QHBoxLayout();
69 newLayout->setContentsMargins( 0, 0, 0, 0 );
70 newLayout->addWidget( mCodeEditor );
72 QVBoxLayout *vLayout =
new QVBoxLayout();
73 vLayout->addWidget( mButton );
74 vLayout->addStretch();
75 newLayout->addLayout( vLayout );
78 setLayout( newLayout );
80 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
82 setFocusProxy( mCodeEditor );
83 connect( mCodeEditor, &QsciScintilla::textChanged,
this,
static_cast < void (
QgsExpressionLineEdit::* )()
> ( &QgsExpressionLineEdit::expressionEdited ) );
87 else if ( !multiLine && !mLineEdit )
90 mCodeEditor =
nullptr;
92 mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
94 QHBoxLayout *newLayout =
new QHBoxLayout();
95 newLayout->setContentsMargins( 0, 0, 0, 0 );
96 newLayout->addWidget( mLineEdit );
97 newLayout->addWidget( mButton );
100 setLayout( newLayout );
102 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
104 setFocusProxy( mLineEdit );
105 connect( mLineEdit, &QLineEdit::textChanged,
this,
static_cast < void (
QgsExpressionLineEdit::* )(
const QString & )
> ( &QgsExpressionLineEdit::expressionEdited ) );
113 return mExpectedOutputFormat;
118 mExpectedOutputFormat = expected;
128 if ( !mExpressionContextGenerator || mExpressionContextGenerator == mLayer )
129 mExpressionContextGenerator = layer;
136 return mLineEdit->text();
137 else if ( mCodeEditor )
138 return mCodeEditor->text();
152 mExpressionContextGenerator = generator;
158 mLineEdit->setText( newExpression );
159 else if ( mCodeEditor )
160 mCodeEditor->setText( newExpression );
163void QgsExpressionLineEdit::editExpression()
165 const QString currentExpression =
expression();
170 dlg.setExpectedOutputFormat( mExpectedOutputFormat );
173 dlg.setGeomCalculator( *mDa );
175 dlg.setWindowTitle( mExpressionDialogTitle );
179 const QString newExpression = dlg.expressionText();
184void QgsExpressionLineEdit::expressionEdited()
189void QgsExpressionLineEdit::expressionEdited(
const QString &expression )
197 if ( event->type() == QEvent::EnabledChange )
203void QgsExpressionLineEdit::updateLineEditStyle(
const QString &expression )
208 QPalette appPalette = qApp->palette();
209 QPalette palette = mLineEdit->palette();
212 palette.setColor( QPalette::Text, appPalette.color( QPalette::Disabled, QPalette::Text ) );
223 palette.setColor( QPalette::Text, Qt::red );
227 palette.setColor( QPalette::Text, appPalette.color( QPalette::Text ) );
230 mLineEdit->setPalette( palette );
233bool QgsExpressionLineEdit::isExpressionValid(
const QString &expressionStr )
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...
The QgsExpressionLineEdit widget includes a line edit for entering expressions together with a button...
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.
Class for parsing and evaluation of expressions (formerly called "search strings").
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 data sets.