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 6d28eb5 commit e35c6b4640f3099b9bddc30617ddd1863e4d2d0d
Showing with 124 additions and 51 deletions.
  1. +52 −0 src/app/qgsattributetypedialog.cpp
  2. +5 −0 src/app/qgsattributetypedialog.h
  3. +67 −51 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::setWidgetType( const QString& type )
QgsDebugMsg( "Oops, couldn't create editor widget config dialog..." );
}
}

//update default expression preview
defaultExpressionChanged();
}

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

setWidgetType( editType );
}

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

QgsExpressionContext context = mLayer->createExpressionContext();

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( editorWidgetType() );
if ( factory )
{
previewText = factory->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val );
}

mDefaultPreviewLabel->setText( "<i>" + previewText + "</i>" );
}
@@ -20,6 +20,7 @@
#include "ui_qgsattributetypeedit.h"

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

class QDialog;

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

void defaultExpressionChanged();

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

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

QgsFeature mPreviewFeature;
};

#endif
@@ -14,51 +14,10 @@
<string>Edit Widget Properties</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QCheckBox" name="isFieldEditableCheckBox">
<property name="text">
<string>Editable</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="labelOnTopCheckBox">
<property name="text">
<string>Label on top</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</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 row="5" column="1">
<widget class="QStackedWidget" name="stackedWidget"/>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QgsCollapsibleGroupBox" name="groupBox">
<property name="title">
<string>Contraints</string>
@@ -112,10 +71,67 @@
</layout>
</widget>
</item>
<item row="4" column="1">
<widget class="QStackedWidget" name="stackedWidget"/>
<item row="0" column="1">
<widget class="QCheckBox" name="isFieldEditableCheckBox">
<property name="text">
<string>Editable</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="5" column="1">
<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">
<string>Label on top</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="0" rowspan="7">
<widget class="QListWidget" name="selectionListWidget"/>
</item>
<item row="6" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -125,9 +141,6 @@
</property>
</widget>
</item>
<item row="0" column="0" rowspan="6">
<widget class="QListWidget" name="selectionListWidget"/>
</item>
</layout>
</widget>
<customwidgets>
@@ -154,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 e35c6b4

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