comparison orpg/tools/orpg_settings.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children 69149263026d
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 # Copyright (C) 2000-2001 The OpenRPG Project
2 #
3 # openrpg-dev@lists.sourceforge.net
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 # --
19 #
20 # File: orpg_settings.py
21 # Author: Dj Gilcrease
22 # Maintainer:
23 # Version:
24 # $Id: orpg_settings.py,v 1.51 2007/07/15 14:25:12 digitalxero Exp $
25 #
26 # Description: classes for orpg settings
27 #
28
29 from orpg.orpg_windows import *
30 import orpg.dirpath
31 from rgbhex import *
32 import sys
33 import os
34
35 class orpgSettings:
36 def __init__(self):
37 self.validate = open_rpg.get_component("validate")
38 self.xml = open_rpg.get_component("xml")
39 self.log = open_rpg.get_component("log")
40 self.changes = []
41 self.validate.config_file("settings.xml","default_settings.xml")
42 self.filename = orpg.dirpath.dir_struct["user"] + "settings.xml"
43 temp_file = open(self.filename)
44 txt = temp_file.read()
45 temp_file.close()
46 self.xml_dom = self.xml.parseXml(txt)
47
48 if self.xml_dom is None:
49 self.rebuildSettings()
50 self.xml_dom = self.xml_dom._get_documentElement()
51
52 def rebuildSettings(self):
53 self.log.log("Settings file has be corrupted, rebuilding settings.", ORPG_INFO, True)
54 try:
55 os.remove(self.filename)
56 except:
57 pass
58
59 self.validate.config_file("settings.xml","default_settings.xml")
60 temp_file = open(self.filename)
61 txt = temp_file.read()
62 temp_file.close()
63 self.xml_dom = self.xml.parseXml(txt)
64
65 def get_setting(self, name):
66 try:
67 return self.xml_dom.getElementsByTagName(name)[0].getAttribute("value")
68 except:
69 return 0
70
71 def get_setting_keys(self):
72 keys = []
73 tabs = self.xml_dom.getElementsByTagName("tab")
74 for i in xrange(0, len(tabs)):
75 if tabs[i].getAttribute("type") == 'grid':
76 children = tabs[i]._get_childNodes()
77 for c in children:
78 keys.append(c._get_tagName())
79 return keys
80
81 def set_setting(self, name, value):
82 self.xml_dom.getElementsByTagName(name)[0].setAttribute("value", value)
83
84 def add_setting(self, tab, setting, value, options, help):
85 if len(self.xml_dom.getElementsByTagName(setting)) > 0:
86 return False
87 tabs = self.xml_dom.getElementsByTagName("tab")
88 newsetting = self.xml.parseXml('<' + setting + ' value="' + value + '" options="' + options + '" help="' + help + '" />')._get_documentElement()
89 for i in xrange(0, len(tabs)):
90 if tabs[i].getAttribute("name") == tab and tabs[i].getAttribute("type") == 'grid':
91 tabs[i].appendChild(newsetting)
92 return True
93 return False
94
95 def add_tab(self, parent, tabname, tabtype):
96 tab_xml = '<tab '
97 if tabtype == 'text':
98 tab_xml += 'name="' + tabname + '" type="text" />'
99 else:
100 tab_xml += 'name="' + tabname + '" type="' + tabtype + '"></tab>'
101 newtab = self.xml.parseXml(tab_xml)._get_documentElement()
102 if parent != None:
103 tabs = self.xml_dom.getElementsByTagName("tab")
104 for i in xrange(0, len(tabs)):
105 if tabs[i].getAttribute("name") == parent and tabs[i].getAttribute("type") == 'tab':
106 children = tabs[i]._get_childNodes()
107 for c in children:
108 if c.getAttribute("name") == tabname:
109 return False
110 tabs[i].appendChild(newtab)
111 return True
112 else:
113 children = self.xml_dom._get_childNodes()
114 for c in children:
115 if c.getAttribute("name") == tabname:
116 return False
117 self.xml_dom.appendChild(newtab)
118 return True
119 return False
120
121 def updateIni(self):
122 defaultFile = orpg.dirpath.dir_struct['template'] + 'default_settings.xml'
123 temp_file = open(defaultFile)
124 txt = temp_file.read()
125 temp_file.close()
126 default_dom = self.xml.parseXml(txt)._get_documentElement()
127 for child in default_dom.getChildren():
128 if child._get_tagName() == 'tab' and child.hasChildNodes():
129 self.proccessChildren(child)
130 default_dom.unlink()
131
132 def proccessChildren(self, dom, parent=None):
133 if dom._get_tagName() == 'tab':
134 self.add_tab(parent, dom.getAttribute("name"), dom.getAttribute("type"))
135
136 for child in dom.getChildren():
137 if child._get_tagName() == 'tab' and child.hasChildNodes():
138 self.proccessChildren(child, dom.getAttribute("name"))
139 else:
140 self.add_setting(dom.getAttribute("name"), child._get_tagName(), child.getAttribute("value"), child.getAttribute("options"), child.getAttribute("help"))
141
142 def save(self):
143 temp_file = open(self.filename, "w")
144 temp_file.write(self.xml.toxml(self.xml_dom,1))
145 temp_file.close()
146
147 class orpgSettingsWnd(wx.Dialog):
148 def __init__(self, parent):
149 wx.Dialog.__init__(self,parent,-1,"OpenRPG Preferences",wx.DefaultPosition,size = wx.Size(-1,-1), style=wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION)
150 self.Freeze()
151 self.validate = open_rpg.get_component("validate")
152 self.settings = open_rpg.get_component("settings")
153 self.chat = open_rpg.get_component("chat")
154 self.changes = []
155 self.SetMinSize((545,500))
156 self.tabber = orpgTabberWnd(self, style=FNB.FNB_NO_X_BUTTON)
157 self.build_gui()
158 self.tabber.SetSelection(0)
159 winsizer = wx.BoxSizer(wx.VERTICAL)
160 sizer = wx.BoxSizer(wx.HORIZONTAL)
161 sizer.Add(wx.Button(self, wx.ID_OK, "OK"), 1, wx.EXPAND)
162 sizer.Add(wx.Size(10,10))
163 sizer.Add(wx.Button(self, wx.ID_CANCEL, "Cancel"), 1, wx.EXPAND)
164 winsizer.Add(self.tabber, 1, wx.EXPAND)
165 winsizer.Add(sizer, 0, wx.EXPAND | wx.ALIGN_BOTTOM)
166 self.winsizer = winsizer
167 self.SetSizer(self.winsizer)
168 self.SetAutoLayout(True)
169 self.Fit()
170 self.Bind(wx.EVT_BUTTON, self.onOk, id=wx.ID_OK)
171 self.Thaw()
172
173 def on_size(self,evt):
174 (w,h) = self.GetClientSizeTuple()
175 self.winsizer.SetDimension(0,0,w,h-25)
176
177 def build_gui(self):
178 self.validate.config_file("settings.xml","default_settings.xml")
179 filename = open_rpg.get_component("dir_struct")["user"] + "settings.xml"
180 temp_file = open(filename)
181 temp_file.close()
182 children = self.settings.xml_dom._get_childNodes()
183 for c in children:
184 self.build_window(c,self.tabber)
185
186 def build_window(self, xml_dom, parent_wnd):
187 name = xml_dom._get_nodeName()
188 #container = 0
189 if name=="tab":
190 temp_wnd = self.do_tab_window(xml_dom,parent_wnd)
191 return temp_wnd
192
193 def do_tab_window(self, xml_dom, parent_wnd):
194 type = xml_dom.getAttribute("type")
195 name = xml_dom.getAttribute("name")
196
197 if type == "grid":
198 temp_wnd = self.do_grid_tab(xml_dom, parent_wnd)
199 parent_wnd.AddPage(temp_wnd, name, False)
200 elif type == "tab":
201 temp_wnd = orpgTabberWnd(parent_wnd, style=FNB.FNB_NO_X_BUTTON)
202 children = xml_dom._get_childNodes()
203 for c in children:
204 if c._get_nodeName() == "tab":
205 self.do_tab_window(c, temp_wnd)
206 temp_wnd.SetSelection(0)
207 parent_wnd.AddPage(temp_wnd, name, False)
208 elif type == "text":
209 temp_wnd = wx.TextCtrl(parent_wnd,-1)
210 parent_wnd.AddPage(temp_wnd, name, False)
211 else:
212 temp_wnd = None
213 return temp_wnd
214
215 def do_grid_tab(self, xml_dom, parent_wnd):
216 settings = []
217 children = xml_dom._get_childNodes()
218 for c in children:
219 name = c._get_nodeName()
220 value = c.getAttribute("value")
221 help = c.getAttribute("help")
222 options = c.getAttribute("options")
223 settings.append([name, value, options, help])
224 temp_wnd = settings_grid(parent_wnd, settings, self.changes)
225 return temp_wnd
226
227 def onOk(self, evt):
228 #This will write the settings back to the XML
229 self.session = open_rpg.get_component("session")
230 tabbedwindows = open_rpg.get_component("tabbedWindows")
231 new = []
232 for wnd in tabbedwindows:
233 try:
234 style = wnd.GetWindowStyleFlag()
235 new.append(wnd)
236 except:
237 pass
238 tabbedwindows = new
239 open_rpg.add_component("tabbedWindows", tabbedwindows)
240 rgbconvert = RGBHex()
241
242 for i in xrange(0,len(self.changes)):
243 self.settings.set_setting(self.changes[i][0], self.changes[i][1])
244 top_frame = open_rpg.get_component('frame')
245
246 if self.changes[i][0] == 'bgcolor' or self.changes[i][0] == 'textcolor':
247 self.chat.chatwnd.SetPage(self.chat.ResetPage())
248 self.chat.chatwnd.scroll_down()
249 if self.settings.get_setting('ColorTree') == '1':
250 top_frame.tree.SetBackgroundColour(self.settings.get_setting('bgcolor'))
251 top_frame.tree.SetForegroundColour(self.settings.get_setting('textcolor'))
252 top_frame.tree.Refresh()
253 top_frame.players.SetBackgroundColour(self.settings.get_setting('bgcolor'))
254 top_frame.players.SetForegroundColour(self.settings.get_setting('textcolor'))
255 top_frame.players.Refresh()
256 else:
257 top_frame.tree.SetBackgroundColour('white')
258 top_frame.tree.SetForegroundColour('black')
259 top_frame.tree.Refresh()
260 top_frame.players.SetBackgroundColour('white')
261 top_frame.players.SetForegroundColour('black')
262 top_frame.players.Refresh()
263
264 if self.changes[i][0] == 'ColorTree':
265 if self.changes[i][1] == '1':
266 top_frame.tree.SetBackgroundColour(self.settings.get_setting('bgcolor'))
267 top_frame.tree.SetForegroundColour(self.settings.get_setting('textcolor'))
268 top_frame.tree.Refresh()
269 top_frame.players.SetBackgroundColour(self.settings.get_setting('bgcolor'))
270 top_frame.players.SetForegroundColour(self.settings.get_setting('textcolor'))
271 top_frame.players.Refresh()
272 else:
273 top_frame.tree.SetBackgroundColour('white')
274 top_frame.tree.SetForegroundColour('black')
275 top_frame.tree.Refresh()
276 top_frame.players.SetBackgroundColour('white')
277 top_frame.players.SetForegroundColour('black')
278 top_frame.players.Refresh()
279
280 if self.changes[i][0] == 'GMWhisperTab' and self.changes[i][1] == '1':
281 self.chat.parent.create_gm_tab()
282 self.toggleToolBars(self.chat, self.changes[i])
283 try:
284 self.toggleToolBars(self.chat.parent.GMChatPanel, self.changes[i])
285 except:
286 pass
287 for panel in self.chat.parent.whisper_tabs:
288 self.toggleToolBars(panel, self.changes[i])
289 for panel in self.chat.parent.group_tabs:
290 self.toggleToolBars(panel, self.changes[i])
291 for panel in self.chat.parent.null_tabs:
292 self.toggleToolBars(panel, self.changes[i])
293
294 if self.changes[i][0] == 'player':
295 self.session.name = self.changes[i][1]
296
297 if (self.changes[i][0] == 'TabTheme' and (self.changes[i][1] == 'customflat' or self.changes[i][1] == 'customslant')) or\
298 (self.changes[i][0] == 'TabTextColor' and (self.settings.get_setting('TabTheme') == 'customflat' or self.settings.get_setting('TabTheme') == 'customslant')) or\
299 (self.changes[i][0] == 'TabGradientFrom' and (self.settings.get_setting('TabTheme') == 'customflat' or self.settings.get_setting('TabTheme') == 'customslant')) or\
300 (self.changes[i][0] == 'TabGradientTo' and (self.settings.get_setting('TabTheme') == 'customflat' or self.settings.get_setting('TabTheme') == 'customslant')):
301 gfrom = self.settings.get_setting('TabGradientFrom')
302 (fred, fgreen, fblue) = rgbconvert.rgb_tuple(gfrom)
303
304 gto = self.settings.get_setting('TabGradientTo')
305 (tored, togreen, toblue) = rgbconvert.rgb_tuple(gto)
306
307 tabtext = self.settings.get_setting('TabTextColor')
308 (tred, tgreen, tblue) = rgbconvert.rgb_tuple(tabtext)
309 for wnd in tabbedwindows:
310 style = wnd.GetWindowStyleFlag()
311 # remove old tabs style
312 mirror = ~(FNB.FNB_VC71 | FNB.FNB_VC8 | FNB.FNB_FANCY_TABS | FNB.FNB_COLORFUL_TABS)
313 style &= mirror
314 if self.settings.get_setting('TabTheme') == 'customslant':
315 style |= FNB.FNB_VC8
316 else:
317 style |= FNB.FNB_FANCY_TABS
318 wnd.SetWindowStyleFlag(style)
319 wnd.SetGradientColourTo(wx.Color(tored, togreen, toblue))
320 wnd.SetGradientColourFrom(wx.Color(fred, fgreen, fblue))
321 wnd.SetNonActiveTabTextColour(wx.Color(tred, tgreen, tblue))
322 wnd.Refresh()
323
324 if self.changes[i][0] == 'TabBackgroundGradient':
325 for wnd in tabbedwindows:
326 (red, green, blue) = rgbconvert.rgb_tuple(self.changes[i][1])
327 wnd.SetTabAreaColour(wx.Color(red, green, blue))
328 wnd.Refresh()
329 self.settings.save()
330 self.Destroy()
331
332 def toggleToolBars(self, panel, changes):
333 if changes[0] == 'AliasTool_On':
334 panel.toggle_alias(changes[1])
335 elif changes[0] == 'DiceButtons_On':
336 panel.toggle_dice(changes[1])
337 elif changes[0] == 'FormattingButtons_On':
338 panel.toggle_formating(changes[1])
339
340 class settings_grid(wx.grid.Grid):
341 """grid for gen info"""
342 def __init__(self, parent, settings, changed = []):
343 wx.grid.Grid.__init__(self, parent, -1, style=wx.SUNKEN_BORDER | wx.WANTS_CHARS)
344 self.setting_data = changed
345 self.Bind(wx.EVT_SIZE, self.on_size)
346 self.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.on_cell_change)
347 self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.on_left_click)
348 self.CreateGrid(len(settings),3)
349 self.SetRowLabelSize(0)
350 self.SetColLabelValue(0,"Setting")
351 self.SetColLabelValue(1,"Value")
352 self.SetColLabelValue(2,"Available Options")
353 self.settings = settings
354 for i in range(len(settings)):
355 self.SetCellValue(i,0,settings[i][0])
356 self.SetCellValue(i,1,settings[i][1])
357 if settings[i][1] and settings[i][1][0] == '#':
358 self.SetCellBackgroundColour(i,1,settings[i][1])
359 self.SetCellValue(i,2,settings[i][2])
360
361 def on_left_click(self,evt):
362 row = evt.GetRow()
363 col = evt.GetCol()
364 if col == 2:
365 return
366 elif col == 0:
367 name = self.GetCellValue(row,0)
368 str = self.settings[row][3]
369 msg = wx.MessageBox(str,name)
370 return
371 elif col == 1:
372 setting = self.GetCellValue(row,0)
373 value = self.GetCellValue(row,1)
374 if value and value[0] == '#':
375 hexcolor = orpg.tools.rgbhex.RGBHex().do_hex_color_dlg(self)
376 if hexcolor:
377 self.SetCellValue(row,2, hexcolor)
378 self.SetCellBackgroundColour(row,1,hexcolor)
379 self.Refresh()
380 setting = self.GetCellValue(row,0)
381 self.setting_data.append([setting, hexcolor])
382 else:
383 evt.Skip()
384
385 def on_cell_change(self,evt):
386 row = evt.GetRow()
387 col = evt.GetCol()
388 if col != 1:
389 return
390 setting = self.GetCellValue(row,0)
391 value = self.GetCellValue(row,1)
392 self.setting_data.append([setting, value])
393
394 def get_h(self):
395 (w,h) = self.GetClientSizeTuple()
396 rows = self.GetNumberRows()
397 minh = 0
398 for i in range (0,rows):
399 minh += self.GetRowSize(i)
400 minh += 120
401 return minh
402
403 def on_size(self,evt):
404 (w,h) = self.GetClientSizeTuple()
405 cols = self.GetNumberCols()
406 col_w = w/(cols)
407 for i in range(0,cols):
408 self.SetColSize(i,col_w)
409 self.Refresh()