Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[API] make primary key field indexes available:
- add QgsVectorLayer::pendingPkAttributeList()
- add QgsVectorDataProvider::pkAttributeIndexes()
- skip primary key attribute when pasting features (fixes #6164)
- default to skip primary key attribute in when merging features/attributes
- reset primary key attributes to default value when splitting features (better fix for #6060)
- comment out unused QgsPasteTransformations
  • Loading branch information
jef-n committed Jan 6, 2013
1 parent 12d1a28 commit 2a002c5
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/app/CMakeLists.txt
Expand Up @@ -91,7 +91,7 @@ SET(QGIS_APP_SRCS
qgsmeasuretool.cpp
qgsmergeattributesdialog.cpp
qgsoptions.cpp
qgspastetransformations.cpp
#qgspastetransformations.cpp
qgspointrotationitem.cpp
qgspluginitem.cpp
qgspluginmanager.cpp
Expand Down Expand Up @@ -244,7 +244,7 @@ SET (QGIS_APP_MOC_HDRS
qgsmeasuretool.h
qgsmergeattributesdialog.h
qgsoptions.h
qgspastetransformations.h
#qgspastetransformations.h
qgspluginmanager.h
qgsprojectlayergroupdialog.h
qgsprojectproperties.h
Expand Down
9 changes: 6 additions & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -147,7 +147,7 @@
#include "qgsmultibandcolorrenderer.h"
#include "qgsnewvectorlayerdialog.h"
#include "qgsoptions.h"
#include "qgspastetransformations.h"
// #include "qgspastetransformations.h"
#include "qgspluginitem.h"
#include "qgspluginlayer.h"
#include "qgspluginlayerregistry.h"
Expand Down Expand Up @@ -4964,11 +4964,13 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )

QHash<int, int> remap;
const QgsFieldMap &fields = clipboard()->fields();
QgsAttributeList pkAttrList = pasteVectorLayer->pendingPkAttributesList();
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); it++ )
{
int dst = pasteVectorLayer->fieldNameIndex( it->name() );
if ( dst < 0 )
if ( dst < 0 || pkAttrList.contains( dst ) )
{
// skip primary key attributes
continue;
}
remap.insert( it.key(), dst );
Expand Down Expand Up @@ -5064,6 +5066,7 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
}
}

#if 0
void QgisApp::pasteTransformations()
{
QgsPasteTransformations *pt = new QgsPasteTransformations();
Expand All @@ -5072,7 +5075,7 @@ void QgisApp::pasteTransformations()

pt->exec();
}

#endif

void QgisApp::refreshMapCanvas()
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.h
Expand Up @@ -1048,7 +1048,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
//! Read Well Known Binary stream from PostGIS
//void readWKB(const char *, QStringList tables);
//! shows the paste-transformations dialog
void pasteTransformations();
// void pasteTransformations();
//! check to see if file is dirty and if so, prompt the user th save it
bool saveDirty();
/** Helper function to union several geometries together (used in function mergeSelectedFeatures)
Expand Down
16 changes: 11 additions & 5 deletions src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -84,6 +84,7 @@ void QgsMergeAttributesDialog::createTableWidgetContents()

//create combo boxes and insert attribute names
const QgsFieldMap& fieldMap = mVectorLayer->pendingFields();
QgsAttributeList pkAttrList = mVectorLayer->pendingPkAttributesList();

int col = 0;
for ( QgsFieldMap::const_iterator fieldIt = fieldMap.constBegin();
Expand All @@ -96,7 +97,12 @@ void QgsMergeAttributesDialog::createTableWidgetContents()

mTableWidget->setColumnCount( col + 1 );

mTableWidget->setCellWidget( 0, col, createMergeComboBox( fieldIt->type() ) );
QComboBox *cb = createMergeComboBox( fieldIt->type() );
if ( pkAttrList.contains( fieldIt.key() ) )
{
cb->setCurrentIndex( cb->findText( tr( "Skip attribute" ) ) );
}
mTableWidget->setCellWidget( 0, col, cb );

QTableWidgetItem *item = new QTableWidgetItem( fieldIt.value().name() );
item->setData( Qt::UserRole, fieldIt.key() );
Expand Down Expand Up @@ -135,14 +141,14 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
}
}

QComboBox* QgsMergeAttributesDialog::createMergeComboBox( QVariant::Type columnType ) const
QComboBox *QgsMergeAttributesDialog::createMergeComboBox( QVariant::Type columnType ) const
{
QComboBox* newComboBox = new QComboBox();
QComboBox *newComboBox = new QComboBox();
//add items for feature
QgsFeatureList::const_iterator f_it = mFeatureList.constBegin();
for ( ; f_it != mFeatureList.constEnd(); ++f_it )
{
newComboBox->addItem( tr( "feature %1" ).arg( f_it->id() ) );
newComboBox->addItem( tr( "Feature %1" ).arg( f_it->id() ) );
}

if ( columnType == QVariant::Double || columnType == QVariant::Int )
Expand Down Expand Up @@ -183,7 +189,7 @@ int QgsMergeAttributesDialog::findComboColumn( QComboBox* c ) const
void QgsMergeAttributesDialog::comboValueChanged( const QString &text )
{
Q_UNUSED( text );
QComboBox* senderComboBox = qobject_cast<QComboBox *>( sender() );
QComboBox *senderComboBox = qobject_cast<QComboBox *>( sender() );
if ( !senderComboBox )
{
return;
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -300,6 +300,12 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
*/
virtual QgsAttributeList attributeIndexes();

