Skip to content

Commit 2a002c5

Browse files
committedJan 6, 2013
[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
1 parent 12d1a28 commit 2a002c5

File tree

8 files changed

+66
-20
lines changed

8 files changed

+66
-20
lines changed
 

‎src/app/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ SET(QGIS_APP_SRCS
9191
qgsmeasuretool.cpp
9292
qgsmergeattributesdialog.cpp
9393
qgsoptions.cpp
94-
qgspastetransformations.cpp
94+
#qgspastetransformations.cpp
9595
qgspointrotationitem.cpp
9696
qgspluginitem.cpp
9797
qgspluginmanager.cpp
@@ -244,7 +244,7 @@ SET (QGIS_APP_MOC_HDRS
244244
qgsmeasuretool.h
245245
qgsmergeattributesdialog.h
246246
qgsoptions.h
247-
qgspastetransformations.h
247+
#qgspastetransformations.h
248248
qgspluginmanager.h
249249
qgsprojectlayergroupdialog.h
250250
qgsprojectproperties.h

‎src/app/qgisapp.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
#include "qgsmultibandcolorrenderer.h"
148148
#include "qgsnewvectorlayerdialog.h"
149149
#include "qgsoptions.h"
150-
#include "qgspastetransformations.h"
150+
// #include "qgspastetransformations.h"
151151
#include "qgspluginitem.h"
152152
#include "qgspluginlayer.h"
153153
#include "qgspluginlayerregistry.h"
@@ -4964,11 +4964,13 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
49644964

49654965
QHash<int, int> remap;
49664966
const QgsFieldMap &fields = clipboard()->fields();
4967+
QgsAttributeList pkAttrList = pasteVectorLayer->pendingPkAttributesList();
49674968
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); it++ )
49684969
{
49694970
int dst = pasteVectorLayer->fieldNameIndex( it->name() );
4970-
if ( dst < 0 )
4971+
if ( dst < 0 || pkAttrList.contains( dst ) )
49714972
{
4973+
// skip primary key attributes
49724974
continue;
49734975
}
49744976
remap.insert( it.key(), dst );
@@ -5064,6 +5066,7 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
50645066
}
50655067
}
50665068

5069+
#if 0
50675070
void QgisApp::pasteTransformations()
50685071
{
50695072
QgsPasteTransformations *pt = new QgsPasteTransformations();
@@ -5072,7 +5075,7 @@ void QgisApp::pasteTransformations()
50725075

50735076
pt->exec();
50745077
}
5075-
5078+
#endif
50765079

