def testCreateCalloutProperty(self):
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())
        vl.setAuxiliaryLayer(al)

        # Create a new callout property on layer without labels
        key = QgsCallout.DestinationX
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        # Labeling, but no callouts
        settings = QgsPalLayerSettings()
        settings.setCallout(None)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        # callouts
        settings = QgsPalLayerSettings()
        callout = QgsSimpleLineCallout()
        callout.setEnabled(True)
        settings.setCallout(callout)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        index = QgsAuxiliaryLayer.createProperty(key, vl)

        p = QgsCallout.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertEqual(
            settings.callout().dataDefinedProperties().property(key),
            QgsProperty.fromField('auxiliary_storage_callouts_destinationx'))

        key2 = QgsCallout.DestinationY
        index = QgsAuxiliaryLayer.createProperty(key2, vl)

        p = QgsCallout.propertyDefinitions()[key2]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertEqual(
            settings.callout().dataDefinedProperties().property(key),
            QgsProperty.fromField('auxiliary_storage_callouts_destinationx'))
        self.assertEqual(
            settings.callout().dataDefinedProperties().property(key2),
            QgsProperty.fromField('auxiliary_storage_callouts_destinationy'))
Exemple #2
0
    def testCreateCalloutProperty(self):
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())
        vl.setAuxiliaryLayer(al)

        # Create a new callout property on layer without labels
        key = QgsCallout.DestinationX
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        # Labeling, but no callouts
        settings = QgsPalLayerSettings()
        settings.setCallout(None)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        # callouts
        settings = QgsPalLayerSettings()
        callout = QgsSimpleLineCallout()
        callout.setEnabled(True)
        settings.setCallout(callout)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        index = QgsAuxiliaryLayer.createProperty(key, vl)

        p = QgsCallout.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertEqual(settings.callout().dataDefinedProperties().property(key), QgsProperty.fromField('auxiliary_storage_callouts_destinationx'))

        key2 = QgsCallout.DestinationY
        index = QgsAuxiliaryLayer.createProperty(key2, vl)

        p = QgsCallout.propertyDefinitions()[key2]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertEqual(settings.callout().dataDefinedProperties().property(key),
                         QgsProperty.fromField('auxiliary_storage_callouts_destinationx'))
        self.assertEqual(settings.callout().dataDefinedProperties().property(key2),
                         QgsProperty.fromField('auxiliary_storage_callouts_destinationy'))

        # with existing property
        key = QgsCallout.OriginX
        settings = QgsPalLayerSettings()
        callout = QgsSimpleLineCallout()
        callout.dataDefinedProperties().setProperty(key, QgsProperty.fromExpression('$x + 20'))
        callout.setEnabled(True)
        settings.setCallout(callout)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        # without overwriting existing, property should be upgraded to coalesce("aux field", 'existing expression') type
        index = QgsAuxiliaryLayer.createProperty(key, vl, False)
        p = QgsCallout.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertTrue(settings.callout().dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.callout().dataDefinedProperties().property(key).asExpression(), 'coalesce("auxiliary_storage_callouts_originx",$x + 20)')

        # with overwrite existing
        key = QgsCallout.OriginY
        callout = QgsSimpleLineCallout()
        callout.dataDefinedProperties().setProperty(key, QgsProperty.fromExpression('$y + 20'))
        settings.setCallout(callout)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        # existing property should be discarded
        index = QgsAuxiliaryLayer.createProperty(key, vl, True)
        p = QgsCallout.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertTrue(settings.callout().dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.callout().dataDefinedProperties().property(key).field(), 'auxiliary_storage_callouts_originy')