root/branch/PacketManipulator/Tabs/ProtocolSelectorTab.py @ 3283

Revision 3283, 5.7 kB (checked in by nopper, 5 years ago)

Tab registering

Line 
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
21import gtk
22import Backend
23
24from views import UmitView
25from collections import defaultdict
26   
27class ProtocolTree(gtk.VBox):
28    COL_STR = 0
29    COL_OBJ = 1
30
31    def __init__(self):
32        super(ProtocolTree, self).__init__(False, 2)
33       
34        toolbar = gtk.Toolbar()
35        toolbar.set_style(gtk.TOOLBAR_ICONS)
36        #toolbar.set_icon_size(gtk.ICON_SIZE_SMALL_TOOLBAR)
37        toolbar.modify_bg(gtk.STATE_NORMAL, toolbar.style.bg[gtk.STATE_NORMAL])
38       
39        stocks = (gtk.STOCK_SORT_DESCENDING,
40                  gtk.STOCK_SORT_ASCENDING,
41                  gtk.STOCK_CONVERT)
42       
43        callbacks = (self.__on_sort_descending,
44                     self.__on_sort_ascending,
45                     self.__on_sort_layer)
46       
47        for stock, cb in zip(stocks, callbacks):
48            action = gtk.Action('', '', '', stock)
49            item = action.create_tool_item()
50            item.connect('clicked', cb)
51           
52            toolbar.insert(item, -1)
53       
54        self.store = gtk.TreeStore(str, object)
55        self.tree = gtk.TreeView()
56       
57        rend = gtk.CellRendererText()
58        col = gtk.TreeViewColumn('Protocols', rend, text=0)
59        col.set_cell_data_func(rend, self.__cell_data_func)
60       
61        self.tree.append_column(col)
62        self.tree.set_enable_tree_lines(True)
63        self.tree.set_rules_hint(True)
64       
65        self.tree.enable_model_drag_source(
66            gtk.gdk.BUTTON1_MASK,
67            [('text/plain', 0, 0)],
68            gtk.gdk.ACTION_COPY
69        )
70       
71        self.tree.connect_after('drag-begin', self.__on_drag_begin)
72        self.tree.connect('drag-data-get', self.__on_drag_data_get)
73
74        sw = gtk.ScrolledWindow()
75        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
76        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
77       
78        sw.add(self.tree)
79       
80        self.pack_start(toolbar, False, False)
81        self.pack_start(sw)
82       
83        self.populate()
84        self.__on_sort_descending(None)
85   
86    def populate(self, fill=True):
87        self.store.clear()
88       
89        if fill:
90            for i in Backend.get_protocols():
91                self.store.append(None, [i.__name__, i])
92        else:
93            return Backend.get_protocols()
94       
95    def __on_sort_descending(self, item):
96        self.populate()
97        model = gtk.TreeModelSort(self.store)
98        model.set_sort_column_id(0, gtk.SORT_DESCENDING)
99        self.tree.set_model(model)
100   
101    def __on_sort_ascending(self, item):
102        self.populate()
103        model = gtk.TreeModelSort(self.store)
104        model.set_sort_column_id(0, gtk.SORT_ASCENDING)
105        self.tree.set_model(model)
106   
107    def __on_sort_layer(self, item):
108        lst = self.populate(False)
109       
110        dct = defaultdict(list)
111       
112        for proto in lst:
113            dct[proto.layer].append(proto)
114       
115        for i in xrange(1, 8, 1):
116            it = self.store.append(None, ["Layer %d" % i, None])
117           
118            if not i in dct:
119                continue
120           
121            for proto in dct[i]:
122                self.store.append(it, [proto.__name__, proto])
123       
124        self.tree.set_model(self.store)
125   
126    def __cell_data_func(self, column, cell, model, iter):
127        val = model.get_value(iter, ProtocolTree.COL_STR)
128        obj = model.get_value(iter, ProtocolTree.COL_OBJ)
129       
130        if not obj:
131            # This is a layer text so markup and color
132            cell.set_property('markup', '<b>%s</b>' % val)
133            cell.set_property('cell-background-gdk',
134                              self.style.base[gtk.STATE_INSENSITIVE])
135        else:
136            cell.set_property('cell-background-gdk', None)
137   
138    def __on_drag_begin(self, widget, ctx):
139        ctx.set_icon_default()
140
141        # We could use that stuff in moo implementation
142        widget = self.tree
143        cmap = widget.get_colormap()
144        width, height = widget.window.get_size()
145
146        pix = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
147        pix = pix.get_from_drawable(widget.window, cmap, 0, 0, 0, 0,
148                                    width, height)
149        pix = pix.scale_simple(width * 4 / 5, height / 2, gtk.gdk.INTERP_HYPER)
150        ctx.set_icon_pixbuf(pix, 0, 0)
151
152        return False
153
154    def __on_drag_data_get(self, btn, ctx, sel, info, time):
155        model, iter = self.tree.get_selection().get_selected()
156        sel.set_text(model.get_value(iter, ProtocolTree.COL_STR))
157
158        return True
159
160class ProtocolSelectorTab(UmitView):
161    "The protocol selector tab"
162
163    icon_name = gtk.STOCK_CONNECT
164    label_text = "Protocols"
165    tab_position = gtk.POS_RIGHT
166
167    def create_ui(self):
168        self.tree = ProtocolTree()
169        self._main_widget.add(self.tree)
170        self._main_widget.show_all()
171
172if __name__ == "__main__":
173    w = gtk.Window()
174    w.add(ProtocolTree())
175    w.show_all()
176    gtk.main()
Note: See TracBrowser for help on using the browser.