def create_join_label_format(precision): format = QgsRendererRangeV2LabelFormat() template = "%1 - %2 metres" format.setFormat(template) format.setPrecision(precision) format.setTrimTrailingZeroes(True) return format
def testQgsRendererRangeV2LabelFormat_1(self): """Test QgsRendererRangeV2LabelFormat getter/setter functions""" format = QgsRendererRangeV2LabelFormat() self.assertTrue(format, "QgsRendererRangeV2LabelFomat construction failed") template = "%1 - %2 metres" precision = 5 format.setFormat(template) self.assertEqual(format.format(), template, "Format getter/setter failed") format.setPrecision(precision) self.assertEqual(format.precision(), precision, "Precision getter/setter failed") format.setTrimTrailingZeroes(True) self.assertTrue(format.trimTrailingZeroes(), "TrimTrailingZeroes getter/setter failed") format.setTrimTrailingZeroes(False) self.assertFalse(format.trimTrailingZeroes(), "TrimTrailingZeroes getter/setter failed") minprecision = -6 maxprecision = 15 self.assertEqual(QgsRendererRangeV2LabelFormat.MinPrecision, minprecision, "Minimum precision != -6") self.assertEqual(QgsRendererRangeV2LabelFormat.MaxPrecision, maxprecision, "Maximum precision != 15") format.setPrecision(-10) self.assertEqual(format.precision(), minprecision, "Minimum precision not enforced") format.setPrecision(20) self.assertEqual(format.precision(), maxprecision, "Maximum precision not enforced")
def createLabelFormat(): format = QgsRendererRangeV2LabelFormat() template = "%1 - %2 meters" precision = 5 format.setFormat(template) format.setPrecision(precision) format.setTrimTrailingZeroes(True) return format
def testQgsRendererRangeV2LabelFormat_2(self): """Test QgsRendererRangeV2LabelFormat number format""" format = QgsRendererRangeV2LabelFormat() # Tests have precision, trim, value, expected # (Note: Not sure what impact of locale is on these tests) tests = ( (2, False, 1.0, '1.00'), (2, True, 1.0, '1'), (2, False, 1.234, '1.23'), (2, True, 1.234, '1.23'), (2, False, 1.236, '1.24'), (2, False, -1.236, '-1.24'), (2, False, -0.004, '0.00'), (2, True, 1.002, '1'), (2, True, 1.006, '1.01'), (2, True, 1.096, '1.1'), (3, True, 1.096, '1.096'), (-2, True, 1496.45, '1500'), (-2, True, 149.45, '100'), (-2, True, 79.45, '100'), (-2, True, 49.45, '0'), (-2, True, -49.45, '0'), (-2, True, -149.45, '-100'), ) for f in tests: precision, trim, value, expected = f format.setPrecision(precision) format.setTrimTrailingZeroes(trim) result = format.formatNumber(value) self.assertEqual( result, expected, "Number format error {0}:{1}:{2} => {3}".format( precision, trim, value, result)) # Label tests - label format, expected result. # Labels will be evaluated with lower=1.23 upper=2.34, precision=2 ltests = ( ("%1 - %2", "1.23 - 2.34"), ("%1", "1.23"), ("%2", "2.34"), ("%2%", "2.34%"), ("%1%1", "1.231.23"), ("from %1 to %2 metres", "from 1.23 to 2.34 metres"), ("from %2 to %1 metres", "from 2.34 to 1.23 metres"), ) format.setPrecision(2) format.setTrimTrailingZeroes(False) lower = 1.232 upper = 2.339 for t in ltests: label, expected = t format.setFormat(label) result = format.labelForLowerUpper(lower, upper) self.assertEqual( result, expected, "Label format error {0} => {1}".format(label, result)) range = QgsRendererRangeV2() range.setLowerValue(lower) range.setUpperValue(upper) label = ltests[0][0] format.setFormat(label) result = format.labelForRange(range) self.assertEqual( result, ltests[0][1], "Label for range error {0} => {1}".format(label, result))