Skip to content

Commit 010c2f4

Browse files
committedSep 12, 2018
Fix model setData implementation, allow renaming symbols through model
1 parent d7edeac commit 010c2f4

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
 

‎src/core/symbology/qgsstylemodel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ bool QgsStyleModel::setData( const QModelIndex &index, const QVariant &value, in
150150
: mRampNames.at( index.row() - mSymbolNames.size() );
151151
const QString newName = value.toString();
152152

153-
return true;
153+
return isColorRamp
154+
? mStyle->renameColorRamp( name, newName )
155+
: mStyle->renameSymbol( name, newName );
154156
}
155157

156158
case Tags:

‎tests/src/python/test_qgsstylemodel.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
QgsStyle,
2424
QgsStyleProxyModel)
2525
from qgis.testing import start_app, unittest
26-
from qgis.PyQt.QtCore import Qt, QSize
26+
from qgis.PyQt.QtCore import Qt, QSize, QModelIndex
2727
from qgis.PyQt.QtGui import QColor
2828

2929
start_app()
@@ -758,6 +758,33 @@ def testIconSize(self):
758758
self.assertEqual(icon.actualSize(QSize(225, 225)), QSize(200, 180))
759759
model.setProperty('icon_sizes', None)
760760

761+
def testSetData(self):
762+
"""
763+
Test model set data
764+
"""
765+
style = QgsStyle()
766+
style.createMemoryDatabase()
767+
768+
symbol_a = createMarkerSymbol()
769+
symbol_a.setColor(QColor(255, 10, 10))
770+
self.assertTrue(style.addSymbol('a', symbol_a, True))
771+
ramp_a = QgsLimitedRandomColorRamp(5)
772+
self.assertTrue(style.addColorRamp('ramp a', ramp_a, True))
773+
774+
model = QgsStyleModel(style)
775+
self.assertEqual(model.rowCount(), 2)
776+
777+
self.assertEqual(style.symbolNames(), ['a'])
778+
779+
self.assertFalse(model.setData(QModelIndex(), 'b', Qt.EditRole))
780+
self.assertFalse(model.setData(model.index(0, 1), 'b', Qt.EditRole))
781+
self.assertTrue(model.setData(model.index(0, 0), 'new symbol name', Qt.EditRole))
782+
self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'new symbol name')
783+
self.assertEqual(style.symbolNames(), ['new symbol name'])
784+
self.assertTrue(model.setData(model.index(1, 0), 'ramp new name', Qt.EditRole))
785+
self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'ramp new name')
786+
self.assertEqual(style.colorRampNames(), ['ramp new name'])
787+
761788

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

0 commit comments

Comments
 (0)
Please sign in to comment.