widgets.py :  » Business-Application » OpenRMS » openres-0.2.1 » client » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » OpenRMS 
OpenRMS » openres 0.2.1 » client » widgets.py
from Tkinter import *
import string
import re
from tkMessageBox import *
from mx import DateTime
import sys

import config
import openrms

from comp import *

### Global Functions

def apply_config(widget):
  keys = widget.keys()
  if keys:
    for k, v in config.widget.items():
      if keys.count(k):
        widget[k] = v

def sum(sequence):
  retval = 0
  for element in sequence:
    retval += element
  return retval

def format_date(dt_object):
  DateFormat = config.DateFormat
  Delim = config.DateDelim

  datetime_iso = str(dt_object)
  elem = string.split(datetime_iso, ' ')
  date = elem[0]
  elem = string.split(date, '-')
  if DateFormat == 'ymd':
    date_str = ('%s%s%s%s%s' % 
        (elem[0], Delim, elem[1], Delim, elem[2]))
  elif DateFormat == 'mdy':
    date_str = ('%s%s%s%s%s' % 
        (elem[1], Delim, elem[2], Delim, elem[0]))
  elif DateFormat == 'dmy':
    date_str = ('%s%s%s%s%s' % 
        (elem[2], Delim, elem[1], Delim, elem[0]))
  else:
    raise "Invalid or missing config.DateFormat"
  return date_str

def dbbool2yn(dbbool):
  if dbbool:
    if DBAL.is_true(dbbool):
      return lang.Y
    else:
      return lang.N
  else:
    return lang.N

### Widget extensions

# Frame manipulation widgets

class extframe(Frame):
  def __init__(self, master):
    Frame.__init__(self, master)
    apply_config(self)

  def add_extframe(self, name):
    if self.__check_avail_attr:
      retval = extframe(self)
      setattr(self, name, retval)
      return retval
    return False

  def make_pane(self):
    self.config(config.frame)

  def __check_avail_attr(self, name):
    if hasattr(self, name):
      return False
    else:
      return True

  def add_row(self, name):
    if self.__check_avail_attr:
      retval = rowframe(self)
      setattr(self, name, retval)
      return retval
    return False

  def add_col(self, name):
    if self.__check_avail_attr:
      retval = colframe(self)
      setattr(self, name, retval)
      return retval
    return False

  def add_widget(self, name, widget):
    if self.__check_avail_attr:
      setattr(self, name, widget)
    return False

class appframe(extframe):
  def __init__(self, master):
    self.master = master
    Frame.__init__(self, master)
    apply_config(self)
    self.pack(side = 'top', expand = 1, fill='both')
    self.create_widgets()
  def exit(self):
    self.destroy()
  def create_widgets(self):
    pass

class rowframe(extframe):
  def __init__(self, master):
    extframe.__init__(self, master)
    self.pack(fill = 'x', side = 'top', expand = 1)


class colframe(extframe):
  def __init__(self, master):
    extframe.__init__(self, master)
    self.pack(fill = 'y', side = 'left', expand = 1)

class spacerrow(rowframe):
  def __init__(self, master):
    rowframe.__init__(self, master)
    self.Label = unilabel(self, ' ')
    self.Label.pack()

class rowpane(rowframe):
  def __init__(self, master):
    rowframe.__init__(self, master)
    self.make_pane()

class colpane(colframe):
  def __init__(self, master):
    rowframe.__init__(self, master)
    self.make_pane()

# Extended frame manipulation widgets (panes, etc)

class main_window(Tk):
  menuframe = None
  appframe = None
  def __init__(self, app_dict, defaultframe):
    Tk.__init__(self)
    apply_config(self)
    self.InitFrames(app_dict, defaultframe)
  def InitFrames(self, app_dict, defaultframe):
    self.menuframe = menuframe(self, app_dict)
    self.config(menu=self.menuframe)
    self.appframe = defaultframe(self)
  def ChangeApp(self, frame):
    self.appframe.exit()
    self.appframe = frame
    
    

class menuframe(Menu):
  GoMenuDefault = lang.GoToApplication + ":"
  AdminMenuDefault = lang.Administration + ":"
  def __init__(self, master, appdict):
    go_menu = appdict.keys();
    self.master = master
    self.appdict = appdict
    self.commanddict = {}
    for k, v in appdict.iteritems():
      self.commanddict[k] = self.__add_cmd(k)
    Menu.__init__(self, master)
    apply_config(self)
    self.GoText = Variable(self)
    self.GoText.set(self.GoMenuDefault)
    self.AdmText = Variable(self)
    self.AdmText.set(self.AdminMenuDefault)
    self.build_widgets(go_menu)
  def __add_cmd(self, index):
    return lambda: self.GoApp(index)
  def build_widgets(self, go_menu):
    self.GoMenu = Menu(self, tearoff=0)
    apply_config(self.GoMenu)
    self.add_cascade(label="Go", menu=self.GoMenu)
    for v in go_menu:
      self.GoMenu.add_command(label=v, 
        command=self.commanddict[v])


  def GoApp(self, command):
    print command

    if command == lang.Quit:
      self.master.appframe.exit()
      sys.exit()
    appframe=self.appdict[command](self.master)
    self.master.ChangeApp(appframe)


