svgui/svguilib.py
changeset 3359 2c924cf26161
parent 3358 7478d0c0dc1c
child 3360 746e3e3f6537
equal deleted inserted replaced
3358:7478d0c0dc1c 3359:2c924cf26161
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 # This file is part of Beremiz, a Integrated Development Environment for
       
     5 # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
       
     6 #
       
     7 # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 # See COPYING file for copyrights details.
       
    10 #
       
    11 # This program is free software; you can redistribute it and/or
       
    12 # modify it under the terms of the GNU General Public License
       
    13 # as published by the Free Software Foundation; either version 2
       
    14 # of the License, or (at your option) any later version.
       
    15 #
       
    16 # This program is distributed in the hope that it will be useful,
       
    17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    19 # GNU General Public License for more details.
       
    20 #
       
    21 # You should have received a copy of the GNU General Public License
       
    22 # along with this program; if not, write to the Free Software
       
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    24 
       
    25 
       
    26 # pylint: disable=old-style-class,undefined-variable
       
    27 
       
    28 
       
    29 class button:
       
    30 
       
    31     def __init__(self, parent, id, args):
       
    32         self.parent = parent
       
    33         self.id = id
       
    34         self.back_elt = getSVGElementById(args.back_id)
       
    35         self.sele_elt = getSVGElementById(args.sele_id)
       
    36         self.toggle = args.toggle
       
    37         self.active = args.active
       
    38         if args.state != undefined:
       
    39             self.state = args.state
       
    40         else:
       
    41             self.state = False
       
    42         self.dragging = False
       
    43         if self.toggle:
       
    44             self.up = not self.state
       
    45         else:
       
    46             self.up = True
       
    47 
       
    48         # Add event on each element of the button
       
    49         if self.active:
       
    50             self.back_elt.addEventListener("mouseup", self, False)
       
    51             self.back_elt.addEventListener("mousedown", self, False)
       
    52             self.back_elt.addEventListener("mouseover", self, False)
       
    53             self.back_elt.addEventListener("mouseout", self, False)
       
    54 
       
    55             self.sele_elt.addEventListener("mouseup", self, False)
       
    56             self.sele_elt.addEventListener("mousedown", self, False)
       
    57             self.sele_elt.addEventListener("mouseover", self, False)
       
    58             self.sele_elt.addEventListener("mouseout", self, False)
       
    59 
       
    60         blockSVGElementDrag(self.back_elt)
       
    61         blockSVGElementDrag(self.sele_elt)
       
    62 
       
    63         self.updateElements()
       
    64 
       
    65     # method to display the current state of interface
       
    66     def updateElements(self):
       
    67         if self.up:
       
    68             self.sele_elt.setAttribute("display", "none")
       
    69             self.back_elt.removeAttribute("display")
       
    70         else:
       
    71             self.sele_elt.removeAttribute("display")
       
    72             self.back_elt.setAttribute("display", "none")
       
    73 
       
    74     def updateValues(self, values):
       
    75         if values.state != self.state:
       
    76             self.state = values.state
       
    77             self.up = not self.state
       
    78             if self.toggle:
       
    79                 updateAttr(self.id, 'state', self.state)
       
    80             self.updateElements()
       
    81 
       
    82     def handleEvent(self, evt):
       
    83         # Quand le bouton de la souris est presse
       
    84         if evt.type == "mousedown":
       
    85             evt.stopPropagation()
       
    86             setCurrentObject(self)
       
    87 
       
    88             self.dragging = True
       
    89 
       
    90             if self.toggle:
       
    91                 self.up = self.state
       
    92             else:
       
    93                 self.up = False
       
    94                 self.state = True
       
    95                 updateAttr(self.id, 'state', self.state)
       
    96             self.updateElements()
       
    97 
       
    98         if isCurrentObject(self) and self.dragging:
       
    99             # Quand le bouton est survole
       
   100             if evt.type == "mouseover" and self.toggle:
       
   101                 self.up = self.state
       
   102                 self.updateElements()
       
   103 
       
   104             # Quand le curseur quitte la zone du bouton
       
   105             elif evt.type == "mouseout" and self.toggle:
       
   106                 self.up = not self.state
       
   107                 self.updateElements()
       
   108 
       
   109             # Quand le bouton de la souris est relache
       
   110             elif evt.type == "mouseup":
       
   111                 evt.stopPropagation()
       
   112                 if self.toggle and self.up == self.state:
       
   113                     self.state = not self.state
       
   114                     updateAttr(self.id, 'state', self.state)
       
   115                 elif not self.toggle:
       
   116                     self.up = True
       
   117                     self.state = False
       
   118                     updateAttr(self.id, 'state', self.state)
       
   119                     self.updateElements()
       
   120                 self.dragging = False
       
   121 
       
   122 
       
   123 class textControl:
       
   124 
       
   125     def __init__(self, parent, id, args):
       
   126         self.parent = parent
       
   127         self.id = id
       
   128         self.back_elt = getSVGElementById(args.back_id)
       
   129         if args.text != undefined:
       
   130             self.text = args.text
       
   131         else:
       
   132             self.text = ""
       
   133         self.updateElements()
       
   134 
       
   135     def updateValues(self, values):
       
   136         if values.text != self.value:
       
   137             self.text = values.text
       
   138             updateAttr(self.id, 'text', self.text)
       
   139             self.updateElements()
       
   140 
       
   141     def updateElements(self):
       
   142         self.back_elt.firstChild.firstChild.textContent = self.text
       
   143 
       
   144     def handleEvent(self, evt):
       
   145         pass