/**
* Return list of indexes of fields that make up the primary key
* @note added in 2.0
*/
virtual QgsAttributeList pkAttributeIndexes() { return QgsAttributeList(); }

/**
* Set whether provider should also return features that don't have
* associated geometry. false by default
Expand Down
40 changes: 32 additions & 8 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2513,16 +2513,13 @@ int QgsVectorLayer::splitFeatures( const QList<QgsPoint>& splitLine, bool topolo

if ( mDataProvider )
{
//use default value where possible (primary key issue), otherwise the value from the original (split) feature
QgsAttributeMap newAttributes = select_it->attributeMap();
QVariant defaultValue;
foreach ( int j, newAttributes.keys() )

// overwrite primary key field with default values
foreach ( int idx, pendingPkAttributesList() )
{
defaultValue = mDataProvider->defaultValue( j );
if ( !defaultValue.isNull() )
{
newAttributes.insert( j, defaultValue );
}
if( newAttributes.contains( idx ) )
newAttributes.insert( idx, mDataProvider->defaultValue( idx ) );
}

newFeature.setAttributeMap( newAttributes );
Expand Down Expand Up @@ -3890,6 +3887,21 @@ QgsAttributeList QgsVectorLayer::pendingAllAttributesList()
return mUpdatedFields.keys();
}

QgsAttributeList QgsVectorLayer::pendingPkAttributesList()
{
QgsAttributeList pkAttributesList;

foreach ( int idx, mDataProvider->pkAttributeIndexes() )
{
if ( !mUpdatedFields.contains( idx ) )
continue;

pkAttributesList << idx;
}

return pkAttributesList;
}

int QgsVectorLayer::pendingFeatureCount()
{
return mDataProvider->featureCount()
Expand Down Expand Up @@ -5792,6 +5804,18 @@ QString QgsVectorLayer::metadata()
myMetadata += "</td></tr>";
}

QgsAttributeList pkAttrList = pendingPkAttributesList();
if ( !pkAttrList.isEmpty() )
{
myMetadata += "<tr><td>";
myMetadata += tr( "Primary key attributes: " );
foreach( int idx, pkAttrList )
{
myMetadata += pendingFields()[ idx ].name() + " ";
}
myMetadata += "</td></tr>";
}


//feature count
myMetadata += "<tr><td>";
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -632,6 +632,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** returns list of attributes */
QgsAttributeList pendingAllAttributesList();

/** returns list of attribute making up the primary key
* @note added in 2.0
*/
QgsAttributeList pendingPkAttributesList();

/** returns feature count after commit */
int pendingFeatureCount();

Expand Down
4 changes: 3 additions & 1 deletion src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -205,6 +205,8 @@ class QgsPostgresProvider : public QgsVectorDataProvider

QgsAttributeList attributeIndexes();

QgsAttributeList pkAttributeIndexes() { return mPrimaryKeyAttrs; }

/**Returns the default value for field specified by @c fieldName */
QVariant defaultValue( QString fieldName, QString tableName = QString::null, QString schemaName = QString::null );

Expand Down Expand Up @@ -445,7 +447,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
long layerId;
};

TopoLayerInfo mTopoLayerInfo;
TopoLayerInfo mTopoLayerInfo;

bool getTopoLayerInfo();

Expand Down

0 comments on commit 2a002c5

Please sign in to comment.