Added a SUMMARY command to schedulator, which allows you to show a
[wvapps.git] / photoreider / resources.py
blob2863c5725843550142ec721ecef8503c909d08fb
1 # resources.py: functions to set preferences, ...
2 # arch-tag: functions to set preferences, ...
3 # author: Alberto Griggio <albgrig@tiscalinet.it>
4 # license: GPL
6 import wx, wx.xrc
7 import os, common
9 class BookMarksEditor:
10 def __init__(self, ctrl, bmark_file):
11 self.ctrl = ctrl
12 self.bmark_file = bmark_file
13 self.selected_index = -1
14 res = wx.xrc.XmlResource_Get()
15 self.dialog = res.LoadDialog(None, 'bookmarks_editor')
16 self.fill_bookmarks_list()
17 wx.EVT_LIST_ITEM_SELECTED(self.dialog,
18 wx.xrc.XRCID('bookmarks_list'),
19 self.on_item_selected)
20 wx.EVT_BUTTON(self.dialog, wx.xrc.XRCID('add'), self.add_item)
21 wx.EVT_BUTTON(self.dialog, wx.xrc.XRCID('remove'), self.remove_item)
22 wx.EVT_BUTTON(self.dialog, wx.xrc.XRCID('move_up'), self.move_item_up)
23 wx.EVT_BUTTON(self.dialog, wx.xrc.XRCID('move_down'),
24 self.move_item_down)
25 wx.EVT_KILL_FOCUS(wx.xrc.XRCCTRL(self.dialog, 'name'),
26 self.update_item)
27 wx.EVT_KILL_FOCUS(wx.xrc.XRCCTRL(self.dialog, 'path'),
28 self.update_item)
29 if self.dialog.ShowModal() == wx.ID_OK:
30 self.save_bookmarks()
31 self.dialog.Destroy()
33 def fill_bookmarks_list(self):
34 list_ctrl = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog,
35 'bookmarks_list'),
36 'wxListCtrl')
37 list_ctrl.InsertColumn(0, _('Name'))
38 list_ctrl.InsertColumn(1, _('Path'))
39 try:
40 bm = open(self.bmark_file)
41 except:
42 return
43 item = -1
44 list_ctrl.Freeze()
45 list_ctrl.DeleteAllItems()
46 for line in bm:
47 line = line.strip()
48 if line.startswith('#'):
49 continue # ignore comments
50 try:
51 name, path = line.split('||', 1)
52 except ValueError:
53 continue # malformed entry, ignore it
54 item = list_ctrl.InsertStringItem(item+1, name.strip())
55 list_ctrl.SetStringItem(item, 1, path.strip())
56 list_ctrl.SetColumnWidth(0, -1)
57 list_ctrl.SetColumnWidth(1, -1)
58 list_ctrl.Thaw()
60 def add_item(self, event):
61 name = wx.PyTypeCast(
62 wx.xrc.XRCCTRL(self.dialog, 'name'),
63 'wxTextCtrl').GetValue().strip()
64 path = wx.PyTypeCast(
65 wx.xrc.XRCCTRL(self.dialog, 'path'),
66 'wxTextCtrl').GetValue().strip()
67 ## if not path or not os.path.isdir(path):
68 ## wx.MessageBox(_('You must insert a valid path!'), _('Error'),
69 ## wx.OK|wx.CENTRE|wx.ICON_ERROR)
70 ## return
71 if not name:
72 name = path
73 list_ctrl = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog,
74 'bookmarks_list'),
75 'wxListCtrl')
76 index = self.selected_index = self.selected_index + 1
77 list_ctrl.InsertStringItem(index, name)
78 list_ctrl.SetStringItem(index, 1, path)
79 list_ctrl.SetItemState(index, wx.LIST_STATE_SELECTED,
80 wx.LIST_STATE_SELECTED)
81 list_ctrl.SetColumnWidth(0, -1)
82 list_ctrl.SetColumnWidth(1, -1)
84 def update_item(self, event):
85 name = wx.PyTypeCast(
86 wx.xrc.XRCCTRL(self.dialog, 'name'),
87 'wxTextCtrl').GetValue().strip()
88 path = wx.PyTypeCast(
89 wx.xrc.XRCCTRL(self.dialog, 'path'),
90 'wxTextCtrl').GetValue().strip()
91 ## if not path or not os.path.isdir(path):
92 ## wx.MessageBox(_('You must insert a valid path!'), _('Error'),
93 ## wx.OK|wx.CENTRE|wx.ICON_ERROR)
94 ## event.Skip()
95 ## return
96 if not name:
97 name = path
98 list_ctrl = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog,
99 'bookmarks_list'),
100 'wxListCtrl')
101 list_ctrl.SetStringItem(self.selected_index, 0, name)
102 list_ctrl.SetStringItem(self.selected_index, 1, path)
103 list_ctrl.SetItemState(self.selected_index, wx.LIST_STATE_SELECTED,
104 wx.LIST_STATE_SELECTED)
105 list_ctrl.SetColumnWidth(0, -1)
106 list_ctrl.SetColumnWidth(1, -1)
107 event.Skip()
109 def on_item_selected(self, event):
110 self.selected_index = event.GetIndex()
111 list_ctrl = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog,
112 'bookmarks_list'),
113 'wxListCtrl')
114 name = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog, 'name'), 'wxTextCtrl')
115 path = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog, 'path'), 'wxTextCtrl')
116 name.SetValue(list_ctrl.GetItem(self.selected_index, 0).GetText())
117 path.SetValue(list_ctrl.GetItem(self.selected_index, 1).GetText())
118 event.Skip()
120 def remove_item(self, event):
121 list_ctrl = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog,
122 'bookmarks_list'),
123 'wxListCtrl')
124 name = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog, 'name'), 'wxTextCtrl')
125 path = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog, 'path'), 'wxTextCtrl')
126 if 0 <= self.selected_index < list_ctrl.GetItemCount():
127 for s in (name, path):
128 s.SetValue("")
129 list_ctrl.DeleteItem(self.selected_index)
130 list_ctrl.SetColumnWidth(0, -1)
131 list_ctrl.SetColumnWidth(1, -1)
133 def move_item_up(self, event):
134 list_ctrl = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog,
135 'bookmarks_list'),
136 'wxListCtrl')
137 list_ctrl.SetFocus()
138 if self.selected_index > 0:
139 index = self.selected_index - 1
140 n1 = list_ctrl.GetItem(self.selected_index, 0).GetText()
141 p1 = list_ctrl.GetItem(self.selected_index, 1).GetText()
142 n2 = list_ctrl.GetItem(index, 0).GetText()
143 p2 = list_ctrl.GetItem(index, 1).GetText()
144 list_ctrl.SetStringItem(self.selected_index, 0, n2)
145 list_ctrl.SetStringItem(self.selected_index, 1, p2)
146 list_ctrl.SetStringItem(index, 0, n1)
147 list_ctrl.SetStringItem(index, 1, p1)
148 state = wx.LIST_STATE_SELECTED | wx.LIST_STATE_FOCUSED
149 list_ctrl.SetItemState(index, state, state)
151 def move_item_down(self, event):
152 list_ctrl = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog,
153 'bookmarks_list'),
154 'wxListCtrl')
155 list_ctrl.SetFocus()
156 if self.selected_index < list_ctrl.GetItemCount()-1:
157 index = self.selected_index + 1
158 n1 = list_ctrl.GetItem(self.selected_index, 0).GetText()
159 p1 = list_ctrl.GetItem(self.selected_index, 1).GetText()
160 n2 = list_ctrl.GetItem(index, 0).GetText()
161 p2 = list_ctrl.GetItem(index, 1).GetText()
162 list_ctrl.SetStringItem(self.selected_index, 0, n2)
163 list_ctrl.SetStringItem(self.selected_index, 1, p2)
164 list_ctrl.SetStringItem(index, 0, n1)
165 list_ctrl.SetStringItem(index, 1, p1)
166 state = wx.LIST_STATE_SELECTED | wx.LIST_STATE_FOCUSED
167 list_ctrl.SetItemState(index, state, state)
169 def save_bookmarks(self):
170 list_ctrl = wx.PyTypeCast(wx.xrc.XRCCTRL(self.dialog,
171 'bookmarks_list'),
172 'wxListCtrl')
173 from time import asctime
174 try:
175 outfile = open(self.bmark_file, 'w')
176 outfile.write('# bookmarks modified %s\n' % asctime())
177 outfile.write('# format: name || path\n')
178 for i in range(list_ctrl.GetItemCount()):
179 name = list_ctrl.GetItem(i, 0).GetText()
180 path = list_ctrl.GetItem(i, 1).GetText()
181 outfile.write(name + '||' + path + '\n')
182 outfile.close()
183 except:
184 wx.LogError(_('Unable to save bookmarks!'))
185 return
186 else:
187 self.ctrl.load_bookmarks()
189 # end of class BookMarksEditor
192 class PreferencesEditor:
193 def __init__(self, config, save_file_name):
194 res = wx.xrc.XmlResource_Get()
195 self.dialog = res.LoadDialog(None, 'prefs_dialog')
196 self.config = config
197 self.save_file_name = save_file_name
198 self.update_view()
199 if self.dialog.ShowModal() == wx.ID_OK:
200 self.save_preferences()
201 self.dialog.Destroy()
203 def update_view(self):
204 for option in self.config.options('photoreider'):
205 ctrl = wx.xrc.XRCCTRL(self.dialog, option)
206 if ctrl is not None:
207 try:
208 option = self.config.getint('photoreider', option)
209 except:
210 option = self.config.get('photoreider', option)
211 try:
212 ctrl.SetValue(option)
213 except AttributeError:
214 ctrl.SetSelection(option)
216 def save_preferences(self):
217 for option in self.config.options('photoreider'):
218 ctrl = wx.xrc.XRCCTRL(self.dialog, option)
219 if ctrl is not None:
220 try:
221 value = ctrl.GetValue()
222 except AttributeError:
223 value = ctrl.GetSelection()
224 self.config.set('photoreider', option, "%s" % value)
225 try:
226 out = open(self.save_file_name, 'w')
227 self.config.write(out)
228 out.close()
229 except Exception, e:
230 wx.LogError(_('Unable to save preferences (%s)') % e)
232 # end of class PreferencesEditor
235 class ScrolledMessageDialog:
236 def __init__(self, parent, message, title, cols=80, rows=24, modal=False):
237 self.dialog = wx.Dialog(parent, -1, title)
238 tc = wx.TextCtrl(self.dialog, -1, message,
239 style=wx.TE_READONLY|wx.TE_MULTILINE)
240 tc.SetFont(wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL, False))
241 w, h = tc.GetTextExtent('M')
242 w *= cols
243 h *= rows
244 tc.SetSize((w, h))
245 sizer = wx.BoxSizer(wx.VERTICAL)
246 sizer.Add(tc, 0, wx.ALL, 5)
247 btn = wx.Button(self.dialog, wx.ID_OK, 'OK')
248 btn.SetDefault()
249 sizer.Add(btn, 0, wx.ALL|wx.ALIGN_CENTER, 15)
250 self.dialog.SetAutoLayout(True)
251 self.dialog.SetSizer(sizer)
252 sizer.Fit(self.dialog)
253 self.dialog.Layout()
254 wx.EVT_CLOSE(self.dialog, self.on_close)
255 if parent:
256 self.dialog.CenterOnParent()
257 if modal:
258 self.dialog.ShowModal()
259 else:
260 self.dialog.Show()
262 def on_close(self, event):
263 self.dialog.Destroy()
265 # end of class ScrolledMessageDialog