Skip to content
Permalink
Browse files

Use widget representValue to preview default expression results

Sponsored by DB Fahrwegdienste GmbH
  • Loading branch information
nyalldawson committed Aug 30, 2016
1 parent 3a14e77 commit c6254873558be4c7deeda1ad853fdec0ae357e5c
Showing with 166 additions and 71 deletions.
  1. +55 −0 src/app/qgsattributetypedialog.cpp
  2. +5 −0 src/app/qgsattributetypedialog.h
  3. +106 −71 src/ui/qgsattributetypeedit.ui
@@ -69,6 +69,8 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
isFieldEditableCheckBox->setEnabled( false );
}

connect( mExpressionWidget, SIGNAL( expressionChanged( QString ) ), this, SLOT( defaultExpressionChanged() ) );

QSettings settings;
restoreGeometry( settings.value( "/Windows/QgsAttributeTypeDialog/geometry" ).toByteArray() );

@@ -158,6 +160,9 @@ void QgsAttributeTypeDialog::setWidgetV2Type( const QString& type )
QgsDebugMsg( "Oops, couldn't create editor widget config dialog..." );
}
}

//update default expression preview
defaultExpressionChanged();
}

void QgsAttributeTypeDialog::setWidgetV2Config( const QgsEditorWidgetConfig& config )
@@ -231,3 +236,53 @@ void QgsAttributeTypeDialog::on_selectionListWidget_currentRowChanged( int index

setWidgetV2Type( editType );
}

void QgsAttributeTypeDialog::defaultExpressionChanged()
{
QString expression = mExpressionWidget->expression();
if ( expression.isEmpty() )
{
mDefaultPreviewLabel->setText( QString() );
return;
}

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );

if ( !mPreviewFeature.isValid() )
{
// get first feature
QgsFeatureIterator it = mLayer->getFeatures( QgsFeatureRequest().setLimit( 1 ) );
it.nextFeature( mPreviewFeature );
}

context.setFeature( mPreviewFeature );

QgsExpression exp = QgsExpression( expression );
exp.prepare( &context );

if ( exp.hasParserError() )
{
mDefaultPreviewLabel->setText( "<i>" + exp.parserErrorString() + "</i>" );
return;
}

QVariant val = exp.evaluate( &context );
if ( exp.hasEvalError() )
{
mDefaultPreviewLabel->setText( "<i>" + exp.evalErrorString() + "</i>" );
return;
}

QString previewText = val.toString();

QgsEditorWidgetFactory *factory = QgsEditorWidgetRegistry::instance()->factory( editorWidgetV2Type() );
if ( factory )
{
previewText = factory->representValue( mLayer, mFieldIdx, editorWidgetV2Config(), QVariant(), val );
}

mDefaultPreviewLabel->setText( "<i>" + previewText + "</i>" );
}
@@ -21,6 +21,7 @@

#include "qgsvectorlayer.h"
#include "qgseditorconfigwidget.h"
#include "qgsfeature.h"

class QDialog;
class QLayout;
@@ -138,6 +139,8 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
*/
void on_selectionListWidget_currentRowChanged( int index );

void defaultExpressionChanged();

private:
QgsVectorLayer *mLayer;
int mFieldIdx;
@@ -146,6 +149,8 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut

//! Cached configuration dialog (lazy loaded)
QMap< QString, QgsEditorConfigWidget* > mEditorConfigWidgets;

QgsFeature mPreviewFeature;
};

#endif
@@ -14,6 +14,63 @@
<string>Edit Widget Properties</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="5" column="1">
<widget class="QStackedWidget" name="stackedWidget"/>
</item>
<item row="4" column="1">
<widget class="QgsCollapsibleGroupBox" name="groupBox">
<property name="title">
<string>Contraints</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QCheckBox" name="notNullCheckBox">
<property name="text">
<string>Not null</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Constraint</string>
</property>
</widget>
</item>
<item>
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Constraint description</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="leConstraintExpressionDescription"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="isFieldEditableCheckBox">
<property name="text">
@@ -24,6 +81,43 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Defaults</string>
</property>
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,1">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Default value</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Preview</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="mDefaultPreviewLabel">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="labelOnTopCheckBox">
<property name="text">
@@ -34,10 +128,10 @@
</property>
</widget>
</item>
<item row="0" column="0" rowspan="11">
<item row="0" column="0" rowspan="7">
<widget class="QListWidget" name="selectionListWidget"/>
</item>
<item row="10" column="1">
<item row="6" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -47,74 +141,6 @@
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QStackedWidget" name="stackedWidget"/>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Default value</string>
</property>
</widget>
</item>
<item>
<widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true">
<property name="maximumSize">
<size>
<width>500</width>
<height>16777215</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="notNullCheckBox">
<property name="text">
<string>Not null</string>
</property>
</widget>
</item>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Constraint</string>
</property>
</widget>
</item>
<item>
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true"/>
</item>
</layout>
</item>
<item row="5" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Constraint description</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="leConstraintExpressionDescription"/>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
@@ -124,6 +150,12 @@
<header>qgsexpressionlineedit.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFieldExpressionWidget</class>
<extends>QWidget</extends>
@@ -135,7 +167,10 @@
<tabstop>selectionListWidget</tabstop>
<tabstop>isFieldEditableCheckBox</tabstop>
<tabstop>labelOnTopCheckBox</tabstop>
<tabstop>buttonBox</tabstop>
<tabstop>mExpressionWidget</tabstop>
<tabstop>notNullCheckBox</tabstop>
<tabstop>constraintExpressionWidget</tabstop>
<tabstop>leConstraintExpressionDescription</tabstop>
</tabstops>
<resources/>
<connections>

0 comments on commit c625487

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