Skip to content
Permalink
Browse files

edit widgets: keep default values of new feature's attributes until t…

…hey are added

(fixes #12391 & #12386 & #13857)

(cherry picked from commit bfb8ab6)
  • Loading branch information
jef-n committed Mar 18, 2015
1 parent ae24b08 commit 4c3cf64f01af32511ef5fc56b0b13aa705c00dd6
@@ -64,6 +64,15 @@ class QgsEditorWidgetWrapper : QObject
*/
QgsField field();

/**
* Access the default value of the field.
*
* @return the default value of the field
*
* @see layer()
*/
QVariant defaultValue();

/**
* Will return a wrapper for a given widget
* @param widget The widget which was created by a wrapper
@@ -6,7 +6,7 @@ class QgsFieldValidator : QValidator
%End

public:
QgsFieldValidator( QObject *parent, const QgsField &field, QString dateFormat = "yyyy-MM-dd" );
QgsFieldValidator( QObject *parent, const QgsField &field, QString defaultValue, QString dateFormat = "yyyy-MM-dd" );
~QgsFieldValidator();

virtual State validate(QString & /In,Out/, int & /In,Out/) const = 0 /API=QString:2 - /;
@@ -15,6 +15,7 @@

#include "qgseditorwidgetwrapper.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsfield.h"

#include <QWidget>
@@ -35,6 +36,11 @@ QgsField QgsEditorWidgetWrapper::field()
return layer()->pendingFields()[mFieldIdx];
}

QVariant QgsEditorWidgetWrapper::defaultValue()
{
return layer()->dataProvider()->defaultValue( mFieldIdx );
}

QgsEditorWidgetWrapper* QgsEditorWidgetWrapper::fromWidget( QWidget* widget )
{
return qobject_cast<QgsEditorWidgetWrapper*>( widget->property( "EWV2Wrapper" ).value<QgsWidgetWrapper*>() );
@@ -81,6 +81,15 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
*/
QgsField field();

/**
* Access the default value of the field.
*
* @return the default value of the field
*
* @see layer()
*/
QVariant defaultValue();

/**
* Will return a wrapper for a given widget
* @param widget The widget which was created by a wrapper
@@ -59,6 +59,11 @@ QVariant QgsTextEditWrapper::value()
v == QSettings().value( "qgis/nullValue", "NULL" ).toString() )
return QVariant( field().type() );

if ( v == defaultValue().toString() )
{
return defaultValue();
}

QVariant res( v );
if ( field().convertCompatible( res ) )
return res;
@@ -99,16 +104,22 @@ void QgsTextEditWrapper::initWidget( QWidget* editor )

if ( mLineEdit )
{
mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field() ) );
mLineEdit->setValidator( new QgsFieldValidator( mLineEdit, field(), defaultValue().toString() ) );

QVariant defVal = defaultValue();
if ( defVal.isNull() )
{
defVal = QSettings().value( "qgis/nullValue", "NULL" );
}

QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
if ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date )
{
mLineEdit->setPlaceholderText( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
mLineEdit->setPlaceholderText( defVal.toString() );
}
else if ( fle )
{
fle->setNullValue( QSettings().value( "qgis/nullValue", "NULL" ).toString() );
fle->setNullValue( defVal.toString() );
}

connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
@@ -29,9 +29,10 @@
#include "qgslonglongvalidator.h"
#include "qgsfield.h"

QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, QString dateFormat )
QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, QString defaultValue, QString dateFormat )
: QValidator( parent )
, mField( field )
, mDefaultValue( defaultValue )
, mDateFormat( dateFormat )
{
switch ( mField.type() )
@@ -105,6 +106,9 @@ QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
return Acceptable;
}

if ( s == mDefaultValue )
return Acceptable;

// delegate to the child validator if any
if ( mValidator )
{
@@ -115,10 +119,10 @@ QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
{
// allow to enter the NULL representation, which might be
// longer than the actual field
if ( mNullValue.size() > 0 &&
s.size() > 0 &&
s.size() < mNullValue.size() &&
s == mNullValue.left( s.size() ) )
if ( mNullValue.size() > 0 && s.size() > 0 && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
return Intermediate;

if ( mDefaultValue.size() > 0 && s.size() > 0 && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
return Intermediate;

if ( s == mNullValue )
@@ -146,7 +150,7 @@ void QgsFieldValidator::fixup( QString &s ) const
{
mValidator->fixup( s );
}
else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() )
else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() && s != mDefaultValue )
{
// if the value is longer, this must be a partial NULL representation
s = mNullValue;
@@ -31,7 +31,7 @@ class GUI_EXPORT QgsFieldValidator : public QValidator
Q_OBJECT

public:
QgsFieldValidator( QObject *parent, const QgsField &field, QString dateFormat = "yyyy-MM-dd" );
QgsFieldValidator( QObject *parent, const QgsField &field, QString defaultValue, QString dateFormat = "yyyy-MM-dd" );
~QgsFieldValidator();

virtual State validate( QString &, int & ) const override;
@@ -46,6 +46,7 @@ class GUI_EXPORT QgsFieldValidator : public QValidator
QValidator *mValidator;
QgsField mField;
QString mNullValue;
QString mDefaultValue;
QString mDateFormat;
};

@@ -3407,10 +3407,10 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
{
res = conn->PQexec( "CREATE TABLE layer_styles("
"id SERIAL PRIMARY KEY"
",f_table_catalog varchar(256)"
",f_table_schema varchar(256)"
",f_table_name varchar(256)"
",f_geometry_column varchar(256)"
",f_table_catalog varchar"
",f_table_schema varchar"
",f_table_name varchar"
",f_geometry_column varchar"
",styleName varchar(30)"
",styleQML xml"
",styleSLD xml"

0 comments on commit 4c3cf64

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