blob: d8fd14c9925a5d30a4a213d6f7e8947c07af2f88 [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 Warsawca07ba01998-10-22 03:25:59 +000020 self.__initfile = initfile
Barry Warsawfda3ace1998-09-29 20:04:19 +000021 self.__colordb = colordb
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000022 self.__optiondb = {}
23 self.__views = []
Barry Warsawa7ba45b1998-10-01 16:46:43 +000024 self.__red = 0
25 self.__green = 0
26 self.__blue = 0
Barry Warsawca07ba01998-10-22 03:25:59 +000027 self.__canceled = 0
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000028 # read the initialization file
29 fp = None
30 if initfile:
31 try:
32 try:
33 fp = open(initfile)
34 self.__optiondb = marshal.load(fp)
35 if type(self.__optiondb) <> DictType:
36 print 'Problem reading options from file:', initfile
37 self.__optiondb = {}
38 except (IOError, EOFError):
39 pass
40 finally:
41 if fp:
42 fp.close()
Barry Warsaw987fb921998-09-28 15:59:21 +000043
44 def add_view(self, view):
45 self.__views.append(view)
46
Barry Warsaw1ac18cd1998-09-28 23:41:12 +000047 def update_views(self, red, green, blue):
Barry Warsawa7ba45b1998-10-01 16:46:43 +000048 self.__red = red
49 self.__green = green
50 self.__blue = blue
Barry Warsaw987fb921998-09-28 15:59:21 +000051 for v in self.__views:
Barry Warsaw1ac18cd1998-09-28 23:41:12 +000052 v.update_yourself(red, green, blue)
Barry Warsawfda3ace1998-09-29 20:04:19 +000053
Barry Warsawa7ba45b1998-10-01 16:46:43 +000054 def update_views_current(self):
55 self.update_views(self.__red, self.__green, self.__blue)
56
Barry Warsawcd098671998-10-05 21:14:12 +000057 def current_rgb(self):
58 return self.__red, self.__green, self.__blue
59
Barry Warsawfda3ace1998-09-29 20:04:19 +000060 def colordb(self):
61 return self.__colordb
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000062
63 def optiondb(self):
64 return self.__optiondb
65
Barry Warsawca07ba01998-10-22 03:25:59 +000066 def save_views(self):
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000067 # save the current color
68 self.__optiondb['RED'] = self.__red
69 self.__optiondb['GREEN'] = self.__green
70 self.__optiondb['BLUE'] = self.__blue
71 for v in self.__views:
Barry Warsawca07ba01998-10-22 03:25:59 +000072 if hasattr(v, 'save_options'):
73 v.save_options(self.__optiondb)
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000074 fp = None
75 try:
76 try:
Barry Warsawca07ba01998-10-22 03:25:59 +000077 fp = open(self.__initfile, 'w')
Barry Warsaw8a09e1c1998-10-20 20:45:46 +000078 except IOError:
79 print 'Cannot write options to file:', file
80 else:
81 marshal.dump(self.__optiondb, fp)
82 finally:
83 if fp:
84 fp.close()
Barry Warsawca07ba01998-10-22 03:25:59 +000085
86 def withdraw_views(self):
87 for v in self.__views:
88 if hasattr(v, 'withdraw'):
89 v.withdraw()
90
91 def canceled(self):
92 self.__canceled = 1
93
94 def canceled_p(self):
95 return self.__canceled