plugins/python/modules/svgui/svguilib.py
author laurent
Wed, 12 Aug 2009 11:46:22 +0200
changeset 371 b7cb57a2da08
child 381 5c0f34a9ab00
permissions -rw-r--r--
Adding new svgui support using twisted website HMI
Updating svgui example
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     1
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     2
class button:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     3
    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     4
    def __init__(self, parent, back_id, sele_id, toggle, state, active):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     5
        self.parent = parent
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     6
        self.back_elt = getSVGElementById(back_id)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     7
        self.sele_elt = getSVGElementById(sele_id)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     8
        self.toggle = toggle
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     9
        self.state = state
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    10
        self.active = active
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    11
        self.dragging = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    12
        if toggle:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    13
            self.up = not state
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    14
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    15
            self.up = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    16
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    17
        # Add event on each element of the button
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    18
        if self.active:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    19
            self.back_elt.addEventListener("mouseup", self, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    20
            self.back_elt.addEventListener("mousedown", self, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    21
            self.back_elt.addEventListener("mouseover", self, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    22
            self.back_elt.addEventListener("mouseout", self, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    23
            
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    24
            self.sele_elt.addEventListener("mouseup", self, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    25
            self.sele_elt.addEventListener("mousedown", self, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    26
            self.sele_elt.addEventListener("mouseover", self, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
            self.sele_elt.addEventListener("mouseout", self, False)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    28
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
        blockSVGElementDrag(self.back_elt)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    30
        blockSVGElementDrag(self.sele_elt)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    31
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    32
        self.updateElements()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    33
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    34
    # method to display the current state of interface
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    35
    def updateElements(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    36
        if self.up:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    37
            self.sele_elt.setAttribute("visibility", "hidden")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    38
            self.back_elt.setAttribute("visibility", "visible")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    39
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    40
            self.sele_elt.setAttribute("visibility", "visible")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    41
            self.back_elt.setAttribute("visibility", "hidden")
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    42
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    43
    def updateState(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    44
        self.up = not value
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    45
        self.updateElements()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    46
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    47
    def handleEvent(self, evt):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
        # Quand le bouton de la souris est presse
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    49
        if evt.type == "mousedown":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    50
            evt.stopPropagation()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    51
            setCurrentObject(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    52
            
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    53
            self.dragging = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    54
            
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    55
            if self.toggle:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    56
                self.up = self.state
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    57
            else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    58
                self.up = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    59
                self.state = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    60
                updateAttr(self.back_elt.id, 'state', self.state)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    61
            self.updateElements()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    62
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    63
        if isCurrentObject(self) and self.dragging:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    64
            # Quand le bouton est survole
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    65
            if evt.type == "mouseover" and self.toggle:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    66
                self.up = self.state
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    67
                self.updateElements()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    68
            
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    69
            # Quand le curseur quitte la zone du bouton
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    70
            elif evt.type == "mouseout" and self.toggle:       
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    71
                self.up = not self.state
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
                self.updateElements()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
            
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    74
            # Quand le bouton de la souris est relache
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    75
            elif evt.type == "mouseup":
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    76
                evt.stopPropagation()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    77
                if self.toggle and self.up == self.state:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    78
                    self.state = not self.state
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    79
                    updateAttr(self.back_elt.id, 'state', self.state)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    80
                elif not self.toggle:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    81
                    self.up = True
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
                    self.state = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    83
                    updateAttr(self.back_elt.id, 'state', self.state)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    84
                    self.updateElements()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    85
                self.dragging = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
        
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
class textControl:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    88
    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    89
    def __init__(self, parent, back_id, state):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    90
        self.parent = parent
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    91
        self.back_elt = getSVGElementById(back_id)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    92
        self.state = state
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    93
        self.setValue(self.state)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    94
    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    95
    def handleEvent(self, evt):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    96
        pass
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    97
    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    98
    def getValue(self):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    99
        return self.back_elt.firstChild.firstChild.textContent
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   100
    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   101
    def setValue(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   102
        self.back_elt.firstChild.firstChild.textContent = value
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   103
    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   104
    def updateState(self, value):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   105
        self.setValue(value)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   106