Skip to content
Permalink
Browse files

[FEATURE] attribute editing improvements:

- 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@14729 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Nov 21, 2010
1 parent 2c84b52 commit c3c979a0f2262fce199d804e3463f585e67fab6f
@@ -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,
@@ -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
@@ -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
@@ -34,6 +34,7 @@
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsfieldcalculator.h"
#include "qgsfeatureaction.h"

class QgsAttributeTableDock : public QDockWidget
{
@@ -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 * ) ) );
@@ -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() ) );
@@ -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() ) );
@@ -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() ) );
}
}
@@ -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:
@@ -60,7 +60,6 @@ bool QgsAttributeTableMemoryModel::featureAtId( int fid )
}
}

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

void QgsAttributeTableMemoryModel::layerDeleted()
{
@@ -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 );
}
@@ -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
@@ -51,7 +50,6 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
* @param fid feature id
*/
virtual void featureAdded( int fid );
#endif
/**
* Launched when layer has been deleted
*/

0 comments on commit c3c979a

Please sign in to comment.
You can’t perform that action at this time.