# Label Manipulation Widgets

class unilabel(Label):
  def __init__(self, master, str_value):
    Label.__init__(self, master)
    self.StrVar = StringVar(self)
    self.StrVar.set(str_value)
    self['textvariable'] = self.StrVar
    apply_config(self)
    self.get = self.StrVar.get
    self.set = self.StrVar.set

class desc_row:
  def __init__(self, master, str_values):
    self.labels = []
    for v in str_values:
      self.labels.append(unilabel(master, v))
  def pack(self, **pack_args):
    for label in self.labels:
      label.pack(pack_args)
  def set(self, index, value):
    if index < len(self.labels):
      self.labels[index].StrVar.set(value)
      return 1
    else:
      return 0

# Entry Extensions

class DateEntry(Frame):
  def __init__(self, master, date_obj = None, format = 'ymd', delimiter = '-'):

    if re.match('ymd|mdy|dmy', format):
      pass
    else:
      raise """Invalid Format for DateEntry.  
        Valid values are: mdy, ymd, and dmy"""

    self.__format = format
    Frame.__init__(self, master)
    apply_config(self)
    self.__delimiter = delimiter
    self.__entries = []
    self.__labels = []

    self.__appendentry()
    self.__appendlabel()
    self.__appendentry()
    self.__appendlabel()
    self.__appendentry()
    if date_obj:
      self.set(date_obj)

  def __appendentry(self):
    self.__entries.append(NumberEntry(self))
    if self.__format[(len(self.__entries) - 1)] == 'y':
      self.__entries[(len(self.__entries) - 1)]['width'] = 4
    else:
      self.__entries[(len(self.__entries) - 1)]['width'] = 2
    self.__entries[(len(self.__entries) - 1)].pack(side = 'left')

  def __appendlabel(self):
    self.__labels.append(unilabel(self, self.__delimiter))
    self.__labels[(len(self.__labels) - 1)].pack(side = 'left')
    
  def set(self, date_obj):
    fields = string.split(str(date_obj), ' ')
    date_iso = fields.pop(0)
    date_fields = string.split(date_iso, '-')
    index = 0
    format = self.__format
    while index < 3:
      if format[index] == 'y':
        self.__entries[index].set(date_fields[0])
      elif format[index] == 'm':
        self.__entries[index].set(date_fields[1])
      elif format[index] == 'd':
        self.__entries[index].set(date_fields[2])
      else:
        raise "Error:  Invalid format for DateEntry."
      index += 1
  def get(self):
    elem_list = [None, None, None]
    format = self.__format
    index = 0
    while index < 3:
      if format[index] == 'y':
        elem_list[0] = self.__entries[index].get()
      elif format[index] == 'm':
        elem_list[1] = self.__entries[index].get()
        if int(elem_list[1]) < 10 and len(elem_list[1])\
        == 1:
          elem_list[1] = "0%s" % elem_list[1] 
      elif format[index] == 'd':
        elem_list[2] = self.__entries[index].get()
        if int(elem_list[2]) < 10 and len(elem_list[2])\
        == 1:
          elem_list[2] = "0%s" % elem_list[2] 
      else:
        raise "Error:  Invalid format for DateEntry."
      index += 1
    date_iso = "%s-%s-%s" % (elem_list[0], elem_list[1], 
      elem_list[2])
    retval = DateTime.DateFrom(date_iso)
    return retval
    

class TextEntry(Entry):
  def __init__(self, master, default = ''):
    Entry.__init__(self, master)
    apply_config(self)

    self.StrVar = StringVar(self)
    self.set = self.StrVar.set
    self.get = self.StrVar.get

    self.set(default)
    self['textvariable'] = self.StrVar

class PasswordEntry(TextEntry):
  def __init__(self, master, default = ''):
    TextEntry.__init__(self, master, default)
    self.config(show = '*')

