File: LObjWrap.py

package info (click to toggle)
pysolfc 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 94,860 kB
  • sloc: python: 82,020; tcl: 4,529; makefile: 66; sh: 57; perl: 48
file content (71 lines) | stat: -rw-r--r-- 2,480 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
# -*- mode: python; coding: utf-8; -*-
# =============================================================================
# Copyright (C) 2017-2023 LB
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Flake8: noqa
#
# =============================================================================

import logging

from kivy.event import EventDispatcher
from kivy.properties import BooleanProperty
from kivy.properties import NumericProperty
from kivy.properties import ListProperty
from kivy.properties import StringProperty

# =============================================================================
# Joins kivy properties to object members of referenced class.
# Usage:
# - use derived classes LBoolWrap etc. according to type.
# - write:  obj.value = <new value>
# - read:   <actual value> = obj.value
# - If you need additional functionality on change add a callback function
#   as 'command'. It will be called  whenever the value changes.

class LObjWrap(EventDispatcher):
    def __init__(self,obj,ref=None,command=None):
        self.obj = obj
        self.ref = ref
        if self.ref is not None:
            self.value = getattr(self.obj,self.ref)
            # logging.info("LObjWrap: setup for %s" % (self.ref))
            self.bind(value=self.on_value)
        if command is not None:
            self.bind(value=command)

    def on_value(self,inst,val):
        # logging.info("LObjWrap: %s = %s" % (self.ref,val))
        if self.ref is not None:
            setattr(self.obj,self.ref,val)

class LBoolWrap(LObjWrap):
    value = BooleanProperty(False)

    def __init__(self,obj,ref=None,command=None):
        super(LBoolWrap,self).__init__(obj,ref,command)

class LNumWrap(LObjWrap):
    value = NumericProperty(0)

    def __init__(self,obj,ref=None,command=None):
        super(LNumWrap,self).__init__(obj,ref,command)

class LStringWrap(LObjWrap):
    value = StringProperty('')

    def __init__(self,obj,ref=None,command=None):
        super(LStringWrap,self).__init__(obj,ref,command)

class LListWrap(LObjWrap):
    value = ListProperty([])

    def __init__(self,obj,ref=None,command=None):
        super(LListWrap,self).__init__(obj,ref,command)

# =============================================================================