1
#thanks to http://www.daa.com.au/pipermail/pygtk/2003-November/006304.html
2
#for the reordering code
6
from ptvDB import T_BUILTIN
15
class FilterSelectorDialog(gtk.Dialog):
16
def __init__(self, xml, main_window):
17
gtk.Dialog.__init__(self)
19
self._main_window = main_window
21
#self._widget = self._xml.get_widget('dialog_tag_favorites')
22
contents = xml.get_widget("dialog-vbox3")
23
p = contents.get_parent()
25
self.vbox.add(contents)
26
gtk.Dialog.set_title(self, p.get_title())
29
self._pane = self._xml.get_widget('hpaned')
31
self._favorites_old_order = []
32
self._favorites_treeview = self._xml.get_widget('favorites_treeview')
33
self._favorites_model = gtk.ListStore(str, #name of filter
38
self._favorites_treeview.set_model(self._favorites_model)
40
column = gtk.TreeViewColumn(_('Favorites'))
41
renderer = gtk.CellRendererText()
42
column.pack_start(renderer)
43
column.set_attributes(renderer, text=F_DISPLAY)
44
column.set_alignment(0.5)
45
self._favorites_treeview.append_column(column)
47
self._all_tags_model = gtk.ListStore(str, str, int, bool) #same as above
48
self._all_tags_treeview = self._xml.get_widget('all_tags_treeview')
49
self._all_tags_treeview.set_model(self._all_tags_model)
50
self._all_tags_treeview.set_row_separator_func(lambda model,iter:model[iter][F_SEP]==True)
52
column = gtk.TreeViewColumn(_('All Tags'))
53
renderer = gtk.CellRendererText()
54
column.pack_start(renderer)
55
column.set_attributes(renderer, text=F_DISPLAY)
56
column.set_alignment(0.5)
57
self._all_tags_treeview.append_column(column)
59
self._TARGET_TYPE_INTEGER = 80
60
self._TARGET_TYPE_REORDER = 81
61
drop_types = [ ('reorder',gtk.TARGET_SAME_WIDGET,self._TARGET_TYPE_REORDER),
62
('integer',gtk.TARGET_SAME_APP,self._TARGET_TYPE_INTEGER)]
63
#for removing items from favorites and reordering
64
self._favorites_treeview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK, drop_types, gtk.gdk.ACTION_MOVE)
65
self._all_tags_treeview.drag_dest_set(gtk.DEST_DEFAULT_ALL, drop_types, gtk.gdk.ACTION_MOVE)
67
#copying items to favorites
68
self._favorites_treeview.enable_model_drag_dest(drop_types, gtk.gdk.ACTION_COPY)
69
self._all_tags_treeview.drag_source_set(gtk.gdk.BUTTON1_MASK, drop_types, gtk.gdk.ACTION_COPY)
71
self._dragging = False
73
for key in dir(self.__class__): #python insaneness
75
self._xml.signal_connect(key, getattr(self, key))
77
self.connect('delete-event', self._on_delete_event)
79
self._pane_position = 0
81
def set_taglists(self, all_tags, favorite_tags):
82
self._all_tags_model.clear()
83
self._favorites_model.clear()
85
self._favorites_old_order = []
87
last_type = all_tags[0][3]
89
for favorite, name,display,f_type in all_tags:
91
if f_type != T_BUILTIN:
92
if f_type != last_type:
94
self._all_tags_model.append(['---','---', -1, True])
95
self._all_tags_model.append([name, display, i, False])
97
self._favorites_old_order.append([favorite, name, display, i])
99
self._favorites_old_order.sort()
100
for fav, name, display, index in self._favorites_old_order:
101
self._favorites_model.append([name, display, index, False])
103
def is_visible(self):
104
return self.get_property('visible')
107
if utils.RUNNING_HILDON:
108
self._all_tags_treeview.set_property('height-request', 150)
111
context = self.create_pango_context()
112
style = self.get_style().copy()
113
font_desc = style.font_desc
114
metrics = context.get_metrics(font_desc, None)
115
char_width = metrics.get_approximate_char_width()
120
for row in self._all_tags_model:
121
width = len(row[F_DISPLAY])
122
if width > widest_right:
125
for row in self._favorites_model:
126
width = len(row[F_DISPLAY])
127
if width > widest_left:
130
self._pane_position = pango.PIXELS((widest_left+10)*char_width)
131
self._window_width = pango.PIXELS((widest_left+widest_right+10)*char_width)+100
133
if not utils.RUNNING_HILDON:
134
self.resize(self._window_width,1)
135
self._pane.set_position(self._pane_position)
136
self._favorites_treeview.columns_autosize()
137
self._all_tags_treeview.columns_autosize()
139
self.set_transient_for(self._main_window.get_parent())
143
if utils.RUNNING_HILDON:
144
self._pane_position = 250
145
self._pane.set_position(self._pane_position)
151
def _on_apply_clicked(self, button):
152
new_order = [r[0] for r in self._favorites_model]
153
old_order = [r[1] for r in self._favorites_old_order]
154
if old_order != new_order:
155
self._main_window.set_tag_favorites(new_order)
158
def _on_close_clicked(self, button):
162
def _on_help_button_activate(self, event):
163
dialog = gtk.Dialog(title=_("Tag Favorites Help"), parent=None, flags=gtk.DIALOG_MODAL, buttons=(gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
166
image = gtk.image_new_from_stock(gtk.STOCK_DIALOG_INFO, gtk.ICON_SIZE_DIALOG)
167
hbox.pack_start(image, False, False, 12)
168
label = gtk.Label(_("""You can drag tags from the righthand side to the favorites list on the left. To remove a favorite, drag it from the lefthand side back to the right."""))
169
label.set_line_wrap(True)
170
hbox.pack_start(label, True, True, 0)
171
dialog.vbox.pack_start(hbox, True, True, 0)
173
dialog.resize(400,-1)
174
response = dialog.run()
178
def _on_delete_event(self, widget, event):
179
return self.hide_on_delete()
181
def _on_drag_data_get(self, treeview, drag_context, selection_data, info, time):
182
selection = treeview.get_selection()
183
model, iter = selection.get_selected()
184
path = model.get_path(iter)
185
selection_data.set(selection_data.target, 8, str(path[0]))
187
def _on_all_tags_drag_data_received(self, treeview, context, x, y, selection, targetType, time):
188
treeview.emit_stop_by_name('drag-data-received')
189
if targetType == self._TARGET_TYPE_INTEGER:
191
for c in selection.data:
192
if c != "\0": #for some reason ever other character is a null. what gives?
193
tag_index = tag_index+c
194
index = int(tag_index)
195
target_iter = self._favorites_model.get_iter((index,))
196
self._favorites_model.remove(target_iter)
197
self._on_drag_end(None, None)
199
def _on_favorites_drag_data_received(self, treeview, context, x, y, selection, targetType, time):
200
treeview.emit_stop_by_name('drag-data-received')
201
if targetType == self._TARGET_TYPE_INTEGER:
203
for c in selection.data:
204
if c != "\0": #for some reason ever other character is a null. what gives?
205
tag_index = tag_index+c
206
index = int(tag_index)
207
source_row = self._all_tags_model[index]
208
for row in self._favorites_model:
209
if source_row[F_NAME] == row[F_NAME]:
211
new_row = [source_row[F_NAME],source_row[F_DISPLAY],index, False]
213
path, pos = treeview.get_dest_row_at_pos(x, y)
214
dest_iter = self._favorites_model.get_iter(path)
215
self.iterCopy(self._favorites_model, dest_iter, new_row, pos)
217
self._favorites_model.append(new_row)
218
if targetType == self._TARGET_TYPE_REORDER:
219
model, iter_to_copy = treeview.get_selection().get_selected()
220
row = list(model[iter_to_copy])
222
path, pos = treeview.get_dest_row_at_pos(x, y)
223
target_iter = model.get_iter(path)
225
if self.checkSanity(model, iter_to_copy, target_iter):
226
self.iterCopy(model, target_iter, row, pos)
227
context.finish(True, True, time)
229
context.finish(False, False, time)
232
context.finish(True, True, time)
233
self._on_drag_end(None, None)
235
def checkSanity(self, model, iter_to_copy, target_iter):
236
path_of_iter_to_copy = model.get_path(iter_to_copy)
237
path_of_target_iter = model.get_path(target_iter)
238
if path_of_target_iter[0:len(path_of_iter_to_copy)] == path_of_iter_to_copy:
243
def iterCopy(self, target_model, target_iter, row, pos):
244
if (pos == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE) or (pos == gtk.TREE_VIEW_DROP_INTO_OR_AFTER):
245
new_iter = target_model.append(row)
246
elif pos == gtk.TREE_VIEW_DROP_BEFORE:
247
new_iter = target_model.insert_before(target_iter, row)
248
elif pos == gtk.TREE_VIEW_DROP_AFTER:
249
new_iter = target_model.insert_after(target_iter, row)
251
def _on_drag_begin(self, widget, drag_context):
252
self._dragging = True
254
def _on_drag_end(self, widget, drag_context):
255
self._dragging = False
258
def _do_unselect(self):
259
self._all_tags_treeview.get_selection().unselect_all()
260
self._favorites_treeview.get_selection().unselect_all()