27 #include <QHBoxLayout> 28 #include <QVBoxLayout> 29 #include <QToolButton> 34 , mExpressionDialogTitle( tr(
"Expression Dialog" ) )
36 mButton =
new QToolButton();
37 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
39 connect( mButton, &QAbstractButton::clicked,
this, &QgsExpressionLineEdit::editExpression );
53 mExpressionDialogTitle = title;
60 if ( multiLine && !mCodeEditor )
63 mCodeEditor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
67 QHBoxLayout *newLayout =
new QHBoxLayout();
68 newLayout->setContentsMargins( 0, 0, 0, 0 );
69 newLayout->addWidget( mCodeEditor );
71 QVBoxLayout *vLayout =
new QVBoxLayout();
72 vLayout->addWidget( mButton );
73 vLayout->addStretch();
74 newLayout->addLayout( vLayout );
77 setLayout( newLayout );
79 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
81 setFocusProxy( mCodeEditor );
82 connect( mCodeEditor, &QsciScintilla::textChanged,
this, static_cast <
void (
QgsExpressionLineEdit::* )() > ( &QgsExpressionLineEdit::expressionEdited ) );
86 else if ( !multiLine && !mLineEdit )
89 mCodeEditor =
nullptr;
91 mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
93 QHBoxLayout *newLayout =
new QHBoxLayout();
94 newLayout->setContentsMargins( 0, 0, 0, 0 );
95 newLayout->addWidget( mLineEdit );
96 newLayout->addWidget( mButton );
99 setLayout( newLayout );
101 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
103 setFocusProxy( mLineEdit );
104 connect( mLineEdit, &QLineEdit::textChanged,
this, static_cast <
void (
QgsExpressionLineEdit::* )(
const QString & ) > ( &QgsExpressionLineEdit::expressionEdited ) );
112 return mExpectedOutputFormat;
117 mExpectedOutputFormat = expected;
127 if ( !mExpressionContextGenerator || mExpressionContextGenerator == mLayer )
128 mExpressionContextGenerator = layer;
135 return mLineEdit->text();
136 else if ( mCodeEditor )
137 return mCodeEditor->text();
150 mExpressionContextGenerator = generator;
156 mLineEdit->setText( newExpression );
157 else if ( mCodeEditor )
158 mCodeEditor->setText( newExpression );
161 void QgsExpressionLineEdit::editExpression()
173 dlg.setWindowTitle( mExpressionDialogTitle );
182 void QgsExpressionLineEdit::expressionEdited()
187 void QgsExpressionLineEdit::expressionEdited(
const QString &
expression )
189 updateLineEditStyle( expression );
195 if ( event->type() == QEvent::EnabledChange )
201 void QgsExpressionLineEdit::updateLineEditStyle(
const QString &
expression )
206 QPalette palette = mLineEdit->palette();
209 palette.setColor( QPalette::Text, Qt::gray );
214 if ( !expression.isEmpty() )
216 isValid = isExpressionValid( expression );
220 palette.setColor( QPalette::Text, Qt::red );
224 palette.setColor( QPalette::Text, Qt::black );
227 mLineEdit->setPalette( palette );
230 bool QgsExpressionLineEdit::isExpressionValid(
const QString &expressionStr )
233 expression.
prepare( &mExpressionContext );
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
~QgsExpressionLineEdit() override
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the dialog.
A QGIS expression editor based on QScintilla2.
void setMultiLine(bool multiLine)
Sets whether the widget should show a multiline text editor.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static bool checkExpression(const QString &text, const QgsExpressionContext *context, QString &errorMessage)
Tests whether a string is a valid expression.
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...
void setExpression(const QString &expression)
Sets the current expression to show in the widget.
QString expression() const
Returns the current expression shown in the widget.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void setGeomCalculator(const QgsDistanceArea &distanceArea)
Set the geometry calculator used in the expression dialog.
Abstract interface for generating an expression context.
void expressionChanged(const QString &expression)
Emitted when the expression is changed.
void changeEvent(QEvent *event) override
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
void setExpressionDialogTitle(const QString &title)
Sets the title used in the expression builder dialog.
void setLayer(QgsVectorLayer *layer)
Sets a layer associated with the widget.
The QgsExpressionLineEdit widget includes a line edit for entering expressions together with a button...
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
static QgsProject * instance()
Returns the QgsProject singleton instance.
QString expectedOutputFormat() const
Returns the expected format string, which is shown in the expression builder dialog for the widget...
void setExpectedOutputFormat(const QString &expected)
Set the expected format string, which is shown in the expression builder dialog for the widget...
bool isValidExpression(QString *expressionError=nullptr) const
Determines if the current expression is valid.
QgsExpressionLineEdit(QWidget *parent=nullptr)
Constructor for QgsExpressionLineEdit.
Represents a vector layer which manages a vector based data sets.
A generic dialog for building expression strings.