Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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
  • Loading branch information
m-kuhn committed Apr 4, 2016
1 parent 48943d4 commit d1c79d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -601,6 +601,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
cbxSnappingOptionsDocked->setChecked( mSettings->value( "/qgis/dockSnapping", false ).toBool() );
cbxAddPostgisDC->setChecked( mSettings->value( "/qgis/addPostgisDC", false ).toBool() );
cbxAddOracleDC->setChecked( mSettings->value( "/qgis/addOracleDC", false ).toBool() );
cbxEvaluateDefaultValues->setChecked( mSettings->value( "/qgis/evaluateDefaultValues", false ).toBool() );
cbxCompileExpressions->setChecked( mSettings->value( "/qgis/compileExpressions", true ).toBool() );
cbxCreateRasterLegendIcons->setChecked( mSettings->value( "/qgis/createRasterLegendIcons", false ).toBool() );
cbxAutoTransaction->setChecked( QgisApp::instance()->autoTransaction() );
Expand Down Expand Up @@ -1153,6 +1154,7 @@ void QgsOptions::saveOptions()
mSettings->setValue( "/qgis/addPostgisDC", cbxAddPostgisDC->isChecked() );
mSettings->setValue( "/qgis/addOracleDC", cbxAddOracleDC->isChecked() );
mSettings->setValue( "/qgis/compileExpressions", cbxCompileExpressions->isChecked() );
mSettings->setValue( "/qgis/evaluateDefaultValues", cbxEvaluateDefaultValues->isChecked() );
mSettings->setValue( "/qgis/defaultLegendGraphicResolution", mLegendGraphicResolutionSpinBox->value() );
bool createRasterLegendIcons = mSettings->value( "/qgis/createRasterLegendIcons", false ).toBool();
mSettings->setValue( "/qgis/createRasterLegendIcons", cbxCreateRasterLegendIcons->isChecked() );
Expand Down
17 changes: 16 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -25,6 +25,7 @@
#include <qgscoordinatereferencesystem.h>

#include <QMessageBox>
#include <QSettings>

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

QgsPostgresResult rmax( connectionRO()->PQexec( sql ) );

return convertValue( fld.type(), rmax.PQgetvalue( 0, 0 ) );
}
catch ( PGFieldNotFound )
Expand All @@ -1625,7 +1627,20 @@ bool QgsPostgresProvider::isValid()

QVariant QgsPostgresProvider::defaultValue( int fieldId )
{
return mDefaultValues.value( fieldId, QString::null );
QSettings settings;

QVariant defVal = mDefaultValues.value( fieldId, QString::null );

if ( settings.value( "/qgis/evaluateDefaultValues", false ).toBool() && !defVal.isNull() )
{
const QgsField& fld = field( fieldId );

QgsPostgresResult res( connectionRO()->PQexec( QString( "SELECT %1" ).arg( defVal.toString() ) ) );

return convertValue( fld.type(), res.PQgetvalue( 0, 0 ) );
}

return defVal;
}

QString QgsPostgresProvider::paramValue( const QString& fieldValue, const QString &defaultValue ) const
Expand Down
76 changes: 43 additions & 33 deletions src/ui/qgsoptionsbase.ui
Expand Up @@ -308,7 +308,7 @@
<item>
<widget class="QStackedWidget" name="mOptionsStackedWidget">
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<widget class="QWidget" name="mOptionsPageGeneral">
<layout class="QVBoxLayout" name="verticalLayout_3">
Expand Down Expand Up @@ -337,8 +337,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>723</width>
<height>670</height>
<width>720</width>
<height>718</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_28">
Expand Down Expand Up @@ -1010,8 +1010,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>663</width>
<height>862</height>
<width>658</width>
<height>877</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_22">
Expand Down Expand Up @@ -1394,8 +1394,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>720</height>
<width>604</width>
<height>788</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_27">
Expand Down Expand Up @@ -1651,6 +1651,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbxEvaluateDefaultValues">
<property name="toolTip">
<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>
</property>
<property name="text">
<string>Evaluate default values</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -1749,8 +1759,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>747</width>
<height>802</height>
<width>736</width>
<height>846</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_29">
Expand Down Expand Up @@ -2469,8 +2479,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>168</width>
<height>276</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_46">
Expand Down Expand Up @@ -2574,8 +2584,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>521</width>
<height>354</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_25">
Expand Down Expand Up @@ -2912,8 +2922,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>650</width>
<height>645</height>
<width>647</width>
<height>710</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_30">
Expand Down Expand Up @@ -3374,8 +3384,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>537</width>
<height>591</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_39">
Expand Down Expand Up @@ -3629,8 +3639,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>668</height>
<width>566</width>
<height>707</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_31">
Expand Down Expand Up @@ -4174,8 +4184,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>462</width>
<height>382</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
Expand Down Expand Up @@ -4313,8 +4323,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>647</height>
<width>560</width>
<height>681</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_15">
Expand Down Expand Up @@ -4559,8 +4569,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>578</height>
<width>303</width>
<height>236</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_32">
Expand Down Expand Up @@ -4668,8 +4678,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>601</width>
<height>740</height>
<width>552</width>
<height>791</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_33">
Expand Down Expand Up @@ -5147,18 +5157,18 @@
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsColorButtonV2</class>
<extends>QToolButton</extends>
<header>qgscolorbuttonv2.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsColorButtonV2</class>
<extends>QToolButton</extends>
<header>qgscolorbuttonv2.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsColorSchemeList</class>
<extends>QWidget</extends>
Expand Down

15 comments on commit d1c79d8

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-kuhn no test case? ;)

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not yet totally convinced that the API via QSettings is the best of all ways

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making it a postgres connection setting?

@3nids
Copy link
Member

@3nids 3nids commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be a field property rather than a QGIS global?

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making it a postgres connection setting?

Does that allow upgrading an existing layer?

Shouldn't it be a field property rather than a QGIS global?

That would allow maximum flexibility but I fear the configuration effort of this approach. Good ideas?

@3nids
Copy link
Member

@3nids 3nids commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the typical use case for this? Isn't this used only in specific case?

@3nids
Copy link
Member

@3nids 3nids commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't move it at the field level, I would make it a project setting rather than a global.
Project sounds like the perfect balance, no?

as for transactions ;)

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use cases are things like

  • allow creating linked entries from the add feature dialog
  • allow adding new entries from the relation reference widget

On the other hand, I'm not sure there are a lot of cases where this behavior is really problematic, but I'm sure there are some.

@3nids
Copy link
Member

@3nids 3nids commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we people want to stay strict with a serial (no cancelled input for instance)...?

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I have put the "really" there ;)

@3nids
Copy link
Member

@3nids 3nids commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you move transaction setting to the project, it will be quite coherent to have this next to it!

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on d1c79d8 Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or more finegrained

@3nids
Copy link
Member

@3nids 3nids commented on d1c79d8 Apr 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what's your decision?

@3nids
Copy link
Member

@3nids 3nids commented on d1c79d8 May 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-kuhn it might have been forgotten....although I'd still be in favor of having this in the project properties. Thoughts?

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on d1c79d8 May 11, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.