~cosmin.lupu/+junk/penguintv

« back to all changes in this revision

Viewing changes to penguintv/FilterSelectorDialog.py

  • Committer: cosmin.lupu at gmail
  • Date: 2010-04-27 16:47:43 UTC
  • Revision ID: cosmin.lupu@gmail.com-20100427164743-ds8xrqonipp5ovdf
initial packaging

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#thanks to http://www.daa.com.au/pipermail/pygtk/2003-November/006304.html
 
2
#for the reordering code
 
3
 
 
4
import gtk
 
5
import pango
 
6
from ptvDB import T_BUILTIN
 
7
 
 
8
import utils
 
9
 
 
10
F_NAME     = 0
 
11
F_DISPLAY  = 1
 
12
F_INDEX    = 2
 
13
F_SEP      = 3
 
14
 
 
15
class FilterSelectorDialog(gtk.Dialog):
 
16
        def __init__(self, xml, main_window):
 
17
                gtk.Dialog.__init__(self)
 
18
                self._xml = xml
 
19
                self._main_window = main_window
 
20
                
 
21
                #self._widget = self._xml.get_widget('dialog_tag_favorites')
 
22
                contents = xml.get_widget("dialog-vbox3")
 
23
                p = contents.get_parent()
 
24
                contents.unparent()
 
25
                self.vbox.add(contents)
 
26
                gtk.Dialog.set_title(self, p.get_title())
 
27
                del p
 
28
                
 
29
                self._pane = self._xml.get_widget('hpaned')
 
30
                
 
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
 
34
                                                                                           str, #text to display
 
35
                                                                                           int, #original id
 
36
                                                                                           bool) #separator
 
37
                                                                                                                        
 
38
                self._favorites_treeview.set_model(self._favorites_model)
 
39
 
 
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)
 
46
                
 
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)
 
51
                
 
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)
 
58
                
 
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)
 
66
                
 
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)
 
70
                
 
71
                self._dragging = False
 
72
                
 
73
                for key in dir(self.__class__): #python insaneness
 
74
                        if key[:3] == '_on':
 
75
                                self._xml.signal_connect(key, getattr(self, key))
 
76
                                
 
77
                self.connect('delete-event', self._on_delete_event)
 
78
                                
 
79
                self._pane_position = 0
 
80
                
 
81
        def set_taglists(self, all_tags, favorite_tags):
 
82
                self._all_tags_model.clear()
 
83
                self._favorites_model.clear()
 
84
                
 
85
                self._favorites_old_order = []
 
86
                
 
87
                last_type = all_tags[0][3]
 
88
                i=-1
 
89
                for favorite, name,display,f_type in all_tags:
 
90
                        i+=1
 
91
                        if f_type != T_BUILTIN:
 
92
                                if f_type != last_type:
 
93
                                        last_type = f_type
 
94
                                        self._all_tags_model.append(['---','---', -1, True])
 
95
                                self._all_tags_model.append([name, display, i, False])
 
96
                                if favorite > 0:
 
97
                                        self._favorites_old_order.append([favorite, name, display, i])
 
98
                                
 
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])
 
102
                
 
103
        def is_visible(self):
 
104
                return self.get_property('visible')
 
105
                
 
106
        def Show(self):
 
107
                if utils.RUNNING_HILDON:
 
108
                        self._all_tags_treeview.set_property('height-request', 150)
 
109
                        self.resize(650,300)
 
110
        
 
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()
 
116
                
 
117
                widest_left = 15
 
118
                widest_right = 0
 
119
                
 
120
                for row in self._all_tags_model:
 
121
                        width = len(row[F_DISPLAY])
 
122
                        if width > widest_right:
 
123
                                widest_right = width
 
124
                                
 
125
                for row in self._favorites_model:
 
126
                        width = len(row[F_DISPLAY])
 
127
                        if width > widest_left:
 
128
                                widest_left = width
 
129
                
 
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
 
132
                
 
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()
 
138
                
 
139
                self.set_transient_for(self._main_window.get_parent())
 
140
                
 
141
                self.show_all()
 
142
                
 
143
                if utils.RUNNING_HILDON:
 
144
                        self._pane_position = 250
 
145
                        self._pane.set_position(self._pane_position)
 
146
                
 
147
        def Hide(self):
 
148
                self._do_unselect()
 
149
                self.hide()
 
150
                
 
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)
 
156
                self.Hide()
 
157
                
 
158
        def _on_close_clicked(self, button):
 
159
                self.Hide()
 
160
                #self.destroy()
 
161
                
 
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))
 
164
                hbox = gtk.HBox()
 
165
                hbox.set_spacing(12)
 
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)
 
172
                dialog.show_all()
 
173
                dialog.resize(400,-1)
 
174
                response = dialog.run()
 
175
                dialog.hide()
 
176
                del dialog
 
177
 
 
178
        def _on_delete_event(self, widget, event):
 
179
                return self.hide_on_delete()
 
180
                
 
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]))
 
186
                
 
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:
 
190
                        tag_index = ""
 
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)
 
198
                
 
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:
 
202
                        tag_index = ""
 
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]:
 
210
                                        return
 
211
                        new_row = [source_row[F_NAME],source_row[F_DISPLAY],index, False]
 
212
                        try:
 
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)
 
216
                        except:
 
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])
 
221
                        try:
 
222
                                path, pos = treeview.get_dest_row_at_pos(x, y)
 
223
                                target_iter = model.get_iter(path)
 
224
                                
 
225
                                if self.checkSanity(model, iter_to_copy, target_iter):
 
226
                                        self.iterCopy(model, target_iter, row, pos)
 
227
                                        context.finish(True, True, time)
 
228
                                else:
 
229
                                        context.finish(False, False, time)
 
230
                        except:
 
231
                                model.append(row)
 
232
                                context.finish(True, True, time)
 
233
                self._on_drag_end(None, None)
 
234
 
 
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:
 
239
                        return False
 
240
                else:
 
241
                        return True
 
242
    
 
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)
 
250
                
 
251
        def _on_drag_begin(self, widget, drag_context):
 
252
                self._dragging = True
 
253
        
 
254
        def _on_drag_end(self, widget, drag_context):
 
255
                self._dragging = False
 
256
                self._do_unselect()
 
257
        
 
258
        def _do_unselect(self):
 
259
                self._all_tags_treeview.get_selection().unselect_all()
 
260
                self._favorites_treeview.get_selection().unselect_all()