Skip to content

Commit c3c979a

Browse files
author
jef
committedNov 21, 2010
[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
1 parent 2c84b52 commit c3c979a

23 files changed

+470
-473
lines changed
 

‎src/app/attributetable/qgsattributetabledelegate.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ int QgsAttributeTableDelegate::fieldIdx( const QModelIndex &index ) const
5252
return -1;
5353
}
5454

55+
int QgsAttributeTableDelegate::featureId( const QModelIndex &index ) const
56+
{
57+
const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( index.model() );
58+
if ( tm )
59+
return tm->rowToId( index.row() );
60+
61+
const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( index.model() );
62+
if ( fm )
63+
return fm->tableModel()->rowToId( index.row() );
64+
65+
return -1;
66+
}
67+
5568

5669
QWidget *QgsAttributeTableDelegate::createEditor(
5770
QWidget *parent,
@@ -87,11 +100,16 @@ void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemMode
87100
if ( vl == NULL )
88101
return;
89102

103+
int idx = fieldIdx( index );
104+
int fid = featureId( index );
105+
90106
QVariant value;
91-
if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx( index ), value ) )
107+
if ( !QgsAttributeEditor::retrieveValue( editor, vl, idx, value ) )
92108
return;
93109

94-
model->setData( index, value );
110+
vl->beginEditCommand( tr( "Attribute changed" ) );
111+
vl->changeAttributeValue( fid, idx, value, true );
112+
vl->endEditCommand();
95113
}
96114

97115
void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const

‎src/app/attributetable/qgsattributetabledelegate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class QgsAttributeTableDelegate : public QItemDelegate
3232

3333
QgsVectorLayer *layer( const QAbstractItemModel *model ) const;
3434
int fieldIdx( const QModelIndex &index ) const;
35+
int featureId( const QModelIndex &index ) const;
3536

3637
public:
3738
/** Constructor

‎src/app/attributetable/qgsattributetabledialog.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "qgslogger.h"
3535
#include "qgsmapcanvas.h"
3636
#include "qgsfieldcalculator.h"
37+
#include "qgsfeatureaction.h"
3738

3839
class QgsAttributeTableDock : public QDockWidget
3940
{
@@ -99,13 +100,16 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
99100
bool canDeleteFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures;
100101
bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes;
101102
bool canDeleteAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteAttributes;
103+
bool canAddFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddFeatures;
102104
mToggleEditingButton->setCheckable( true );
103105
mToggleEditingButton->setChecked( mLayer->isEditable() );
104106
mToggleEditingButton->setEnabled( canChangeAttributes && !mLayer->isReadOnly() );
105107
mOpenFieldCalculator->setEnabled( canChangeAttributes && mLayer->isEditable() );
106108
mDeleteSelectedButton->setEnabled( canDeleteFeatures && mLayer->isEditable() );
107109
mAddAttribute->setEnabled( canAddAttributes && mLayer->isEditable() );
108110
mRemoveAttribute->setEnabled( canDeleteAttributes && mLayer->isEditable() );
111+
mAddFeature->setEnabled( canAddFeatures && mLayer->isEditable() && mLayer->geometryType() == QGis::NoGeometry );
112+
mAddFeature->setHidden( !canAddFeatures || mLayer->geometryType() != QGis::NoGeometry );
109113

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

116120
connect( searchButton, SIGNAL( clicked() ), this, SLOT( search() ) );
121+
connect( mAddFeature, SIGNAL( clicked() ), this, SLOT( addFeature() ) );
117122

118123
connect( mLayer, SIGNAL( selectionChanged() ), this, SLOT( updateSelectionFromLayer() ) );
119124
connect( mLayer, SIGNAL( layerDeleted() ), this, SLOT( close() ) );
@@ -674,10 +679,12 @@ void QgsAttributeTableDialog::editingToggled()
674679
bool canDeleteFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures;
675680
bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes;
676681
bool canDeleteAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteAttributes;
682+
bool canAddFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddFeatures;
677683
mOpenFieldCalculator->setEnabled( canChangeAttributes && mLayer->isEditable() );
678684
mDeleteSelectedButton->setEnabled( canDeleteFeatures && mLayer->isEditable() );
679685
mAddAttribute->setEnabled( canAddAttributes && mLayer->isEditable() );
680686
mRemoveAttribute->setEnabled( canDeleteAttributes && mLayer->isEditable() );
687+
mAddFeature->setEnabled( canAddFeatures && mLayer->isEditable() && mLayer->geometryType() == QGis::NoGeometry );
681688

682689
// (probably reload data if user stopped editing - possible revert)
683690
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
@@ -775,9 +782,18 @@ void QgsAttributeTableDialog::on_mRemoveAttribute_clicked()
775782
void QgsAttributeTableDialog::on_mOpenFieldCalculator_clicked()
776783
{
777784
QgsFieldCalculator calc( mLayer );
778-
if ( calc.exec() == QDialog::Accepted )
785+
calc.exec();
786+
}
787+
788+
void QgsAttributeTableDialog::addFeature()
789+
{
790+
if ( !mLayer->isEditable() )
791+
return;
792+
793+
QgsFeature f;
794+
QgsFeatureAction action( tr( "Geometryless feature added" ), f, mLayer, -1, this );
795+
if ( action.addFeature() )
779796
{
780-
// update model - a field has been added or updated
781797
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
782798
}
783799
}

‎src/app/attributetable/qgsattributetabledialog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ class QgsAttributeTableDialog : public QDialog, private Ui::QgsAttributeTableDia
151151
*/
152152
void on_mDeleteSelectedButton_clicked();
153153

154+
/**
155+
* add feature
156+
*/
157+
void addFeature();
158+
154159
void on_mHelpButton_clicked() { QgsContextHelp::run( metaObject()->className() ); }
155160

156161
signals:

‎src/app/attributetable/qgsattributetablememorymodel.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ bool QgsAttributeTableMemoryModel::featureAtId( int fid )
6060
}
6161
}
6262

63-
#if 0
6463
void QgsAttributeTableMemoryModel::featureDeleted( int fid )
6564
{
6665
QgsDebugMsg( "entered." );
@@ -76,7 +75,6 @@ void QgsAttributeTableMemoryModel::featureAdded( int fid )
7675
mFeatureMap.insert( fid, f );
7776
QgsAttributeTableModel::featureAdded( fid );
7877
}
79-
#endif
8078

8179
void QgsAttributeTableMemoryModel::layerDeleted()
8280
{
@@ -89,5 +87,5 @@ void QgsAttributeTableMemoryModel::attributeValueChanged( int fid, int idx, cons
8987
{
9088
QgsDebugMsg( "entered." );
9189
mFeatureMap[fid].changeAttribute( idx, value );
92-
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
90+
QgsAttributeTableModel::attributeValueChanged( fid, idx, value );
9391
}

‎src/app/attributetable/qgsattributetablememorymodel.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
4040
QgsAttributeTableMemoryModel( QgsVectorLayer *theLayer );
4141

4242
protected slots:
43-
#if 0
4443
/**
4544
* Launched when a feature has been deleted
4645
* @param fid feature id
@@ -51,7 +50,6 @@ class QgsAttributeTableMemoryModel : public QgsAttributeTableModel
5150
* @param fid feature id
5251
*/
5352
virtual void featureAdded( int fid );
54-
#endif
5553
/**
5654
* Launched when layer has been deleted
5755
*/

0 commit comments

Comments
 (0)
Please sign in to comment.