GuiPrefs: Fix L10n.
[fpdb-dooglus.git] / pyfpdb / GuiPrefs.py
blob166a2500fb99d720d08631a09579eefea5c93b96
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 #Copyright 2008-2010 Carl Gherardi
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
9 #This program is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 #GNU General Public License for more details.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt.
18 import L10n
19 _ = L10n.get_translation()
21 import xml.dom.minidom
22 from xml.dom.minidom import Node
24 import pygtk
25 pygtk.require('2.0')
26 import gtk
27 import gobject
29 import Configuration
31 rewrite = { 'general' : 'General', 'supported_databases' : 'Databases'
32 , 'import' : 'Import', 'hud_ui' : 'HUD'
33 , 'supported_sites' : 'Sites', 'supported_games' : 'Games'
34 , 'popup_windows' : 'Popup Windows', 'pu' : 'Window'
35 , 'pu_name' : 'Popup Name', 'pu_stat' : 'Stat'
36 , 'pu_stat_name' : 'Stat Name'
37 , 'aux_windows' : 'Auxiliary Windows', 'aw stud_mucked' : 'stud_mucked'
38 , 'aw mucked' : 'mucked', 'hhcs' : 'Hand History Converters'
39 , 'gui_cash_stats' : 'Ring Player Stats', 'field_type' : 'Field Type'
40 , 'col_title' : 'Column Heading', 'xalignment' : 'Left/Right Align'
41 , 'disp_all' : 'Show in Summaries', 'disp_posn' : 'Show in Position Stats'
42 , 'col_name' : 'Stat Name', 'field_format' : 'Format'
45 class GuiPrefs:
47 def __init__(self, config, mainwin, dia, parentwin):
48 self.config = config
49 self.main_window = mainwin
50 self.dialog = dia
51 self.parent_window = parentwin #need to pass reference of parent, to set transient
53 self.tree_box = gtk.ScrolledWindow()
54 self.tree_box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
56 self.dialog.add(self.tree_box)
57 self.dialog.show()
59 self.doc = None
60 self.configStore = None
61 self.configView = None
63 self.fillFrames()
65 def fillFrames(self):
66 self.doc = self.config.get_doc()
68 self.configStore = gtk.TreeStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING, gobject.TYPE_STRING)
69 self.configView = gtk.TreeView(self.configStore)
70 self.configView.set_enable_tree_lines(True)
72 configColumn = gtk.TreeViewColumn(_("Setting"))
73 self.configView.append_column(configColumn)
74 cRender = gtk.CellRendererText()
75 configColumn.pack_start(cRender, True)
76 configColumn.add_attribute(cRender, 'text', 1)
78 configColumn = gtk.TreeViewColumn(_("Value (double-click to change)"))
79 self.configView.append_column(configColumn)
80 cRender = gtk.CellRendererText()
81 configColumn.pack_start(cRender, True)
82 configColumn.add_attribute(cRender, 'text', 2)
84 if self.doc.documentElement.tagName == 'FreePokerToolsConfig':
85 self.configStore.clear()
86 self.root = self.configStore.append( None, [self.doc.documentElement, "fpdb", None] )
87 for elem in self.doc.documentElement.childNodes:
88 iter = self.addTreeRows(self.root, elem)
89 if self.root != None:
90 self.configView.expand_row(self.configStore.get_path(self.root), False)
91 self.configView.connect("row-activated", self.rowChosen)
92 self.configView.show()
93 self.tree_box.add(self.configView)
94 self.tree_box.show()
95 self.dialog.show()
97 def rewriteText(self, s):
98 upd = False
99 if s in rewrite:
100 s = rewrite[s]
101 upd = True
102 return( (s,upd) )
104 def addTreeRows(self, parent, node):
105 if (node.nodeType == node.ELEMENT_NODE):
106 (setting, value) = (node.nodeName, None)
107 elif (node.nodeType == node.TEXT_NODE):
108 # text nodes hold the whitespace (or whatever) between the xml elements, not used here
109 (setting, value) = ("TEXT: ["+node.nodeValue+"|"+node.nodeValue+"]", node.data)
110 else:
111 (setting, value) = ("?? "+node.nodeValue, "type="+str(node.nodeType))
113 #iter = self.configStore.append( parent, [node.nodeValue, None] )
114 iter = None
115 if node.nodeType != node.TEXT_NODE and node.nodeType != node.COMMENT_NODE:
116 name = ""
117 iter = self.configStore.append( parent, [node, setting, value] )
118 if node.hasAttributes():
119 for i in xrange(node.attributes.length):
120 localName,updated = self.rewriteText( node.attributes.item(i).localName )
121 self.configStore.append( iter, [node, localName, node.attributes.item(i).value] )
122 if node.attributes.item(i).localName in ('site_name', 'game_name', 'stat_name', 'name', 'db_server', 'site', 'col_name'):
123 name = " " + node.attributes.item(i).value
125 label,updated = self.rewriteText(setting+name)
126 if name != "" or updated:
127 self.configStore.set_value(iter, 1, label)
129 if node.hasChildNodes():
130 for elem in node.childNodes:
131 self.addTreeRows(iter, elem)
132 return iter
134 def rowChosen(self, tview, path, something2, data=None):
135 # tview should= self.configStore
136 tmodel = tview.get_model()
137 iter = tmodel.get_iter(path)
138 if tmodel.iter_has_child(iter):
139 # toggle children display
140 if tview.row_expanded(path):
141 tview.collapse_row(tmodel.get_path(iter))
142 else:
143 tview.expand_row(tmodel.get_path(iter), False)
144 else:
145 # display value and allow edit
146 name = tmodel.get_value( iter, 1 )
147 val = tmodel.get_value( iter, 2 )
148 dia_edit = gtk.Dialog(name,
149 self.parent_window,
150 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
151 (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
152 gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
153 #dia_edit.set_default_size(350, 100)
154 entry = gtk.Entry()
155 if val:
156 entry.set_text(val)
157 entry.set_width_chars(40)
158 dia_edit.vbox.pack_start(entry, False, False, 0)
159 entry.show()
160 entry.connect("activate", self.__set_entry, dia_edit)
161 response = dia_edit.run()
162 if response == gtk.RESPONSE_ACCEPT:
163 # update configStore
164 new_val = entry.get_text()
165 tmodel.set_value(iter, 2, new_val)
166 tmodel.get_value(iter, 0).setAttribute(name, new_val)
167 dia_edit.destroy()
169 def __set_entry(self, w, dia=None):
170 if dia is not None:
171 dia.response(gtk.RESPONSE_ACCEPT)
173 if __name__=="__main__":
175 config = Configuration.Config()
177 win = gtk.Window(gtk.WINDOW_TOPLEVEL)
178 win.set_title(_("Test Preferences Dialog"))
179 win.set_border_width(1)
180 win.set_default_size(600, 500)
181 win.set_resizable(True)
183 dia = gtk.Dialog(_("Preferences"),
184 win,
185 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
186 (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
187 gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT))
188 dia.set_default_size(700, 500)
189 pw=dia #pass parent window
190 prefs = GuiPrefs(config, win, dia.vbox,pw)
191 response = dia.run()
192 if response == gtk.RESPONSE_ACCEPT:
193 # save updated config
194 config.save()
195 dia.destroy()