def testSelectorWidget(self): w = QgsNumericFormatSelectorWidget() spy = QSignalSpy(w.changed) # should default to a default format self.assertIsInstance(w.format(), QgsFallbackNumericFormat) w.setFormat(QgsBearingNumericFormat()) self.assertIsInstance(w.format(), QgsBearingNumericFormat) self.assertEqual(len(spy), 1) w.setFormat(QgsBearingNumericFormat()) self.assertEqual(len(spy), 2) w.setFormat(QgsCurrencyNumericFormat()) self.assertIsInstance(w.format(), QgsCurrencyNumericFormat) self.assertEqual(len(spy), 3) QgsApplication.numericFormatRegistry().addFormat(TestFormat()) QgsGui.numericFormatGuiRegistry().addFormatConfigurationWidgetFactory( 'test', TestWidgetFactory()) original = TestFormat() original.xx = 55 w = QgsNumericFormatSelectorWidget() w.setFormat(original) new = w.format() self.assertEqual(new.xx, 55)
def testProperties(self): c = QgsTableCell('test') props = c.properties(QgsReadWriteContext()) c2 = QgsTableCell() c2.setProperties(props, QgsReadWriteContext()) self.assertEqual(c2.content(), 'test') self.assertFalse(c2.backgroundColor().isValid()) self.assertFalse(c2.foregroundColor().isValid()) self.assertFalse(c2.numericFormat()) self.assertFalse(c2.textFormat().isValid()) c.setBackgroundColor(QColor(255, 0, 0)) c.setForegroundColor(QColor(255, 0, 255)) format = QgsBearingNumericFormat() format.setShowPlusSign(True) c.setNumericFormat(format) text_format = QgsTextFormat() text_format.setSize(16.8) c.setTextFormat(text_format) props = c.properties(QgsReadWriteContext()) c3 = QgsTableCell() c3.setProperties(props, QgsReadWriteContext()) self.assertEqual(c3.content(), 'test') self.assertEqual(c3.backgroundColor().name(), '#ff0000') self.assertEqual(c3.foregroundColor().name(), '#ff00ff') self.assertIsInstance(c3.numericFormat(), QgsBearingNumericFormat) self.assertTrue(c3.numericFormat().showPlusSign()) self.assertEqual(c3.textFormat().size(), 16.8) self.assertTrue(c3.textFormat().isValid())
def testCell(self): c = QgsTableCell('test') self.assertEqual(c.content(), 'test') c.setContent(5) self.assertEqual(c.content(), 5) self.assertFalse(c.backgroundColor().isValid()) self.assertFalse(c.foregroundColor().isValid()) self.assertFalse(c.numericFormat()) c.setBackgroundColor(QColor(255, 0, 0)) c.setForegroundColor(QColor(255, 0, 255)) c.setNumericFormat(QgsBearingNumericFormat()) self.assertEqual(c.backgroundColor().name(), '#ff0000') self.assertEqual(c.foregroundColor().name(), '#ff00ff') self.assertIsInstance(c.numericFormat(), QgsBearingNumericFormat)
def testReadWrite(self): p = QgsProjectDisplaySettings() format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(9) format.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360) p.setBearingFormat(format) doc = QDomDocument("testdoc") elem = p.writeXml(doc, QgsReadWriteContext()) p2 = QgsProjectDisplaySettings() spy = QSignalSpy(p2.bearingFormatChanged) self.assertTrue(p2.readXml(elem, QgsReadWriteContext())) self.assertEqual(len(spy), 1) self.assertEqual(p2.bearingFormat().numberDecimalPlaces(), 9) self.assertEqual(p2.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRange0To360)
def testBearingFormat(self): s = QgsLocalDefaultSettings() format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(9) format.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360) s.setBearingFormat(format) self.assertEqual(s.bearingFormat().numberDecimalPlaces(), 9) self.assertEqual(s.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRange0To360) format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(3) format.setDirectionFormat(QgsBearingNumericFormat.UseRangeNegative180ToPositive180) s.setBearingFormat(format) self.assertEqual(s.bearingFormat().numberDecimalPlaces(), 3) self.assertEqual(s.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRangeNegative180ToPositive180) # new settings object, should persist. s2 = QgsLocalDefaultSettings() self.assertEqual(s2.bearingFormat().numberDecimalPlaces(), 3) self.assertEqual(s2.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRangeNegative180ToPositive180)
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())
def testReset(self): """ Test that resetting inherits local default settings """ format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(3) format.setDirectionFormat( QgsBearingNumericFormat.UseRangeNegative180ToPositive180) p = QgsProjectDisplaySettings() p.setBearingFormat(format) self.assertEqual(p.bearingFormat().numberDecimalPlaces(), 3) self.assertEqual( p.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRangeNegative180ToPositive180) format = QgsGeographicCoordinateNumericFormat() format.setNumberDecimalPlaces(7) format.setAngleFormat(QgsGeographicCoordinateNumericFormat.AngleFormat. DegreesMinutesSeconds) p.setGeographicCoordinateFormat(format) self.assertEqual(p.geographicCoordinateFormat().numberDecimalPlaces(), 7) self.assertEqual( p.geographicCoordinateFormat().angleFormat(), QgsGeographicCoordinateNumericFormat.AngleFormat. DegreesMinutesSeconds) # setup a local default bearing format s = QgsLocalDefaultSettings() format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(9) format.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360) s.setBearingFormat(format) format = QgsGeographicCoordinateNumericFormat() format.setNumberDecimalPlaces(5) format.setAngleFormat( QgsGeographicCoordinateNumericFormat.AngleFormat.DegreesMinutes) s.setGeographicCoordinateFormat(format) spy = QSignalSpy(p.bearingFormatChanged) spy2 = QSignalSpy(p.geographicCoordinateFormatChanged) p.reset() self.assertEqual(len(spy), 1) self.assertEqual(len(spy2), 1) # project should default to local default format self.assertEqual(p.bearingFormat().numberDecimalPlaces(), 9) self.assertEqual(p.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRange0To360) self.assertEqual(p.geographicCoordinateFormat().numberDecimalPlaces(), 5) self.assertEqual( p.geographicCoordinateFormat().angleFormat(), QgsGeographicCoordinateNumericFormat.AngleFormat.DegreesMinutes)
def testBearingFormat(self): p = QgsProjectDisplaySettings() format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(9) format.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360) spy = QSignalSpy(p.bearingFormatChanged) p.setBearingFormat(format) self.assertEqual(len(spy), 1) self.assertEqual(p.bearingFormat().numberDecimalPlaces(), 9) self.assertEqual(p.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRange0To360) format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(3) format.setDirectionFormat( QgsBearingNumericFormat.UseRangeNegative180ToPositive180) p.setBearingFormat(format) self.assertEqual(len(spy), 2) self.assertEqual(p.bearingFormat().numberDecimalPlaces(), 3) self.assertEqual( p.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRangeNegative180ToPositive180)
def testReadWrite(self): p = QgsProjectDisplaySettings() format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(9) format.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360) p.setBearingFormat(format) format = QgsGeographicCoordinateNumericFormat() format.setNumberDecimalPlaces(7) format.setAngleFormat(QgsGeographicCoordinateNumericFormat.AngleFormat. DegreesMinutesSeconds) p.setGeographicCoordinateFormat(format) doc = QDomDocument("testdoc") elem = p.writeXml(doc, QgsReadWriteContext()) p2 = QgsProjectDisplaySettings() spy = QSignalSpy(p2.bearingFormatChanged) spy2 = QSignalSpy(p2.geographicCoordinateFormatChanged) self.assertTrue(p2.readXml(elem, QgsReadWriteContext())) self.assertEqual(len(spy), 1) self.assertEqual(len(spy2), 1) self.assertEqual(p2.bearingFormat().numberDecimalPlaces(), 9) self.assertEqual(p2.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRange0To360) self.assertEqual(p.geographicCoordinateFormat().numberDecimalPlaces(), 7) self.assertEqual( p.geographicCoordinateFormat().angleFormat(), QgsGeographicCoordinateNumericFormat.AngleFormat. DegreesMinutesSeconds)
def testReset(self): """ Test that resetting inherits local default settings """ format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(3) format.setDirectionFormat( QgsBearingNumericFormat.UseRangeNegative180ToPositive180) p = QgsProjectDisplaySettings() p.setBearingFormat(format) self.assertEqual(p.bearingFormat().numberDecimalPlaces(), 3) self.assertEqual( p.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRangeNegative180ToPositive180) # setup a local default bearing format s = QgsLocalDefaultSettings() format = QgsBearingNumericFormat() format.setNumberDecimalPlaces(9) format.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360) s.setBearingFormat(format) spy = QSignalSpy(p.bearingFormatChanged) p.reset() self.assertEqual(len(spy), 1) # project should default to local default format self.assertEqual(p.bearingFormat().numberDecimalPlaces(), 9) self.assertEqual(p.bearingFormat().directionFormat(), QgsBearingNumericFormat.UseRange0To360)
def testBearingFormat(self): w = QgsNumericFormatSelectorWidget() original = QgsBearingNumericFormat() original.setNumberDecimalPlaces(4) original.setShowTrailingZeros(True) original.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360) w.setFormat(original) new = w.format() self.assertIsInstance(new, QgsBearingNumericFormat) self.assertEqual(new.numberDecimalPlaces(), original.numberDecimalPlaces()) self.assertEqual(new.showTrailingZeros(), original.showTrailingZeros()) self.assertEqual(new.directionFormat(), original.directionFormat())
def testBearingFormat(self): """ test bearing formatter """ f = QgsBearingNumericFormat() f.setDirectionFormat( QgsBearingNumericFormat.UseRange0To180WithEWDirectionalSuffix) context = QgsNumericFormatContext() self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(90, context), '90°E') self.assertEqual(f.formatDouble(180, context), '180°') self.assertEqual(f.formatDouble(270, context), '90°W') self.assertEqual(f.formatDouble(300, context), '60°W') self.assertEqual(f.formatDouble(5, context), '5°E') self.assertEqual(f.formatDouble(5.5, context), '5.5°E') self.assertEqual(f.formatDouble(-5, context), '5°W') self.assertEqual(f.formatDouble(-5.5, context), '5.5°W') context.setDecimalSeparator('x') self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(-5.5, context), '5x5°W') self.assertEqual(f.formatDouble(180, context), '180°') context.setDecimalSeparator('.') f.setNumberDecimalPlaces(0) self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(5.5, context), '6°E') self.assertEqual(f.formatDouble(-5.5, context), '6°W') self.assertEqual(f.formatDouble(180, context), '180°') f.setNumberDecimalPlaces(3) self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(5.5, context), '5.5°E') self.assertEqual(f.formatDouble(-5.5, context), '5.5°W') self.assertEqual(f.formatDouble(180, context), '180°') f.setShowTrailingZeros(True) self.assertEqual(f.formatDouble(0, context), '0.000°E') # todo - fix and avoid E self.assertEqual(f.formatDouble(5, context), '5.000°E') self.assertEqual(f.formatDouble(-5, context), '5.000°W') self.assertEqual(f.formatDouble(5.5, context), '5.500°E') self.assertEqual(f.formatDouble(-5.5, context), '5.500°W') self.assertEqual(f.formatDouble(180, context), '180.000°E') # todo fix and avoid E f = QgsBearingNumericFormat() f.setDirectionFormat( QgsBearingNumericFormat.UseRangeNegative180ToPositive180) self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(90, context), '90°') self.assertEqual(f.formatDouble(180, context), '180°') self.assertEqual(f.formatDouble(270, context), '-90°') self.assertEqual(f.formatDouble(5, context), '5°') self.assertEqual(f.formatDouble(5.5, context), '5.5°') self.assertEqual(f.formatDouble(-5, context), '-5°') self.assertEqual(f.formatDouble(-5.5, context), '-5.5°') context.setDecimalSeparator('x') self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(-5.5, context), '-5x5°') self.assertEqual(f.formatDouble(180, context), '180°') context.setDecimalSeparator('.') f.setNumberDecimalPlaces(0) self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(5.5, context), '6°') self.assertEqual(f.formatDouble(-5.5, context), '-6°') self.assertEqual(f.formatDouble(180, context), '180°') f.setNumberDecimalPlaces(3) self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(5.5, context), '5.5°') self.assertEqual(f.formatDouble(-5.5, context), '-5.5°') self.assertEqual(f.formatDouble(180, context), '180°') f.setShowTrailingZeros(True) self.assertEqual(f.formatDouble(0, context), '0.000°') self.assertEqual(f.formatDouble(5, context), '5.000°') self.assertEqual(f.formatDouble(-5, context), '-5.000°') self.assertEqual(f.formatDouble(5.5, context), '5.500°') self.assertEqual(f.formatDouble(-5.5, context), '-5.500°') self.assertEqual(f.formatDouble(180, context), '180.000°') f = QgsBearingNumericFormat() f.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360) self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(90, context), '90°') self.assertEqual(f.formatDouble(180, context), '180°') self.assertEqual(f.formatDouble(270, context), '270°') self.assertEqual(f.formatDouble(5, context), '5°') self.assertEqual(f.formatDouble(5.5, context), '5.5°') self.assertEqual(f.formatDouble(-5, context), '355°') self.assertEqual(f.formatDouble(-5.5, context), '354.5°') context.setDecimalSeparator('x') self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(-5.5, context), '354x5°') self.assertEqual(f.formatDouble(180, context), '180°') context.setDecimalSeparator('.') f.setNumberDecimalPlaces(0) self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(5.5, context), '6°') self.assertEqual(f.formatDouble(-5.4, context), '355°') self.assertEqual(f.formatDouble(180, context), '180°') f.setNumberDecimalPlaces(3) self.assertEqual(f.formatDouble(0, context), '0°') self.assertEqual(f.formatDouble(5.5, context), '5.5°') self.assertEqual(f.formatDouble(-5.5, context), '354.5°') self.assertEqual(f.formatDouble(180, context), '180°') f.setShowTrailingZeros(True) self.assertEqual(f.formatDouble(0, context), '0.000°') self.assertEqual(f.formatDouble(5, context), '5.000°') self.assertEqual(f.formatDouble(-5, context), '355.000°') self.assertEqual(f.formatDouble(5.5, context), '5.500°') self.assertEqual(f.formatDouble(-5.5, context), '354.500°') self.assertEqual(f.formatDouble(180, context), '180.000°') f2 = f.clone() self.assertIsInstance(f2, QgsBearingNumericFormat) self.assertEqual(f2.showTrailingZeros(), f.showTrailingZeros()) self.assertEqual(f2.showPlusSign(), f.showPlusSign()) self.assertEqual(f2.numberDecimalPlaces(), f.numberDecimalPlaces()) self.assertEqual(f2.showThousandsSeparator(), f.showThousandsSeparator()) self.assertEqual(f2.directionFormat(), f.directionFormat()) doc = QDomDocument("testdoc") elem = doc.createElement("test") f2.writeXml(elem, doc, QgsReadWriteContext()) f3 = QgsNumericFormatRegistry().createFromXml(elem, QgsReadWriteContext()) self.assertIsInstance(f3, QgsBearingNumericFormat) self.assertEqual(f3.showTrailingZeros(), f.showTrailingZeros()) self.assertEqual(f3.showPlusSign(), f.showPlusSign()) self.assertEqual(f3.numberDecimalPlaces(), f.numberDecimalPlaces()) self.assertEqual(f3.showThousandsSeparator(), f.showThousandsSeparator()) self.assertEqual(f3.directionFormat(), f.directionFormat())