Skip to content

Commit d1c79d8

Browse files
committedApr 4, 2016
[feature] allow generating default values on postgres just in time
The expression used to generate default values for a field on the postgres database are currently being printed on the feature form. nextval('serial') A new option in the properties allows evaluating the expression before usage and will directly print the new value in the feature form. 23
1 parent 48943d4 commit d1c79d8

File tree

3 files changed

+61
-34
lines changed

3 files changed

+61
-34
lines changed
 

‎src/app/qgsoptions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
601601
cbxSnappingOptionsDocked->setChecked( mSettings->value( "/qgis/dockSnapping", false ).toBool() );
602602
cbxAddPostgisDC->setChecked( mSettings->value( "/qgis/addPostgisDC", false ).toBool() );
603603
cbxAddOracleDC->setChecked( mSettings->value( "/qgis/addOracleDC", false ).toBool() );
604+
cbxEvaluateDefaultValues->setChecked( mSettings->value( "/qgis/evaluateDefaultValues", false ).toBool() );
604605
cbxCompileExpressions->setChecked( mSettings->value( "/qgis/compileExpressions", true ).toBool() );
605606
cbxCreateRasterLegendIcons->setChecked( mSettings->value( "/qgis/createRasterLegendIcons", false ).toBool() );
606607
cbxAutoTransaction->setChecked( QgisApp::instance()->autoTransaction() );
@@ -1153,6 +1154,7 @@ void QgsOptions::saveOptions()
11531154
mSettings->setValue( "/qgis/addPostgisDC", cbxAddPostgisDC->isChecked() );
11541155
mSettings->setValue( "/qgis/addOracleDC", cbxAddOracleDC->isChecked() );
11551156
mSettings->setValue( "/qgis/compileExpressions", cbxCompileExpressions->isChecked() );
1157+
mSettings->setValue( "/qgis/evaluateDefaultValues", cbxEvaluateDefaultValues->isChecked() );
11561158
mSettings->setValue( "/qgis/defaultLegendGraphicResolution", mLegendGraphicResolutionSpinBox->value() );
11571159
bool createRasterLegendIcons = mSettings->value( "/qgis/createRasterLegendIcons", false ).toBool();
11581160
mSettings->setValue( "/qgis/createRasterLegendIcons", cbxCreateRasterLegendIcons->isChecked() );

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <qgscoordinatereferencesystem.h>
2626

2727
#include <QMessageBox>
28+
#include <QSettings>
2829

2930
#include "qgsvectorlayerimport.h"
3031
#include "qgsprovidercountcalcevent.h"
@@ -1609,6 +1610,7 @@ QVariant QgsPostgresProvider::maximumValue( int index )
16091610
sql = QString( "SELECT %1 FROM (%2) foo" ).arg( connectionRO()->fieldExpression( fld ), sql );
16101611

16111612
QgsPostgresResult rmax( connectionRO()->PQexec( sql ) );
1613+
16121614
return convertValue( fld.type(), rmax.PQgetvalue( 0, 0 ) );
16131615
}
16141616
catch ( PGFieldNotFound )
@@ -1625,7 +1627,20 @@ bool QgsPostgresProvider::isValid()
16251627

16261628
QVariant QgsPostgresProvider::defaultValue( int fieldId )
16271629
{
1628-
return mDefaultValues.value( fieldId, QString::null );
1630+
QSettings settings;
1631+
1632+
QVariant defVal = mDefaultValues.value( fieldId, QString::null );
1633+
1634+
if ( settings.value( "/qgis/evaluateDefaultValues", false ).toBool() && !defVal.isNull() )
1635+
{
1636+
const QgsField& fld = field( fieldId );
1637+
1638+
QgsPostgresResult res( connectionRO()->PQexec( QString( "SELECT %1" ).arg( defVal.toString() ) ) );
1639+
1640+
return convertValue( fld.type(), res.PQgetvalue( 0, 0 ) );
1641+
}
1642+
1643+
return defVal;
16291644
}
16301645

16311646
QString QgsPostgresProvider::paramValue( const QString& fieldValue, const QString &defaultValue ) const

