Barry Warsaw | 4638c5b | 1998-10-02 16:20:14 +0000 | [diff] [blame] | 1 | """Switchboard class. |
| 2 | |
| 3 | This class is used to coordinate updates among all Viewers. Every Viewer must |
| 4 | conform 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 Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 15 | from types import DictType |
| 16 | import marshal |
| 17 | |
Barry Warsaw | 987fb92 | 1998-09-28 15:59:21 +0000 | [diff] [blame] | 18 | class Switchboard: |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 19 | def __init__(self, colordb, initfile): |
Barry Warsaw | fda3ace | 1998-09-29 20:04:19 +0000 | [diff] [blame] | 20 | self.__colordb = colordb |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 21 | self.__optiondb = {} |
| 22 | self.__views = [] |
Barry Warsaw | a7ba45b | 1998-10-01 16:46:43 +0000 | [diff] [blame] | 23 | self.__red = 0 |
| 24 | self.__green = 0 |
| 25 | self.__blue = 0 |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 26 | # 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 Warsaw | 987fb92 | 1998-09-28 15:59:21 +0000 | [diff] [blame] | 41 | |
| 42 | def add_view(self, view): |
| 43 | self.__views.append(view) |
| 44 | |
Barry Warsaw | 1ac18cd | 1998-09-28 23:41:12 +0000 | [diff] [blame] | 45 | def update_views(self, red, green, blue): |
Barry Warsaw | a7ba45b | 1998-10-01 16:46:43 +0000 | [diff] [blame] | 46 | self.__red = red |
| 47 | self.__green = green |
| 48 | self.__blue = blue |
Barry Warsaw | 987fb92 | 1998-09-28 15:59:21 +0000 | [diff] [blame] | 49 | for v in self.__views: |
Barry Warsaw | 1ac18cd | 1998-09-28 23:41:12 +0000 | [diff] [blame] | 50 | v.update_yourself(red, green, blue) |
Barry Warsaw | fda3ace | 1998-09-29 20:04:19 +0000 | [diff] [blame] | 51 | |
Barry Warsaw | a7ba45b | 1998-10-01 16:46:43 +0000 | [diff] [blame] | 52 | def update_views_current(self): |
| 53 | self.update_views(self.__red, self.__green, self.__blue) |
| 54 | |
Barry Warsaw | cd09867 | 1998-10-05 21:14:12 +0000 | [diff] [blame] | 55 | def current_rgb(self): |
| 56 | return self.__red, self.__green, self.__blue |
| 57 | |
Barry Warsaw | fda3ace | 1998-09-29 20:04:19 +0000 | [diff] [blame] | 58 | def colordb(self): |
| 59 | return self.__colordb |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 60 | |
| 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() |