Skip to content

Commit

Permalink
* initialize pasted feature with default values (fixes #8800)
Browse files Browse the repository at this point in the history
* show default values of new features in attribute table and identify
  results
  • Loading branch information
jef-n committed Jun 30, 2016
1 parent 6a93df7 commit 174d112
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -7379,16 +7379,26 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
QgsAttributes srcAttr = featureIt->attributes();
QgsAttributes dstAttr( dstAttrCount );

// pre-initialized with default values
for ( int dst = 0; dst < dstAttr.count(); ++dst )
{
QVariant defVal( pasteVectorLayer->dataProvider()->defaultValue( dst ) );
if( !defVal.isNull() )
{
dstAttr[ dst ] = defVal;
}
}

for ( int src = 0; src < srcAttr.count(); ++src )
{
int dst = remap.value( src, -1 );
if ( dst < 0 )
continue;

// use default value for primary key fields if it's NOT NULL
// don't overwrite default value for primary key fields if it's NOT NULL
// or for spatialite layers
if ( pkAttrList.contains( dst ) )
{
dstAttr[ dst ] = pasteVectorLayer->dataProvider()->defaultValue( dst );
if ( !dstAttr.at( dst ).isNull() )
continue;
else if ( pasteVectorLayer->providerType() == "spatialite" )
Expand Down
7 changes: 6 additions & 1 deletion src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -34,6 +34,7 @@
#include "qgsproject.h"
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgswebview.h"
#include "qgswebframe.h"

Expand Down Expand Up @@ -470,7 +471,11 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
if ( i >= fields.count() )
continue;

QString value = fields.at( i ).displayString( attrs.at( i ) );
QString defVal;
if ( fields.fieldOrigin( i ) == QgsFields::OriginProvider && vlayer->dataProvider() )
defVal = vlayer->dataProvider()->defaultValue( fields.fieldOriginIndex( i ) ).toString();

QString value = defVal == attrs.at( i ) ? defVal : fields.at( i ).displayString( attrs.at( i ) );
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << value );

attrItem->setData( 0, Qt::DisplayRole, vlayer->attributeDisplayName( i ) );
Expand Down
7 changes: 6 additions & 1 deletion src/gui/editorwidgets/core/qgseditorwidgetfactory.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgsdefaultsearchwidgetwrapper.h"
#include "qgssearchwidgetwrapper.h"
#include "qgsfield.h"
#include "qgsvectordataprovider.h"

#include <QSettings>

Expand Down Expand Up @@ -67,7 +68,11 @@ QString QgsEditorWidgetFactory::representValue( QgsVectorLayer* vl, int fieldIdx
Q_UNUSED( cache )
Q_UNUSED( value )

return vl->fields().at( fieldIdx ).displayString( value );
QString defVal;
if ( vl->fields().fieldOrigin( fieldIdx ) == QgsFields::OriginProvider && vl->dataProvider() )
defVal = vl->dataProvider()->defaultValue( vl->fields().fieldOriginIndex( fieldIdx ) ).toString();

return value == defVal ? defVal : vl->fields().at( fieldIdx ).displayString( value );
}

Qt::AlignmentFlag QgsEditorWidgetFactory::alignmentFlag( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config ) const
Expand Down

0 comments on commit 174d112

Please sign in to comment.