Skip to content

Commit

Permalink
[FEATURE] attribute editing improvements:
Browse files Browse the repository at this point in the history
- refactor attribute dialog calls to QgsFeatureAttribute
- add QgsVectorLayer::featureAdded signal
- improve interactive attribute editing in table (adding/deleting
  features, attribute update)
- allow adding of geometryless features
- fix attribute undo/redo

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14729 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 21, 2010
1 parent 2d8de27 commit 491c1d7
Show file tree
Hide file tree
Showing 23 changed files with 470 additions and 473 deletions.
22 changes: 20 additions & 2 deletions src/app/attributetable/qgsattributetabledelegate.cpp
Expand Up @@ -52,6 +52,19 @@ int QgsAttributeTableDelegate::fieldIdx( const QModelIndex &index ) const
return -1;
}

int QgsAttributeTableDelegate::featureId( const QModelIndex &index ) const
{
const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( index.model() );
if ( tm )
return tm->rowToId( index.row() );

const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( index.model() );
if ( fm )
return fm->tableModel()->rowToId( index.row() );

return -1;
}


QWidget *QgsAttributeTableDelegate::createEditor(
QWidget *parent,
Expand Down Expand Up @@ -87,11 +100,16 @@ void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemMode
if ( vl == NULL )
return;

int idx = fieldIdx( index );
int fid = featureId( index );

QVariant value;
if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx( index ), value ) )
if ( !QgsAttributeEditor::retrieveValue( editor, vl, idx, value ) )
return;

model->setData( index, value );
vl->beginEditCommand( tr( "Attribute changed" ) );
vl->changeAttributeValue( fid, idx, value, true );
vl->endEditCommand();
}

void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
Expand Down
1 change: 1 addition & 0 deletions src/app/attributetable/qgsattributetabledelegate.h
Expand Up @@ -32,6 +32,7 @@ class QgsAttributeTableDelegate : public QItemDelegate

QgsVectorLayer *layer( const QAbstractItemModel *model ) const;
int fieldIdx( const QModelIndex &index ) const;
int featureId( const QModelIndex &index ) const;

public:
/** Constructor
Expand Down
20 changes: 18 additions & 2 deletions src/app/attributetable/qgsattributetabledialog.cpp
Expand Up @@ -34,6 +34,7 @@
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsfieldcalculator.h"
#include "qgsfeatureaction.h"

class QgsAttributeTableDock : public QDockWidget
{
Expand Down Expand Up @@ -99,13 +100,16 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
bool canDeleteFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures;
bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes;
bool canDeleteAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteAttributes;
bool canAddFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddFeatures;
mToggleEditingButton->setCheckable( true );
mToggleEditingButton->setChecked( mLayer->isEditable() );
mToggleEditingButton->setEnabled( canChangeAttributes && !mLayer->isReadOnly() );
mOpenFieldCalculator->setEnabled( canChangeAttributes && mLayer->isEditable() );
mDeleteSelectedButton->setEnabled( canDeleteFeatures && mLayer->isEditable() );
mAddAttribute->setEnabled( canAddAttributes && mLayer->isEditable() );
mRemoveAttribute->setEnabled( canDeleteAttributes && mLayer->isEditable() );
mAddFeature->setEnabled( canAddFeatures && mLayer->isEditable() && mLayer->geometryType() == QGis::NoGeometry );
mAddFeature->setHidden( !canAddFeatures || mLayer->geometryType() != QGis::NoGeometry );

// info from table to application
connect( this, SIGNAL( editingToggled( QgsMapLayer * ) ), QgisApp::instance(), SLOT( toggleEditing( QgsMapLayer * ) ) );
Expand All @@ -114,6 +118,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
connect( mLayer, SIGNAL( editingStopped() ), this, SLOT( editingToggled() ) );

connect( searchButton, SIGNAL( clicked() ), this, SLOT( search() ) );
connect( mAddFeature, SIGNAL( clicked() ), this, SLOT( addFeature() ) );

connect( mLayer, SIGNAL( selectionChanged() ), this, SLOT( updateSelectionFromLayer() ) );
connect( mLayer, SIGNAL( layerDeleted() ), this, SLOT( close() ) );
Expand Down Expand Up @@ -674,10 +679,12 @@ void QgsAttributeTableDialog::editingToggled()
bool canDeleteFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures;
bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes;
bool canDeleteAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteAttributes;
bool canAddFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddFeatures;
mOpenFieldCalculator->setEnabled( canChangeAttributes && mLayer->isEditable() );
mDeleteSelectedButton->setEnabled( canDeleteFeatures && mLayer->isEditable() );
mAddAttribute->setEnabled( canAddAttributes && mLayer->isEditable() );
mRemoveAttribute->setEnabled( canDeleteAttributes && mLayer->isEditable() );
mAddFeature->setEnabled( canAddFeatures && mLayer->isEditable() && mLayer->geometryType() == QGis::NoGeometry );

// (probably reload data if user stopped editing - possible revert)
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
Expand Down Expand Up @@ -775,9 +782,18 @@ void QgsAttributeTableDialog::on_mRemoveAttribute_clicked()
void QgsAttributeTableDialog::on_mOpenFieldCalculator_clicked()
{
QgsFieldCalculator calc( mLayer );
if ( calc.exec() == QDialog::Accepted )
calc.exec();
}

void QgsAttributeTableDialog::addFeature()
{
if ( !mLayer->isEditable() )
return;

QgsFeature f;
QgsFeatureAction action( tr( "Geometryless feature added" ), f, mLayer, -1, this );
if ( action.addFeature() )
{
// update model - a field has been added or updated
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
}
}
5 changes: 5 additions & 0 deletions src/app/attributetable/qgsattributetabledialog.h
Expand Up @@ -151,6 +151,11 @@ class QgsAttributeTableDialog : public QDialog, private Ui::QgsAttributeTableDia
*/
void on_mDeleteSelectedButton_clicked();

/**
* add feature
*/
void addFeature();

void on_mHelpButton_clicked() { QgsContextHelp::run( metaObject()->className() ); }

signals:
Expand Down
4 changes: 1 addition & 3 deletions src/app/attributetable/qgsattributetablememorymodel.cpp
Expand Up @@ -60,7 +60,6 @@ bool QgsAttributeTableMemoryModel::featureAtId( int fid )
}
}

#if 0
void QgsAttributeTableMemoryModel::featureDeleted( int fid )
{
QgsDebugMsg( "entered." );
Expand All @@ -76,7 +75,6 @@ void QgsAttributeTableMemoryModel::featureAdded( int fid )
mFeatureMap.insert( fid, f );
QgsAttributeTableModel::featureAdded( fid );
}
#endif

void QgsAttributeTableMemoryModel::layerDeleted()
{
Expand All @@ -89,5 +87,5 @@ void QgsAttributeTableMemoryModel::attributeValueChanged( int fid, int idx, cons
{
QgsDebugMsg( "entered." );
mFeatureMap[fid].changeAttribute( idx, value );
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
QgsAttributeTableModel::attributeValueChanged( fid, idx, value );
}
2 changes: 0 additions & 2 deletions src/app/attributetable/qgsattributetablememorymodel.h
Expand Up @@ -40,7 +40,6 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
QgsAttributeTableMemoryModel( QgsVectorLayer *theLayer );

protected slots:
#if 0
/**
* Launched when a feature has been deleted
* @param fid feature id
Expand All @@ -51,7 +50,6 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
* @param fid feature id
*/
virtual void featureAdded( int fid );
#endif
/**
* Launched when layer has been deleted
*/
Expand Down

0 comments on commit 491c1d7

Please sign in to comment.