Skip to content
Permalink
Browse files

Merge pull request #40699 from suricactus/attr_tbl_cond_fmt

Refresh the attribute table styles when feature modified programmatically
  • Loading branch information
m-kuhn committed Dec 21, 2020
2 parents e10e42d + 44200e5 commit f503cb5d86481aea6c76f48f740af930bf456202
@@ -766,11 +766,11 @@ bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &
if ( !index.isValid() || index.column() >= mFieldCount || role != Qt::EditRole || !layer()->isEditable() )
return false;

mRowStylesMap.remove( mFeat.id() );

if ( !layer()->isModified() )
return false;

mRowStylesMap.remove( mFeat.id() );

return true;
}

@@ -19,9 +19,11 @@
QgsGeometry,
QgsPointXY,
QgsVectorLayer,
QgsVectorLayerCache
QgsVectorLayerCache,
QgsConditionalStyle,
)

from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QColor
from qgis.testing import (start_app,
unittest
)
@@ -118,6 +120,45 @@ def testEdit(self):
# check that index from layer and model are sync
self.assertEqual(feature.attribute(field_idx), feature_model.attribute(field_idx))

def testStyle(self):
style_threshold = 2
color = QColor(133, 133, 133)
style = QgsConditionalStyle()
style.setRule(f'"fldint" <= {style_threshold}')
style.setTextColor(color)
self.layer.conditionalStyles().setRowStyles([style])

for f in self.layer.getFeatures():
model_index = self.am.idToIndex(f.id())
text_color = self.am.data(model_index, Qt.TextColorRole)

if f['fldint'] <= style_threshold:
self.assertEqual(text_color, color)
else:
self.assertIsNone(text_color)

self.assertTrue(self.layer.startEditing())

feature1 = self.layer.getFeature(2)
feature1['fldint'] = style_threshold + 1
feature2 = self.layer.getFeature(8)
feature2['fldint'] = style_threshold

self.assertTrue(self.layer.updateFeature(feature1))
self.assertTrue(self.layer.updateFeature(feature2))
self.assertTrue(self.layer.commitChanges())

for f in self.layer.getFeatures():
model_index = self.am.idToIndex(f.id())
text_color = self.am.data(model_index, Qt.TextColorRole)

if f['fldint'] <= style_threshold:
self.assertEqual(color, text_color, f'Feature {f.id()} should have color')
else:
self.assertIsNone(text_color, f'Feature {f.id()} should have no color')

self.layer.conditionalStyles().setRowStyles([])


if __name__ == '__main__':
unittest.main()

0 comments on commit f503cb5

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