2012-02-27 13:51:52 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
import __init__
|
|
|
|
|
2012-04-08 20:30:49 +00:00
|
|
|
import wx, wx.lib.stattext, os, sys, platform, types
|
2012-02-27 13:51:52 +00:00
|
|
|
|
2012-08-31 13:22:30 +00:00
|
|
|
from util import validators
|
2012-03-28 12:26:40 +00:00
|
|
|
from util import profile
|
2012-02-27 13:51:52 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
app = wx.App(False)
|
|
|
|
mainWindow()
|
|
|
|
app.MainLoop()
|
|
|
|
|
|
|
|
class configWindowBase(wx.Frame):
|
|
|
|
"A base class for configuration dialogs. Handles creation of settings, and popups"
|
2012-10-29 08:07:40 +00:00
|
|
|
def __init__(self, title, style=wx.DEFAULT_FRAME_STYLE):
|
|
|
|
super(configWindowBase, self).__init__(None, title=title, style=style)
|
2012-02-27 13:51:52 +00:00
|
|
|
|
|
|
|
self.settingControlList = []
|
2012-03-12 18:52:55 +00:00
|
|
|
|
2012-02-27 13:51:52 +00:00
|
|
|
#Create the popup window
|
2012-03-14 16:11:42 +00:00
|
|
|
self.popup = wx.PopupWindow(self, flags=wx.BORDER_SIMPLE)
|
2012-02-27 13:51:52 +00:00
|
|
|
self.popup.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_INFOBK))
|
|
|
|
self.popup.setting = None
|
|
|
|
self.popup.text = wx.StaticText(self.popup, -1, '');
|
|
|
|
self.popup.sizer = wx.BoxSizer()
|
|
|
|
self.popup.sizer.Add(self.popup.text, flag=wx.EXPAND|wx.ALL, border=1)
|
|
|
|
self.popup.SetSizer(self.popup.sizer)
|
2012-03-12 18:52:55 +00:00
|
|
|
|
2012-02-27 13:51:52 +00:00
|
|
|
def CreateConfigTab(self, nb, name):
|
|
|
|
leftConfigPanel, rightConfigPanel, configPanel = self.CreateConfigPanel(nb)
|
|
|
|
nb.AddPage(configPanel, name)
|
|
|
|
return leftConfigPanel, rightConfigPanel
|
|
|
|
|
|
|
|
def CreateConfigPanel(self, parent):
|
|
|
|
configPanel = wx.Panel(parent);
|
|
|
|
leftConfigPanel = wx.Panel(configPanel)
|
|
|
|
rightConfigPanel = wx.Panel(configPanel)
|
|
|
|
sizer = wx.GridBagSizer(2, 2)
|
|
|
|
leftConfigPanel.SetSizer(sizer)
|
|
|
|
sizer = wx.GridBagSizer(2, 2)
|
|
|
|
rightConfigPanel.SetSizer(sizer)
|
|
|
|
sizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
configPanel.SetSizer(sizer)
|
2012-06-28 22:23:47 +00:00
|
|
|
sizer.Add(leftConfigPanel, border=35, flag=wx.RIGHT)
|
2012-02-27 13:51:52 +00:00
|
|
|
sizer.Add(rightConfigPanel)
|
|
|
|
leftConfigPanel.main = self
|
|
|
|
rightConfigPanel.main = self
|
|
|
|
return leftConfigPanel, rightConfigPanel, configPanel
|
2012-03-12 18:52:55 +00:00
|
|
|
|
2012-02-27 13:51:52 +00:00
|
|
|
def OnPopupDisplay(self, setting):
|
|
|
|
self.popup.setting = setting
|
|
|
|
self.UpdatePopup(setting)
|
|
|
|
self.popup.Show(True)
|
|
|
|
|
|
|
|
def OnPopupHide(self, e):
|
|
|
|
self.popup.Show(False)
|
|
|
|
|
|
|
|
def UpdatePopup(self, setting):
|
|
|
|
if self.popup.setting == setting:
|
|
|
|
if setting.validationMsg != '':
|
|
|
|
self.popup.text.SetLabel(setting.validationMsg + '\n\n' + setting.helpText)
|
|
|
|
else:
|
|
|
|
self.popup.text.SetLabel(setting.helpText)
|
|
|
|
self.popup.text.Wrap(350)
|
|
|
|
self.popup.Fit()
|
2012-10-15 09:58:00 +00:00
|
|
|
x, y = setting.ctrl.ClientToScreenXY(0, 0)
|
|
|
|
sx, sy = setting.ctrl.GetSizeTuple()
|
2012-03-09 14:47:39 +00:00
|
|
|
#if platform.system() == "Windows":
|
|
|
|
# for some reason, under windows, the popup is relative to the main window... in some cases. (Wierd ass bug)
|
|
|
|
# wx, wy = self.ClientToScreenXY(0, 0)
|
|
|
|
# x -= wx
|
|
|
|
# y -= wy
|
|
|
|
self.popup.SetPosition((x, y+sy))
|
2012-02-27 13:51:52 +00:00
|
|
|
|
|
|
|
def updateProfileToControls(self):
|
|
|
|
"Update the configuration wx controls to show the new configuration settings"
|
|
|
|
for setting in self.settingControlList:
|
2012-03-05 21:30:54 +00:00
|
|
|
if setting.type == 'profile':
|
2012-03-17 11:03:38 +00:00
|
|
|
setting.SetValue(profile.getProfileSetting(setting.configName))
|
2012-03-05 21:30:54 +00:00
|
|
|
else:
|
2012-03-17 11:03:38 +00:00
|
|
|
setting.SetValue(profile.getPreference(setting.configName))
|
2012-09-02 12:26:39 +00:00
|
|
|
self.Update()
|
2012-02-27 13:51:52 +00:00
|
|
|
|
|
|
|
class TitleRow():
|
|
|
|
def __init__(self, panel, name):
|
|
|
|
"Add a title row to the configuration panel"
|
|
|
|
sizer = panel.GetSizer()
|
2012-03-09 14:50:37 +00:00
|
|
|
x = sizer.GetRows()
|
2012-02-27 13:51:52 +00:00
|
|
|
self.title = wx.StaticText(panel, -1, name)
|
2012-03-09 15:12:59 +00:00
|
|
|
self.title.SetFont(wx.Font(wx.SystemSettings.GetFont(wx.SYS_ANSI_VAR_FONT).GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD))
|
2012-10-15 09:58:00 +00:00
|
|
|
sizer.Add(self.title, (x,0), (1,3), flag=wx.EXPAND|wx.TOP|wx.LEFT, border=10)
|
|
|
|
sizer.Add(wx.StaticLine(panel), (x+1,0), (1,3), flag=wx.EXPAND|wx.LEFT,border=10)
|
2012-03-09 14:50:37 +00:00
|
|
|
sizer.SetRows(x + 2)
|
2012-02-27 13:51:52 +00:00
|
|
|
|
|
|
|
class SettingRow():
|
2012-03-05 21:30:54 +00:00
|
|
|
def __init__(self, panel, label, configName, defaultValue = '', helpText = 'Help: TODO', type = 'profile'):
|
2012-02-27 13:51:52 +00:00
|
|
|
"Add a setting to the configuration panel"
|
|
|
|
sizer = panel.GetSizer()
|
|
|
|
x = sizer.GetRows()
|
2012-03-09 14:47:39 +00:00
|
|
|
y = 0
|
2012-07-30 10:26:30 +00:00
|
|
|
flag = 0
|
2012-02-27 13:51:52 +00:00
|
|
|
|
|
|
|
self.validators = []
|
|
|
|
self.validationMsg = ''
|
|
|
|
self.helpText = helpText
|
|
|
|
self.configName = configName
|
|
|
|
self.panel = panel
|
2012-03-05 21:30:54 +00:00
|
|
|
self.type = type
|
2012-04-08 20:30:49 +00:00
|
|
|
|
|
|
|
self.label = wx.lib.stattext.GenStaticText(panel, -1, label)
|
|
|
|
self.label.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
|
|
|
|
self.label.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
|
|
|
|
|
2012-03-17 11:03:38 +00:00
|
|
|
getSettingFunc = profile.getPreference
|
2012-03-05 21:30:54 +00:00
|
|
|
if self.type == 'profile':
|
2012-03-17 11:03:38 +00:00
|
|
|
getSettingFunc = profile.getProfileSetting
|
2012-03-07 15:58:04 +00:00
|
|
|
if isinstance(defaultValue, types.StringTypes):
|
2012-03-17 11:03:38 +00:00
|
|
|
self.ctrl = wx.TextCtrl(panel, -1, getSettingFunc(configName))
|
2012-03-07 15:58:04 +00:00
|
|
|
self.ctrl.Bind(wx.EVT_TEXT, self.OnSettingChange)
|
2012-07-30 10:26:30 +00:00
|
|
|
flag = wx.EXPAND
|
2012-03-07 15:58:04 +00:00
|
|
|
elif isinstance(defaultValue, types.BooleanType):
|
|
|
|
self.ctrl = wx.CheckBox(panel, -1, style=wx.ALIGN_RIGHT)
|
2012-03-17 11:03:38 +00:00
|
|
|
self.SetValue(getSettingFunc(configName))
|
2012-03-07 15:58:04 +00:00
|
|
|
self.ctrl.Bind(wx.EVT_CHECKBOX, self.OnSettingChange)
|
2012-07-30 10:26:30 +00:00
|
|
|
elif isinstance(defaultValue, wx.Colour):
|
|
|
|
self.ctrl = wx.ColourPickerCtrl(panel, -1)
|
|
|
|
self.SetValue(getSettingFunc(configName))
|
|
|
|
self.ctrl.Bind(wx.EVT_COLOURPICKER_CHANGED, self.OnSettingChange)
|
2012-02-27 13:51:52 +00:00
|
|
|
else:
|
2012-03-17 11:03:38 +00:00
|
|
|
self.ctrl = wx.ComboBox(panel, -1, getSettingFunc(configName), choices=defaultValue, style=wx.CB_DROPDOWN|wx.CB_READONLY)
|
2012-03-21 09:46:04 +00:00
|
|
|
self.ctrl.Bind(wx.EVT_COMBOBOX, self.OnSettingChange)
|
2012-10-26 08:47:22 +00:00
|
|
|
self.ctrl.Bind(wx.EVT_LEFT_DOWN, self.OnMouseExit)
|
2012-07-30 10:26:30 +00:00
|
|
|
flag = wx.EXPAND
|
2012-03-14 16:11:42 +00:00
|
|
|
|
2012-10-15 09:58:00 +00:00
|
|
|
sizer.Add(self.label, (x,y), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT,border=10)
|
2012-07-30 10:26:30 +00:00
|
|
|
sizer.Add(self.ctrl, (x,y+1), flag=wx.ALIGN_BOTTOM|flag)
|
2012-03-14 16:11:42 +00:00
|
|
|
sizer.SetRows(x+1)
|
2012-04-10 13:49:09 +00:00
|
|
|
|
|
|
|
self.ctrl.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
|
|
|
|
self.ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseExit)
|
2012-02-27 13:51:52 +00:00
|
|
|
|
2012-03-12 11:31:27 +00:00
|
|
|
self.defaultBGColour = self.ctrl.GetBackgroundColour()
|
|
|
|
|
2012-02-27 13:51:52 +00:00
|
|
|
panel.main.settingControlList.append(self)
|
2012-03-14 16:11:42 +00:00
|
|
|
|
|
|
|
def OnMouseEnter(self, e):
|
|
|
|
self.panel.main.OnPopupDisplay(self)
|
|
|
|
|
|
|
|
def OnMouseExit(self, e):
|
|
|
|
self.panel.main.OnPopupHide(self)
|
2012-10-26 08:47:22 +00:00
|
|
|
e.Skip()
|
2012-02-27 13:51:52 +00:00
|
|
|
|
2012-03-07 15:58:04 +00:00
|
|
|
def OnSettingChange(self, e):
|
2012-03-05 21:30:54 +00:00
|
|
|
if self.type == 'profile':
|
2012-03-17 11:03:38 +00:00
|
|
|
profile.putProfileSetting(self.configName, self.GetValue())
|
2012-03-05 21:30:54 +00:00
|
|
|
else:
|
2012-03-17 11:03:38 +00:00
|
|
|
profile.putPreference(self.configName, self.GetValue())
|
2012-02-27 13:51:52 +00:00
|
|
|
result = validators.SUCCESS
|
|
|
|
msgs = []
|
|
|
|
for validator in self.validators:
|
|
|
|
res, err = validator.validate()
|
|
|
|
if res == validators.ERROR:
|
|
|
|
result = res
|
|
|
|
elif res == validators.WARNING and result != validators.ERROR:
|
|
|
|
result = res
|
|
|
|
if res != validators.SUCCESS:
|
|
|
|
msgs.append(err)
|
|
|
|
if result == validators.ERROR:
|
|
|
|
self.ctrl.SetBackgroundColour('Red')
|
|
|
|
elif result == validators.WARNING:
|
|
|
|
self.ctrl.SetBackgroundColour('Yellow')
|
|
|
|
else:
|
2012-03-12 11:31:27 +00:00
|
|
|
self.ctrl.SetBackgroundColour(self.defaultBGColour)
|
2012-02-27 13:51:52 +00:00
|
|
|
self.ctrl.Refresh()
|
|
|
|
|
|
|
|
self.validationMsg = '\n'.join(msgs)
|
|
|
|
self.panel.main.UpdatePopup(self)
|
|
|
|
|
|
|
|
def GetValue(self):
|
2012-07-30 10:26:30 +00:00
|
|
|
if isinstance(self.ctrl, wx.ColourPickerCtrl):
|
|
|
|
return str(self.ctrl.GetColour().GetAsString(wx.C2S_HTML_SYNTAX))
|
|
|
|
else:
|
|
|
|
return str(self.ctrl.GetValue())
|
2012-02-27 13:51:52 +00:00
|
|
|
|
|
|
|
def SetValue(self, value):
|
2012-03-07 15:58:04 +00:00
|
|
|
if isinstance(self.ctrl, wx.CheckBox):
|
|
|
|
self.ctrl.SetValue(str(value) == "True")
|
2012-07-30 10:26:30 +00:00
|
|
|
elif isinstance(self.ctrl, wx.ColourPickerCtrl):
|
|
|
|
self.ctrl.SetColour(value)
|
2012-03-07 15:58:04 +00:00
|
|
|
else:
|
|
|
|
self.ctrl.SetValue(value)
|
2012-02-27 13:51:52 +00:00
|
|
|
|
|
|
|
#Settings notify works as a validator, but instead of validating anything, it calls another function, which can use the value.
|
2012-03-09 14:47:39 +00:00
|
|
|
# A bit hacky, bit it works.
|
2012-02-27 13:51:52 +00:00
|
|
|
class settingNotify():
|
|
|
|
def __init__(self, setting, func):
|
|
|
|
self.setting = setting
|
|
|
|
self.setting.validators.append(self)
|
|
|
|
self.func = func
|
|
|
|
|
|
|
|
def validate(self):
|
2012-04-23 09:09:56 +00:00
|
|
|
self.func()
|
|
|
|
return validators.SUCCESS, ''
|
|
|
|
|