Skip to content

Commit c625487

Browse files
committedAug 30, 2016
Use widget representValue to preview default expression results
Sponsored by DB Fahrwegdienste GmbH
1 parent 3a14e77 commit c625487

File tree

3 files changed

+166
-71
lines changed

3 files changed

+166
-71
lines changed
 

‎src/app/qgsattributetypedialog.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl, int fieldIdx
6969
isFieldEditableCheckBox->setEnabled( false );
7070
}
7171

72+
connect( mExpressionWidget, SIGNAL( expressionChanged( QString ) ), this, SLOT( defaultExpressionChanged() ) );
73+
7274
QSettings settings;
7375
restoreGeometry( settings.value( "/Windows/QgsAttributeTypeDialog/geometry" ).toByteArray() );
7476

@@ -158,6 +160,9 @@ void QgsAttributeTypeDialog::setWidgetV2Type( const QString& type )
158160
QgsDebugMsg( "Oops, couldn't create editor widget config dialog..." );
159161
}
160162
}
163+
164+
//update default expression preview
165+
defaultExpressionChanged();
161166
}
162167

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

232237
setWidgetV2Type( editType );
233238
}
239+
240+
void QgsAttributeTypeDialog::defaultExpressionChanged()
241+
{
242+
QString expression = mExpressionWidget->expression();
243+
if ( expression.isEmpty() )
244+
{
245+
mDefaultPreviewLabel->setText( QString() );
246+
return;
247+
}
248+
249+
QgsExpressionContext context;
250+
context << QgsExpressionContextUtils::globalScope()
251+
<< QgsExpressionContextUtils::projectScope()
252+
<< QgsExpressionContextUtils::layerScope( mLayer );
253+
254+
if ( !mPreviewFeature.isValid() )
255+
{
256+
// get first feature
257+
QgsFeatureIterator it = mLayer->getFeatures( QgsFeatureRequest().setLimit( 1 ) );
258+
it.nextFeature( mPreviewFeature );
259+
}
260+
261+
context.setFeature( mPreviewFeature );
262+
263+
QgsExpression exp = QgsExpression( expression );
264+
exp.prepare( &context );
265+
266+
if ( exp.hasParserError() )
267+
{
268+
mDefaultPreviewLabel->setText( "<i>" + exp.parserErrorString() + "</i>" );
269+
return;
270+
}
271+
272+
QVariant val = exp.evaluate( &context );
273+
if ( exp.hasEvalError() )
274+
{
275+
mDefaultPreviewLabel->setText( "<i>" + exp.evalErrorString() + "</i>" );
276+
return;
277+
}
278+
279+
QString previewText = val.toString();
280+
281+
QgsEditorWidgetFactory *factory = QgsEditorWidgetRegistry::instance()->factory( editorWidgetV2Type() );
282+
if ( factory )
283+
{
284+
previewText = factory->representValue( mLayer, mFieldIdx, editorWidgetV2Config(), QVariant(), val );
285+
}
286+
287+
mDefaultPreviewLabel->setText( "<i>" + previewText + "</i>" );
288+
}

‎src/app/qgsattributetypedialog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "qgsvectorlayer.h"
2323
#include "qgseditorconfigwidget.h"
24+
#include "qgsfeature.h"
2425

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

142+
void defaultExpressionChanged();
143+
141144
private:
142145
QgsVectorLayer *mLayer;
143146
int mFieldIdx;
@@ -146,6 +149,8 @@ class APP_EXPORT QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttribut
146149

147150
//! Cached configuration dialog (lazy loaded)
148151
QMap< QString, QgsEditorConfigWidget* > mEditorConfigWidgets;
152+
153+
QgsFeature mPreviewFeature;
149154
};
150155

151156
#endif

‎src/ui/qgsattributetypeedit.ui

