Skip to content

Commit

Permalink
Merge pull request #4776 from pblottiere/bugfix_attributetable
Browse files Browse the repository at this point in the history
[bugfix] attribute table
  • Loading branch information
Hugo Mercier committed Jun 28, 2017
2 parents 9f5e33a + b354986 commit f59d6d7
Show file tree
Hide file tree
Showing 15 changed files with 553 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/qgsvectorlayerjoinbuffer.cpp
Expand Up @@ -348,6 +348,7 @@ void QgsVectorLayerJoinBuffer::resolveReferences( QgsProject *project )
if ( QgsVectorLayer *joinedLayer = qobject_cast<QgsVectorLayer *>( project->mapLayer( it->joinLayerId() ) ) )
{
it->setJoinLayer( joinedLayer );
connectJoinedLayer( joinedLayer );
resolved = true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -303,7 +303,8 @@ void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, c
// No filter request: skip all possibly heavy checks
if ( mFeatureRequest.filterType() == QgsFeatureRequest::FilterNone )
{
setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
if ( loadFeatureAtId( fid ) )
setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
}
else
{
Expand Down
29 changes: 29 additions & 0 deletions tests/src/python/test_qgsattributetablemodel.py
Expand Up @@ -91,6 +91,35 @@ def testRemoveColumns(self):

self.assertEqual(self.am.columnCount(), 1)

def testEdit(self):
fid = 2
field_idx = 1
new_value = 333

# get the same feature from model and layer
feature = self.layer.getFeature(fid)
model_index = self.am.idToIndex(fid)
feature_model = self.am.feature(model_index)

# check that feature from layer and model are sync
self.assertEqual(feature.attribute(field_idx), feature_model.attribute(field_idx))

# change attribute value for a feature and commit
self.layer.startEditing()
self.layer.changeAttributeValue(fid, field_idx, new_value)
self.layer.commitChanges()

# check the feature in layer is good
feature = self.layer.getFeature(fid)
self.assertEqual(feature.attribute(field_idx), new_value)

# get the same feature from model and layer
model_index = self.am.idToIndex(fid)
feature_model = self.am.feature(model_index)

# check that index from layer and model are sync
self.assertEqual(feature.attribute(field_idx), feature_model.attribute(field_idx))


if __name__ == '__main__':
unittest.main()
64 changes: 64 additions & 0 deletions tests/src/python/test_qgsvectorlayer.py
Expand Up @@ -36,6 +36,7 @@
QgsSymbol,
QgsSingleSymbolRenderer,
QgsCoordinateReferenceSystem,
QgsVectorLayerCache,
QgsReadWriteContext,
QgsProject,
QgsUnitTypes,
Expand All @@ -54,6 +55,9 @@
QgsTextFormat,
QgsVectorLayerSelectedFeatureSource,
NULL)
from qgis.gui import (QgsAttributeTableModel,
QgsGui
)
from qgis.testing import start_app, unittest
from featuresourcetestbase import FeatureSourceTestCase
from utilities import unitTestDataPath
Expand Down Expand Up @@ -209,6 +213,7 @@ def getSource(cls):
@classmethod
def setUpClass(cls):
"""Run before all tests"""
QgsGui.editorWidgetRegistry().initEditors()
# Create test layer for FeatureSourceTestCase
cls.source = cls.getSource()

Expand Down Expand Up @@ -1321,6 +1326,65 @@ def test_JoinStats(self):
self.assertEqual(layer.maximumValue(3), 321)
self.assertEqual(set(layer.uniqueValues(3)), set([111, 321]))

def test_valid_join_when_opening_project(self):
join_field = "id"
fid = 4
attr_idx = 4
join_attr_idx = 1
new_value = 33.0

# read project and get layers
myPath = os.path.join(unitTestDataPath(), 'joins.qgs')
rc = QgsProject.instance().read(myPath)

layer = QgsProject.instance().mapLayersByName("polys_with_id")[0]
join_layer = QgsProject.instance().mapLayersByName("polys_overlapping_with_id")[0]

# create an attribute table for the main_layer and the
# joined layer
cache = QgsVectorLayerCache(layer, 100)
am = QgsAttributeTableModel(cache)
am.loadLayer()

join_cache = QgsVectorLayerCache(join_layer, 100)
join_am = QgsAttributeTableModel(join_cache)
join_am.loadLayer()

# check feature value of a joined field from the attribute model
model_index = am.idToIndex(fid)
feature_model = am.feature(model_index)

join_model_index = join_am.idToIndex(fid)
join_feature_model = join_am.feature(join_model_index)

self.assertEqual(feature_model.attribute(attr_idx), join_feature_model.attribute(join_attr_idx))

# change attribute value for a feature of the joined layer
join_layer.startEditing()
join_layer.changeAttributeValue(fid, join_attr_idx, new_value)
join_layer.commitChanges()

# check the feature previously modified
join_model_index = join_am.idToIndex(fid)
join_feature_model = join_am.feature(join_model_index)
self.assertEqual(join_feature_model.attribute(join_attr_idx), new_value)

# recreate a new cache and model to simulate the opening of
# a new attribute table
cache = QgsVectorLayerCache(layer, 100)
am = QgsAttributeTableModel(cache)
am.loadLayer()

# test that the model is up to date with the joined layer
model_index = am.idToIndex(fid)
feature_model = am.feature(model_index)
self.assertEqual(feature_model.attribute(attr_idx), new_value)

# restore value
join_layer.startEditing()
join_layer.changeAttributeValue(fid, join_attr_idx, 7.0)
join_layer.commitChanges()

def testUniqueValue(self):
""" test retrieving unique values """
layer = createLayerWithFivePoints()
Expand Down

3 comments on commit f59d6d7

@andreasneumann
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

I wonder which issue reports are fixed by this bug fix? I ask, because I also have open bugs on the attribute table and would like to see if there are overlaps with my bug reports.

Thanks,
Andreas

@luipir
Copy link
Contributor

@luipir luipir commented on f59d6d7 Jun 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreasneumann it's explained here #4776

@andreasneumann
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah - thanks! That helps!

Please sign in to comment.