Gnumed.pycommon.gmGuiBroker

Package Gnumed :: Package pycommon :: Module gmGuiBroker |
[frames] | no frames]

# Source Code for Module Gnumed.pycommon.gmGuiBroker

      1  """GNUMed GUI element brokerage
      2
      3  This module provides wrappers for the equivalent of global
      4  variables needed for a gnumed GUI client interface
      5
      6  @author: Dr. Horst Herb
      7  @version: 0.2
      8  @copyright: GPL v2 or later
      9  """
     10  #-----------------------------------------------------------
     11  # $Source: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/client/pycommon/gmGuiBroker.py,v $
     12  # $Id: gmGuiBroker.py,v 1.4 2004-06-20 15:44:28 ncq Exp $
     13  __version__ = "$Revision: 1.4 $"
     14  __author__ = "H.Herb <hherb@gnumed.net>, H.Berger <Hilmar.Berger@gmx.de>"
     15  #===========================================================
     16  if __name__ == '__main__':
     17          _ = lambda x:x
     18
     19  # FIXME !!! hack moved here from gmConf. This definitely must be replaced by some
     20  # structure getting data from the backend
     21  # FIXME: hardcoded color/width !?! move to DB (?)
     22  config = {'main.use_notebook':1, 'main.shadow.colour':(131, 129, 131), 'main.shadow.width':10}
     23
     24  #===========================================================
     25 -class GuiBroker:
     26          "Wrapper for global objects needed by GNUMmed GUI clients"
     27
     28          #This class wraps all global gui objects (variables)for a gnumed
     29          #application. The static (at application level)dictionary
     30          #__objects can be accessed through the method addobject
     31          #and getobject.
     32          #So, if you need to access the main window frame, you would
     33          #query an instance of GuiBroker for it.
     34
     35          __objects = {}
     36          __keycounter=0
     37
     38
     39 -        def __init__(self):
     40                  pass
     41
     42
     43 -        def addobject(self, widget, key=None):
     44                  "Add an object to the gnumed gui object dictionary"
     45
     46                  #An object can be anything (class, variable, widget)
     47                  #The "key" is a key expression (number, text) that
     48                  #allows you to retrieve the object.
     49                  #Convention for keys is the widget or variable name
     50                  #as a text string
     51                  #If key is not passed as parameter, a unique serial
     52                  #number is allocated as key and returned
     53
     54                  if not key:
     55                  # create a new sequential key that doesn't exist yet
     56                          key = GuiBroker.__keycounter + 1
     57                          while GuiBroker.__objects.has_key(key):
     58                                  key +=1
     59                  GuiBroker.__keycounter = key
     60                  GuiBroker.__objects[key]=widget
     61                  return key
     62
     63
     64
     65 -        def getobject(self, key):
     66                  "allows to retrieve a gnumed gui element; see addobject() regarding the key parameter"
     67                  return GuiBroker.__objects[key]
     68
     69 -        def has_key( self, key):
     70                  return GuiBroker.__objects.has_key(key)
     71
     72
     73
     74 -        def keylist(self):
     75                  " returns a list of all keys; see documentation for the dictionary data type"
     76                  return GuiBroker.__objects.keys()
     77
     78
     79
     80 -        def valuelist(self):
     81                  "returns a list of all values; see documentation for the dictionary data type"
     82                  return GuiBroker.__objects.values()
     83
     84
     85
     86 -        def itemlist(self):
     87                  "returns a list of all key:value pairs; see documentation for the dictionary data type"
     88                  return GuiBroker.__objects.items()
     89
     90
     91
     92 -        def __getitem__(self, key):
     93                  "Allows retrieving the value via value = instance[key]"
     94                  return self.getobject(key)
     95
     96
     97
     98 -        def __setitem__(self, key, object):
     99                  "Allows access in the style of instance[key]=value"
    100                  return self.addobject(object, key)
    101
    102  #===========================================================
    103  # you can test this module by invoking it as main program
    104  if __name__ == "__main__":
    105      print '>>> gmGuiBroker.GuiBroker test'
    106      test = GuiBroker()
    107
    108      print '>>> test.addobject("something", 3)'
    109      var = test.addobject("something", 3)
    110      print var, "\n"
    111
    112      print '>>> test.addobject("something else without a specified key")'
    113      var = test.addobject("something else without a specified key")
    114      print var, "\n"
    115
    116      print '>>> test.addobject(test)'
    117      testreference = test.addobject(test)
    118      print testreference, "\n"
    119
    120      print '>>> test.addobject(100, "hundred)'
    121      var = test.addobject(100, "hundred")
    122      print var, "\n"
    123
    124      print ">>> test.keylist()"
    125      var = test.keylist()
    126      print var, "\n"
    127
    128      print ">>> test.valuelist()"
    129      var = test.valuelist()
    130      print var, "\n"
    131
    132      print ">>> test.itemlist()"
    133      var = test.itemlist()
    134      print var, "\n"
    135
    136      print ">>> test[3]"
    137      var = test[3]
    138      print var, "\n"
    139
    140      print ">>> test[testreference].getobject('hundred')"
    141      var = test[testreference].getobject('hundred')
    142      print var, "\n"
    143
    144      print ">>> var = test[testreference]"
    145      var = test[testreference]
    146      print var, "\n"
    147
    148      print ">>> var = var['hundred']"
    149      var = var['hundred']
    150      print var, "\n"
    151
    152      print '>>> try: test.addobject["duplicate key", 3]'
    153      print '>>> except KeyError: print "Duplicate keys not allowed!"'
    154      try: test["duplicate key", 3]
    155      except KeyError: print "Duplicate keys not allowed!"
    156
    157      print ">>> test['key']='value'"
    158      test['key']='value'
    159      print test['key']
    160
    161  #===========================================================
    162  # $Log: gmGuiBroker.py,v $
    163  # Revision 1.4  2004-06-20 15:44:28  ncq
    164  # - we need to be more careful in pleasing epydoc
    165  #
    166  # Revision 1.3  2004/06/20 06:49:21  ihaywood
    167  # changes required due to Epydoc's OCD
    168  #
    169  # Revision 1.2  2004/03/10 00:14:04  ncq
    170  # - fix imports
    171  #
    172  # Revision 1.1  2004/02/25 09:30:13  ncq
    173  # - moved here from python-common
    174  #
    175  # Revision 1.8  2003/11/17 10:56:36  sjtan
    176  #
    177  # synced and commiting.
    178  #
    179  # Revision 1.1  2003/10/23 06:02:39  sjtan
    180  #
    181  # manual edit areas modelled after r.terry's specs.
    182  #
    183  # Revision 1.7  2003/02/09 11:48:59  ncq
    184  # - just some silly cvs keyword
    185  #
    186  # Revision 1.6  2003/02/09 09:41:57  sjtan
    187  #
    188  # clean up new code, make it less intrusive.
    189  #
    190  # Revision 1.5  2003/01/16 14:45:03  ncq
    191  # - debianized
    192  #
    193  # Revision 1.4  2003/01/12 00:20:04  ncq
    194  # - fixed __author__
    195  #
    196  # Revision 1.3  2003/01/12 00:17:44  ncq
    197  # - fixed typo, added CVS keywords
    198  #
    199