Skip to content

Commit

Permalink
[Fix #7849] Layer properties > fields delete correct field
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 18, 2013
1 parent 094c97b commit 34c85c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 60 deletions.
48 changes: 9 additions & 39 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -191,7 +191,7 @@ QgsFieldsProperties::QgsFieldsProperties( QgsVectorLayer *layer, QWidget* parent
attrTreeLayout->setMargin( 0 );
attrListLayout->setMargin( 0 );
mAttributesTree = new QgsAttributesTree( mAttributesTreeFrame );
mAttributesList = new QgsAttributesList( mAttributesListFrame );
mAttributesList = new QTableWidget( mAttributesListFrame );
attrTreeLayout->addWidget( mAttributesTree );
attrListLayout->addWidget( mAttributesList );
mAttributesTreeFrame->setLayout( attrTreeLayout );
Expand Down Expand Up @@ -536,14 +536,7 @@ void QgsFieldsProperties::attributeAdded( int idx )

void QgsFieldsProperties::attributeDeleted( int idx )
{
for ( int i = 0; i < mAttributesList->rowCount(); i++ )
{
if ( mAttributesList->item( i, 0 )->text().toInt() == idx )
{
mAttributesList->removeRow( i );
break;
}
}
mAttributesList->removeRow( idx );
}

void QgsFieldsProperties::addAttribute()
Expand Down Expand Up @@ -580,24 +573,6 @@ bool QgsFieldsProperties::addAttribute( const QgsField &field )
}
}

void QgsFieldsProperties::deleteAttribute()
{
QList<QTableWidgetItem*> items = mAttributesList->selectedItems();
QList<int> idxs;

for ( QList<QTableWidgetItem*>::const_iterator it = items.begin(); it != items.end(); it++ )
{
if (( *it )->column() == 0 )
idxs << ( *it )->text().toInt();
}
for ( QList<int>::const_iterator it = idxs.begin(); it != idxs.end(); it++ )
{
mLayer->beginEditCommand( tr( "Deleted attribute" ) );
mLayer->deleteAttribute( *it );
mLayer->endEditCommand();
}
}

void QgsFieldsProperties::editingToggled()
{
if ( !mLayer->isEditable() )
Expand Down Expand Up @@ -626,20 +601,15 @@ void QgsFieldsProperties::on_mAddAttributeButton_clicked()

void QgsFieldsProperties::on_mDeleteAttributeButton_clicked()
{
QList<QTableWidgetItem*> items = mAttributesList->selectedItems();
QList<int> idxs;

for ( QList<QTableWidgetItem*>::const_iterator it = items.begin(); it != items.end(); it++ )
QSet<int> attrs;
foreach ( QTableWidgetItem* item, mAttributesList->selectedItems() )
{
if (( *it )->column() == 0 )
idxs << ( *it )->text().toInt();
}
for ( QList<int>::const_iterator it = idxs.begin(); it != idxs.end(); it++ )
{
mLayer->beginEditCommand( tr( "Deleted attribute" ) );
mLayer->deleteAttribute( *it );
mLayer->endEditCommand();
attrs << mAttributesList->row( item );
}

mLayer->beginEditCommand( tr( "Deleted attribute" ) );
mLayer->deleteAttributes( attrs.toList() );
mLayer->endEditCommand();
}

void QgsFieldsProperties::updateButtons()
Expand Down
22 changes: 1 addition & 21 deletions src/app/qgsfieldsproperties.h
Expand Up @@ -24,20 +24,6 @@
#include "qgsvectorlayer.h"
#include "ui_qgsfieldspropertiesbase.h"

class QgsAttributesList : public QTableWidget
{
Q_OBJECT
public:
QgsAttributesList( QWidget* parent = 0 )
: QTableWidget( parent )
{}

protected:
// virtual void dragMoveEvent( QDragMoveEvent *event );
// QMimeData *mimeData( const QList<QTableWidgetItem *> items ) const;
// Qt::DropActions supportedDropActions() const;
};

class QgsAttributesTree : public QTreeWidget
{
Q_OBJECT
Expand Down Expand Up @@ -70,11 +56,6 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
@return false in case of a name conflict, true in case of success */
bool addAttribute( const QgsField &field );

/**Deletes an attribute (but does not commit it)
@param name attribute name
@return false in case of a non-existing attribute.*/
bool deleteAttribute( int attr );

/**Creates the a proper item to save from the tree
* @param item The tree widget item to process
* @return A widget definition. Containing another container or the final field
Expand Down Expand Up @@ -103,7 +84,6 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
void on_mEditorLayoutComboBox_currentIndexChanged( int index );

void addAttribute();
void deleteAttribute();
void attributeAdded( int idx );
void attributeDeleted( int idx );
void attributeTypeDialog();
Expand All @@ -123,7 +103,7 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
protected:
QgsVectorLayer* mLayer;
QgsAttributesTree* mAttributesTree;
QgsAttributesList* mAttributesList;
QTableWidget* mAttributesList;

QMap<int, bool> mFieldEditables;
QMap<int, QgsVectorLayer::ValueRelationData> mValueRelationData;
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2477,6 +2477,9 @@ bool QgsVectorLayer::deleteAttributes( QList<int> attrs )
{
bool deleted = false;

// Remove multiple occurences of same attribute
attrs = attrs.toSet().toList();

qSort( attrs.begin(), attrs.end(), qGreater<int>() );

foreach ( int attr, attrs )
Expand Down

0 comments on commit 34c85c8

Please sign in to comment.