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 );
51 mExpressionDialogTitle = title;
58 if ( multiLine && !mCodeEditor )
61 mCodeEditor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
65 QHBoxLayout *newLayout =
new QHBoxLayout();
66 newLayout->setContentsMargins( 0, 0, 0, 0 );
67 newLayout->addWidget( mCodeEditor );
69 QVBoxLayout *vLayout =
new QVBoxLayout();
70 vLayout->addWidget( mButton );
71 vLayout->addStretch();
72 newLayout->addLayout( vLayout );
75 setLayout( newLayout );
77 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
79 setFocusProxy( mCodeEditor );
80 connect( mCodeEditor, &QsciScintilla::textChanged,
this, static_cast <
void (
QgsExpressionLineEdit::* )() > ( &QgsExpressionLineEdit::expressionEdited ) );
84 else if ( !multiLine && !mLineEdit )
87 mCodeEditor =
nullptr;
89 mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
91 QHBoxLayout *newLayout =
new QHBoxLayout();
92 newLayout->setContentsMargins( 0, 0, 0, 0 );
93 newLayout->addWidget( mLineEdit );
94 newLayout->addWidget( mButton );
97 setLayout( newLayout );
99 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
101 setFocusProxy( mLineEdit );
102 connect( mLineEdit, &QLineEdit::textChanged,
this, static_cast <
void (
QgsExpressionLineEdit::* )(
const QString & ) > ( &QgsExpressionLineEdit::expressionEdited ) );
110 return mExpectedOutputFormat;
115 mExpectedOutputFormat = expected;
125 if ( !mExpressionContextGenerator || mExpressionContextGenerator == mLayer )
126 mExpressionContextGenerator = layer;
133 return mLineEdit->text();
134 else if ( mCodeEditor )
135 return mCodeEditor->text();
148 mExpressionContextGenerator = generator;
154 mLineEdit->setText( newExpression );
155 else if ( mCodeEditor )
156 mCodeEditor->setText( newExpression );
159 void QgsExpressionLineEdit::editExpression()
171 dlg.setWindowTitle( mExpressionDialogTitle );
180 void QgsExpressionLineEdit::expressionEdited()
185 void QgsExpressionLineEdit::expressionEdited(
const QString &
expression )
187 updateLineEditStyle( expression );
193 if ( event->type() == QEvent::EnabledChange )
199 void QgsExpressionLineEdit::updateLineEditStyle(
const QString &
expression )
204 QPalette palette = mLineEdit->palette();
207 palette.setColor( QPalette::Text, Qt::gray );
212 if ( !expression.isEmpty() )
214 isValid = isExpressionValid( expression );
218 palette.setColor( QPalette::Text, Qt::red );
222 palette.setColor( QPalette::Text, Qt::black );
225 mLineEdit->setPalette( palette );
228 bool QgsExpressionLineEdit::isExpressionValid(
const QString &expressionStr )
231 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.
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.