Lines changed: 106 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,63 @@
1414
<string>Edit Widget Properties</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout">
17+
<item row="5" column="1">
18+
<widget class="QStackedWidget" name="stackedWidget"/>
19+
</item>
20+
<item row="4" column="1">
21+
<widget class="QgsCollapsibleGroupBox" name="groupBox">
22+
<property name="title">
23+
<string>Contraints</string>
24+
</property>
25+
<layout class="QVBoxLayout" name="verticalLayout">
26+
<item>
27+
<widget class="QCheckBox" name="notNullCheckBox">
28+
<property name="text">
29+
<string>Not null</string>
30+
</property>
31+
</widget>
32+
</item>
33+
<item>
34+
<layout class="QHBoxLayout" name="horizontalLayout_2">
35+
<property name="topMargin">
36+
<number>0</number>
37+
</property>
38+
<item>
39+
<widget class="QLabel" name="label">
40+
<property name="text">
41+
<string>Constraint</string>
42+
</property>
43+
</widget>
44+
</item>
45+
<item>
46+
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true">
47+
<property name="focusPolicy">
48+
<enum>Qt::StrongFocus</enum>
49+
</property>
50+
</widget>
51+
</item>
52+
</layout>
53+
</item>
54+
<item>
55+
<layout class="QHBoxLayout" name="horizontalLayout">
56+
<property name="bottomMargin">
57+
<number>0</number>
58+
</property>
59+
<item>
60+
<widget class="QLabel" name="label_2">
61+
<property name="text">
62+
<string>Constraint description</string>
63+
</property>
64+
</widget>
65+
</item>
66+
<item>
67+
<widget class="QLineEdit" name="leConstraintExpressionDescription"/>
68+
</item>
69+
</layout>
70+
</item>
71+
</layout>
72+
</widget>
73+
</item>
1774
<item row="0" column="1">
1875
<widget class="QCheckBox" name="isFieldEditableCheckBox">
1976
<property name="text">
@@ -24,6 +81,43 @@
2481
</property>
2582
</widget>
2683
</item>
84+
<item row="2" column="1">
85+
<widget class="QGroupBox" name="groupBox_2">
86+
<property name="title">
87+
<string>Defaults</string>
88+
</property>
89+
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,1">
90+
<item row="0" column="0">
91+
<widget class="QLabel" name="label_3">
92+
<property name="text">
93+
<string>Default value</string>
94+
</property>
95+
</widget>
96+
</item>
97+
<item row="1" column="0">
98+
<widget class="QLabel" name="label_4">
99+
<property name="text">
100+
<string>Preview</string>
101+
</property>
102+
</widget>
103+
</item>
104+
<item row="0" column="1">
105+
<widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true">
106+
<property name="focusPolicy">
107+
<enum>Qt::StrongFocus</enum>
108+
</property>
109+
</widget>
110+
</item>
111+
<item row="1" column="1">
112+
<widget class="QLabel" name="mDefaultPreviewLabel">
113+
<property name="text">
114+
<string/>
115+
</property>
116+
</widget>
117+
</item>
118+
</layout>
119+
</widget>
120+
</item>
27121
<item row="1" column="1">
28122
<widget class="QCheckBox" name="labelOnTopCheckBox">
29123
<property name="text">
@@ -34,10 +128,10 @@
34128
</property>
35129
</widget>
36130
</item>
37-
<item row="0" column="0" rowspan="11">
131+
<item row="0" column="0" rowspan="7">
38132
<widget class="QListWidget" name="selectionListWidget"/>
39133
</item>
40-
<item row="10" column="1">
134+
<item row="6" column="1">
41135
<widget class="QDialogButtonBox" name="buttonBox">
42136
<property name="orientation">
43137
<enum>Qt::Horizontal</enum>
@@ -47,74 +141,6 @@
47141
</property>
48142
</widget>
49143
</item>
50-
<item row="6" column="1">
51-
<widget class="QStackedWidget" name="stackedWidget"/>
52-
</item>
53-
<item row="2" column="1">
54-
<layout class="QHBoxLayout" name="horizontalLayout_3">
55-
<item>
56-
<widget class="QLabel" name="label_3">
57-
<property name="text">
58-
<string>Default value</string>
59-
</property>
60-
</widget>
61-
</item>
62-
<item>
63-
<widget class="QgsExpressionLineEdit" name="mExpressionWidget" native="true">
64-
<property name="maximumSize">
65-
<size>
66-
<width>500</width>
67-
<height>16777215</height>
68-
</size>
69-
</property>
70-
<property name="focusPolicy">
71-
<enum>Qt::StrongFocus</enum>
72-
</property>
73-
</widget>
74-
</item>
75-
</layout>
76-
</item>
77-
<item row="3" column="1">
78-
<widget class="QCheckBox" name="notNullCheckBox">
79-
<property name="text">
80-
<string>Not null</string>
81-
</property>
82-
</widget>
83-
</item>
84-
<item row="4" column="1">
85-
<layout class="QHBoxLayout" name="horizontalLayout_2">
86-
<property name="topMargin">
87-
<number>0</number>
88-
</property>
89-
<item>
90-
<widget class="QLabel" name="label">
91-
<property name="text">
92-
<string>Constraint</string>
93-
</property>
94-
</widget>
95-
</item>
96-
<item>
97-
<widget class="QgsFieldExpressionWidget" name="constraintExpressionWidget" native="true"/>
98-
</item>
99-
</layout>
100-
</item>
101-
<item row="5" column="1">
102-
<layout class="QHBoxLayout" name="horizontalLayout">
103-
<property name="bottomMargin">
104-
<number>0</number>
105-
</property>
106-
<item>
107-
<widget class="QLabel" name="label_2">
108-
<property name="text">
109-
<string>Constraint description</string>
110-
</property>
111-
</widget>
112-
</item>
113-
<item>
114-
<widget class="QLineEdit" name="leConstraintExpressionDescription"/>
115-
</item>
116-
</layout>
117-
</item>
118144
</layout>
119145
</widget>
120146
<customwidgets>
@@ -124,6 +150,12 @@
124150
<header>qgsexpressionlineedit.h</header>
125151
<container>1</container>
126152
</customwidget>
153+
<customwidget>
154+
<class>QgsCollapsibleGroupBox</class>
155+
<extends>QGroupBox</extends>
156+
<header>qgscollapsiblegroupbox.h</header>
157+
<container>1</container>
158+
</customwidget>
127159
<customwidget>
128160
<class>QgsFieldExpressionWidget</class>
129161
<extends>QWidget</extends>
@@ -135,7 +167,10 @@
135167
<tabstop>selectionListWidget</tabstop>
136168
<tabstop>isFieldEditableCheckBox</tabstop>
137169
<tabstop>labelOnTopCheckBox</tabstop>
138-
<tabstop>buttonBox</tabstop>
170+
<tabstop>mExpressionWidget</tabstop>
171+
<tabstop>notNullCheckBox</tabstop>
172+
<tabstop>constraintExpressionWidget</tabstop>
173+
<tabstop>leConstraintExpressionDescription</tabstop>
139174
</tabstops>
140175
<resources/>
141176
<connections>

0 commit comments

Comments
 (0)
Please sign in to comment.