[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
This commit is contained in:
Juergen E. Fischer 2013-01-06 04:48:37 +01:00
parent 12d1a28992
commit 2a002c558e
8 changed files with 66 additions and 20 deletions

View File

@ -91,7 +91,7 @@ SET(QGIS_APP_SRCS
qgsmeasuretool.cpp
qgsmergeattributesdialog.cpp
qgsoptions.cpp
qgspastetransformations.cpp
#qgspastetransformations.cpp
qgspointrotationitem.cpp
qgspluginitem.cpp
qgspluginmanager.cpp
@ -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

View File

@ -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"
@ -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 );
@ -5064,6 +5066,7 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
}
}
#if 0
void QgisApp::pasteTransformations()
{
QgsPasteTransformations *pt = new QgsPasteTransformations();
@ -5072,7 +5075,7 @@ void QgisApp::pasteTransformations()
pt->exec();
}
#endif
void QgisApp::refreshMapCanvas()
{

View File

@ -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)

View File

@ -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();
@ -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() );
@ -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 )
@ -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;

View File

@ -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

View File

@ -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 );
@ -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()
@ -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>";

View File

@ -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();

View File

@ -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 );
@ -445,7 +447,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
long layerId;
};
TopoLayerInfo mTopoLayerInfo;
TopoLayerInfo mTopoLayerInfo;
bool getTopoLayerInfo();