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 ) );
113 if ( !mExpressionContextGenerator || mExpressionContextGenerator == mLayer )
114 mExpressionContextGenerator = layer;
121 return mLineEdit->text();
122 else if ( mCodeEditor )
123 return mCodeEditor->text();
131 return QgsExpression::checkExpression(
expression(), &mExpressionContext, expressionError ? *expressionError : temp );
136 mExpressionContextGenerator = generator;
142 mLineEdit->setText( newExpression );
143 else if ( mCodeEditor )
144 mCodeEditor->setText( newExpression );
147 void QgsExpressionLineEdit::editExpression()
158 dlg.setWindowTitle( mExpressionDialogTitle );
167 void QgsExpressionLineEdit::expressionEdited()
172 void QgsExpressionLineEdit::expressionEdited(
const QString &
expression )
174 updateLineEditStyle( expression );
180 if ( event->type() == QEvent::EnabledChange )
186 void QgsExpressionLineEdit::updateLineEditStyle(
const QString &
expression )
194 palette.setColor( QPalette::Text, Qt::gray );
199 if ( !expression.isEmpty() )
201 isValid = isExpressionValid( expression );
205 palette.setColor( QPalette::Text, Qt::red );
209 palette.setColor( QPalette::Text, Qt::black );
212 mLineEdit->setPalette( palette );
215 bool QgsExpressionLineEdit::isExpressionValid(
const QString &expressionStr )
218 expression.prepare( &mExpressionContext );
219 return !expression.hasParserError();
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.
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.
A SQL editor based on QScintilla2.
The QgsExpressionLineEdit widget includes a line edit for entering expressions together with a button...
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.
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.