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();
151 mExpressionContextGenerator = generator;
157 mLineEdit->setText( newExpression );
158 else if ( mCodeEditor )
159 mCodeEditor->setText( newExpression );
162 void QgsExpressionLineEdit::editExpression()
164 const QString currentExpression =
expression();
169 dlg.setExpectedOutputFormat( mExpectedOutputFormat );
172 dlg.setGeomCalculator( *mDa );
174 dlg.setWindowTitle( mExpressionDialogTitle );
178 const QString newExpression = dlg.expressionText();
183 void QgsExpressionLineEdit::expressionEdited()
188 void QgsExpressionLineEdit::expressionEdited(
const QString &expression )
196 if ( event->type() == QEvent::EnabledChange )
202 void QgsExpressionLineEdit::updateLineEditStyle(
const QString &expression )
207 QPalette palette = mLineEdit->palette();
210 palette.setColor( QPalette::Text, Qt::gray );
221 palette.setColor( QPalette::Text, Qt::red );
225 palette.setColor( QPalette::Text, Qt::black );
228 mLineEdit->setPalette( palette );
231 bool 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.