1 # resources.py: functions to set preferences, ...
2 # arch-tag: functions to set preferences, ...
3 # author: Alberto Griggio <albgrig@tiscalinet.it>
10 def __init__(self
, ctrl
, bmark_file
):
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'),
25 wx
.EVT_KILL_FOCUS(wx
.xrc
.XRCCTRL(self
.dialog
, 'name'),
27 wx
.EVT_KILL_FOCUS(wx
.xrc
.XRCCTRL(self
.dialog
, 'path'),
29 if self
.dialog
.ShowModal() == wx
.ID_OK
:
33 def fill_bookmarks_list(self
):
34 list_ctrl
= wx
.PyTypeCast(wx
.xrc
.XRCCTRL(self
.dialog
,
37 list_ctrl
.InsertColumn(0, _('Name'))
38 list_ctrl
.InsertColumn(1, _('Path'))
40 bm
= open(self
.bmark_file
)
45 list_ctrl
.DeleteAllItems()
48 if line
.startswith('#'):
49 continue # ignore comments
51 name
, path
= line
.split('||', 1)
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)
60 def add_item(self
, event
):
62 wx
.xrc
.XRCCTRL(self
.dialog
, 'name'),
63 'wxTextCtrl').GetValue().strip()
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)
73 list_ctrl
= wx
.PyTypeCast(wx
.xrc
.XRCCTRL(self
.dialog
,
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
):
86 wx
.xrc
.XRCCTRL(self
.dialog
, 'name'),
87 'wxTextCtrl').GetValue().strip()
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)
98 list_ctrl
= wx
.PyTypeCast(wx
.xrc
.XRCCTRL(self
.dialog
,
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)
109 def on_item_selected(self
, event
):
110 self
.selected_index
= event
.GetIndex()
111 list_ctrl
= wx
.PyTypeCast(wx
.xrc
.XRCCTRL(self
.dialog
,
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())
120 def remove_item(self
, event
):
121 list_ctrl
= wx
.PyTypeCast(wx
.xrc
.XRCCTRL(self
.dialog
,
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
):
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
,
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
,
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
,
173 from time
import asctime
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')
184 wx
.LogError(_('Unable to save bookmarks!'))
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')
197 self
.save_file_name
= save_file_name
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
)
208 option
= self
.config
.getint('photoreider', option
)
210 option
= self
.config
.get('photoreider', option
)
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
)
221 value
= ctrl
.GetValue()
222 except AttributeError:
223 value
= ctrl
.GetSelection()
224 self
.config
.set('photoreider', option
, "%s" % value
)
226 out
= open(self
.save_file_name
, 'w')
227 self
.config
.write(out
)
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')
245 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
246 sizer
.Add(tc
, 0, wx
.ALL
, 5)
247 btn
= wx
.Button(self
.dialog
, wx
.ID_OK
, 'OK')
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
)
254 wx
.EVT_CLOSE(self
.dialog
, self
.on_close
)
256 self
.dialog
.CenterOnParent()
258 self
.dialog
.ShowModal()
262 def on_close(self
, event
):
263 self
.dialog
.Destroy()
265 # end of class ScrolledMessageDialog