class NumberEntry(Entry):
  def __init__(self, master):
    self._key = '9'
    Entry.__init__(self, master)
    apply_config(self)
    self.bind('<KeyPress>', self.__keypress)
    self._str_var = StringVar(self)
    self['validate'] = 'key'
    self['vcmd'] = self.validate
    self['invcmd'] = self.invalidate
    self['textvariable'] = self._str_var

  def __keypress(self, event):
    self._key = event.keysym

  def validate(self):
    key_ok = re.match("(\d)+|BackSpace|Delete", self._key)
    if (key_ok):
      return 1
    else:
      return 0

  def invalidate(self):
    self.bell()

  def set(self, value):
    value = str(value)
    if re.match("^\d*$", value):
      self._str_var.set(value)
      return 1
    else:
      return 0

class FloatEntry(NumberEntry):
  def validate(self):
    if self._key == 'period':
      if re.match("^\d*\.", self._str_var.get()):
        return 0
      else:
        return 1
    key_ok = re.match("(\d)+|BackSpace|Delete", self._key)
    if (key_ok):
      return 1
    else:
      return 0

  def set(self, value):
    value = str(value)
    if re.match("^\d*\.?\d*$", value):
      self._str_var.set(value)
      return 1
    else:
      return 0

# Misc widget extensions

class Unicheckbutton(Checkbutton):
  def __init__(self, master, label, on = 1, off = 0, checked = None):
    Checkbutton.__init__(self, master)
    apply_config(self)
    self.StrVar = StringVar(self)
    self.Var = StringVar(self)
    self.Var.set(off)

    self.on = on
    self.off = off

    if checked:
      self.Var.set(on)
    self.StrVar.set(label)
    self.config(textvariable = self.StrVar, variable = self.Var,
      onvalue = on, offvalue = off)

    self.setLabel = self.StrVar.set
    self.getLabel = self.StrVar.get
    self.set = self.Var.set
    self.get = self.Var.get

class Radio_Label(Frame):
  def __init__(self, master, StrVar, value, label):
    Frame.__init__(self, master)
    apply_config(self)

    self.Radio = Radiobutton(self)
    apply_config(self.Radio)
    self.Radio['variable'] = StrVar
    self.Radio['value'] = value
    self.Radio['text'] = label
    self.Radio.pack(side = 'left')

class ExtOptionMenu(OptionMenu):
  def __init__ (self, master, default, items, *add_items):

    self.StrVar = StringVar(master)
    self.StrVar.set(default)
    self.get = self.StrVar.get
    self.set = self.StrVar.set

    self.default = default
    self.RevertOnUpdate = 0

    self.OnUpdate = None
    add_items = list(add_items)

    methods = dir(items)
    if hasattr(items, 'pop'):
      items_is_list = 1
      value = items.pop(0)
    else:
      items_is_list = 0
      value = items

    OptionMenu.__init__(self, master, self.StrVar, value)
    self.Menu = self.children['menu']
    self.Menu.entryconfig(0, command = self.__onUpdate)
    if items_is_list:
      while len(items):
        value = items.pop(0)
        self.addOption(value)
    for add_value in add_items:
      if hasattr(add_value, 'pop'):
        for value in add_value:
          self.addOption(value)
      else:
        self.addOption(add_value)
          

    apply_config(self)
    apply_config(self.Menu)

    ei = 0
    excepted = None

  def addOption(self, option):
    self.Menu.add_command(Widget)
    self.Menu.entryconfig('end', label = option)
    self.Menu.entryconfig('end', command = self.__onUpdate)

  def RevertDefault(self):
    self.StrVar.set(self.default)

  def __onUpdate(self):
    value = self.Menu.entrycget('active', 'label')

    if (self.RevertOnUpdate == None or self.RevertOnUpdate == 0 or
      self.RevertOnUpdate == ''):
      self.StrVar.set(value)
    if self.OnUpdate:
      self.OnUpdate(value)
    

def chooseColor():
  widget = Frame()
  retval = widget.tk.call('tk_chooseColor')
  widget.destroy()
  return retval

# Larger Widget extensions

class ynna_radiobutton(Frame):
  def __init__(self, master, na_val= '', default = None, orient = 'row'):
    if orient != 'row' and orient != 'col':
      raise 'orient must equal "row" or "col"'

    if default == None:
      default = na_val

    Frame.__init__(self, master)
    apply_config(self)
    self.StrVar = StringVar(self)
    self.StrVar.set(default)
    self.na_val = na_val
    self.__lblformat = '  %s '
    self.set = self.StrVar.set

    self.Y = Radio_Label(self, self.StrVar, '1', 
      self.__lblformat % lang.Y)
    self.N = Radio_Label(self, self.StrVar, '0',
      self.__lblformat % lang.N)
    self.NA = Radio_Label(self, self.StrVar, self.na_val,
      self.__lblformat % lang.NA)

    if orient == 'row':
      self.Y.pack(side = 'left')
      self.N.pack(side = 'left')
      self.NA.pack(side = 'left')
    elif orient == 'col':
      self.Y.pack()
      self.N.pack()
      self.NA.pack()
    else:
      raise 'Invalid orient for ynna_radiobutton'

  def get(self):
    return self.StrVar.get()

