Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refresh the attribute table styles when feature modified programmatic…
…ally
  • Loading branch information
m-kuhn authored and nyalldawson committed Dec 22, 2020
1 parent bb3605b commit 0ba9655
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -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;
}

Expand Down
45 changes: 43 additions & 2 deletions tests/src/python/test_qgsattributetablemodel.py
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 0ba9655

Please sign in to comment.