Saving/Restoring state into ~/.pynche file

This commit is contained in:
Barry Warsaw 1998-10-20 20:45:46 +00:00
parent 28e7b4cce1
commit 8a09e1ccda
10 changed files with 160 additions and 27 deletions

View File

@ -110,3 +110,6 @@ class ChipViewer:
colorname = self.__nearest.get_color() colorname = self.__nearest.get_color()
red, green, blue = self.__sb.colordb().find_byname(colorname) red, green, blue = self.__sb.colordb().find_byname(colorname)
self.__sb.update_views(red, green, blue) self.__sb.update_views(red, green, blue)
def save_options(self, optiondb):
pass

View File

@ -63,6 +63,7 @@ GRAV = 'Squash'
class DetailsViewer: class DetailsViewer:
def __init__(self, switchboard, parent=None): def __init__(self, switchboard, parent=None):
self.__sb = switchboard self.__sb = switchboard
optiondb = switchboard.optiondb()
self.__red, self.__green, self.__blue = switchboard.current_rgb() self.__red, self.__green, self.__blue = switchboard.current_rgb()
# GUI # GUI
root = self.__root = Toplevel(parent, class_='Pynche') root = self.__root = Toplevel(parent, class_='Pynche')
@ -87,21 +88,21 @@ class DetailsViewer:
self.__l1 = Label(frame, text='Move Sliders:') self.__l1 = Label(frame, text='Move Sliders:')
self.__l1.grid(row=1, column=0, sticky=E) self.__l1.grid(row=1, column=0, sticky=E)
self.__rvar = IntVar() self.__rvar = IntVar()
self.__rvar.set(4) self.__rvar.set(optiondb.get('RSLIDER', 4))
self.__radio1 = Checkbutton(frame, text='Red', self.__radio1 = Checkbutton(frame, text='Red',
variable=self.__rvar, variable=self.__rvar,
command=self.__effect, command=self.__effect,
onvalue=4, offvalue=0) onvalue=4, offvalue=0)
self.__radio1.grid(row=1, column=1, sticky=W) self.__radio1.grid(row=1, column=1, sticky=W)
self.__gvar = IntVar() self.__gvar = IntVar()
self.__gvar.set(2) self.__gvar.set(optiondb.get('GSLIDER', 2))
self.__radio2 = Checkbutton(frame, text='Green', self.__radio2 = Checkbutton(frame, text='Green',
variable=self.__gvar, variable=self.__gvar,
command=self.__effect, command=self.__effect,
onvalue=2, offvalue=0) onvalue=2, offvalue=0)
self.__radio2.grid(row=2, column=1, sticky=W) self.__radio2.grid(row=2, column=1, sticky=W)
self.__bvar = IntVar() self.__bvar = IntVar()
self.__bvar.set(1) self.__bvar.set(optiondb.get('BSLIDER', 1))
self.__radio3 = Checkbutton(frame, text='Blue', self.__radio3 = Checkbutton(frame, text='Blue',
variable=self.__bvar, variable=self.__bvar,
command=self.__effect, command=self.__effect,
@ -115,7 +116,7 @@ class DetailsViewer:
self.__l3 = Label(frame, text='At boundary:') self.__l3 = Label(frame, text='At boundary:')
self.__l3.grid(row=5, column=0, sticky=E) self.__l3.grid(row=5, column=0, sticky=E)
self.__boundvar = StringVar() self.__boundvar = StringVar()
self.__boundvar.set(STOP) self.__boundvar.set(optiondb.get('ATBOUND', STOP))
self.__omenu = OptionMenu(frame, self.__boundvar, self.__omenu = OptionMenu(frame, self.__boundvar,
STOP, WRAP, RATIO, GRAV) STOP, WRAP, RATIO, GRAV)
self.__omenu.grid(row=5, column=1, sticky=W) self.__omenu.grid(row=5, column=1, sticky=W)
@ -262,3 +263,9 @@ class DetailsViewer:
self.__red = red self.__red = red
self.__green = green self.__green = green
self.__blue = blue self.__blue = blue
def save_options(self, optiondb):
optiondb['RSLIDER'] = self.__rvar.get()
optiondb['GSLIDER'] = self.__gvar.get()
optiondb['BSLIDER'] = self.__bvar.get()
optiondb['ATBOUND'] = self.__boundvar.get()

View File

@ -21,6 +21,7 @@ import ColorDB
class ListViewer: class ListViewer:
def __init__(self, switchboard, parent=None): def __init__(self, switchboard, parent=None):
self.__sb = switchboard self.__sb = switchboard
optiondb = switchboard.optiondb()
self.__lastbox = None self.__lastbox = None
self.__dontcenter = 0 self.__dontcenter = 0
# GUI # GUI
@ -76,7 +77,7 @@ class ListViewer:
# #
# Update on click # Update on click
self.__uoc = BooleanVar() self.__uoc = BooleanVar()
self.__uoc.set(1) self.__uoc.set(optiondb.get('UPONCLICK', 1))
self.__uocbtn = Checkbutton(root, self.__uocbtn = Checkbutton(root,
text='Update on Click', text='Update on Click',
variable=self.__uoc, variable=self.__uoc,
@ -160,3 +161,6 @@ class ListViewer:
ig, ig, ig, y2 = canvas.coords(self.__bboxes[-1]) ig, ig, ig, y2 = canvas.coords(self.__bboxes[-1])
h = int(canvas['height']) * 0.5 h = int(canvas['height']) * 0.5
canvas.yview('moveto', (y1-h) / y2) canvas.yview('moveto', (y1-h) / y2)
def save_options(self, optiondb):
optiondb['UPONCLICK'] = self.__uoc.get()

View File

@ -12,13 +12,26 @@ This program currently requires Python 1.5 with Tkinter. It has only been
tested on Solaris 2.6. Feedback is greatly appreciated. Send email to tested on Solaris 2.6. Feedback is greatly appreciated. Send email to
bwarsaw@python.org bwarsaw@python.org
Usage: %(PROGRAM)s [-d file] [-h] [initialcolor] Usage: %(PROGRAM)s [-d file] [-i file] [-X] [-h] [initialcolor]
Where: Where:
--database file --database file
-d file -d file
Alternate location of a color database file Alternate location of a color database file
--initfile file
-i file
Alternate location of the initialization file. This file contains a
persistent database of the current Pynche options and color. This
means that Pynche restores its option settings and current color when
it restarts, using this file (unless the -X option is used). The
default is ~/.pynche
--ignore
-X
Ignore the initialization file when starting up. Pynche will still
write the current option settings to this file when it quits.
--help --help
-h -h
print this message print this message
@ -49,7 +62,7 @@ RGB_TXT = [
# Solaris OpenWindows # Solaris OpenWindows
'/usr/openwin/lib/rgb.txt', '/usr/openwin/lib/rgb.txt',
# The X11R6.4 rgb.txt file # The X11R6.4 rgb.txt file
os.path.join(sys.path[0], 'rgb.txt'), os.path.join(sys.path[0], 'X/rgb.txt'),
# add more here # add more here
] ]
@ -95,23 +108,29 @@ def main():
try: try:
opts, args = getopt.getopt( opts, args = getopt.getopt(
sys.argv[1:], sys.argv[1:],
'hd:', 'hd:i:X',
['database=', 'help']) ['database=', 'initfile=', 'ignore', 'help'])
except getopt.error, msg: except getopt.error, msg:
usage(1, msg) usage(1, msg)
if len(args) == 0: if len(args) == 0:
initialcolor = 'grey50' initialcolor = None
elif len(args) == 1: elif len(args) == 1:
initialcolor = args[0] initialcolor = args[0]
else: else:
usage(1) usage(1)
ignore = 0
initfile = os.path.expanduser('~/.pynche')
for opt, arg in opts: for opt, arg in opts:
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
usage(0) usage(0)
elif opt in ('-d', '--database'): elif opt in ('-d', '--database'):
RGB_TXT.insert(0, arg) RGB_TXT.insert(0, arg)
elif opt in ('-X', '--ignore'):
ignore = 1
elif opt in ('-i', '--initfile'):
initfile = arg
# create the windows and go # create the windows and go
for f in RGB_TXT: for f in RGB_TXT:
@ -124,11 +143,8 @@ def main():
else: else:
usage(1, 'No color database file found, see the -d option.') usage(1, 'No color database file found, see the -d option.')
# get the initial color as components
red, green, blue = initial_color(initialcolor, colordb)
# create all output widgets # create all output widgets
s = Switchboard(colordb) s = Switchboard(colordb, not ignore and initfile)
# create the application window decorations # create the application window decorations
app = PyncheWidget(__version__, s) app = PyncheWidget(__version__, s)
@ -137,6 +153,20 @@ def main():
s.add_view(StripViewer(s, parent)) s.add_view(StripViewer(s, parent))
s.add_view(ChipViewer(s, parent)) s.add_view(ChipViewer(s, parent))
s.add_view(TypeinViewer(s, parent)) s.add_view(TypeinViewer(s, parent))
# get the initial color as components and set the color on all views. if
# there was no initial color given on the command line, use the one that's
# stored in the option database
if initialcolor is None:
optiondb = s.optiondb()
red = optiondb.get('RED')
green = optiondb.get('GREEN')
blue = optiondb.get('BLUE')
# but if there wasn't any stored in the database, use grey50
if red is None or blue is None or green is None:
red, green, blue = initial_color('grey50', colordb)
else:
red, green, blue = initial_color(initialcolor, colordb)
s.update_views(red, green, blue) s.update_views(red, green, blue)
try: try:
@ -144,6 +174,9 @@ def main():
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
# save the option database
s.save_views(initfile)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -60,7 +60,7 @@ class PyncheWidget:
# Help menu # Help menu
# #
helpmenu = Menu(menubar, name='help', tearoff=0) helpmenu = Menu(menubar, name='help', tearoff=0)
helpmenu.add_command(label='About...', helpmenu.add_command(label='About Pynche...',
command=self.__popup_about, command=self.__popup_about,
underline=0) underline=0)
# #
@ -111,7 +111,7 @@ Color and Hue Editor
Copyright (C) 1998 CNRI Copyright (C) 1998 CNRI
All rights reserved All rights reserved
For information about Pynche contact For information contact
author: Barry A. Warsaw author: Barry A. Warsaw
email : bwarsaw@python.org''' % __version__) email : bwarsaw@python.org''' % __version__)

View File

@ -298,11 +298,14 @@ class StripWidget:
class StripViewer: class StripViewer:
def __init__(self, switchboard, parent=None): def __init__(self, switchboard, parent=None):
self.__sb = switchboard self.__sb = switchboard
optiondb = switchboard.optiondb()
# create a frame inside the parent # create a frame inside the parent
self.__frame = Frame(parent) #, relief=GROOVE, borderwidth=2) self.__frame = Frame(parent) #, relief=GROOVE, borderwidth=2)
self.__frame.grid(row=1, column=0, columnspan=2, sticky='EW') self.__frame.grid(row=1, column=0, columnspan=2, sticky='EW')
uwd = BooleanVar() uwd = self.__uwdvar = BooleanVar()
hexp = BooleanVar() uwd.set(optiondb.get('UPWHILEDRAG', 0))
hexp = self.__hexpvar = BooleanVar()
hexp.set(optiondb.get('HEXSTRIP', 0))
self.__reds = StripWidget(switchboard, self.__frame, self.__reds = StripWidget(switchboard, self.__frame,
generator=constant_cyan_generator, generator=constant_cyan_generator,
axis=0, axis=0,
@ -349,3 +352,7 @@ class StripViewer:
def __togglehex(self, event=None): def __togglehex(self, event=None):
red, green, blue = self.__sb.current_rgb() red, green, blue = self.__sb.current_rgb()
self.update_yourself(red, green, blue) self.update_yourself(red, green, blue)
def save_options(self, optiondb):
optiondb['UPWHILEDRAG'] = self.__uwdvar.get()
optiondb['HEXSTRIP'] = self.__hexpvar.get()

View File

@ -12,13 +12,32 @@ conform to the following interface:
since this would cause it to get updated twice. since this would cause it to get updated twice.
""" """
from types import DictType
import marshal
class Switchboard: class Switchboard:
def __init__(self, colordb): def __init__(self, colordb, initfile):
self.__views = []
self.__colordb = colordb self.__colordb = colordb
self.__optiondb = {}
self.__views = []
self.__red = 0 self.__red = 0
self.__green = 0 self.__green = 0
self.__blue = 0 self.__blue = 0
# read the initialization file
fp = None
if initfile:
try:
try:
fp = open(initfile)
self.__optiondb = marshal.load(fp)
if type(self.__optiondb) <> DictType:
print 'Problem reading options from file:', initfile
self.__optiondb = {}
except (IOError, EOFError):
pass
finally:
if fp:
fp.close()
def add_view(self, view): def add_view(self, view):
self.__views.append(view) self.__views.append(view)
@ -38,3 +57,25 @@ class Switchboard:
def colordb(self): def colordb(self):
return self.__colordb return self.__colordb
def optiondb(self):
return self.__optiondb
def save_views(self, file):
# save the current color
self.__optiondb['RED'] = self.__red
self.__optiondb['GREEN'] = self.__green
self.__optiondb['BLUE'] = self.__blue
for v in self.__views:
v.save_options(self.__optiondb)
fp = None
try:
try:
fp = open(file, 'w')
except IOError:
print 'Cannot write options to file:', file
else:
marshal.dump(self.__optiondb, fp)
finally:
if fp:
fp.close()

View File

@ -21,6 +21,7 @@ import ColorDB
class TextViewer: class TextViewer:
def __init__(self, switchboard, parent=None): def __init__(self, switchboard, parent=None):
self.__sb = switchboard self.__sb = switchboard
optiondb = switchboard.optiondb()
root = self.__root = Toplevel(parent, class_='Pynche') root = self.__root = Toplevel(parent, class_='Pynche')
root.protocol('WM_DELETE_WINDOW', self.__withdraw) root.protocol('WM_DELETE_WINDOW', self.__withdraw)
root.title('Pynche Text Window') root.title('Pynche Text Window')
@ -33,11 +34,20 @@ class TextViewer:
# create the text widget # create the text widget
# #
self.__text = Text(root, relief=SUNKEN, self.__text = Text(root, relief=SUNKEN,
background='black', background=optiondb.get('TEXTBG', 'black'),
foreground='white', foreground=optiondb.get('TEXTFG', 'white'),
width=35, height=15) width=35, height=15)
sfg = optiondb.get('TEXT_SFG')
if sfg:
self.__text.configure(selectforeground=sfg)
sbg = optiondb.get('TEXT_SBG')
if sbg:
self.__text.configure(selectbackground=sbg)
ibg = optiondb.get('TEXT_IBG')
if ibg:
self.__text.configure(insertbackground=ibg)
self.__text.pack() self.__text.pack()
self.__text.insert(0.0, '''\ self.__text.insert(0.0, optiondb.get('TEXT', '''\
Insert some stuff here and play Insert some stuff here and play
with the buttons below to see with the buttons below to see
how the colors interact in how the colors interact in
@ -45,14 +55,23 @@ textual displays.
See how the selection can also See how the selection can also
be affected by tickling the buttons be affected by tickling the buttons
and choosing a color.''') and choosing a color.'''))
self.__text.tag_add(SEL, 6.0, END) insert = optiondb.get('TEXTINS')
if insert:
self.__text.mark_set(INSERT, insert)
try:
start, end = optiondb.get('TEXTSEL', (6.0, END))
self.__text.tag_add(SEL, start, end)
except ValueError:
# selection wasn't set
pass
self.__text.focus_set()
# #
# variables # variables
self.__trackp = BooleanVar() self.__trackp = BooleanVar()
self.__trackp.set(0) self.__trackp.set(optiondb.get('TRACKP', 0))
self.__which = IntVar() self.__which = IntVar()
self.__which.set(0) self.__which.set(optiondb.get('WHICH', 0))
# #
# track toggle # track toggle
self.__t = Checkbutton(root, text='Track color changes', self.__t = Checkbutton(root, text='Track color changes',
@ -130,3 +149,15 @@ and choosing a color.''')
self.__text.configure(selectbackground=colorname) self.__text.configure(selectbackground=colorname)
elif which == 5: elif which == 5:
self.__text.configure(insertbackground=colorname) self.__text.configure(insertbackground=colorname)
def save_options(self, optiondb):
optiondb['TRACKP'] = self.__trackp.get()
optiondb['WHICH'] = self.__which.get()
optiondb['TEXT'] = self.__text.get(0.0, 'end - 1c')
optiondb['TEXTSEL'] = self.__text.tag_ranges(SEL)[0:2]
optiondb['TEXTINS'] = self.__text.index(INSERT)
optiondb['TEXTFG'] = self.__text['foreground']
optiondb['TEXTBG'] = self.__text['background']
optiondb['TEXT_SFG'] = self.__text['selectforeground']
optiondb['TEXT_SBG'] = self.__text['selectbackground']
optiondb['TEXT_IBG'] = self.__text['insertbackground']

View File

@ -20,8 +20,11 @@ class TypeinViewer:
def __init__(self, switchboard, parent=None): def __init__(self, switchboard, parent=None):
# non-gui ivars # non-gui ivars
self.__sb = switchboard self.__sb = switchboard
optiondb = switchboard.optiondb()
self.__hexp = BooleanVar() self.__hexp = BooleanVar()
self.__hexp.set(optiondb.get('HEXTYPE', 0))
self.__uwtyping = BooleanVar() self.__uwtyping = BooleanVar()
self.__uwtyping.set(optiondb.get('UPWHILETYPE', 0))
# create the gui # create the gui
self.__frame = Frame(parent) #, relief=GROOVE, borderwidth=2) self.__frame = Frame(parent) #, relief=GROOVE, borderwidth=2)
self.__frame.grid(row=3, column=1, sticky='NS') self.__frame.grid(row=3, column=1, sticky='NS')
@ -130,3 +133,7 @@ class TypeinViewer:
def hexp_var(self): def hexp_var(self):
return self.__hexp return self.__hexp
def save_options(self, optiondb):
optiondb['HEXTYPE'] = self.__hexp.get()
optiondb['UPWHILETYPE'] = self.__uwtyping.get()

0
Tools/pynche/__init__.py Normal file
View File