Skip to content

Commit

Permalink
[Fix #7858] Toggle editing mode resets edit widget
Browse files Browse the repository at this point in the history
And introduces full mapping between table row and field index ( Fix for loosing index mapping after sorting rows )
  • Loading branch information
m-kuhn committed May 18, 2013
1 parent a98ffed commit 99aaa59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -286,7 +286,6 @@ void QgsFieldsProperties::loadRows()
mAttributesList->clear();

mAttributesList->setColumnCount( attrColCount );
mAttributesList->setRowCount( fields.size() );
mAttributesList->setSelectionBehavior( QAbstractItemView::SelectRows );
mAttributesList->setDragDropMode( QAbstractItemView::DragOnly );
mAttributesList->setHorizontalHeaderItem( attrIdCol, new QTableWidgetItem( tr( "Id" ) ) );
Expand All @@ -306,7 +305,7 @@ void QgsFieldsProperties::loadRows()
mAttributesList->verticalHeader()->hide();

for ( int i = 0; i < fields.count(); ++i )
setRow( i, i, fields[i] );
attributeAdded( i );

mAttributesList->resizeColumnsToContents();
connect( mAttributesList, SIGNAL( cellChanged( int, int ) ), this, SLOT( attributesListCellChanged( int, int ) ) );
Expand Down Expand Up @@ -526,17 +525,32 @@ void QgsFieldsProperties::attributeTypeDialog()

void QgsFieldsProperties::attributeAdded( int idx )
{
bool sorted = mAttributesList->isSortingEnabled();
mAttributesList->setSortingEnabled( false );
const QgsFields &fields = mLayer->pendingFields();
int row = mAttributesList->rowCount();
mAttributesList->insertRow( row );
setRow( row, idx, fields[idx] );
mIndexedWidgets.insert( idx, mAttributesList->item( row, 0 ) );

for ( int i = idx; i < mIndexedWidgets.count(); i++ )
{
mIndexedWidgets[i]->setText( QString::number( i ) );
}

mAttributesList->setCurrentCell( row, idx );
mAttributesList->setSortingEnabled( sorted );
}


void QgsFieldsProperties::attributeDeleted( int idx )
{
mAttributesList->removeRow( idx );
mAttributesList->removeRow( mIndexedWidgets.at( idx )->row() );
mIndexedWidgets.removeAt( idx );
for ( int i = idx; i < mIndexedWidgets.count(); i++ )
{
mIndexedWidgets[i]->setText( QString::number( i ) );
}
}

void QgsFieldsProperties::addAttribute()
Expand Down Expand Up @@ -575,9 +589,6 @@ bool QgsFieldsProperties::addAttribute( const QgsField &field )

void QgsFieldsProperties::editingToggled()
{
if ( !mLayer->isEditable() )
loadRows();

updateButtons();
}

Expand All @@ -604,7 +615,10 @@ void QgsFieldsProperties::on_mDeleteAttributeButton_clicked()
QSet<int> attrs;
foreach ( QTableWidgetItem* item, mAttributesList->selectedItems() )
{
attrs << mAttributesList->row( item );
if ( item->column() == 0 )
{
attrs << mIndexedWidgets.indexOf( item );
}
}

mLayer->beginEditCommand( tr( "Deleted attribute" ) );
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsfieldsproperties.h
Expand Up @@ -114,6 +114,10 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
QMap<int, QPushButton*> mButtonMap;
QMap<int, QString> mDateFormat;
QMap<int, QSize> mWidgetSize;
// Holds all the first column items (header: id) of the table.
// The index in the list is the fieldIdx, and therefore acts as a mapping
// between fieldIdx and QTableWidgetItem->row()
QList<QTableWidgetItem*> mIndexedWidgets;

enum attrColumns
{
Expand Down

0 comments on commit 99aaa59

Please sign in to comment.