Skip to content

Commit 4d4fa44

Browse files
committed
[Feature] Allow custom expression functions in expression widget
1 parent 98c0771 commit 4d4fa44

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

src/gui/qgsexpressionbuilderwidget.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsexpression.h"
1919
#include "qgsmessageviewer.h"
2020
#include "qgsapplication.h"
21+
#include "qgspythonrunner.h"
2122

2223
#include <QSettings>
2324
#include <QMenu>
@@ -112,6 +113,11 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
112113
splitter_2->restoreState( settings.value( "/windows/QgsExpressionBuilderWidget/splitter2" ).toByteArray() );
113114

114115
txtExpressionString->setFoldingVisible( false );
116+
customFunctionBotton->setVisible( QgsPythonRunner::isValid() );
117+
txtPython->setVisible( false );
118+
txtPython->setText("@qgsfunction(args=-1, group='Custom')\n"
119+
"def func(values, feature, parent):\n"
120+
" return str(values)");
115121
}
116122

117123

@@ -292,6 +298,10 @@ void QgsExpressionBuilderWidget::setGeomCalculator( const QgsDistanceArea & da )
292298

293299
QString QgsExpressionBuilderWidget::expressionText()
294300
{
301+
if ( QgsPythonRunner::isValid() ) {
302+
QString pythontext = txtPython->text();
303+
QgsPythonRunner::run( pythontext );
304+
}
295305
return txtExpressionString->text();
296306
}
297307

@@ -302,7 +312,7 @@ void QgsExpressionBuilderWidget::setExpressionText( const QString& expression )
302312

303313
void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
304314
{
305-
QString text = txtExpressionString->text();
315+
QString text = expressionText();
306316

307317
// If the string is empty the expression will still "fail" although
308318
// we don't show the user an error as it will be confusing.
@@ -316,8 +326,6 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
316326
return;
317327
}
318328

319-
320-
321329
QgsExpression exp( text );
322330

323331
if ( mLayer )

src/ui/qgsexpressionbuilder.ui

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>418</width>
10-
<height>447</height>
9+
<width>538</width>
10+
<height>535</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -453,7 +453,7 @@
453453
<property name="bottomMargin">
454454
<number>0</number>
455455
</property>
456-
<item row="1" column="0">
456+
<item row="3" column="0">
457457
<widget class="QLabel" name="label_2">
458458
<property name="sizePolicy">
459459
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
@@ -469,7 +469,7 @@
469469
</property>
470470
</widget>
471471
</item>
472-
<item row="1" column="1">
472+
<item row="3" column="1">
473473
<widget class="QLabel" name="lblPreview">
474474
<property name="sizePolicy">
475475
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
@@ -514,9 +514,25 @@
514514
</property>
515515
</widget>
516516
</item>
517-
<item row="0" column="0" colspan="2">
517+
<item row="2" column="0" colspan="2">
518518
<widget class="QgsCodeEditorSQL" name="txtExpressionString" native="true"/>
519519
</item>
520+
<item row="1" column="0" colspan="2">
521+
<widget class="QgsCodeEditorPython" name="txtPython" native="true"/>
522+
</item>
523+
<item row="0" column="0" colspan="2">
524+
<widget class="QToolButton" name="customFunctionBotton">
525+
<property name="text">
526+
<string>Define custom function</string>
527+
</property>
528+
<property name="checkable">
529+
<bool>true</bool>
530+
</property>
531+
<property name="autoRaise">
532+
<bool>true</bool>
533+
</property>
534+
</widget>
535+
</item>
520536
</layout>
521537
</widget>
522538
</item>
@@ -544,7 +560,30 @@
544560
<header>qgscodeeditorsql.h</header>
545561
<container>1</container>
546562
</customwidget>
563+
<customwidget>
564+
<class>QgsCodeEditorPython</class>
565+
<extends>QWidget</extends>
566+
<header>qgscodeeditorpython.h</header>
567+
<container>1</container>
568+
</customwidget>
547569
</customwidgets>
548570
<resources/>
549-
<connections/>
571+
<connections>
572+
<connection>
573+
<sender>customFunctionBotton</sender>
574+
<signal>toggled(bool)</signal>
575+
<receiver>txtPython</receiver>
576+
<slot>setVisible(bool)</slot>
577+
<hints>
578+
<hint type="sourcelabel">
579+
<x>74</x>
580+
<y>468</y>
581+
</hint>
582+
<hint type="destinationlabel">
583+
<x>116</x>
584+
<y>485</y>
585+
</hint>
586+
</hints>
587+
</connection>
588+
</connections>
550589
</ui>

0 commit comments

Comments
 (0)