Пример #1
0
    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())
Пример #2
0
    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())