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

# Source Code for Module Gnumed.pycommon.gmGuiBroker

      1
      2
      3
      4  __doc__ = """GNUmed GUI element brokerage
      5
      6  This module provides wrappers for the equivalent of global
      7  variables needed for a gnumed GUI client interface
      8
      9  @author: Dr. Horst Herb
     10  @version: 0.2
     11  @copyright: GPL v2 or later
     12  """
     13
     14  __author__ = "H.Herb <hherb@gnumed.net>, H.Berger <Hilmar.Berger@gmx.de>"
     15  #===========================================================
     16  # FIXME !!! hack moved here from gmConf. This definitely must be replaced by some
     17  # structure getting data from the backend
     18  # FIXME: hardcoded color/width !?! move to DB (?)
     19  config = {'main.use_notebook':1, 'main.shadow.colour':(131, 129, 131), 'main.shadow.width':10}
     20
     21  #===========================================================
     22 -class GuiBroker:
     23          "Wrapper for global objects needed by GNUMmed GUI clients"
     24
     25          #This class wraps all global gui objects (variables)for a gnumed
     26          #application. The static (at application level)dictionary
     27          #__objects can be accessed through the method addobject
     28          #and getobject.
     29          #So, if you need to access the main window frame, you would
     30          #query an instance of GuiBroker for it.
     31
     32          __objects = {}
     33          __keycounter=0
     34
     35
     36 -        def __init__(self):
     37                  pass
     38
     39
     40 -        def addobject(self, widget, key=None):
     41                  "Add an object to the gnumed gui object dictionary"
     42
     43                  #An object can be anything (class, variable, widget)
     44                  #The "key" is a key expression (number, text) that
     45                  #allows you to retrieve the object.
     46                  #Convention for keys is the widget or variable name
     47                  #as a text string
     48                  #If key is not passed as parameter, a unique serial
     49                  #number is allocated as key and returned
     50
     51                  if not key:
     52                  # create a new sequential key that doesn't exist yet
     53                          key = GuiBroker.__keycounter + 1
     54                          while key in GuiBroker.__objects:
     55                                  key +=1
     56                  GuiBroker.__keycounter = key
     57                  GuiBroker.__objects[key]=widget
     58                  return key
     59
     60
     61
     62 -        def getobject(self, key):
     63                  "allows to retrieve a gnumed gui element; see addobject() regarding the key parameter"
     64                  return GuiBroker.__objects[key]
     65
     66 -        def has_key( self, key):
     67                  return key in GuiBroker.__objects
     68
     69
     70
     71 -        def keylist(self):
     72                  " returns a list of all keys; see documentation for the dictionary data type"
     73                  return GuiBroker.__objects.keys()
     74
     75
     76
     77 -        def valuelist(self):
     78                  "returns a list of all values; see documentation for the dictionary data type"
     79                  return GuiBroker.__objects.values()
     80
     81
     82
     83 -        def itemlist(self):
     84                  "returns a list of all key:value pairs; see documentation for the dictionary data type"
     85                  return GuiBroker.__objects.items()
     86
     87
     88
     89 -        def __getitem__(self, key):
     90                  "Allows retrieving the value via value = instance[key]"
     91                  return self.getobject(key)
     92
     93
     94
     95 -        def __setitem__(self, key, object):
     96                  "Allows access in the style of instance[key]=value"
     97                  return self.addobject(object, key)
     98
     99  #===========================================================
    100  if __name__ == "__main__":
    101
    102          import sys
    103
    104          if len(sys.argv) < 2:
    105                  sys.exit()
    106
    107          if sys.argv[1] != 'test':
    108                  sys.exit()
    109
    110          # you can test this module by invoking it as main program
    111          print('>>> gmGuiBroker.GuiBroker test')
    112          test = GuiBroker()
    113
    114          print('>>> test.addobject("something", 3)')
    115          var = test.addobject("something", 3)
    116          print(var, "\n")
    117
    118          print('>>> test.addobject("something else without a specified key")')
    119          var = test.addobject("something else without a specified key")
    120          print(var, "\n")
    121
    122          print('>>> test.addobject(test)')
    123          testreference = test.addobject(test)
    124          print(testreference, "\n")
    125
    126          print('>>> test.addobject(100, "hundred)')
    127          var = test.addobject(100, "hundred")
    128          print(var, "\n")
    129
    130          print(">>> test.keylist()")
    131          var = test.keylist()
    132          print(var, "\n")
    133
    134          print(">>> test.valuelist()")
    135          var = test.valuelist()
    136          print(var, "\n")
    137
    138          print(">>> test.itemlist()")
    139          var = test.itemlist()
    140          print(var, "\n")
    141
    142          print(">>> test[3]")
    143          var = test[3]
    144          print(var, "\n")
    145
    146          print(">>> test[testreference].getobject('hundred')")
    147          var = test[testreference].getobject('hundred')
    148          print(var, "\n")
    149
    150          print(">>> var = test[testreference]")
    151          var = test[testreference]
    152          print(var, "\n")
    153
    154          print(">>> var = var['hundred']")
    155          var = var['hundred']
    156          print(var, "\n")
    157
    158          print('>>> try: test.addobject["duplicate key", 3]')
    159          print('>>> except KeyError: print("Duplicate keys not allowed!"')
    160          try: test["duplicate key", 3]
    161          except KeyError: print("Duplicate keys not allowed!")
    162
    163          print(">>> test['key']='value'")
    164          test['key']='value'
    165          print(test['key'])
    166