Skip to content
Permalink
Browse files

[Feature] Allow custom expression functions in expression widget

  • Loading branch information
NathanW2 committed Nov 30, 2014
1 parent 98c0771 commit 4d4fa4460569c40bbcd936db7b38d67fcd0252e6
Showing with 56 additions and 9 deletions.
  1. +11 −3 src/gui/qgsexpressionbuilderwidget.cpp
  2. +45 −6 src/ui/qgsexpressionbuilder.ui
@@ -18,6 +18,7 @@
#include "qgsexpression.h"
#include "qgsmessageviewer.h"
#include "qgsapplication.h"
#include "qgspythonrunner.h"

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

txtExpressionString->setFoldingVisible( false );
customFunctionBotton->setVisible( QgsPythonRunner::isValid() );
txtPython->setVisible( false );
txtPython->setText("@qgsfunction(args=-1, group='Custom')\n"
"def func(values, feature, parent):\n"
" return str(values)");
}


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

QString QgsExpressionBuilderWidget::expressionText()
{
if ( QgsPythonRunner::isValid() ) {
QString pythontext = txtPython->text();
QgsPythonRunner::run( pythontext );
}
return txtExpressionString->text();
}

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

void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
{
QString text = txtExpressionString->text();
QString text = expressionText();

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



QgsExpression exp( text );

if ( mLayer )
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>418</width>
<height>447</height>
<width>538</width>
<height>535</height>
</rect>
</property>
<property name="sizePolicy">
@@ -453,7 +453,7 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="1" column="0">
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
@@ -469,7 +469,7 @@
</property>
</widget>
</item>
<item row="1" column="1">
<item row="3" column="1">
<widget class="QLabel" name="lblPreview">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
@@ -514,9 +514,25 @@
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<item row="2" column="0" colspan="2">
<widget class="QgsCodeEditorSQL" name="txtExpressionString" native="true"/>
</item>
<item row="1" column="0" colspan="2">
<widget class="QgsCodeEditorPython" name="txtPython" native="true"/>
</item>
<item row="0" column="0" colspan="2">
<widget class="QToolButton" name="customFunctionBotton">
<property name="text">
<string>Define custom function</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@@ -544,7 +560,30 @@
<header>qgscodeeditorsql.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCodeEditorPython</class>
<extends>QWidget</extends>
<header>qgscodeeditorpython.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
<connections>
<connection>
<sender>customFunctionBotton</sender>
<signal>toggled(bool)</signal>
<receiver>txtPython</receiver>
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>74</x>
<y>468</y>
</hint>
<hint type="destinationlabel">
<x>116</x>
<y>485</y>
</hint>
</hints>
</connection>
</connections>
</ui>

2 comments on commit 4d4fa44

@slarosa

This comment has been minimized.

Copy link
Member

@slarosa slarosa replied Dec 2, 2014

Thanks @NathanW2 , this is a nice new feature. I only noticed a messed effect on Filed calculator dialog although it looks great in Select by expression dialog.
schermata del 2014-12-02 18 17 26
Also, sometimes I see the collapsible group box expanded but without python editor.
schermata del 2014-12-02 18 30 56

@NathanW2

This comment has been minimized.

Copy link
Member Author

@NathanW2 NathanW2 replied Dec 2, 2014

Please sign in to comment.
You can’t perform that action at this time.