Skip to content

Commit 34c85c8

Browse files
committed
[Fix #7849] Layer properties > fields delete correct field
1 parent 094c97b commit 34c85c8

File tree

3 files changed

+13
-60
lines changed

3 files changed

+13
-60
lines changed

src/app/qgsfieldsproperties.cpp

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ QgsFieldsProperties::QgsFieldsProperties( QgsVectorLayer *layer, QWidget* parent
191191
attrTreeLayout->setMargin( 0 );
192192
attrListLayout->setMargin( 0 );
193193
mAttributesTree = new QgsAttributesTree( mAttributesTreeFrame );
194-
mAttributesList = new QgsAttributesList( mAttributesListFrame );
194+
mAttributesList = new QTableWidget( mAttributesListFrame );
195195
attrTreeLayout->addWidget( mAttributesTree );
196196
attrListLayout->addWidget( mAttributesList );
197197
mAttributesTreeFrame->setLayout( attrTreeLayout );
@@ -536,14 +536,7 @@ void QgsFieldsProperties::attributeAdded( int idx )
536536

537537
void QgsFieldsProperties::attributeDeleted( int idx )
538538
{
539-
for ( int i = 0; i < mAttributesList->rowCount(); i++ )
540-
{
541-
if ( mAttributesList->item( i, 0 )->text().toInt() == idx )
542-
{
543-
mAttributesList->removeRow( i );
544-
break;
545-
}
546-
}
539+
mAttributesList->removeRow( idx );
547540
}
548541

549542
void QgsFieldsProperties::addAttribute()
@@ -580,24 +573,6 @@ bool QgsFieldsProperties::addAttribute( const QgsField &field )
580573
}
581574
}
582575

583-
void QgsFieldsProperties::deleteAttribute()
584-
{
585-
QList<QTableWidgetItem*> items = mAttributesList->selectedItems();
586-
QList<int> idxs;
587-
588-
for ( QList<QTableWidgetItem*>::const_iterator it = items.begin(); it != items.end(); it++ )
589-
{
590-
if (( *it )->column() == 0 )
591-
idxs << ( *it )->text().toInt();
592-
}
593-
for ( QList<int>::const_iterator it = idxs.begin(); it != idxs.end(); it++ )
594-
{
595-
mLayer->beginEditCommand( tr( "Deleted attribute" ) );
596-
mLayer->deleteAttribute( *it );
597-
mLayer->endEditCommand();
598-
}
599-
}
600-
601576
void QgsFieldsProperties::editingToggled()
602577
{
603578
if ( !mLayer->isEditable() )
@@ -626,20 +601,15 @@ void QgsFieldsProperties::on_mAddAttributeButton_clicked()
626601

627602
void QgsFieldsProperties::on_mDeleteAttributeButton_clicked()
628603
{
629-
QList<QTableWidgetItem*> items = mAttributesList->selectedItems();
630-
QList<int> idxs;
631-
632-
for ( QList<QTableWidgetItem*>::const_iterator it = items.begin(); it != items.end(); it++ )
604+
QSet<int> attrs;
605+
foreach ( QTableWidgetItem* item, mAttributesList->selectedItems() )
633606
{
634-
if (( *it )->column() == 0 )
635-
idxs << ( *it )->text().toInt();
636-
}
637-
for ( QList<int>::const_iterator it = idxs.begin(); it != idxs.end(); it++ )
638-
{
639-
mLayer->beginEditCommand( tr( "Deleted attribute" ) );
640-
mLayer->deleteAttribute( *it );
641-
mLayer->endEditCommand();
607+
attrs << mAttributesList->row( item );
642608
}
609+
610+
mLayer->beginEditCommand( tr( "Deleted attribute" ) );
611+
mLayer->deleteAttributes( attrs.toList() );
612+
mLayer->endEditCommand();
643613
}
644614

645615
void QgsFieldsProperties::updateButtons()

src/app/qgsfieldsproperties.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@
2424
#include "qgsvectorlayer.h"
2525
#include "ui_qgsfieldspropertiesbase.h"
2626

27-
class QgsAttributesList : public QTableWidget
28-
{
29-
Q_OBJECT
30-
public:
31-
QgsAttributesList( QWidget* parent = 0 )
32-
: QTableWidget( parent )
33-
{}
34-
35-
protected:
36-
// virtual void dragMoveEvent( QDragMoveEvent *event );
37-
// QMimeData *mimeData( const QList<QTableWidgetItem *> items ) const;
38-
// Qt::DropActions supportedDropActions() const;
39-
};
40-
4127
class QgsAttributesTree : public QTreeWidget
4228
{
4329
Q_OBJECT
@@ -70,11 +56,6 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
7056
@return false in case of a name conflict, true in case of success */
7157
bool addAttribute( const QgsField &field );
7258

73-
/**Deletes an attribute (but does not commit it)
74-
@param name attribute name
75-
@return false in case of a non-existing attribute.*/
76-
bool deleteAttribute( int attr );
77-
7859
/**Creates the a proper item to save from the tree
7960
* @param item The tree widget item to process
8061
* @return A widget definition. Containing another container or the final field
@@ -103,7 +84,6 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
10384
void on_mEditorLayoutComboBox_currentIndexChanged( int index );
10485

10586
void addAttribute();
106-
void deleteAttribute();
10787
void attributeAdded( int idx );
10888
void attributeDeleted( int idx );
10989
void attributeTypeDialog();
@@ -123,7 +103,7 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
123103
protected:
124104
QgsVectorLayer* mLayer;
125105
QgsAttributesTree* mAttributesTree;
126-
QgsAttributesList* mAttributesList;
106+
QTableWidget* mAttributesList;
127107

128108
QMap<int, bool> mFieldEditables;
129109
QMap<int, QgsVectorLayer::ValueRelationData> mValueRelationData;

src/core/qgsvectorlayer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,9 @@ bool QgsVectorLayer::deleteAttributes( QList<int> attrs )
24772477
{
24782478
bool deleted = false;
24792479

2480+
// Remove multiple occurences of same attribute
2481+
attrs = attrs.toSet().toList();
2482+
24802483
qSort( attrs.begin(), attrs.end(), qGreater<int>() );
24812484

24822485
foreach ( int attr, attrs )

0 commit comments

Comments
 (0)