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

Revision 1030, 6.5 kB (checked in by ggpolo, 6 years ago)

Graph updates as data updates now.

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        self.connector.connect('data-update', self._update_graph_data)
51
52
53    def packed(self, *widgets):
54        """
55        Pack widgets in a gtk.HBox and returns a gtk.ToolItem
56        """
57        tbox = gtk.HBox()
58        for w in widgets:
59            tbox.pack_start(w, False, False, 0)
60
61        titem = gtk.ToolItem()
62        titem.add(tbox)
63
64        return titem
65
66
67    def timerange_control(self):
68        """
69        Creates widgets for handling graph timerange visualization.
70        """
71       
72       
73
74    def zoom_control(self):
75        """
76        Setup combobox for managing graph zoom level.
77        """
78        views = (_("Yearly View"), _("Monthly View"), _("Daily View"), _("Hourly View"))     
79        self.zoomgraph = gtk.combo_box_new_text()
80
81        for v in views:
82            self.zoomgraph.append_text(v)
83
84        self.zoomgraph.set_active(0)
85        self.zoomgraph.connect('changed', self._change_graph_zoom)
86   
87        return self.packed(self.zoomgraph)
88
89   
90    def viewing_kind(self):
91        """
92        Combobox that displays possible ways on how graph may grab changes.
93        """
94        modes = (_("Sum of changes"), _("By category"))
95        self.evtsgraph = gtk.combo_box_new_text()
96        self.evtsgraph.append_text(_("Select visualization"))
97       
98        for mode in modes:
99            self.evtsgraph.append_text(mode)
100
101        self.evtsgraph.set_active(1)
102        self.evtsgraph.connect('changed', self._change_graph_data)
103
104        return self.packed(self.evtsgraph)
105
106
107    def graph_kind(self):
108        """
109        Combobox for setting graph kind. Right now there is only line graph
110        and area graph.
111        """
112        modes = (_("Line Graph"), _("Area Graph"))
113        self.cbgraphs = gtk.combo_box_new_text()
114        self.cbgraphs.append_text(_("Select a graph style"))
115
116        for mode in modes:
117            self.cbgraphs.append_text(mode)
118
119        self.cbgraphs.set_active(1)
120        self.cbgraphs.connect('changed', self._change_graph_kind)
121
122        return self.packed(self.cbgraphs)
123
124
125    def graph_pref(self):
126        """
127        Button for opening Graph Preferences window.
128        """
129        self.btn_gpref = gtk.Button(stock=gtk.STOCK_EDIT)
130        self.sched_name_edit = gtk.Button(stock=gtk.STOCK_EDIT)
131        # get label widget to change default label text "Edit" to
132        # "Graph Preferences"
133        blbl = self.btn_gpref.get_children()[0].get_children()[0].get_children()[1]
134        blbl.set_text(_("Graph Preferences"))
135       
136        self.btn_gpref.connect('clicked', self._open_graph_pref)
137        self.btn_gpref.show()
138
139        self.tooltips.set_tip(self.btn_gpref, _("Edit Graph Preferences"))
140
141        return self.packed(self.btn_gpref)
142   
143
144    def update_graph(self):
145        """
146        Active options set.
147        """
148        self.daddy.setup_new_graph()
149
150
151    def _change_graph_zoom(self, event):
152        """
153        Set new zoom level
154        """
155        self.connector.emit('zoom-update', event.get_active())
156
157
158    def _change_graph_kind(self, event):
159        """
160        Set new graph kind.
161        """
162        active = event.get_active()
163        if active: # Didnt select "Select a graph style"
164            setattr(self.daddy, "graph_type", active-1)
165
166
167    def _update_graph_data(self, obj):
168        self._change_graph_data(self.evtsgraph.get_active())
169
170
171    def _change_graph_data(self, event):
172        """
173        Changing how graph displays events will result in grabbing events in
174        a different way.
175        """
176        active = (type(0) == type(event)) and event or event.get_active()
177        if active: # Didnt select "Select visualization"
178
179            # this may be changed soon! (1)
180            if active == 1:
181                max, start, evts = DataGrabber().changes_in_year_by_week(2007)
182                filter = { }
183                filter[0] = (True, _("Changes Sum"))
184            elif active == 2:
185                filter, max, start, \
186                evts = DataGrabber().changes_in_year_by_category_by_week(2007)
187               
188                for key, value in filter.items():
189                    filter[key-1] = (True, value)
190
191                if len(filter): # database has no changes registered yet
192                    del filter[len(filter)-1]
193            # (1)
194           
195            self.daddy.max = max
196            self.daddy.start_pts_data = start
197            self.daddy.graph_data = evts
198           
199            self.connector.emit('filter-update', filter)
200           
201            self.update_graph()
202
203
204    def _open_graph_pref(self, event):
205        """
206        Open Graph Preferences window.
207        """
208        w = GraphPreferences(self)
209        w.show_all()
210
211
212    def __set_graph_mode(self, mode):
213        """
214        Change graph mode.
215        """
216        getattr(self.daddy, mode)()
217
218   
219    def __set_graph_attr(self, (attr, value)):
220        """
221        Change some graph attribute.
222        """
223        setattr(self.daddy, attr, value)
224   
225
226    # Properties
227    change_graph_mode = property(fset=__set_graph_mode)
228    change_graph_attr = property(fset=__set_graph_attr)
229
230
Note: See TracBrowser for help on using the browser.