25 #include <QHBoxLayout> 26 #include <QVBoxLayout> 27 #include <QToolButton> 32 , mExpressionDialogTitle( tr(
"Expression Dialog" ) )
34 mButton =
new QToolButton();
35 mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
37 connect( mButton, &QAbstractButton::clicked,
this, &QgsExpressionLineEdit::editExpression );
49 mExpressionDialogTitle = title;
56 if ( multiLine && !mCodeEditor )
59 mCodeEditor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
63 QHBoxLayout *newLayout =
new QHBoxLayout();
64 newLayout->setContentsMargins( 0, 0, 0, 0 );
65 newLayout->addWidget( mCodeEditor );
67 QVBoxLayout *vLayout =
new QVBoxLayout();
68 vLayout->addWidget( mButton );
69 vLayout->addStretch();
70 newLayout->addLayout( vLayout );
73 setLayout( newLayout );
75 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
77 setFocusProxy( mCodeEditor );
78 connect( mCodeEditor, &QsciScintilla::textChanged,
this, static_cast <
void (
QgsExpressionLineEdit::* )() > ( &QgsExpressionLineEdit::expressionEdited ) );
82 else if ( !multiLine && !mLineEdit )
85 mCodeEditor =
nullptr;
87 mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
89 QHBoxLayout *newLayout =
new QHBoxLayout();
90 newLayout->setContentsMargins( 0, 0, 0, 0 );
91 newLayout->addWidget( mLineEdit );
92 newLayout->addWidget( mButton );
95 setLayout( newLayout );
97 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
99 setFocusProxy( mLineEdit );
100 connect( mLineEdit, &QLineEdit::textChanged,
this, static_cast <
void (
QgsExpressionLineEdit::* )(
const QString & ) > ( &QgsExpressionLineEdit::expressionEdited ) );
108 return mExpectedOutputFormat;
113 mExpectedOutputFormat = expected;
123 if ( !mExpressionContextGenerator || mExpressionContextGenerator == mLayer )
124 mExpressionContextGenerator = layer;
131 return mLineEdit->text();
132 else if ( mCodeEditor )
133 return mCodeEditor->text();
146 mExpressionContextGenerator = generator;
152 mLineEdit->setText( newExpression );
153 else if ( mCodeEditor )
154 mCodeEditor->setText( newExpression );
157 void QgsExpressionLineEdit::editExpression()
169 dlg.setWindowTitle( mExpressionDialogTitle );
178 void QgsExpressionLineEdit::expressionEdited()
183 void QgsExpressionLineEdit::expressionEdited(
const QString &
expression )
185 updateLineEditStyle( expression );
191 if ( event->type() == QEvent::EnabledChange )
197 void QgsExpressionLineEdit::updateLineEditStyle(
const QString &
expression )
202 QPalette palette = mLineEdit->palette();
205 palette.setColor( QPalette::Text, Qt::gray );
210 if ( !expression.isEmpty() )
212 isValid = isExpressionValid( expression );
216 palette.setColor( QPalette::Text, Qt::red );
220 palette.setColor( QPalette::Text, Qt::black );
223 mLineEdit->setPalette( palette );
226 bool QgsExpressionLineEdit::isExpressionValid(
const QString &expressionStr )
229 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.
QString expectedOutputFormat() const
Returns the expected format string, which is shown in the expression builder dialog for the widget...
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.
QString expression() const
Returns the current expression shown in the widget.
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.
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.
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
Returns true 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.