50775080
void QgisApp::refreshMapCanvas()
50785081
{

‎src/app/qgisapp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
10481048
//! Read Well Known Binary stream from PostGIS
10491049
//void readWKB(const char *, QStringList tables);
10501050
//! shows the paste-transformations dialog
1051-
void pasteTransformations();
1051+
// void pasteTransformations();
10521052
//! check to see if file is dirty and if so, prompt the user th save it
10531053
bool saveDirty();
10541054
/** Helper function to union several geometries together (used in function mergeSelectedFeatures)

‎src/app/qgsmergeattributesdialog.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
8484

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

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

9798
mTableWidget->setColumnCount( col + 1 );
9899

99-
mTableWidget->setCellWidget( 0, col, createMergeComboBox( fieldIt->type() ) );
100+
QComboBox *cb = createMergeComboBox( fieldIt->type() );
101+
if ( pkAttrList.contains( fieldIt.key() ) )
102+
{
103+
cb->setCurrentIndex( cb->findText( tr( "Skip attribute" ) ) );
104+
}
105+
mTableWidget->setCellWidget( 0, col, cb );
100106

101107
QTableWidgetItem *item = new QTableWidgetItem( fieldIt.value().name() );
102108
item->setData( Qt::UserRole, fieldIt.key() );
@@ -135,14 +141,14 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
135141
}
136142
}
137143

138-
QComboBox* QgsMergeAttributesDialog::createMergeComboBox( QVariant::Type columnType ) const
144+
QComboBox *QgsMergeAttributesDialog::createMergeComboBox( QVariant::Type columnType ) const
139145
{
140-
QComboBox* newComboBox = new QComboBox();
146+
QComboBox *newComboBox = new QComboBox();
141147
//add items for feature
142148
QgsFeatureList::const_iterator f_it = mFeatureList.constBegin();
143149
for ( ; f_it != mFeatureList.constEnd(); ++f_it )
144150
{
145-
newComboBox->addItem( tr( "feature %1" ).arg( f_it->id() ) );
151+
newComboBox->addItem( tr( "Feature %1" ).arg( f_it->id() ) );
146152
}
147153

148154
if ( columnType == QVariant::Double || columnType == QVariant::Int )
@@ -183,7 +189,7 @@ int QgsMergeAttributesDialog::findComboColumn( QComboBox* c ) const
183189
void QgsMergeAttributesDialog::comboValueChanged( const QString &text )
184190
{
185191
Q_UNUSED( text );
186-
QComboBox* senderComboBox = qobject_cast<QComboBox *>( sender() );
192+
QComboBox *senderComboBox = qobject_cast<QComboBox *>( sender() );
187193
if ( !senderComboBox )
188194
{
189195
return;

‎src/core/qgsvectordataprovider.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
300300
*/
301301
virtual QgsAttributeList attributeIndexes();
302302

303+
/**
304+
* Return list of indexes of fields that make up the primary key
305+
* @note added in 2.0
306+
*/
307+
virtual QgsAttributeList pkAttributeIndexes() { return QgsAttributeList(); }
308+
303309
/**
304310
* Set whether provider should also return features that don't have
305311
* associated geometry. false by default

‎src/core/qgsvectorlayer.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,16 +2513,13 @@ int QgsVectorLayer::splitFeatures( const QList<QgsPoint>& splitLine, bool topolo
25132513

25142514
if ( mDataProvider )
25152515
{
2516-
//use default value where possible (primary key issue), otherwise the value from the original (split) feature
25172516
QgsAttributeMap newAttributes = select_it->attributeMap();
2518-
QVariant defaultValue;
2519-
foreach ( int j, newAttributes.keys() )
2517+
2518+
// overwrite primary key field with default values
2519+
foreach ( int idx, pendingPkAttributesList() )
25202520
{
2521-
defaultValue = mDataProvider->defaultValue( j );
2522-
if ( !defaultValue.isNull() )
2523-
{
2524-
newAttributes.insert( j, defaultValue );
2525-
}
2521+
if( newAttributes.contains( idx ) )
2522+
newAttributes.insert( idx, mDataProvider->defaultValue( idx ) );
25262523
}
25272524

25282525
newFeature.setAttributeMap( newAttributes );
@@ -3890,6 +3887,21 @@ QgsAttributeList QgsVectorLayer::pendingAllAttributesList()
38903887
return mUpdatedFields.keys();
38913888
}
38923889

3890+
QgsAttributeList QgsVectorLayer::pendingPkAttributesList()
3891+
{
3892+
QgsAttributeList pkAttributesList;
3893+
3894+
foreach ( int idx, mDataProvider->pkAttributeIndexes() )
3895+
{
3896+
if ( !mUpdatedFields.contains( idx ) )
3897+
continue;
3898+
3899+
pkAttributesList << idx;
3900+
}
3901+
3902+
return pkAttributesList;
3903+
}
3904+
38933905
int QgsVectorLayer::pendingFeatureCount()
38943906
{
38953907
return mDataProvider->featureCount()
@@ -5792,6 +5804,18 @@ QString QgsVectorLayer::metadata()
57925804
myMetadata += "</td></tr>";
57935805
}
57945806

5807+
QgsAttributeList pkAttrList = pendingPkAttributesList();
5808+
if ( !pkAttrList.isEmpty() )
5809+
{
5810+
myMetadata += "<tr><td>";
5811+
myMetadata += tr( "Primary key attributes: " );
5812+
foreach( int idx, pkAttrList )
5813+
{
5814+
myMetadata += pendingFields()[ idx ].name() + " ";
5815+
}
5816+
myMetadata += "</td></tr>";
5817+
}
5818+
57955819

57965820
//feature count
57975821
myMetadata += "<tr><td>";

‎src/core/qgsvectorlayer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
632632
/** returns list of attributes */
633633
QgsAttributeList pendingAllAttributesList();
634634

635+
/** returns list of attribute making up the primary key
636+
* @note added in 2.0
637+
*/
638+
QgsAttributeList pendingPkAttributesList();
639+
635640
/** returns feature count after commit */
636641
int pendingFeatureCount();
637642

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class QgsPostgresProvider : public QgsVectorDataProvider
205205

206206
QgsAttributeList attributeIndexes();
207207

208+
QgsAttributeList pkAttributeIndexes() { return mPrimaryKeyAttrs; }
209+
208210
/**Returns the default value for field specified by @c fieldName */
209211
QVariant defaultValue( QString fieldName, QString tableName = QString::null, QString schemaName = QString::null );
210212

@@ -445,7 +447,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
445447
long layerId;
446448
};
447449

448-
TopoLayerInfo mTopoLayerInfo;
450+
TopoLayerInfo mTopoLayerInfo;
449451

450452
bool getTopoLayerInfo();
451453

0 commit comments

Comments
 (0)