blob: 7ac3df11b450b2b83761567d65a14be11a4b73d7 [file] [log] [blame]
Barry Warsaw4638c5b1998-10-02 16:20:14 +00001"""Switchboard class.
2
3This class is used to coordinate updates among all Viewers. Every Viewer must
4conform to the following interface:
5
6 - it must include a method called update_yourself() which takes three
7 arguments; the red, green, and blue values of the selected color.
8
9 - When a Viewer selects a color and wishes to update all other Views, it
10 should call update_views() on the Switchboard object. Not that the
11 Viewer typically does *not* update itself before calling update_views(),
12 since this would cause it to get updated twice.
13"""
14
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000015from types import DictType
16import marshal
17
Barry Warsaw987fb921998-09-28 15:59:21 +000018class Switchboard:
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000019 def __init__(self, colordb, initfile):
Barry Warsawfda3ace1998-09-29 20:04:19 +000020 self.__colordb = colordb
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000021 self.__optiondb = {}
22 self.__views = []
Barry Warsawa7ba45b1998-10-01 16:46:43 +000023 self.__red = 0
24 self.__green = 0
25 self.__blue = 0
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000026 # read the initialization file
27 fp = None
28 if initfile:
29 try:
30 try:
31 fp = open(initfile)
32 self.__optiondb = marshal.load(fp)
33 if type(self.__optiondb) <> DictType:
34 print 'Problem reading options from file:', initfile
35 self.__optiondb = {}
36 except (IOError, EOFError):
37 pass
38 finally:
39 if fp:
40 fp.close()
Barry Warsaw987fb921998-09-28 15:59:21 +000041
42 def add_view(self, view):
43 self.__views.append(view)
44
Barry Warsaw1ac18cd1998-09-28 23:41:12 +000045 def update_views(self, red, green, blue):
Barry Warsawa7ba45b1998-10-01 16:46:43 +000046 self.__red = red
47 self.__green = green
48 self.__blue = blue
Barry Warsaw987fb921998-09-28 15:59:21 +000049 for v in self.__views:
Barry Warsaw1ac18cd1998-09-28 23:41:12 +000050 v.update_yourself(red, green, blue)
Barry Warsawfda3ace1998-09-29 20:04:19 +000051
Barry Warsawa7ba45b1998-10-01 16:46:43 +000052 def update_views_current(self):
53 self.update_views(self.__red, self.__green, self.__blue)
54
Barry Warsawcd098671998-10-05 21:14:12 +000055 def current_rgb(self):
56 return self.__red, self.__green, self.__blue
57
Barry Warsawfda3ace1998-09-29 20:04:19 +000058 def colordb(self):
59 return self.__colordb
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000060
61 def optiondb(self):
62 return self.__optiondb
63
64 def save_views(self, file):
65 # save the current color
66 self.__optiondb['RED'] = self.__red
67 self.__optiondb['GREEN'] = self.__green
68 self.__optiondb['BLUE'] = self.__blue
69 for v in self.__views:
70 v.save_options(self.__optiondb)
71 fp = None
72 try:
73 try:
74 fp = open(file, 'w')
75 except IOError:
76 print 'Cannot write options to file:', file
77 else:
78 marshal.dump(self.__optiondb, fp)
79 finally:
80 if fp:
81 fp.close()