def test_add_delete_symbols(self): style = QgsStyle() style.createMemoryDatabase() model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 0) symbol = createMarkerSymbol() self.assertTrue(style.addSymbol('a', symbol, True)) self.assertEqual(model.rowCount(), 1) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') symbol = createMarkerSymbol() self.assertTrue(style.addSymbol('c', symbol, True)) self.assertEqual(model.rowCount(), 2) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'c') symbol = createMarkerSymbol() self.assertTrue(style.addSymbol('b', symbol, True)) self.assertEqual(model.rowCount(), 3) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'b') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'c') symbol = createMarkerSymbol() self.assertTrue(style.addSymbol('1', symbol, True)) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), '1') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'b') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'c') self.assertFalse(style.removeSymbol('xxxx')) self.assertEqual(model.rowCount(), 4) self.assertTrue(style.removeSymbol('b')) self.assertEqual(model.rowCount(), 3) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), '1') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'c') self.assertTrue(style.removeSymbol('1')) self.assertEqual(model.rowCount(), 2) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'c') self.assertTrue(style.removeSymbol('c')) self.assertEqual(model.rowCount(), 1) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertTrue(style.removeSymbol('a')) self.assertEqual(model.rowCount(), 0)
def testSetData(self): """ Test model set data """ style = QgsStyle() style.createMemoryDatabase() symbol_a = createMarkerSymbol() symbol_a.setColor(QColor(255, 10, 10)) self.assertTrue(style.addSymbol('a', symbol_a, True)) ramp_a = QgsLimitedRandomColorRamp(5) self.assertTrue(style.addColorRamp('ramp a', ramp_a, True)) model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 2) self.assertEqual(style.symbolNames(), ['a']) self.assertFalse(model.setData(QModelIndex(), 'b', Qt.EditRole)) self.assertFalse(model.setData(model.index(0, 1), 'b', Qt.EditRole)) self.assertTrue(model.setData(model.index(0, 0), 'new symbol name', Qt.EditRole)) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'new symbol name') self.assertEqual(style.symbolNames(), ['new symbol name']) self.assertTrue(model.setData(model.index(1, 0), 'ramp new name', Qt.EditRole)) self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'ramp new name') self.assertEqual(style.colorRampNames(), ['ramp new name'])
def test_style_with_ramps(self): style = QgsStyle() style.createMemoryDatabase() # style with only ramps ramp_a = QgsLimitedRandomColorRamp(5) self.assertTrue(style.addColorRamp('a', ramp_a, True)) style.tagSymbol(QgsStyle.ColorrampEntity, 'a', ['tag 1', 'tag 2']) symbol_B = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('B ', symbol_B, True)) symbol_b = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('b', symbol_b, True)) symbol_C = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('C', symbol_C, True)) style.tagSymbol(QgsStyle.ColorrampEntity, 'C', ['tag 3']) symbol_C = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp(' ----c/- ', symbol_C, True)) model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 5) self.assertEqual(model.columnCount(), 2) self.assertTrue(model.index(0, 0).isValid()) self.assertFalse(model.index(10, 0).isValid()) self.assertFalse(model.index(0, 10).isValid()) self.assertFalse(model.parent(model.index(0, 0)).isValid()) for role in (Qt.DisplayRole, Qt.EditRole): self.assertIsNone(model.data(model.index(-1, 0), role)) self.assertIsNone(model.data(model.index(-1, 1), role)) self.assertEqual(model.data(model.index(0, 0), role), ' ----c/- ') self.assertFalse(model.data(model.index(0, 1), role)) self.assertIsNone(model.data(model.index(0, 2), role)) self.assertIsNone(model.data(model.index(0, -1), role)) self.assertEqual(model.data(model.index(1, 0), role), 'B ') self.assertFalse(model.data(model.index(1, 1), role)) self.assertEqual(model.data(model.index(2, 0), role), 'C') self.assertEqual(model.data(model.index(2, 1), role), 'tag 3') self.assertEqual(model.data(model.index(3, 0), role), 'a') self.assertEqual(model.data(model.index(3, 1), role), 'tag 1, tag 2') self.assertEqual(model.data(model.index(4, 0), role), 'b') self.assertFalse(model.data(model.index(4, 1), role)) self.assertIsNone(model.data(model.index(5, 0), role)) self.assertIsNone(model.data(model.index(5, 1), role)) # decorations self.assertIsNone(model.data(model.index(-1, 0), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(0, 1), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(5, 0), Qt.DecorationRole)) self.assertFalse(model.data(model.index(0, 0), Qt.DecorationRole).isNull()) self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity) self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity) self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity)
def test_add_remove_ramps(self): style = QgsStyle() style.createMemoryDatabase() model = QgsStyleModel(style) symbol = createMarkerSymbol() self.assertTrue(style.addSymbol('a', symbol, True)) symbol = createMarkerSymbol() self.assertTrue(style.addSymbol('c', symbol, True)) self.assertEqual(model.rowCount(), 2) ramp_a = QgsLimitedRandomColorRamp(5) self.assertTrue(style.addColorRamp('ramp a', ramp_a, True)) self.assertEqual(model.rowCount(), 3) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'c') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp a') symbol_B = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('ramp c', symbol_B, True)) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'c') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp a') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp c') symbol_b = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('ramp b', symbol_b, True)) self.assertEqual(model.rowCount(), 5) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'c') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp a') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp b') self.assertEqual(model.data(model.index(4, 0), Qt.DisplayRole), 'ramp c') self.assertTrue(style.removeColorRamp('ramp a')) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'c') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp b') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp c')
def testIconSize(self): """ Test that model has responsive icon sizes for decorations """ style = QgsStyle() style.createMemoryDatabase() symbol_a = createMarkerSymbol() symbol_a.setColor(QColor(255, 10, 10)) self.assertTrue(style.addSymbol('a', symbol_a, True)) ramp_a = QgsLimitedRandomColorRamp(5) self.assertTrue(style.addColorRamp('ramp a', ramp_a, True)) for i in range(2): model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 2) icon = model.data(model.index(i, 0), Qt.DecorationRole) # by default, only 24x24 icon self.assertEqual(icon.availableSizes(), [QSize(24, 24)]) self.assertEqual(icon.actualSize(QSize(10, 10)), QSize(10, 10)) self.assertEqual(icon.actualSize(QSize(24, 24)), QSize(24, 24)) self.assertEqual(icon.actualSize(QSize(90, 90)), QSize(24, 24)) model.addDesiredIconSize(QSize(24, 24)) model.addDesiredIconSize(QSize(100, 90)) icon = model.data(model.index(i, 0), Qt.DecorationRole) self.assertEqual(icon.availableSizes(), [QSize(24, 24), QSize(100, 90)]) self.assertEqual(icon.actualSize(QSize(10, 10)), QSize(10, 10)) self.assertEqual(icon.actualSize(QSize(24, 24)), QSize(24, 24)) self.assertEqual(icon.actualSize(QSize(25, 25)), QSize(25, 22)) self.assertEqual(icon.actualSize(QSize(90, 90)), QSize(90, 81)) self.assertEqual(icon.actualSize(QSize(125, 125)), QSize(100, 90)) model = QgsStyleModel(style) model.addDesiredIconSize(QSize(100, 90)) model.addDesiredIconSize(QSize(200, 180)) icon = model.data(model.index(i, 0), Qt.DecorationRole) self.assertEqual(icon.availableSizes(), [QSize(100, 90), QSize(200, 180)]) self.assertEqual(icon.actualSize(QSize(24, 24)), QSize(24, 21)) self.assertEqual(icon.actualSize(QSize(25, 25)), QSize(25, 22)) self.assertEqual(icon.actualSize(QSize(90, 90)), QSize(90, 81)) self.assertEqual(icon.actualSize(QSize(125, 125)), QSize(125, 112)) self.assertEqual(icon.actualSize(QSize(225, 225)), QSize(200, 180))
def test_style_with_symbols(self): style = QgsStyle() style.createMemoryDatabase() # style with only symbols symbol_a = createMarkerSymbol() symbol_a.setColor(QColor(255, 10, 10)) self.assertTrue(style.addSymbol('a', symbol_a, True)) style.tagSymbol(QgsStyle.SymbolEntity, 'a', ['tag 1', 'tag 2']) symbol_B = createMarkerSymbol() symbol_B.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('B ', symbol_B, True)) symbol_b = createFillSymbol() symbol_b.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('b', symbol_b, True)) symbol_C = createLineSymbol() symbol_C.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('C', symbol_C, True)) style.tagSymbol(QgsStyle.SymbolEntity, 'C', ['tag 3']) symbol_C = createMarkerSymbol() symbol_C.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol(' ----c/- ', symbol_C, True)) model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 5) self.assertEqual(model.columnCount(), 2) self.assertEqual(model.headerData(0, Qt.Horizontal), 'Name') self.assertEqual(model.headerData(1, Qt.Horizontal), 'Tags') self.assertTrue(model.index(0, 0).isValid()) self.assertFalse(model.index(10, 0).isValid()) self.assertFalse(model.index(0, 10).isValid()) self.assertFalse(model.parent(model.index(0, 0)).isValid()) self.assertFalse(model.flags(model.index(-1, 0)) & Qt.ItemIsEditable) self.assertFalse(model.flags(model.index(5, 0)) & Qt.ItemIsEditable) self.assertFalse(model.flags(model.index(0, 1)) & Qt.ItemIsEditable) self.assertTrue(model.flags(model.index(0, 0)) & Qt.ItemIsEditable) for role in (Qt.DisplayRole, Qt.EditRole): self.assertIsNone(model.data(model.index(-1, 0), role)) self.assertIsNone(model.data(model.index(-1, 1), role)) self.assertEqual(model.data(model.index(0, 0), role), ' ----c/- ') self.assertFalse(model.data(model.index(0, 1), role)) self.assertIsNone(model.data(model.index(0, 2), role)) self.assertIsNone(model.data(model.index(0, -1), role)) self.assertEqual(model.data(model.index(1, 0), role), 'B ') self.assertFalse(model.data(model.index(1, 1), role)) self.assertEqual(model.data(model.index(2, 0), role), 'C') self.assertEqual(model.data(model.index(2, 1), role), 'tag 3') self.assertEqual(model.data(model.index(3, 0), role), 'a') self.assertEqual(model.data(model.index(3, 1), role), 'tag 1, tag 2') self.assertEqual(model.data(model.index(4, 0), role), 'b') self.assertFalse(model.data(model.index(4, 1), role)) self.assertIsNone(model.data(model.index(5, 0), role)) self.assertIsNone(model.data(model.index(5, 1), role)) # decorations self.assertIsNone(model.data(model.index(-1, 0), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(0, 1), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(5, 0), Qt.DecorationRole)) self.assertFalse( model.data(model.index(0, 0), Qt.DecorationRole).isNull()) self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity)
def test_renamed(self): style = QgsStyle() style.createMemoryDatabase() model = QgsStyleModel(style) symbol = createMarkerSymbol() self.assertTrue(style.addSymbol('a', symbol, True)) symbol = createMarkerSymbol() self.assertTrue(style.addSymbol('c', symbol, True)) ramp_a = QgsLimitedRandomColorRamp(5) self.assertTrue(style.addColorRamp('ramp a', ramp_a, True)) symbol_B = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('ramp c', symbol_B, True)) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'a') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'c') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp a') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp c') self.assertTrue(style.renameSymbol('a', 'b')) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'b') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'c') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp a') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp c') self.assertTrue(style.renameSymbol('b', 'd')) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'c') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'd') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp a') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp c') self.assertTrue(style.renameSymbol('d', 'e')) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'c') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'e') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp a') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp c') self.assertTrue(style.renameSymbol('c', 'f')) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'e') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'f') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp a') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp c') self.assertTrue(style.renameColorRamp('ramp a', 'ramp b')) self.assertEqual(model.rowCount(), 4) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'e') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'f') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp b') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp c') self.assertTrue(style.renameColorRamp('ramp b', 'ramp d')) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'e') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'f') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp c') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp d') self.assertTrue(style.renameColorRamp('ramp d', 'ramp e')) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'e') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'f') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp c') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp e') self.assertTrue(style.renameColorRamp('ramp c', 'ramp f')) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'e') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'f') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'ramp e') self.assertEqual(model.data(model.index(3, 0), Qt.DisplayRole), 'ramp f')
def test_mixed_style(self): """ Test style with both symbols and ramps """ style = QgsStyle() style.createMemoryDatabase() # style with only symbols symbol_a = createMarkerSymbol() symbol_a.setColor(QColor(255, 10, 10)) self.assertTrue(style.addSymbol('a', symbol_a, True)) style.tagSymbol(QgsStyle.SymbolEntity, 'a', ['tag 1', 'tag 2']) symbol_B = createMarkerSymbol() symbol_B.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('B ', symbol_B, True)) symbol_b = createFillSymbol() symbol_b.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('b', symbol_b, True)) symbol_C = createLineSymbol() symbol_C.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('C', symbol_C, True)) style.tagSymbol(QgsStyle.SymbolEntity, 'C', ['tag 3']) symbol_C = createMarkerSymbol() symbol_C.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol(' ----c/- ', symbol_C, True)) ramp_a = QgsLimitedRandomColorRamp(5) self.assertTrue(style.addColorRamp('ramp a', ramp_a, True)) style.tagSymbol(QgsStyle.ColorrampEntity, 'ramp a', ['tag 1', 'tag 2']) symbol_B = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('ramp B ', symbol_B, True)) symbol_b = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('ramp c', symbol_b, True)) model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 8) self.assertEqual(model.columnCount(), 2) self.assertTrue(model.index(0, 0).isValid()) self.assertFalse(model.index(10, 0).isValid()) self.assertFalse(model.index(0, 10).isValid()) self.assertFalse(model.parent(model.index(0, 0)).isValid()) for role in (Qt.DisplayRole, Qt.EditRole): self.assertIsNone(model.data(model.index(-1, 0), role)) self.assertIsNone(model.data(model.index(-1, 1), role)) self.assertEqual(model.data(model.index(0, 0), role), ' ----c/- ') self.assertFalse(model.data(model.index(0, 1), role)) self.assertIsNone(model.data(model.index(0, 2), role)) self.assertIsNone(model.data(model.index(0, -1), role)) self.assertEqual(model.data(model.index(1, 0), role), 'B ') self.assertFalse(model.data(model.index(1, 1), role)) self.assertEqual(model.data(model.index(2, 0), role), 'C') self.assertEqual(model.data(model.index(2, 1), role), 'tag 3') self.assertEqual(model.data(model.index(3, 0), role), 'a') self.assertEqual(model.data(model.index(3, 1), role), 'tag 1, tag 2') self.assertEqual(model.data(model.index(4, 0), role), 'b') self.assertFalse(model.data(model.index(4, 1), role)) self.assertEqual(model.data(model.index(5, 0), role), 'ramp B ') self.assertFalse(model.data(model.index(5, 1), role)) self.assertEqual(model.data(model.index(6, 0), role), 'ramp a') self.assertEqual(model.data(model.index(6, 1), role), 'tag 1, tag 2') self.assertEqual(model.data(model.index(7, 0), role), 'ramp c') self.assertFalse(model.data(model.index(7, 1), role)) self.assertIsNone(model.data(model.index(8, 0), role)) self.assertIsNone(model.data(model.index(8, 1), role)) # decorations self.assertIsNone(model.data(model.index(-1, 0), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(0, 1), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(8, 0), Qt.DecorationRole)) self.assertFalse( model.data(model.index(0, 0), Qt.DecorationRole).isNull()) self.assertFalse( model.data(model.index(5, 0), Qt.DecorationRole).isNull()) self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(5, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity) self.assertEqual(model.data(model.index(6, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity) self.assertEqual(model.data(model.index(7, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity)
def test_style_with_ramps(self): style = QgsStyle() style.createMemoryDatabase() # style with only ramps ramp_a = QgsLimitedRandomColorRamp(5) self.assertTrue(style.addColorRamp('a', ramp_a, True)) style.tagSymbol(QgsStyle.ColorrampEntity, 'a', ['tag 1', 'tag 2']) symbol_B = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('B ', symbol_B, True)) symbol_b = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('b', symbol_b, True)) symbol_C = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('C', symbol_C, True)) style.tagSymbol(QgsStyle.ColorrampEntity, 'C', ['tag 3']) symbol_C = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp(' ----c/- ', symbol_C, True)) model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 5) self.assertEqual(model.columnCount(), 2) self.assertTrue(model.index(0, 0).isValid()) self.assertFalse(model.index(10, 0).isValid()) self.assertFalse(model.index(0, 10).isValid()) self.assertFalse(model.parent(model.index(0, 0)).isValid()) for role in (Qt.DisplayRole, Qt.EditRole): self.assertIsNone(model.data(model.index(-1, 0), role)) self.assertIsNone(model.data(model.index(-1, 1), role)) self.assertEqual(model.data(model.index(0, 0), role), ' ----c/- ') self.assertFalse(model.data(model.index(0, 1), role)) self.assertIsNone(model.data(model.index(0, 2), role)) self.assertIsNone(model.data(model.index(0, -1), role)) self.assertEqual(model.data(model.index(1, 0), role), 'B ') self.assertFalse(model.data(model.index(1, 1), role)) self.assertEqual(model.data(model.index(2, 0), role), 'C') self.assertEqual(model.data(model.index(2, 1), role), 'tag 3') self.assertEqual(model.data(model.index(3, 0), role), 'a') self.assertEqual(model.data(model.index(3, 1), role), 'tag 1, tag 2') self.assertEqual(model.data(model.index(4, 0), role), 'b') self.assertFalse(model.data(model.index(4, 1), role)) self.assertIsNone(model.data(model.index(5, 0), role)) self.assertIsNone(model.data(model.index(5, 1), role)) # decorations self.assertIsNone(model.data(model.index(-1, 0), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(0, 1), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(5, 0), Qt.DecorationRole)) self.assertFalse( model.data(model.index(0, 0), Qt.DecorationRole).isNull()) self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity) self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity) self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity)
def test_style_with_symbols(self): style = QgsStyle() style.createMemoryDatabase() # style with only symbols symbol_a = createMarkerSymbol() symbol_a.setColor(QColor(255, 10, 10)) self.assertTrue(style.addSymbol('a', symbol_a, True)) style.tagSymbol(QgsStyle.SymbolEntity, 'a', ['tag 1', 'tag 2']) symbol_B = createMarkerSymbol() symbol_B.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('B ', symbol_B, True)) symbol_b = createFillSymbol() symbol_b.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('b', symbol_b, True)) symbol_C = createLineSymbol() symbol_C.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('C', symbol_C, True)) style.tagSymbol(QgsStyle.SymbolEntity, 'C', ['tag 3']) symbol_C = createMarkerSymbol() symbol_C.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol(' ----c/- ', symbol_C, True)) model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 5) self.assertEqual(model.columnCount(), 2) self.assertEqual(model.headerData(0, Qt.Horizontal), 'Name') self.assertEqual(model.headerData(1, Qt.Horizontal), 'Tags') self.assertTrue(model.index(0, 0).isValid()) self.assertFalse(model.index(10, 0).isValid()) self.assertFalse(model.index(0, 10).isValid()) self.assertFalse(model.parent(model.index(0, 0)).isValid()) self.assertFalse(model.flags(model.index(-1, 0)) & Qt.ItemIsEditable) self.assertFalse(model.flags(model.index(5, 0)) & Qt.ItemIsEditable) self.assertFalse(model.flags(model.index(0, 1)) & Qt.ItemIsEditable) self.assertTrue(model.flags(model.index(0, 0)) & Qt.ItemIsEditable) for role in (Qt.DisplayRole, Qt.EditRole): self.assertIsNone(model.data(model.index(-1, 0), role)) self.assertIsNone(model.data(model.index(-1, 1), role)) self.assertEqual(model.data(model.index(0, 0), role), ' ----c/- ') self.assertFalse(model.data(model.index(0, 1), role)) self.assertIsNone(model.data(model.index(0, 2), role)) self.assertIsNone(model.data(model.index(0, -1), role)) self.assertEqual(model.data(model.index(1, 0), role), 'B ') self.assertFalse(model.data(model.index(1, 1), role)) self.assertEqual(model.data(model.index(2, 0), role), 'C') self.assertEqual(model.data(model.index(2, 1), role), 'tag 3') self.assertEqual(model.data(model.index(3, 0), role), 'a') self.assertEqual(model.data(model.index(3, 1), role), 'tag 1, tag 2') self.assertEqual(model.data(model.index(4, 0), role), 'b') self.assertFalse(model.data(model.index(4, 1), role)) self.assertIsNone(model.data(model.index(5, 0), role)) self.assertIsNone(model.data(model.index(5, 1), role)) # decorations self.assertIsNone(model.data(model.index(-1, 0), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(0, 1), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(5, 0), Qt.DecorationRole)) self.assertFalse(model.data(model.index(0, 0), Qt.DecorationRole).isNull()) self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity)
def test_mixed_style(self): """ Test style with both symbols and ramps """ style = QgsStyle() style.createMemoryDatabase() # style with only symbols symbol_a = createMarkerSymbol() symbol_a.setColor(QColor(255, 10, 10)) self.assertTrue(style.addSymbol('a', symbol_a, True)) style.tagSymbol(QgsStyle.SymbolEntity, 'a', ['tag 1', 'tag 2']) symbol_B = createMarkerSymbol() symbol_B.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('B ', symbol_B, True)) symbol_b = createFillSymbol() symbol_b.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('b', symbol_b, True)) symbol_C = createLineSymbol() symbol_C.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol('C', symbol_C, True)) style.tagSymbol(QgsStyle.SymbolEntity, 'C', ['tag 3']) symbol_C = createMarkerSymbol() symbol_C.setColor(QColor(10, 255, 10)) self.assertTrue(style.addSymbol(' ----c/- ', symbol_C, True)) ramp_a = QgsLimitedRandomColorRamp(5) self.assertTrue(style.addColorRamp('ramp a', ramp_a, True)) style.tagSymbol(QgsStyle.ColorrampEntity, 'ramp a', ['tag 1', 'tag 2']) symbol_B = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('ramp B ', symbol_B, True)) symbol_b = QgsLimitedRandomColorRamp() self.assertTrue(style.addColorRamp('ramp c', symbol_b, True)) model = QgsStyleModel(style) self.assertEqual(model.rowCount(), 8) self.assertEqual(model.columnCount(), 2) self.assertTrue(model.index(0, 0).isValid()) self.assertFalse(model.index(10, 0).isValid()) self.assertFalse(model.index(0, 10).isValid()) self.assertFalse(model.parent(model.index(0, 0)).isValid()) for role in (Qt.DisplayRole, Qt.EditRole): self.assertIsNone(model.data(model.index(-1, 0), role)) self.assertIsNone(model.data(model.index(-1, 1), role)) self.assertEqual(model.data(model.index(0, 0), role), ' ----c/- ') self.assertFalse(model.data(model.index(0, 1), role)) self.assertIsNone(model.data(model.index(0, 2), role)) self.assertIsNone(model.data(model.index(0, -1), role)) self.assertEqual(model.data(model.index(1, 0), role), 'B ') self.assertFalse(model.data(model.index(1, 1), role)) self.assertEqual(model.data(model.index(2, 0), role), 'C') self.assertEqual(model.data(model.index(2, 1), role), 'tag 3') self.assertEqual(model.data(model.index(3, 0), role), 'a') self.assertEqual(model.data(model.index(3, 1), role), 'tag 1, tag 2') self.assertEqual(model.data(model.index(4, 0), role), 'b') self.assertFalse(model.data(model.index(4, 1), role)) self.assertEqual(model.data(model.index(5, 0), role), 'ramp B ') self.assertFalse(model.data(model.index(5, 1), role)) self.assertEqual(model.data(model.index(6, 0), role), 'ramp a') self.assertEqual(model.data(model.index(6, 1), role), 'tag 1, tag 2') self.assertEqual(model.data(model.index(7, 0), role), 'ramp c') self.assertFalse(model.data(model.index(7, 1), role)) self.assertIsNone(model.data(model.index(8, 0), role)) self.assertIsNone(model.data(model.index(8, 1), role)) # decorations self.assertIsNone(model.data(model.index(-1, 0), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(0, 1), Qt.DecorationRole)) self.assertIsNone(model.data(model.index(8, 0), Qt.DecorationRole)) self.assertFalse(model.data(model.index(0, 0), Qt.DecorationRole).isNull()) self.assertFalse(model.data(model.index(5, 0), Qt.DecorationRole).isNull()) self.assertEqual(model.data(model.index(0, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(1, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(4, 0), QgsStyleModel.TypeRole), QgsStyle.SymbolEntity) self.assertEqual(model.data(model.index(5, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity) self.assertEqual(model.data(model.index(6, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity) self.assertEqual(model.data(model.index(7, 0), QgsStyleModel.TypeRole), QgsStyle.ColorrampEntity)