class yn_radiobutton(ynna_radiobutton):
  def __init__(self, master, default = None, orient = 'row'):
    ynna_radiobutton.__init__(self, master, '', default,
      'row')
    self.NA.destroy()

class grid_listbox(rowframe):
  def __init__(self, master, colwidths, labels):
    rowframe.__init__(self, master)
    self.master = master
    self.colwidths = colwidths
    self.OnDoubleClick = None
    self.OnSelect = None

    self.main_row = rowframe(self)
    while len(labels) < len(colwidths):
      labels.append(' ')
    self.__labels = labels

    self.Listbox = Listbox(self.main_row)
    apply_config(self.Listbox)
    self.Listbox['width'] = sum(self.colwidths)
    self.Listbox['font'] = 'courier'
    self.Listbox.bind('<Double-Button-1>', self.__OnDoubleClick, 
      '+')
    self.Listbox.bind('<ButtonRelease-1>', self.__OnSelect, '+')
    self.Listbox.bind('<KeyRelease>', self.__OnKeyRelease, '+')
    self.AddRow(0, self.__labels)

    self.ScrollBar = Scrollbar(self.main_row)
    apply_config(self.ScrollBar)
    
    self.Listbox['xscrollcommand'] = self.ScrollBar.set
    self.ScrollBar['command'] = self.Listbox.yview

    self.PackAll()

  def __format(self, str_list):
    retval = []
    for colwidth in self.colwidths:
      if len(str_list):
        value = str_list.pop(0)
      else:
        value = ' '
      if len(str(value)) < colwidth:
        value_str = string.ljust(str(value), colwidth)
      elif len(str(value)) == colwidth:
        value_str = str(value)
      else:
        value_temp = str(value)
        value_str = value_temp[0:colwidth]
      retval.append(value_str)
    return retval
  def AddRow(self, id, values):
    line_str = ""
    values = self.__format(values)
    for value in values:
      line_str += value
    line_str += "  %s" % id
    self.Listbox.insert('end', line_str)
  def PackAll(self):
    self.main_row.pack(side = 'top')
    self.Listbox.pack(side = 'left')
    self.ScrollBar.pack(side = 'left', fill = 'y')
  def __GetRowID(self, row):
    elems = re.split("\s", row)
    id = elems.pop()
    return id
  def __OnKeyRelease(self, event):
    key = event.keysym
    # print key
    if key == 'Up' or key == 'Down':
      self.__OnSelect(event)
    elif key == 'Return':
      self.__OnDoubleClick(event)
  def __OnSelect(self, event):
    if self.OnSelect != None:
      row = self.Listbox.selection_get()
      id = self.__GetRowID(row)
      if id != '0':
        self.OnSelect(id)

  def __OnDoubleClick(self, event):

    row = self.Listbox.selection_get()
    id = self.__GetRowID(row)
    if id != '0':
      if self.OnDoubleClick != None:  # has been set
        self.OnDoubleClick(id)

  def GetSelectID(self):
    row = self.Listbox.selection_get()
    id = self.__GetRowID(row)
    return id
    
  def GetList(self):
    retval = []
    rows = self.ListBox.get()
    rows = string.split("\n", rows)
    for row in rows:
      id = self.__GetRowID(row)
    retval.append(id)
    return retval

  def destroy(self):
    self.OnSelect = None
    Frame.destroy(self)

####  Dialog Boxes

#### Common App Frames

