#!/usr/bin/env python
#
# $Id: PrefsDialog.py,v 1.5 2001/11/03 11:05:22 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Preferences dialog used by GuiAppD.
This preferences dialog allows the user to edit the values which can
be controled by the preferences file package from 'pybox'.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: PrefsDialog.py,v $',
'rcs_id' : '$Id: PrefsDialog.py,v 1.5 2001/11/03 11:05:22 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'PmwContribD',
'created' : 'Sun, 01-Apr-2001 13:13:45 EDT',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.5 $',
'date' : '$Date: 2001/11/03 11:05:22 $',
}
#
# Import system modules
#
import Pmw
from Tkinter import *
import string
#
# Import Local modules
#
from OptionToggle import OptionToggle
from OptionColorButton import OptionColorButton
from OptionFontButton import OptionFontButton
#
# Module
#
class PrefsDialog(Pmw.Dialog):
"""Preference management dialog.
Options
'buttons' -- Names of buttons to include on the dialog.
'command' -- Callback to execute when a button is pressed. The
button name is the first argument passed, and indicates the
action to take.
"""
def __init__(self, parent=None,
userPrefs={},
**kw):
"""Create a PrefsDialog.
Arguments
'parent=None' -- Widget to serve as parent for dialog.
'userPrefs={}' -- Instance of a class which is a subclass of
'UserPrefs'. The preference names and values are retrieved
from here, and updates are written back out here.
"""
self._user_prefs = userPrefs
optiondefs = (
('buttons', ('Save', 'Cancel'), self._buttons),
('command', self.command, Pmw.INITOPT),
('title', 'Options', self._settitle),
('savecommand', None, Pmw.INITOPT),
)
self.defineoptions(kw, optiondefs)
Pmw.Dialog.__init__(self, parent=parent)
self.withdraw()
inside = self.component('dialogchildsite')
#
# Create a scrolled frame to hold the widgets
# for editing options.
#
frame = self.createcomponent(
'scrolledframe',
(), None,
Pmw.ScrolledFrame,
(inside,),
horizflex='elastic',
#vertflex='shrink',
)
frame_interior = frame.interior()
#
# Create editing widgets
#
widgets = []
prefs = self._user_prefs.keys()
self._entry_fields = {}
self._variables = {}
for pref in prefs:
pref_type = self._user_prefs.allowedValues[pref][0]
pref_comment = self._user_prefs.allowedValues[pref][2]
#print 'Creating ui for %s' % pref
if pref_type == 'boolean':
v = BooleanVar()
v.set(self._user_prefs[pref])
w = self.createcomponent(
pref,
(), None,
OptionToggle,
(frame_interior,),
labelpos='w',
checkbutton_variable=v,
checkbutton_anchor='w',
label_text=pref_comment,
label_justify='left',
)
elif pref_type == 'enum':
v = StringVar()
v.set(str(self._user_prefs[pref]))
w = self.createcomponent(
pref,
(), None,
Pmw.ComboBox,
(frame_interior,),
labelpos='w',
label_text=pref_comment,
entry_textvariable=v,
)
w.component('scrolledlist').setlist(
self._user_prefs.enumValues[pref]
)
elif pref_type == 'integer':
v = IntVar()
v.set(self._user_prefs[pref])
w = self.createcomponent(
pref,
(), None,
Pmw.Counter,
(frame_interior,),
labelpos='w',
label_text=pref_comment,
datatype='integer',
entryfield_entry_textvariable=v,
)
elif pref_type == 'float':
v = DoubleVar()
v.set(self._user_prefs[pref])
w = self.createcomponent(
pref,
(), None,
Pmw.Counter,
(frame_interior,),
labelpos='w',
label_text=pref_comment,
datatype='real',
entryfield_entry_textvariable=v,
)
elif pref_type == 'color':
v = StringVar()
v.set(self._user_prefs[pref])
w = self.createcomponent(
pref,
(), None,
OptionColorButton,
(frame_interior,),
labelpos='w',
label_text=pref_comment,
variable=v,
)
elif pref_type == 'text':
v = StringVar()
w = self.createcomponent(
pref,
(), None,
Pmw.ScrolledText,
(frame_interior,),
labelpos='w',
label_text=pref_comment,
#text_variable=v,
text_width=20,
text_height=5,
text_wrap='word',
)
w.settext(string.replace(self._user_prefs[pref], '|', '\n'))
elif pref_type == 'font':
v = StringVar()
v.set(self._user_prefs[pref])
w = self.createcomponent(
pref,
(), None,
OptionFontButton,
(frame_interior,),
labelpos='w',
label_text=pref_comment,
#title=pref_comment,
variable=v,
)
else:
v = StringVar()
v.set(str(self._user_prefs[pref]))
w = self.createcomponent(
pref,
(), None,
Pmw.EntryField,
(frame_interior,),
labelpos='w',
label_text=pref_comment,
entry_textvariable=v,
)
if pref_type == 'password':
w.configure(entry_show='*')
w.pack(
side=TOP,
expand=YES,
fill=X,
pady=2,
)
widgets.append(w)
self._entry_fields[pref] = w
self._variables[pref] = v
Pmw.alignlabels(widgets)
#
frame.pack(
side=TOP,
expand=YES,
fill=BOTH,
)
self.initialiseoptions(self.__class__)
return
def _text_callback(self, *args):
print 'text callback:', args
return
def _enum_callback(self, variable, value):
#print 'enum callback:', args
variable.set(value)
return
def show(self):
"Update values and show the dialog."
prefs = self._user_prefs.keys()
#prefs.sort()
for pref in prefs:
#self._entry_fields[pref].setentry(self._user_prefs[pref])
value = self._user_prefs[pref]
pref_type = self._user_prefs.allowedValues[pref][0]
self._variables[pref].set(value)
if pref_type == 'enum':
self._entry_fields[pref].setentry(value)
Pmw.Dialog.show(self)
return
def command(self, *args):
"Callback executed when button is pressed."
if args[0] == 'Save':
prefs = self._user_prefs.keys()
for pref in prefs:
#val = self._entry_fields[pref].component('entry').get()
if self._user_prefs.allowedValues[pref][0] == 'text':
val = string.rstrip(self._entry_fields[pref].get())
else:
val = self._variables[pref].get()
self._user_prefs[pref] = val
if self['savecommand']:
self['savecommand'](self._user_prefs)
elif args[0] == 'Cancel':
pass
else:
print 'Command: ', args
#self.withdraw()
self.deactivate()
return
if __name__ == '__main__':
from UserPrefs import UserPrefs
import time
class testprefsrc(UserPrefs):
allowedValues = {
'fontStyle' : ('font', '("Courier",)', 'Font for displaying values'),
'userName':('string', None, 'The name of the user'),
'message':('text', 'one|two', 'Multi-line text'),
'userId':('integer', 0, 'The id number of the user'),
'timeout':('float', 0.0, 'How long to wait?'),
'updatetime':('string', time.ctime(time.time()),
'What time is it?'),
'exampleenum':('enum', 'one', 'Pick one of the enum'),
'booltest':('boolean', 1, 'Toggle the boolean'),
'colortest':('color', 'blue', 'Pick a color'),
}
order = (
'fontStyle',
'userName',
'message',
'userId',
'updatetime',
'timeout',
'booltest',
'exampleenum',
'colortest',
)
enumValues = {
'exampleenum':( 'zero', 'one', 'two', 'three' ),
}
tp = testprefsrc()
pd = PrefsDialog(userPrefs=tp)
pd.show()
pd.activate()
|