Package port :: Package lazyview :: Module tree
[frames] | no frames]

Source Code for Module port.lazyview.tree

  1  # This program is free software; you can redistribute it and/or modify 
  2  # it under the terms of the GNU General Public License as published by 
  3  # the Free Software Foundation; either version 2 of the License, or 
  4  # (at your option) any later version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU General Public License for more details. 
 10  # 
 11  # You should have received a copy of the GNU General Public License 
 12  # along with this program; if not, write to the Free Software 
 13  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 14   
 15  import gtk 
 16  import gobject 
 17  import logging 
 18   
 19  from sugar.graphics.palette import Invoker 
 20   
 21  from model import Model 
 22   
 23  _SHOW_PALETTE_TIMEOUT = 200 
 24   
25 -class TreeView(gtk.TreeView):
26 - def __init__(self):
27 gtk.TreeView.__init__(self) 28 self._invoker = _TreeInvoker(self)
29
30 - def set_cursor(self, path, column, edit=False):
31 if path is not None: 32 gtk.TreeView.set_cursor(self, path, column, edit)
33
34 - def append_column(self, column):
35 if isinstance(column, TreeViewColumn): 36 column.view = self 37 return gtk.TreeView.append_column(self, column)
38
39 - def create_palette(self):
40 return self._invoker.cell_palette
41
42 -class TreeViewColumn(gtk.TreeViewColumn):
43 - def __init__(self, title=None, cell=None, **kwargs):
44 gtk.TreeViewColumn.__init__(self, title, cell, **kwargs) 45 self.view = None 46 self.order_by = None 47 self.palette_cb = None 48 self.connect('clicked', self._clicked_cb)
49
50 - def set_sort_column_id(self, field):
51 self.props.clickable = True 52 self.order_by = field
53
54 - def _clicked_cb(self, column):
55 if not self.view: 56 return 57 58 if self.props.sort_indicator: 59 if self.props.sort_order == gtk.SORT_DESCENDING: 60 new_order = gtk.SORT_ASCENDING 61 else: 62 new_order = gtk.SORT_DESCENDING 63 else: 64 new_order = gtk.SORT_ASCENDING 65 66 self.view.get_model().set_order(self.order_by, new_order)
67
68 -class _TreeInvoker(Invoker):
69 - def __init__(self, tree=None):
70 Invoker.__init__(self) 71 self._position_hint = self.AT_CURSOR 72 73 self._tree = None 74 self.cell_palette = None 75 self._palette_pos = None 76 self._enter_timeout = None 77 78 self._enter_hid = None 79 self._motion_hid = None 80 self._leave_hid = None 81 self._button_hid = None 82 83 if tree: 84 self.attach(tree)
85
86 - def get_toplevel(self):
87 return self._tree.get_toplevel()
88
89 - def attach(self, tree):
90 self._tree = tree 91 self._enter_hid = tree.connect('enter-notify-event', self._enter_cb) 92 self._motion_hid = tree.connect('motion-notify-event', self._enter_cb) 93 self._leave_hid = tree.connect('leave-notify-event', self._leave_cb) 94 self._button_hid = tree.connect('button-release-event', self._button_cb) 95 Invoker.attach(self, tree)
96
97 - def detach(self):
98 Invoker.detach(self) 99 self._tree.disconnect(self._enter_hid) 100 self._tree.disconnect(self._motion_hid) 101 self._tree.disconnect(self._leave_hid) 102 self._tree.disconnect(self._button_cb)
103
104 - def _close_palette(self):
105 if self._enter_timeout: 106 gobject.source_remove(self._enter_timeout) 107 self._enter_timeout = None 108 self.cell_palette = None 109 self._palette_pos = None
110
111 - def _open_palette(self, notify, force):
112 if self._enter_timeout: 113 gobject.source_remove(self._enter_timeout) 114 self._enter_timeout = None 115 116 coords = self._tree.convert_widget_to_bin_window_coords( 117 *self._tree.get_pointer()) 118 119 pos = self._tree.get_path_at_pos(*coords) 120 if not pos: 121 self._close_palette() 122 return False 123 124 path, column, x, y = pos 125 if not hasattr(column, 'palette_cb') or not column.palette_cb: 126 self._close_palette() 127 return False 128 129 row = self._tree.props.model.get_row(path) 130 if not row: 131 logging.debug('_open_palette: wait for row %s' % path) 132 self._enter_timeout = gobject.timeout_add(500, self._open_palette, 133 self.notify_mouse_enter, False) 134 return False 135 136 palette = column.palette_cb(self._tree.props.model, row, x, y) 137 if not palette: 138 self._close_palette() 139 return False 140 141 if self._palette_pos != (path, column) or self.cell_palette != palette: 142 if self.palette: 143 self.palette.popdown(True) 144 self.palette = None 145 146 self._palette_pos = (path, column) 147 self.cell_palette = palette 148 notify() 149 150 return False
151
152 - def notify_popup(self):
153 Invoker.notify_popup(self)
154
155 - def notify_popdown(self):
156 Invoker.notify_popdown(self)
157
158 - def _enter_cb(self, widget, event):
159 if self._enter_timeout: 160 gobject.source_remove(self._enter_timeout) 161 self._enter_timeout = gobject.timeout_add(_SHOW_PALETTE_TIMEOUT, 162 self._open_palette, self.notify_mouse_enter, False)
163
164 - def _leave_cb(self, widget, event):
165 self.notify_mouse_leave() 166 self._close_palette()
167
168 - def _button_cb(self, widget, event):
169 if event.button == 3: 170 return self._open_palette(self.notify_right_click, True) 171 else: 172 return False
173