class configframe(appframe):
  def create_widgets(self):
    self.CreateTopRow()
    self.CreateDBPane()
    self.CreateThemesPane()
    self.CreateCompPane()
    self.CreateBtnRow()

  def CreateTopRow(self):
    self.TopRow = rowframe(self)
    self.AppTitle = unilabel(
      self.TopRow, '%s: ' % lang.ApplicationConfiguration
    )
    self.AppTitle.pack(side = 'left')
    self.AppStatus = unilabel(self.TopRow, ' ')
    self.AppStatus.pack(side = 'right')

  def CreateDBPane(self):
    self.add_row('DBPane')
    self.DBPane.make_pane()
    self.DBPane.PaneDesc = unilabel(
      self.DBPane, '%s: ' % lang.DatabaseConfiguration
    )
    self.DBPane.PaneDesc.pack(side = 'left')
    self.DBPane.DBBtn = Button(self.DBPane)
    apply_config(self.DBPane.DBBtn)
    self.DBPane.DBBtn.config(
      text = lang.Configure, command = self.OnDBBtn
    )
    self.DBPane.DBBtn.pack(side = 'right')

  def OnDBBtn(self):
    self.ShowDBConfigPane()

  def CreateThemesPane(self):
    self.ThemesPane = rowpane(self)
    pane = self.ThemesPane
    pane.DescLbl = unilabel(pane, '%s: ' % lang.ThemesAndColors)
    pane.DescLbl.pack(side = 'left')
    pane.Btn = Button(pane)
    apply_config(pane.Btn)
    pane.Btn.config(
      text = lang.Configure, command = self.OnThemeBtn
    )
    pane.Btn.pack(side = 'right')

  def OnThemeBtn(self):
    self.ShowTCP()

  def CreateCompPane(self):
    self.CompPane = rowpane(self)
    pane = self.CompPane
    pane.DescLbl = unilabel(
      pane, '%s: ' % lang.ApplicationComponents
    )
    pane.DescLbl.pack(side = 'left')
    pane.Btn = Button(pane)
    apply_config(pane.Btn)
    pane.Btn.config(
      text = lang.Configure, command = self.ShowCCP
    )
    pane.Btn.pack(side = 'right')

  def OnCompBtn(self):
    self.ShowCCP()

  def CreateBtnRow(self):
    self.BtnRow = rowframe(self)
    row = self.BtnRow

    row.Try = colframe(row)
    row.Try.pack_forget()
    row.Try.pack(side = 'left')
    row.Try.Btn = Button(row.Try)
    apply_config(row.Try.Btn)
    row.Try.Btn.config(text = lang.Try, command = self.OnTryBtn)
    row.Try.Btn.pack()

    row.Save = colframe(row)
    row.Save.pack_forget()
    row.Save.pack(side = 'left')
    row.Save.Btn =  Button(row.Save)
    apply_config(row.Save.Btn)
    row.Save.Btn.config(text = lang.Save, command = self.OnSaveBtn)
    row.Save.Btn.pack()

    row.Cancel = colframe(row)
    row.Cancel.pack_forget()
    row.Cancel.pack(side = 'right')
    row.Cancel.Btn = Button(row.Cancel)
    apply_config(row.Cancel.Btn)
    row.Cancel.Btn.config(
      text = lang.Cancel, command = self.OnCancelBtn
    )
    row.Cancel.Btn.pack()

    row.Save.pack_forget()
    row.Save.pack(side = 'left', fill = 'both', expand = 1)

  def OnTryBtn(self):
    self.destroy()

  def OnSaveBtn(self):
    self.AppStatus.set('%s...' % lang.SavingConfiguration)
    self.SaveConfig()
    reload(config)
    self.AppStatus.set(' ')

  def OnCancelBtn(self):
    reload(config)

  def ShowDBConfigPane(self):
    self.pack_forget()
    self.DBCP = rowpane(self.master)

    self.DBCP.add_row('UserRow')
    self.DBCP.UserRow.Label = unilabel(
      self.DBCP.UserRow, '%s: ' % lang.UserName
    )
    self.DBCP.UserRow.Entry = TextEntry(
      self.DBCP.UserRow, config.user
    )
    self.DBCP.UserRow.Label.pack(side = 'left')
    self.DBCP.UserRow.Entry.pack(side = 'right')

    self.DBCP.add_row('PasswdRow')
    self.DBCP.PasswdRow.Label = unilabel(
      self.DBCP.PasswdRow, '%s: ' % lang.Password
    )
    self.DBCP.PasswdRow.Entry = PasswordEntry(
      self.DBCP.PasswdRow, config.password
    )
    self.DBCP.PasswdRow.Label.pack(side = 'left')
    self.DBCP.PasswdRow.Entry.pack(side = 'right')

    self.DBCP.add_row('DBRow')
    self.DBCP.DBRow.Label = unilabel(
      self.DBCP.DBRow, '%s: ' % lang.Database
    )
    self.DBCP.DBRow.Entry = TextEntry(
      self.DBCP.DBRow, config.dsn
    )
    self.DBCP.DBRow.Label.pack(side = 'left')
    self.DBCP.DBRow.Entry.pack(side = 'left')

    self.DBCP.space1 = spacerrow(self.DBCP)
    self.DBCP.add_row('BtnRow')
    self.DBCP.BtnRow.Apply = Button(self.DBCP.BtnRow)
    apply_config(self.DBCP.BtnRow.Apply)
    self.DBCP.BtnRow.Apply.config(
      text = lang.Apply, command = self.OnDBCPApply
    )
    self.DBCP.BtnRow.Apply.pack(side = 'left')
    self.DBCP.BtnRow.Cancel = Button(self.DBCP.BtnRow)
    apply_config(self.DBCP.BtnRow.Cancel)
    self.DBCP.BtnRow.Cancel.config(
      text = lang.Cancel, command = self.OnDBCPCancel
    )
    self.DBCP.BtnRow.Cancel.pack(side = 'right')
    
  def OnDBCPApply(self):
    self.ApplyConfig(
      user = self.DBCP.UserRow.Entry.get(),
      password = self.DBCP.PasswdRow.Entry.get(),
      dsn = self.DBCP.DBRow.Entry.get()
    )
    self.OnDBCPCancel()

  def OnDBCPCancel(self):
    self.DBCP.pack_forget()
    self.DBCP.destroy()
    del self.DBCP
    self.pack()

  def ShowTCP(self):

    self.pack_forget()
    self.TCP = rowpane(self.master)
    pane = self.TCP
    pane.widget_prop = config.widget.copy()
    pane.frame_prop = config.frame.copy()

    pane.add_row('TopRow')
    row = self.TopRow
    pane.Title = unilabel(row, lang.ThemesAndColors)
    pane.Title.pack(side = 'left')
    pane.Status = unilabel(row, ' ')
    pane.Status.pack(side = 'right')

    pane.add_row('Widget')
    row = pane.Widget

    row.label = unilabel(row, '%s: ' % lang.Colors)

    row.menu = ExtOptionMenu(row, lang.Property, 
      ['background', 'foreground', 'activebackground', 
        'activeforeground']
    )
    row.menu.pack(side = 'left')
    row.colorbtn = Button(row)
    apply_config(row.colorbtn)
    row.colorbtn.config(
      text = lang.Colors, command = self.OnTCPWidgetColorBtn
    )
    row.colorbtn.pack(side = 'left')

    pane.add_row('PaneRow')
    row = pane.PaneRow
    row.label = unilabel(row, '%s: ' % lang.Panes)
    row.label.pack(side = 'left')
    row.bdlabel = unilabel (row, '  %s: ' % lang.Border)
    row.bdlabel.pack(side = 'left')
    row.border = NumberEntry(row)
    row.border.config(width = 2)
    row.border.pack(side = 'left', padx = 3)
    row.relief = ExtOptionMenu(row, 'Relief', 
      ['raised', 'sunken', 'flat', 'ridge', 'solid', 'groove']
    )
    row.relief.pack(side = 'left', padx = 3)
    row.Update = Button(row)
    apply_config(row.Update)
    row.Update.config(
      text = lang.Update, command = self.OnTCPPaneUpdateBtn
    )
    row.Update.pack(side = 'right')

    pane.spacer1 = spacerrow(pane)
    pane.add_row('Theme')
    row = pane.Theme
    row.Menu = ExtOptionMenu(row, lang.ChooseTheme,
      lang.No_Change__, config.Themes.keys())
    row.Menu.OnUpdate = self.OnTCPThemeUpdate
    row.Menu.pack(side = 'left')
    row.Name = TextEntry(row)
    row.Save = Button(row)
    apply_config(row.Save)
    row.Save.config(text = lang.Save, command = self.OnTCPSaveBtn)
    row.Save.pack(side = 'right')
    row.Name.pack(side = 'right')
    row.SaveLbl = unilabel(row, lang.SaveAs)

    pane.spacer2 = spacerrow(pane)
    pane.add_row('Btns')
    row = pane.Btns
    row.Apply = Button(row)
    apply_config(row.Apply)
    row.Apply.config(text = lang.Apply, command = self.OnTCPApply)
    row.Apply.pack(side = 'left')
    row.Cancel = Button(row)
    apply_config(row.Cancel)
    row.Cancel.config(
      text = lang.Cancel, command = self.OnTCPCancel
    )
    row.Cancel.pack(side = 'right')

  def OnTCPWidgetColorBtn(self):
    color = chooseColor()
    property = self.TCP.Widget.menu.get()
    if property == lang.Property:
      return 0
    else:
      self.TCP.widget_prop[property] = color

  def OnTCPPaneUpdateBtn(self):
    bd = self.TCP.PaneRow.border.get()
    if bd:
      self.TCP.frame_prop['bd'] = bd
    relief = self.TCP.PaneRow.relief.get()
    if relief != 'Relief':
      self.TCP.frame_prop['relief'] = relief

  def OnTCPThemeUpdate(self, themename):
    if config.Themes.keys().count(themename):
      self.TCP.widget_prop = \
        config.Themes[themename]['widget']
      self.TCP.frame_prop = config.Themes[themename]['frame']
      self.TCP.widget_prop['current_theme'] = themename

  def OnTCPSaveBtn(self):
    theme_name = self.TCP.Theme.Name.get()
    config.widget['current_theme'] = theme_name
    config.Themes[theme_name] = {}
    config.Themes[theme_name]['widget'] = config.widget
    config.Themes[theme_name]['frame'] = config.frame

  def OnTCPApply(self):
    self.ApplyConfig(
      widget = self.TCP.widget_prop,
      frame = self.TCP.frame_prop
    )
    self.OnTCPCancel()

  def OnTCPCancel(self):
    self.TCP.pack_forget()
    self.TCP.destroy()
    del self.TCP
    self.pack()

  def ShowCCP(self):
    self.pack_forget()
    self.CCP = rowframe(self.master)
    pane = self.CCP

    pane.add_row('TopRow')
    row = pane.TopRow
    pane.Title = unilabel(row, '%s:' % lang.ApplicationComponents)
    pane.Title.pack(side = 'left')
    pane.Status = unilabel(row, ' ')
    pane.Status.pack(side = 'right')

    pane.add_row('DBALPane')
    subpane = pane.DBALPane
    subpane.make_pane()

    subpane.add_row('TopRow')
    row = subpane.TopRow
    subpane.Title = unilabel(row, 'DBAL:')

    subpane.add_row('Select')
    row = subpane.Select
    row.Label = unilabel(row, '%s: ' % lang.ChooseSQLEngine)
    row.Label.pack(side = 'left')
    row.Menu = ExtOptionMenu(row, lang.Available,
      config.DBALS.keys())
    row.Menu.pack(side = 'left')
    row.Choose = Button(row)
    apply_config(row.Choose)
    row.Choose.config(
      text = lang.Use, command = self.OnCCPDBALChooseBtn
    )
    row.Choose.pack(side = 'left', padx = 3)
    row.Delete = Button(row)
    apply_config(row.Delete)
    row.Delete.config(
      text = lang.Delete, command = self.OnCCPDBALDeleteBtn
    )
    row.Delete.pack(side = 'left')
    row.Confirm = Unicheckbutton(row, lang.Confirm)
    row.Confirm.pack(side = 'left')

    subpane.add_row('Add')
    row = subpane.Add
    row.NameLbl = unilabel(row, '%s: ' % lang.Name)
    row.NameLbl.pack(side = 'left')
    row.Name = TextEntry(row)
    row.Name.pack(side = 'left')
    row.FileBtn = Button(row)
    apply_config(row.FileBtn)
    row.FileBtn.config(
      text = lang.File, command = self.OnCCPDBALFileBtn
    )
    row.FileBtn.pack(side = 'left', padx = 3)

    pane.add_row('LangPane')
    subpane = pane.LangPane
    subpane.make_pane()
    

    subpane.add_row('Select')
    row = subpane.Select
    subpane.Title = unilabel(row, '%s:' % lang.Languages)
    subpane.Title.pack(side = 'left')
    row.Menu = ExtOptionMenu(row, lang.Select, config.langs.keys())
    row.Menu.pack(side = 'left')
    row.Choose = Button(row)
    apply_config(row.Choose)
    row.Choose.config(
      text = lang.Use, command = self.OnCCPlangChooseBtn
    )
    row.Choose.pack(side = 'left')
    row.Delete = Button(row)
    apply_config(row.Delete)
    row.Delete.config(
      text = lang.Delete, command = self.OnCCPlangDeleteBtn
    )
    row.Confirm = Unicheckbutton(row, lang.Confirm)
    row.Confirm.pack(side = 'right')
    row.Delete.pack(side = 'right')

    subpane.add_row('Add')
    row = subpane.Add
    row.NameLbl = unilabel(row, '%s: ' % lang.Name)
    row.NameLbl.pack(side = 'left')
    row.Name = TextEntry(row)
    row.Name.pack(side = 'left')
    row.File = Button(row)
    apply_config(row.File)
    row.File.config(
      text = lang.File, command = self.OnCCPlangFileBtn
    )
    row.File.pack(side = 'right')

    pane.add_row('DateTime')
    subpane = pane.DateTime
    subpane.make_pane()

    subpane.add_row('Configure')
    row = subpane.Configure
    subpane.Title = unilabel(row, '%s: ' % lang.Date)
    subpane.Title.pack(side = 'left')
    row.Format = ExtOptionMenu(
      row,
      lang.DateFormat,
      [lang.DMY, lang.MDY, lang.YMD]
    )
    row.Format.pack(side = 'left')
    row.DelimLbl = unilabel(row, '  %s: ' % lang.Delimiter)
    row.DelimLbl.pack(side = 'left')
    row.Delim = TextEntry(row)
    row.Delim.config(width = 2)
    row.Delim.pack(side = 'left')
    row.Update = Button(row)
    apply_config(row.Update)
    row.Update.config(
      text = lang.Update, command = self.OnCCPDateUpdateBtn
    )
    row.Update.pack(side = 'left', padx = 3)

    pane.add_row('BtnRow')
    row = pane.BtnRow
    row.Apply = Button(row)
    apply_config(row.Apply)
    row.Apply.config(text = lang.Apply, command = self.OnCCPApply)
    row.Apply.pack(side = 'left')
    row.Cancel = Button(row)
    apply_config(row.Cancel)
    row.Cancel.config(
      text = lang.Cancel, command = self.OnCCPCancel
    )
    row.Cancel.pack(side = 'right')
    
  def OnCCPDBALChooseBtn(self):
    DBAL = self.CCP.DBALPane.Select.Menu.get()
    self.DBAL = config.DBALS[DBAL]

  def OnCCPDBALDeleteBtn(self):
    DBAL = self.CCP.DBALPane.Select.Menu.get()
    confirm = int(self.CCP.DBALPane.Select.Confirm.get())
    if confirm:
      if config.DBALS.keys().count(DBAL):
        del config.DBALS[DBAL]
        self.OnCCPCancel()
        self.ShowCCP()
    else:
      self.CCP.Status.set('%s...' % lang.MustConfirm)
      self.bell()

  def OnCCPDBALFileBtn(self):
    file = self.tk.call('tk_getOpenFile', '-initialdir', './')
    file = re.split('/', file).pop()
    config.DBALS[self.CCP.DBALPane.Add.Name.get()] = file

  def OnCCPlangChooseBtn(self):
    lang = self.CCP.LangPane.Select.Menu.get()
    self.CCP.lang = config.langs[lang]

  def OnCCPlangDeleteBtn(self):
    lang = self.CCP.LangPane.Select.Menu.get()
    if int(self.CCP.LangPane.Select.Confirm.get()):
      if config.langs.keys().count(lang):
        del config.langs[lang]
        self.OnCCPCancel()
        self.ShowCCP()

  def OnCCPlangFileBtn(self):
    file = self.tk.call('tk_getOpenFile', '-initialdir', './')
    file = re.split('/', file).pop()
    config.langs[self.CCP.LangPane.Add.Name.get()] = file

  def OnCCPDateUpdateBtn(self):
    format = self.CCP.DateTime.Configure.Format.get()
    delim = self.CCP.DateTime.Configure.Delim.get()

    if format == lang.DMY:
      format = 'dmy'
    elif format == lang.MDY:
      format = 'mdy'
    elif format == lang.YMD:
      format = 'ymd'
    else:
      self.Status.set(lang.InvalidDateFormat)
      self.bell()
      return 0

    self.ApplyConfig(DateDelim = delim, DateFormat = format)

  def OnCCPApply(self):
    if bool(hasattr(self.CCP, 'DBAL')) == False or\
      bool(hasattr(self.CCP, 'lang')) == False:

      comp = file('comp.py', 'r')
      linelist = comp.readlines()
      comp.close()

      for line in linelist:
        if re.match('DBAL =', line) and \
          bool(hasattr(self.CCP, 'DBAL')) == \
            False:
          elems = re.split('\s=\s', line)
          self.CCP.DBAL = elems.pop()
        if re.match('lang =', line) and \
          bool(hasattr(self.CCP, 'lang')) == \
            False:
          elems = re.split('\s=\s', line)
          self.CCP.lang = elems.pop()
          
    comp = file('comp.py', 'w')
    comp.writelines(
      ['import %s\n' % self.CCP.DBAL,
        'import %s\n' % self.CCP.lang,
        'DBAL = %s\n' % self.CCP.DBAL,
        'lang = %s\n' % self.CCP.lang]
    )
    comp.close()
    reload(lang)
    reload(DBAL)

  def OnCCPCancel(self):
    self.CCP.pack_forget()
    self.CCP.destroy()
    del self.CCP
    self.pack()

  def ApplyConfig(self, **kw):
    for k, v in kw.items():
      setattr(config, k, v)
    
  def SaveConfig(self):
    config_items = dir(config)
    config_file = file('config.py', 'w')
    lines = []
    for item in config_items:
      if bool(re.match('__', item)) == False:
        var = getattr(config, item)
        if isinstance(var, str):
          var = "'%s'" % var
        lines.append('%s = %s\n' % (item, var)
        )
    config_file.writelines(lines)
    config_file.close()
    reload(config)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.