Skip to content
Permalink
Browse files

Merge pull request #4776 from pblottiere/bugfix_attributetable

[bugfix] attribute table
  • Loading branch information
Hugo Mercier
Hugo Mercier committed Jun 28, 2017
2 parents 9f5e33a + b354986 commit f59d6d79620b5c463a35322000768c146756cb98
@@ -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;
}
}
@@ -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
{
@@ -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()
@@ -36,6 +36,7 @@
QgsSymbol,
QgsSingleSymbolRenderer,
QgsCoordinateReferenceSystem,
QgsVectorLayerCache,
QgsReadWriteContext,
QgsProject,
QgsUnitTypes,
@@ -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
@@ -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()

@@ -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()

3 comments on commit f59d6d7

@andreasneumann

This comment has been minimized.

Copy link
Member

@andreasneumann andreasneumann replied Jun 28, 2017

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

This comment has been minimized.

Copy link
Contributor

@luipir luipir replied Jun 28, 2017

@andreasneumann it's explained here #4776

@andreasneumann

This comment has been minimized.

Copy link
Member

@andreasneumann andreasneumann replied Jun 28, 2017

ah - thanks! That helps!

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