def test_draw_text_format(self): r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200)) # need a layer in order to make legend nodes layer = QgsVectorLayer('dummy', 'test', 'memory') layer_tree_layer = QgsLayerTreeLayer(layer) settings = QgsColorRampLegendNodeSettings() tf = QgsTextFormat() tf.setFont(QgsFontUtils.getStandardTestFont('Bold', 18)) tf.setSize(30) tf.setColor(QColor(200, 100, 50)) settings.setTextFormat(tf) node = QgsColorRampLegendNode(layer_tree_layer, r, settings, 5, 10) ls = QgsLegendSettings() item_style = ls.style(QgsLegendStyle.SymbolLabel) item_style.setFont(QgsFontUtils.getStandardTestFont('Bold', 18)) ls.setStyle(QgsLegendStyle.SymbolLabel, item_style) item_context = QgsLayerTreeModelLegendNode.ItemContext() image = QImage(400, 250, QImage.Format_ARGB32) image.fill(QColor(255, 255, 255)) p = QPainter(image) ms = QgsMapSettings() ms.setExtent(QgsRectangle(1, 10, 1, 10)) ms.setOutputSize(image.size()) context = QgsRenderContext.fromMapSettings(ms) context.setPainter(p) context.setScaleFactor(150 / 25.4) # 150 DPI p.scale(context.scaleFactor(), context.scaleFactor()) item_context.context = context item_context.painter = p item_context.top = 1 item_context.columnLeft = 3 item_context.columnRight = 30 item_context.patchSize = QSizeF(12, 40) symbol_size = node.drawSymbol(ls, item_context, 0) node.drawSymbolText(ls, item_context, symbol_size) p.end() self.assertTrue( self.imageCheck('color_ramp_legend_node_text_format_draw', 'color_ramp_legend_node_text_format_draw', image))
def test_icon_horizontal(self): r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200)) # need a layer in order to make legend nodes layer = QgsVectorLayer('dummy', 'test', 'memory') layer_tree_layer = QgsLayerTreeLayer(layer) settings = QgsColorRampLegendNodeSettings() settings.setOrientation(Qt.Horizontal) node = TestColorRampLegend(layer_tree_layer, r, settings, 5, 10) pixmap = node.data(Qt.DecorationRole) im = QImage(pixmap.size(), QImage.Format_ARGB32) im.fill(QColor(255, 255, 255)) p = QPainter(im) p.drawPixmap(0, 0, pixmap) p.end() self.assertTrue(self.imageCheck('color_ramp_legend_node_horizontal_icon', 'color_ramp_legend_node_horizontal_icon', im, 10))
def test_icon_with_settings(self): r = QgsGradientColorRamp(QColor(200, 0, 0, 100), QColor(0, 200, 0, 200)) # need a layer in order to make legend nodes layer = QgsVectorLayer('dummy', 'test', 'memory') layer_tree_layer = QgsLayerTreeLayer(layer) settings = QgsColorRampLegendNodeSettings() format = QgsBasicNumericFormat() format.setShowTrailingZeros(True) format.setNumberDecimalPlaces(3) settings.setNumericFormat(format) settings.setDirection(QgsColorRampLegendNodeSettings.MaximumToMinimum) node = TestColorRampLegend(layer_tree_layer, r, settings, 5, 10) pixmap = node.data(Qt.DecorationRole) im = QImage(pixmap.size(), QImage.Format_ARGB32) im.fill(QColor(255, 255, 255)) p = QPainter(im) p.drawPixmap(0, 0, pixmap) p.end() self.assertTrue(self.imageCheck('color_ramp_legend_node_settings_icon', 'color_ramp_legend_node_settings_icon', im, 10))
def test_settings(self): settings = QgsColorRampLegendNodeSettings() settings.setDirection(QgsColorRampLegendNodeSettings.MaximumToMinimum) self.assertEqual(settings.direction(), QgsColorRampLegendNodeSettings.MaximumToMinimum) settings.setMinimumLabel('min') self.assertEqual(settings.minimumLabel(), 'min') settings.setMaximumLabel('max') self.assertEqual(settings.maximumLabel(), 'max') settings.setPrefix('pref') self.assertEqual(settings.prefix(), 'pref') settings.setSuffix('suff') self.assertEqual(settings.suffix(), 'suff') self.assertEqual(settings.orientation(), Qt.Vertical) settings.setOrientation(Qt.Horizontal) self.assertEqual(settings.orientation(), Qt.Horizontal) self.assertFalse(settings.textFormat().isValid()) tf = QgsTextFormat() tf.setSize(13) settings.setTextFormat(tf) self.assertEqual(settings.textFormat().size(), 13) self.assertIsNotNone(settings.numericFormat()) settings.setNumericFormat(QgsBearingNumericFormat()) self.assertIsInstance(settings.numericFormat(), QgsBearingNumericFormat) settings2 = QgsColorRampLegendNodeSettings(settings) self.assertEqual(settings2.direction(), QgsColorRampLegendNodeSettings.MaximumToMinimum) self.assertEqual(settings2.minimumLabel(), 'min') self.assertEqual(settings2.maximumLabel(), 'max') self.assertIsInstance(settings2.numericFormat(), QgsBearingNumericFormat) self.assertEqual(settings2.prefix(), 'pref') self.assertEqual(settings2.suffix(), 'suff') self.assertEqual(settings2.textFormat().size(), 13) self.assertEqual(settings2.orientation(), Qt.Horizontal) settings2.setTextFormat(QgsTextFormat()) settings2a = QgsColorRampLegendNodeSettings(settings2) self.assertFalse(settings2a.textFormat().isValid()) doc = QDomDocument("testdoc") elem = doc.createElement('test') settings.writeXml(doc, elem, QgsReadWriteContext()) settings3 = QgsColorRampLegendNodeSettings() settings3.readXml(elem, QgsReadWriteContext()) self.assertEqual(settings3.direction(), QgsColorRampLegendNodeSettings.MaximumToMinimum) self.assertEqual(settings3.minimumLabel(), 'min') self.assertEqual(settings3.maximumLabel(), 'max') self.assertIsInstance(settings3.numericFormat(), QgsBearingNumericFormat) self.assertEqual(settings3.prefix(), 'pref') self.assertEqual(settings3.suffix(), 'suff') self.assertEqual(settings3.textFormat().size(), 13) self.assertEqual(settings3.orientation(), Qt.Horizontal) # no text format elem = doc.createElement('test2') settings2.writeXml(doc, elem, QgsReadWriteContext()) settings3a = QgsColorRampLegendNodeSettings() settings3a.readXml(elem, QgsReadWriteContext()) self.assertFalse(settings3a.textFormat().isValid())