| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # Copyright (C) 2008 Adriano Monteiro Marques |
|---|
| 4 | # |
|---|
| 5 | # Author: Francesco Piccinno <stack.box@gmail.com> |
|---|
| 6 | # |
|---|
| 7 | # This program is free software; you can redistribute it and/or modify |
|---|
| 8 | # it under the terms of the GNU General Public License as published by |
|---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | # (at your option) any later version. |
|---|
| 11 | # |
|---|
| 12 | # This program is distributed in the hope that it will be useful, |
|---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | # GNU General Public License for more details. |
|---|
| 16 | # |
|---|
| 17 | # You should have received a copy of the GNU General Public License |
|---|
| 18 | # along with this program; if not, write to the Free Software |
|---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | import gtk |
|---|
| 22 | import Backend |
|---|
| 23 | |
|---|
| 24 | from views import UmitView |
|---|
| 25 | from collections import defaultdict |
|---|
| 26 | from Icons import get_pixbuf |
|---|
| 27 | |
|---|
| 28 | class ProtocolTree(gtk.VBox): |
|---|
| 29 | COL_PIX = 0 |
|---|
| 30 | COL_STR = 1 |
|---|
| 31 | COL_OBJ = 2 |
|---|
| 32 | |
|---|
| 33 | def __init__(self): |
|---|
| 34 | super(ProtocolTree, self).__init__(False, 2) |
|---|
| 35 | |
|---|
| 36 | toolbar = gtk.Toolbar() |
|---|
| 37 | toolbar.set_style(gtk.TOOLBAR_ICONS) |
|---|
| 38 | #toolbar.set_icon_size(gtk.ICON_SIZE_SMALL_TOOLBAR) |
|---|
| 39 | toolbar.modify_bg(gtk.STATE_NORMAL, toolbar.style.bg[gtk.STATE_NORMAL]) |
|---|
| 40 | |
|---|
| 41 | stocks = (gtk.STOCK_SORT_DESCENDING, |
|---|
| 42 | gtk.STOCK_SORT_ASCENDING, |
|---|
| 43 | gtk.STOCK_CONVERT) |
|---|
| 44 | |
|---|
| 45 | callbacks = (self.__on_sort_descending, |
|---|
| 46 | self.__on_sort_ascending, |
|---|
| 47 | self.__on_sort_layer) |
|---|
| 48 | |
|---|
| 49 | for stock, cb in zip(stocks, callbacks): |
|---|
| 50 | action = gtk.Action('', '', '', stock) |
|---|
| 51 | item = action.create_tool_item() |
|---|
| 52 | item.connect('clicked', cb) |
|---|
| 53 | |
|---|
| 54 | toolbar.insert(item, -1) |
|---|
| 55 | |
|---|
| 56 | self.store = gtk.TreeStore(gtk.gdk.Pixbuf, str, object) |
|---|
| 57 | self.tree = gtk.TreeView() |
|---|
| 58 | |
|---|
| 59 | txt = gtk.CellRendererText() |
|---|
| 60 | pix = gtk.CellRendererPixbuf() |
|---|
| 61 | |
|---|
| 62 | col = gtk.TreeViewColumn('Protocols') |
|---|
| 63 | col.pack_start(pix, False) |
|---|
| 64 | col.pack_start(txt) |
|---|
| 65 | |
|---|
| 66 | col.set_attributes(pix, pixbuf=ProtocolTree.COL_PIX) |
|---|
| 67 | col.set_attributes(txt, text=ProtocolTree.COL_STR) |
|---|
| 68 | |
|---|
| 69 | col.set_cell_data_func(pix, self.__pix_cell_data_func) |
|---|
| 70 | col.set_cell_data_func(txt, self.__txt_cell_data_func) |
|---|
| 71 | |
|---|
| 72 | txt.set_property('xpad', 6) |
|---|
| 73 | pix.set_property('xpad', 0) |
|---|
| 74 | |
|---|
| 75 | self.tree.append_column(col) |
|---|
| 76 | self.tree.set_enable_tree_lines(True) |
|---|
| 77 | self.tree.set_rules_hint(True) |
|---|
| 78 | |
|---|
| 79 | self.tree.enable_model_drag_source( |
|---|
| 80 | gtk.gdk.BUTTON1_MASK, |
|---|
| 81 | [('text/plain', 0, 0)], |
|---|
| 82 | gtk.gdk.ACTION_COPY |
|---|
| 83 | ) |
|---|
| 84 | |
|---|
| 85 | self.tree.connect_after('drag-begin', self.__on_drag_begin) |
|---|
| 86 | self.tree.connect('drag-data-get', self.__on_drag_data_get) |
|---|
| 87 | |
|---|
| 88 | sw = gtk.ScrolledWindow() |
|---|
| 89 | sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) |
|---|
| 90 | sw.set_shadow_type(gtk.SHADOW_ETCHED_IN) |
|---|
| 91 | |
|---|
| 92 | sw.add(self.tree) |
|---|
| 93 | |
|---|
| 94 | self.pack_start(toolbar, False, False) |
|---|
| 95 | self.pack_start(sw) |
|---|
| 96 | |
|---|
| 97 | self.proto_icon = get_pixbuf('protocol_small') |
|---|
| 98 | self.layer_icon = get_pixbuf('layer_small') |
|---|
| 99 | |
|---|
| 100 | self.populate() |
|---|
| 101 | self.__on_sort_descending(None) |
|---|
| 102 | |
|---|
| 103 | def populate(self, fill=True): |
|---|
| 104 | self.store.clear() |
|---|
| 105 | |
|---|
| 106 | if fill: |
|---|
| 107 | for i in Backend.get_protocols(): |
|---|
| 108 | self.store.append(None, [self.proto_icon, i.__name__, i]) |
|---|
| 109 | else: |
|---|
| 110 | return Backend.get_protocols() |
|---|
| 111 | |
|---|
| 112 | def __on_sort_descending(self, item): |
|---|
| 113 | self.populate() |
|---|
| 114 | model = gtk.TreeModelSort(self.store) |
|---|
| 115 | model.set_sort_column_id(ProtocolTree.COL_STR, gtk.SORT_DESCENDING) |
|---|
| 116 | self.tree.set_model(model) |
|---|
| 117 | |
|---|
| 118 | def __on_sort_ascending(self, item): |
|---|
| 119 | self.populate() |
|---|
| 120 | model = gtk.TreeModelSort(self.store) |
|---|
| 121 | model.set_sort_column_id(ProtocolTree.COL_STR, gtk.SORT_ASCENDING) |
|---|
| 122 | self.tree.set_model(model) |
|---|
| 123 | |
|---|
| 124 | def __on_sort_layer(self, item): |
|---|
| 125 | lst = self.populate(False) |
|---|
| 126 | |
|---|
| 127 | dct = defaultdict(list) |
|---|
| 128 | |
|---|
| 129 | for proto in lst: |
|---|
| 130 | dct[proto.layer].append(proto) |
|---|
| 131 | |
|---|
| 132 | for i in xrange(1, 8, 1): |
|---|
| 133 | it = self.store.append(None, [self.layer_icon, "Layer %d" % i, None]) |
|---|
| 134 | |
|---|
| 135 | if not i in dct: |
|---|
| 136 | continue |
|---|
| 137 | |
|---|
| 138 | for proto in dct[i]: |
|---|
| 139 | self.store.append(it, [self.proto_icon, proto.__name__, proto]) |
|---|
| 140 | |
|---|
| 141 | self.tree.set_model(self.store) |
|---|
| 142 | |
|---|
| 143 | def __pix_cell_data_func(self, column, cell, model, iter): |
|---|
| 144 | val = model.get_value(iter, ProtocolTree.COL_STR) |
|---|
| 145 | obj = model.get_value(iter, ProtocolTree.COL_OBJ) |
|---|
| 146 | |
|---|
| 147 | if not obj: |
|---|
| 148 | cell.set_property('cell-background-gdk', |
|---|
| 149 | self.style.base[gtk.STATE_INSENSITIVE]) |
|---|
| 150 | else: |
|---|
| 151 | cell.set_property('cell-background-gdk', None) |
|---|
| 152 | |
|---|
| 153 | def __txt_cell_data_func(self, column, cell, model, iter): |
|---|
| 154 | val = model.get_value(iter, ProtocolTree.COL_STR) |
|---|
| 155 | obj = model.get_value(iter, ProtocolTree.COL_OBJ) |
|---|
| 156 | |
|---|
| 157 | if not obj: |
|---|
| 158 | # This is a layer text so markup and color |
|---|
| 159 | cell.set_property('markup', '<b>%s</b>' % val) |
|---|
| 160 | cell.set_property('cell-background-gdk', |
|---|
| 161 | self.style.base[gtk.STATE_INSENSITIVE]) |
|---|
| 162 | else: |
|---|
| 163 | cell.set_property('cell-background-gdk', None) |
|---|
| 164 | |
|---|
| 165 | def __on_drag_begin(self, widget, ctx): |
|---|
| 166 | ctx.set_icon_default() |
|---|
| 167 | |
|---|
| 168 | # We could use that stuff in moo implementation |
|---|
| 169 | widget = self.tree |
|---|
| 170 | cmap = widget.get_colormap() |
|---|
| 171 | width, height = widget.window.get_size() |
|---|
| 172 | |
|---|
| 173 | pix = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height) |
|---|
| 174 | pix = pix.get_from_drawable(widget.window, cmap, 0, 0, 0, 0, |
|---|
| 175 | width, height) |
|---|
| 176 | pix = pix.scale_simple(width * 4 / 5, height / 2, gtk.gdk.INTERP_HYPER) |
|---|
| 177 | ctx.set_icon_pixbuf(pix, 0, 0) |
|---|
| 178 | |
|---|
| 179 | return False |
|---|
| 180 | |
|---|
| 181 | def __on_drag_data_get(self, btn, ctx, sel, info, time): |
|---|
| 182 | model, iter = self.tree.get_selection().get_selected() |
|---|
| 183 | sel.set_text(model.get_value(iter, ProtocolTree.COL_STR)) |
|---|
| 184 | |
|---|
| 185 | return True |
|---|
| 186 | |
|---|
| 187 | class ProtocolSelectorTab(UmitView): |
|---|
| 188 | "The protocol selector tab" |
|---|
| 189 | |
|---|
| 190 | icon_name = gtk.STOCK_CONNECT |
|---|
| 191 | label_text = "Protocols" |
|---|
| 192 | tab_position = gtk.POS_RIGHT |
|---|
| 193 | |
|---|
| 194 | def create_ui(self): |
|---|
| 195 | self.tree = ProtocolTree() |
|---|
| 196 | self._main_widget.add(self.tree) |
|---|
| 197 | self._main_widget.show_all() |
|---|
| 198 | |
|---|
| 199 | if __name__ == "__main__": |
|---|
| 200 | w = gtk.Window() |
|---|
| 201 | w.add(ProtocolTree()) |
|---|
| 202 | w.show_all() |
|---|
| 203 | gtk.main() |
|---|