root/branch/ggpolo/umitInventory/TLGraphToolbar.py @ 1023

Revision 1023, 6.3 kB (checked in by ggpolo, 6 years ago)

Several updates for Timeline integration.

Line 
1# Copyright (C) 2007 Insecure.Com LLC.
2#
3# Authors: Guilherme Polo <ggpolo@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18# USA
19
20import gtk
21
22from umitCore.I18N import _
23
24from DataGrabber import DataGrabber
25from TLGraphPreferences import GraphPreferences
26
27"""
28ToDO: Add support for time range change, show current visualization date.
29"""
30
31class GraphControllerTB(gtk.Toolbar):
32    """
33    Builds a Toolbar for controlling Interactive Timeline Graph.
34    """
35   
36    def __init__(self, daddy, connector):
37        gtk.Toolbar.__init__(self)
38       
39        self.daddy = daddy
40        self.connector = connector
41       
42        index = (i for i in xrange(7))
43        self.tooltips = gtk.Tooltips()
44       
45        #self.insert(self.zoom_control(), index.next())
46        self.insert(self.viewing_kind(), index.next())
47        self.insert(self.graph_kind(), index.next())
48        self.insert(self.graph_pref(), index.next())
49
50
51    def packed(self, *widgets):
52        """
53        Pack widgets in a gtk.HBox and returns a gtk.ToolItem
54        """
55        tbox = gtk.HBox()
56        for w in widgets:
57            tbox.pack_start(w, False, False, 0)
58
59        titem = gtk.ToolItem()
60        titem.add(tbox)
61
62        return titem
63
64
65    def timerange_control(self):
66        """
67        Creates widgets for handling graph timerange visualization.
68        """
69       
70       
71
72    def zoom_control(self):
73        """
74        Setup combobox for managing graph zoom level.
75        """
76        views = (_("Yearly View"), _("Monthly View"), _("Daily View"), _("Hourly View"))     
77        self.zoomgraph = gtk.combo_box_new_text()
78
79        for v in views:
80            self.zoomgraph.append_text(v)
81
82        self.zoomgraph.set_active(0)
83        self.zoomgraph.connect('changed', self._change_graph_zoom)
84   
85        return self.packed(self.zoomgraph)
86
87   
88    def viewing_kind(self):
89        """
90        Combobox that displays possible ways on how graph may grab changes.
91        """
92        modes = (_("Sum of changes"), _("By category"))
93        self.evtsgraph = gtk.combo_box_new_text()
94        self.evtsgraph.append_text(_("Select visualization"))
95       
96        for mode in modes:
97            self.evtsgraph.append_text(mode)
98
99        self.evtsgraph.set_active(1)
100        self.evtsgraph.connect('changed', self._change_graph_data)
101
102        return self.packed(self.evtsgraph)
103
104
105    def graph_kind(self):
106        """
107        Combobox for setting graph kind. Right now there is only line graph
108        and area graph.
109        """
110        modes = (_("Line Graph"), _("Area Graph"))
111        self.cbgraphs = gtk.combo_box_new_text()
112        self.cbgraphs.append_text(_("Select a graph style"))
113
114        for mode in modes:
115            self.cbgraphs.append_text(mode)
116
117        self.cbgraphs.set_active(1)
118        self.cbgraphs.connect('changed', self._change_graph_kind)
119
120        return self.packed(self.cbgraphs)
121
122
123    def graph_pref(self):
124        """
125        Button for opening Graph Preferences window.
126        """
127        self.btn_gpref = gtk.Button(stock=gtk.STOCK_EDIT)
128        self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT)
129        # get label widget to change default label text "Edit" to
130        # "Graph Preferences"
131        blbl = self.btn_gpref.get_children()[0].get_children()[0].get_children()[1]
132        blbl.set_text(_("Graph Preferences"))
133       
134        self.btn_gpref.connect('clicked', self._open_graph_pref)
135        self.btn_gpref.show()
136
137        self.tooltips.set_tip(self.btn_gpref, _("Edit Graph Preferences"))
138
139        return self.packed(self.btn_gpref)
140   
141
142    def update_graph(self):
143        """
144        Active options set.
145        """
146        self.daddy.setup_new_graph()
147
148
149    def _change_graph_zoom(self, event):
150        """
151        Set new zoom level
152        """
153        self.connector.emit('zoom-update', event.get_active())
154
155
156    def _change_graph_kind(self, event):
157        """
158        Set new graph kind.
159        """
160        active = event.get_active()
161        if active: # Didnt select "Select a graph style"
162            setattr(self.daddy, "graph_type", active-1)
163
164
165    def _change_graph_data(self, event):
166        """
167        Changing how graph displays events will result in grabbing events in
168        a different way.
169        """
170        active = event.get_active()
171        if active: # Didnt select "Select visualization"
172
173            # this may be changed soon! (1)
174            if active == 1:
175                max, start, evts = DataGrabber().changes_in_year_by_week(2007)
176                filter = { }
177                filter[0] = (True, _("Changes Sum"))
178            elif active == 2:
179                filter, max, start, \
180                evts = DataGrabber().changes_in_year_by_category_by_week(2007)
181               
182                for key, value in filter.items():
183                    filter[key-1] = (True, value)
184
185                del filter[len(filter)-1]
186            # (1)
187           
188            self.daddy.max = max
189            self.daddy.start_pts_data = start
190            self.daddy.graph_data = evts
191           
192            self.connector.emit('filter-update', filter)
193           
194            self.update_graph()
195
196
197    def _open_graph_pref(self, event):
198        """
199        Open Graph Preferences window.
200        """
201        w = GraphPreferences(self)
202        w.show_all()
203
204
205    def __set_graph_mode(self, mode):
206        """
207        Change graph mode.
208        """
209        getattr(self.daddy, mode)()
210
211   
212    def __set_graph_attr(self, (attr, value)):
213        """
214        Change some graph attribute.
215        """
216        setattr(self.daddy, attr, value)
217   
218
219    # Properties
220    change_graph_mode = property(fset=__set_graph_mode)
221    change_graph_attr = property(fset=__set_graph_attr)
222
223
Note: See TracBrowser for help on using the browser.