Skip to content

Commit b98fe38

Browse files
author
mhugent
committedSep 7, 2007
Fixes parts of bug #763 'problem editing postgis table'. But it is still a problem to add attributes and commit changes to the new attributes in one go
git-svn-id: http://svn.osgeo.org/qgis/trunk@7181 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent e7d7722 commit b98fe38

File tree

2 files changed

+36
-50
lines changed

2 files changed

+36
-50
lines changed
 

‎src/app/qgsattributetable.cpp

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,29 @@ bool QgsAttributeTable::commitChanges(QgsVectorLayer* layer)
426426
{
427427
deletedIds.insert(provider->indexFromFieldName(*it));
428428
}
429+
430+
QgsChangedAttributesMap attributeChanges; //convert mChangedValues to QgsChangedAttributesMap
431+
int fieldIndex;
432+
433+
QMap<int, QMap<QString, QString> >::const_iterator att_it = mChangedValues.constBegin();
434+
for(; att_it != mChangedValues.constEnd(); ++att_it)
435+
{
436+
QgsAttributeMap newAttMap;
437+
QMap<QString, QString>::const_iterator record_it = att_it->constBegin();
438+
for(; record_it != att_it->constEnd(); ++record_it)
439+
{
440+
fieldIndex = provider->indexFromFieldName(record_it.key());
441+
if(fieldIndex != -1)
442+
{
443+
newAttMap.insert(fieldIndex, record_it.value());
444+
}
445+
}
446+
attributeChanges.insert(att_it.key(), newAttMap);
447+
}
429448

430449
isSuccessful = layer->commitAttributeChanges(deletedIds,
431450
mAddedAttributes,
432-
mChangedValues);
451+
attributeChanges);
433452
}
434453
}
435454

@@ -528,52 +547,23 @@ void QgsAttributeTable::putFeatureInTable(int row, QgsFeature& fet)
528547
}
529548
}
530549

531-
int QgsAttributeTable::colIndexFromFieldIndex(int fieldId)
532-
{
533-
int colIndex = 1; // index 0 is feature ID
534-
QgsFieldMap::const_iterator it;
535-
for (it = mFields.begin(); it != mFields.end(); ++it, ++colIndex)
536-
{
537-
if (it.key() == fieldId)
538-
return colIndex;
539-
}
540-
return -1;
541-
}
542-
543-
int QgsAttributeTable::fieldIndexFromColIndex(int colIndex)
544-
{
545-
colIndex--; // first one is feature ID
546-
QgsFieldMap::const_iterator it;
547-
for (it = mFields.begin(); it != mFields.end(); ++it, --colIndex)
548-
{
549-
if (colIndex == 0)
550-
return it.key();
551-
}
552-
return -1;
553-
}
554-
555550
void QgsAttributeTable::storeChangedValue(int row, int column)
556551
{
557-
//id column is not editable
558-
if(column>0)
552+
//id column is not editable
553+
if(column>0)
559554
{
560-
//find feature id
561-
int id=text(row,0).toInt();
562-
int field = fieldIndexFromColIndex(column);
563-
564-
QgsDebugMsg("feature id: " + QString::number(id));
565-
QgsDebugMsg("attribute: " + QString::number(field) + ": " + mFields[field].name());
566-
567-
// add empty map for feature if doesn't exist
568-
if (!mChangedValues.contains(id))
569-
{
570-
mChangedValues.insert(id, QgsAttributeMap());
571-
}
572-
573-
mChangedValues[id].insert(field, QVariant(text(row,column)) );
574-
575-
QgsDebugMsg("value: " + text(row,column));
576-
mEdited=true;
555+
//find feature id
556+
int id=text(row,0).toInt();
557+
QString field = horizontalHeader()->label(column);
558+
559+
// add empty map for feature if doesn't exist
560+
if (!mChangedValues.contains(id))
561+
{
562+
mChangedValues.insert(id, QMap<QString, QString>());
563+
}
564+
565+
mChangedValues[id].insert(field, text(row,column));
566+
mEdited=true;
577567
}
578568
}
579569

‎src/app/qgsattributetable.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class QgsAttributeTable:public Q3Table
133133
QSet<QString> mDeletedAttributes;
134134
/**Nested map containing the changed attribute values. The int is the feature id,
135135
the first QString the attribute name and the second QString the new value*/
136-
QgsChangedAttributesMap mChangedValues;
136+
QMap<int, QMap<QString, QString> > mChangedValues;
137+
137138
/**Stors the numbers of the last selected rows. This is used to check for selection changes before emit repaintRequested()*/
138139
std::set<int> mLastSelectedRows;
139140

@@ -154,11 +155,6 @@ class QgsAttributeTable:public Q3Table
154155
/**This function compares the current selection and the selection of the last repaint. Returns true if there are differences in the selection.
155156
Also, mLastSelectedRows is updated*/
156157
bool checkSelectionChanges();
157-
158-
/** returns column index for field index or -1 on invalid field index */
159-
int colIndexFromFieldIndex(int fieldId);
160-
/** returns field index for a column or -1 when on invalid column */
161-
int fieldIndexFromColIndex(int colIndex);
162158

163159
signals:
164160

0 commit comments

Comments
 (0)
Please sign in to comment.