‎src/ui/qgsoptionsbase.ui

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@
308308
<item>
309309
<widget class="QStackedWidget" name="mOptionsStackedWidget">
310310
<property name="currentIndex">
311-
<number>0</number>
311+
<number>2</number>
312312
</property>
313313
<widget class="QWidget" name="mOptionsPageGeneral">
314314
<layout class="QVBoxLayout" name="verticalLayout_3">
@@ -337,8 +337,8 @@
337337
<rect>
338338
<x>0</x>
339339
<y>0</y>
340-
<width>723</width>
341-
<height>670</height>
340+
<width>720</width>
341+
<height>718</height>
342342
</rect>
343343
</property>
344344
<layout class="QVBoxLayout" name="verticalLayout_28">
@@ -1010,8 +1010,8 @@
10101010
<rect>
10111011
<x>0</x>
10121012
<y>0</y>
1013-
<width>663</width>
1014-
<height>862</height>
1013+
<width>658</width>
1014+
<height>877</height>
10151015
</rect>
10161016
</property>
10171017
<layout class="QVBoxLayout" name="verticalLayout_22">
@@ -1394,8 +1394,8 @@
13941394
<rect>
13951395
<x>0</x>
13961396
<y>0</y>
1397-
<width>601</width>
1398-
<height>720</height>
1397+
<width>604</width>
1398+
<height>788</height>
13991399
</rect>
14001400
</property>
14011401
<layout class="QVBoxLayout" name="verticalLayout_27">
@@ -1651,6 +1651,16 @@
16511651
</property>
16521652
</widget>
16531653
</item>
1654+
<item>
1655+
<widget class="QCheckBox" name="cbxEvaluateDefaultValues">
1656+
<property name="toolTip">
1657+
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;When digitizing a new feature, default values are retrieved from the database. With this option turned on, the default values will be evaluated at the time of digitizing. With this option turned off, the default values will be evaluated at the time of saving.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
1658+
</property>
1659+
<property name="text">
1660+
<string>Evaluate default values</string>
1661+
</property>
1662+
</widget>
1663+
</item>
16541664
</layout>
16551665
</widget>
16561666
</item>
@@ -1749,8 +1759,8 @@
17491759
<rect>
17501760
<x>0</x>
17511761
<y>0</y>
1752-
<width>747</width>
1753-
<height>802</height>
1762+
<width>736</width>
1763+
<height>846</height>
17541764
</rect>
17551765
</property>
17561766
<layout class="QVBoxLayout" name="verticalLayout_29">
@@ -2469,8 +2479,8 @@
24692479
<rect>
24702480
<x>0</x>
24712481
<y>0</y>
2472-
<width>617</width>
2473-
<height>578</height>
2482+
<width>168</width>
2483+
<height>276</height>
24742484
</rect>
24752485
</property>
24762486
<layout class="QHBoxLayout" name="horizontalLayout_46">
@@ -2574,8 +2584,8 @@
25742584
<rect>
25752585
<x>0</x>
25762586
<y>0</y>
2577-
<width>617</width>
2578-
<height>578</height>
2587+
<width>521</width>
2588+
<height>354</height>
25792589
</rect>
25802590
</property>
25812591
<layout class="QVBoxLayout" name="verticalLayout_25">
@@ -2912,8 +2922,8 @@
29122922
<rect>
29132923
<x>0</x>
29142924
<y>0</y>
2915-
<width>650</width>
2916-
<height>645</height>
2925+
<width>647</width>
2926+
<height>710</height>
29172927
</rect>
29182928
</property>
29192929
<layout class="QVBoxLayout" name="verticalLayout_30">
@@ -3374,8 +3384,8 @@
33743384
<rect>
33753385
<x>0</x>
33763386
<y>0</y>
3377-
<width>617</width>
3378-
<height>578</height>
3387+
<width>537</width>
3388+
<height>591</height>
33793389
</rect>
33803390
</property>
33813391
<layout class="QVBoxLayout" name="verticalLayout_39">
@@ -3629,8 +3639,8 @@
36293639
<rect>
36303640
<x>0</x>
36313641
<y>0</y>
3632-
<width>601</width>
3633-
<height>668</height>
3642+
<width>566</width>
3643+
<height>707</height>
36343644
</rect>
36353645
</property>
36363646
<layout class="QVBoxLayout" name="verticalLayout_31">
@@ -4174,8 +4184,8 @@
41744184
<rect>
41754185
<x>0</x>
41764186
<y>0</y>
4177-
<width>617</width>
4178-
<height>578</height>
4187+
<width>462</width>
4188+
<height>382</height>
41794189
</rect>
41804190
</property>
41814191
<layout class="QVBoxLayout" name="verticalLayout_6">
@@ -4313,8 +4323,8 @@
43134323
<rect>
43144324
<x>0</x>
43154325
<y>0</y>
4316-
<width>601</width>
4317-
<height>647</height>
4326+
<width>560</width>
4327+
<height>681</height>
43184328
</rect>
43194329
</property>
43204330
<layout class="QGridLayout" name="gridLayout_15">
@@ -4559,8 +4569,8 @@
45594569
<rect>
45604570
<x>0</x>
45614571
<y>0</y>
4562-
<width>617</width>
4563-
<height>578</height>
4572+
<width>303</width>
4573+
<height>236</height>
45644574
</rect>
45654575
</property>
45664576
<layout class="QVBoxLayout" name="verticalLayout_32">
@@ -4668,8 +4678,8 @@
46684678
<rect>
46694679
<x>0</x>
46704680
<y>0</y>
4671-
<width>601</width>
4672-
<height>740</height>
4681+
<width>552</width>
4682+
<height>791</height>
46734683
</rect>
46744684
</property>
46754685
<layout class="QVBoxLayout" name="verticalLayout_33">
@@ -5147,18 +5157,18 @@
51475157
</widget>
51485158
<layoutdefault spacing="6" margin="11"/>
51495159
<customwidgets>
5150-
<customwidget>
5151-
<class>QgsColorButtonV2</class>
5152-
<extends>QToolButton</extends>
5153-
<header>qgscolorbuttonv2.h</header>
5154-
<container>1</container>
5155-
</customwidget>
51565160
<customwidget>
51575161
<class>QgsCollapsibleGroupBox</class>
51585162
<extends>QGroupBox</extends>
51595163
<header>qgscollapsiblegroupbox.h</header>
51605164
<container>1</container>
51615165
</customwidget>
5166+
<customwidget>
5167+
<class>QgsColorButtonV2</class>
5168+
<extends>QToolButton</extends>
5169+
<header>qgscolorbuttonv2.h</header>
5170+
<container>1</container>
5171+
</customwidget>
51625172
<customwidget>
51635173
<class>QgsColorSchemeList</class>
51645174
<extends>QWidget</extends>

0 commit comments

Comments
 (0)