Example #1
0
    def test_manager(self):
        source = QObject()
        target = QObject()

        manager = BindingManager(submitPolicy=BindingManager.ManualSubmit)

        manager.bind((target, "target"), None).to((source, "source"))

        tbind = DynamicPropertyBinding(target, "target_copy")
        sbind = DynamicPropertyBinding(source, "source")
        schanged = []
        sbind.changed[object].connect(schanged.append)

        manager.bind(tbind, None).to(sbind)

        source.setProperty("source", 1)
        self.assertEqual(len(schanged), 1)

        self.assertEqual(toPyObject(target.property("target")), None)

        manager.commit()

        self.assertEqual(toPyObject(target.property("target")), 1)
        self.assertEqual(toPyObject(target.property("target_copy")), 1)

        source.setProperty("source", 2)

        manager.setSubmitPolicy(BindingManager.AutoSubmit)

        self.assertEqual(toPyObject(target.property("target")), 2)
        self.assertEqual(toPyObject(target.property("target_copy")), 2)
Example #2
0
    def test_expr(self):
        obj1 = QObject()

        obj1.setProperty("value", 1)
        obj1.setProperty("other", 2)

        result = DynamicPropertyBinding(obj1, "result")
        result.bindTo(
            PropertyBindingExpr(
                "value + other",
                locals={"value": binding_for(obj1, "value"),
                        "other": binding_for(obj1, "other")}
                )
            )

        expr = PropertyBindingExpr(
           "True if value < 3 else False",
           dict(value=DynamicPropertyBinding(obj1, "result"))
        )

        result_values = []
        result.changed[object].connect(result_values.append)

        expr_values = []
        expr.changed[object].connect(expr_values.append)

        self.assertEqual(result.get(), 3)
        self.assertEqual(expr.get(), False)

        obj1.setProperty("value", 0)
        self.assertEqual(result_values[-1], 2)
        self.assertEqual(expr_values[-1], True)

        self.assertEqual(result.get(), 2)
        self.assertEqual(expr.get(), True)
Example #3
0
    def test_dyn(self):
        obj = QObject()
        changed = []

        binding = DynamicPropertyBinding(obj, "test")
        binding.changed[object].connect(changed.append)

        self.assertIs(binding.get(), None)

        obj.setProperty("test", 1)
        self.assertEqual(binding.get(), 1)
        self.assertEqual(len(changed), 1)
        self.assertEqual(changed[-1], 1)

        binding.set(2)
        self.assertEqual(binding.get(), 2)
        self.assertEqual(len(changed), 2)
        self.assertEqual(changed[-1], 2)

        target = QObject()
        binding1 = DynamicPropertyBinding(target, "prop")

        binding1.bindTo(binding)

        self.assertEqual(binding1.get(), binding.get())
        self.assertEqual(target.property("prop"), 2)

        binding.set("a string")
        self.assertEqual(binding1.get(), "a string")
        self.assertEqual(binding1.get(), binding.get())
        self.assertEqual(target.property("prop"), "a string")

        binding1.unbind()
        binding.set(1)
        self.assertEqual(binding1.get(), "a string")
        self.assertEqual(target.property("prop"), "a string")
        self.assertEqual(binding.get(), 1)
        self.assertEqual(obj.property("test"), 1)