root/branch/ggpolo/umitInventory/itg-integration/GraphPreferences.py @ 1020

Revision 1020, 16.1 kB (checked in by ggpolo, 6 years ago)

added more options for changes retrieve from database, GraphPreferences? now sets custom options, fixed ballon positioning on TLGraph

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 higwidgets.higwindows import HIGWindow
23from higwidgets.higboxes import HIGVBox, HIGHBox, hig_box_space_holder
24from higwidgets.higlabels import HIGEntryLabel
25from higwidgets.higframe import HIGFrame
26from higwidgets.higbuttons import HIGButton
27
28"""
29ToDO: Load preferences based on current graph settings.
30"""
31
32class GraphPreferences(HIGWindow):
33    """
34    Graph Preferences Editor
35    """
36   
37    def __init__(self, daddy=None):
38        HIGWindow.__init__(self)
39
40        self.daddy = daddy
41        self.wtitle = _("Graph Preferences Editor")
42       
43        # header
44        self.title_markup = "<span size='16500' weight='heavy'>%s</span>"
45        self.ttitle = HIGEntryLabel("")
46        self.ttitle.set_line_wrap(False)
47        self.ttitle.set_markup(self.title_markup % self.wtitle)
48        #self.umit_logo = gtk.Image()
49        #self.umit_logo.set_from_file(logo)
50        # profiles
51        self.graph_profile_lbl = HIGEntryLabel(_("Profile"))
52        self.graph_profile = gtk.combo_box_new_text()
53        self.graph_profile.connect('changed', self.setup_controls)
54        # horizontal divisors/balloons
55        self.hdivs_lbl = HIGEntryLabel(_("Horizontal divisors"))
56        self.hdivs = gtk.SpinButton(gtk.Adjustment(value=5, lower=2, upper=10, 
57                                                   step_incr=1), 1)
58        self.hdivs.connect('value-changed', lambda c: \
59                           self.graph_profile.set_active(0))
60        # arc drawing
61        self.draw_arc_lbl = HIGEntryLabel('')
62        self.draw_arc_lbl.set_markup(_("<b>Points higlight</b>"))
63        self.draw_arc_onsel = gtk.RadioButton(label=_("On Selection"))
64        self.draw_arc_always = gtk.RadioButton(self.draw_arc_onsel, 
65                                               label=_("Always"))
66        self.draw_arc_always.connect('clicked', lambda c: \
67                                     self.graph_profile.set_active(0))
68        self.draw_arc_bounds = gtk.RadioButton(label=_("Boundary points only"))
69        self.draw_arc_bounds.connect('clicked', lambda c: \
70                                     self.graph_profile.set_active(0))
71        self.draw_arc_allpts = gtk.RadioButton(self.draw_arc_bounds, 
72                                               label=_("Every point"))
73        self.draw_arc_allpts.connect('clicked', lambda c: \
74                                     self.graph_profile.set_active(0))
75        self.balloons = gtk.CheckButton(_("Show balloons on selection"))
76        self.balloons.connect('toggled', lambda c: \
77                              self.graph_profile.set_active(0))
78        # vertical divisors
79        self.draw_vertdiv = gtk.CheckButton(_("Draw vertical lines"))
80        self.draw_vertdiv.connect('toggled', self._vertdiv_changed)
81        self.draw_vertdiv_dash = gtk.RadioButton(label=_("Dashed"))
82        self.draw_vertdiv_dash.connect('clicked', lambda c: \
83                                       self.graph_profile.set_active(0))
84        self.draw_vertdiv_solid = gtk.RadioButton(self.draw_vertdiv_dash, 
85                                                  label=_("Solid"))
86        self.draw_vertdiv_solid.connect('clicked', lambda c: \
87                                        self.graph_profile.set_active(0))
88        # background fill
89        self.bg_gradient = gtk.CheckButton(_("Gradient background"))
90        self.bg_gradient.connect('toggled', self._bgfill_changed)
91        self.bg_gradient_vert = gtk.RadioButton(label=_("Vertical gradient"))
92        self.bg_gradient_vert.connect('clicked', lambda c: \
93                                      self.graph_profile.set_active(0))
94        self.bg_gradient_horiz = gtk.RadioButton(self.bg_gradient_vert, 
95                                                 label=_("Horizontal gradient"))
96        self.bg_gradient_horiz.connect('clicked', lambda c: \
97                                       self.graph_profile.set_active(0))
98        # selection fill
99        self.selection_fill = gtk.CheckButton(_("On selection do \
100progressive fill"))
101        self.selection_fill.connect('toggled', self._selfill_changed)
102        self.selection_fill_solid = gtk.RadioButton(label=_("Solid fill"))
103        self.selection_fill_solid.connect('clicked', lambda c: \
104                                          self.graph_profile.set_active(0))
105        self.selection_fill_gradient = gtk.RadioButton(self.selection_fill_solid,
106                                                       label=_("Gradient fill"))
107        self.selection_fill_gradient.connect('clicked', lambda c: \
108                                             self.graph_profile.set_active(0))
109        # bottom buttons
110        self.help = HIGButton(stock=gtk.STOCK_HELP)
111        self.help.connect('clicked', self._show_help)
112        self.apply = HIGButton(stock=gtk.STOCK_APPLY)
113        self.apply.connect('clicked', self._save_pref)
114        self.cancel = HIGButton(stock=gtk.STOCK_CANCEL)
115        self.cancel.connect('clicked', self._exit)
116        self.ok = HIGButton(stock=gtk.STOCK_OK)
117        self.ok.connect('clicked', self._save_pref_and_leave)
118       
119
120        self.append_profiles()
121        self.setup_controls(None)
122        self.__set_props()
123        self.__do_layout()
124       
125
126    def append_profiles(self):
127        """
128        Write profiles to combobox.
129        """
130        profiles = ( _("Custom"), _("Standard"), _("Best Performance"),
131                     _("Best Visual") )
132       
133        for p in profiles:
134            self.graph_profile.append_text(p)
135       
136        self.graph_profile.set_active(1)
137       
138
139    def setup_controls(self, event):
140        """
141        Enable/Disable controls.
142        """
143        opt_profiles = ( _("Standard"), _("Best Performance"), 
144                         _("Best Visual") )
145       
146        profile = self.graph_profile.get_active_text()
147        indx_active = self.graph_profile.get_active()
148        if not profile in opt_profiles:
149            return
150               
151        profiles = { _("Standard"): (5, 0, 0, True, True, 0, False, -1, 
152                                     True, 0),
153                     _("Best Performance"): (3, 0, 0, True, False, -1,
154                                             False, -1, False, -1),
155                     _("Best Visual"): (5, 0, 1, True, True, 0, True, 1,
156                                        True, 1) }
157       
158        controls = (self.hdivs, (self.draw_arc_onsel, self.draw_arc_always), 
159                    (self.draw_arc_bounds, self.draw_arc_allpts),
160                    self.balloons, self.draw_vertdiv, (self.draw_vertdiv_dash, 
161                    self.draw_vertdiv_solid), self.bg_gradient, 
162                    (self.bg_gradient_vert, self.bg_gradient_horiz), 
163                    self.selection_fill, (self.selection_fill_solid, 
164                    self.selection_fill_gradient))
165       
166       
167        for indx, control in enumerate(controls):
168            value = profiles[profile][indx]
169           
170            if value >= 2:
171                control.set_text("%d" % value)
172            elif type(value) == type(True):
173                control.set_active(value)
174            elif value != -1:
175                control[value].set_active(True)
176         
177       
178        checkbtns = ( self.draw_vertdiv, self.bg_gradient,
179                     self.selection_fill )
180        methods = ( self._vertdiv_changed, self._bgfill_changed, 
181                    self._selfill_changed )
182         
183        for indx, checkbtn in enumerate(checkbtns):
184            methods[indx](checkbtn.get_active(), indx_active)
185       
186        return True
187       
188       
189    def _vertdiv_changed(self, event, custom=0):
190        """
191        Vertical divisors enabled/disabled.
192        """
193        status = self.get_status(event)
194           
195        self.draw_vertdiv_dash.set_sensitive(status)
196        self.draw_vertdiv_solid.set_sensitive(status)
197       
198        self.graph_profile.set_active(custom)
199       
200   
201    def _bgfill_changed(self, event, custom=0):
202        """
203        Gradient background fill enabled/disabled.
204        """
205        status = self.get_status(event)
206                   
207        self.bg_gradient_vert.set_sensitive(status)
208        self.bg_gradient_horiz.set_sensitive(status)
209       
210        self.graph_profile.set_active(custom)
211       
212   
213    def _selfill_changed(self, event, custom=0):
214        """
215        Selection fill effect enabled/disabled.
216        """
217        status = self.get_status(event)
218           
219        self.selection_fill_gradient.set_sensitive(status)
220        self.selection_fill_solid.set_sensitive(status)
221   
222        self.graph_profile.set_active(custom)   
223       
224       
225    def get_status(self, event):
226        """
227        Get event status.
228        """
229        if type(event) == type(True):
230            status = event
231        else:
232            status = event.get_active()
233
234        return status
235
236
237    def _show_help(self, event):
238        """
239        Show help for Graph Preferences.
240        """
241       
242   
243    def _save_pref(self, event):
244        """
245        Save preferences.
246        """
247        profile = self.graph_profile.get_active_text()
248
249        cmds = { _("Standard"): "standard_mode",
250                 _("Best Performance"): "speedup_performance", 
251                 _("Best Visual"): "best_visual" }
252       
253        if profile != _("Custom"):
254            self.daddy.change_graph_mode = cmds[profile]
255            return
256       
257        attrs = ( "hdivisors", "draw_arcs_always", "draw_every_arc",
258                  "show_balloons", "draw_dashed_vert", "draw_solid_vert",
259                  "gradient_fill", "gradient_direction", "selection_effect",
260                  "selection_gradient" )
261
262        controls = (self.hdivs, self.draw_arc_always, self.draw_arc_allpts,
263                    self.balloons, (self.draw_vertdiv, self.draw_vertdiv_dash, 
264                    self.draw_vertdiv_solid), self.bg_gradient, 
265                    self.bg_gradient_horiz, self.selection_fill, 
266                    self.selection_fill_gradient)
267
268        indx = 0
269        for control in controls:
270            if type(control) == type(tuple()): # group of controls
271                enable = True
272                if not control[0].get_active():
273                    enable = False
274
275                for c in control[1:]:
276                    if enable == False:
277                        self.daddy.change_graph_attr = attrs[indx], False
278                    else:
279                        self.daddy.change_graph_attr = attrs[indx], \
280                                                       c.get_active()
281                    indx += 1
282
283                continue
284
285            elif hasattr(control, "get_active"): # radio/checkbutton
286                self.daddy.change_graph_attr = attrs[indx], control.get_active()
287                   
288            elif hasattr(control, "set_width_chars"): # spinbutton
289                nhdiv = int(control.get_text())
290                self.daddy.change_graph_attr = attrs[indx], nhdiv
291           
292            indx += 1
293
294        self.daddy.update_graph()
295       
296
297    def _save_pref_and_leave(self, event):
298        """
299        Saves preferences and close window.
300        """
301        self._save_pref(None)
302        self._exit(None)
303       
304       
305    def _exit(self, event):
306        """
307        Close window.
308        """
309        self.destroy()
310
311       
312    def __set_props(self):
313        """
314        Set window properties.
315        """
316        self.set_title(self.wtitle)
317       
318       
319    def __do_layout(self):
320        """
321        Layout widgets in window.
322        """
323       
324        def left_padding(widget):
325            """
326            Add left padding for a widget.
327            """
328            left_padding_align = gtk.Alignment(0.5, 0.5, 1, 1)
329            left_padding_align.set_padding(0, 0, 12, 0)
330            left_padding_align.add(widget)
331       
332            return left_padding_align
333       
334       
335        main_vbox = HIGVBox()
336        main_vbox.set_border_width(5)
337        main_vbox.set_spacing(12)
338        header_hbox = HIGHBox()
339        profile_hbox = HIGHBox()
340        hdivs_hbox = HIGHBox()
341        arcdraw_vbox = HIGVBox()
342        vdivs_vbox = HIGVBox()
343        bgfill_vbox = HIGVBox()
344        selectfill_vbox = HIGVBox()
345        balloons_vbox = HIGVBox()
346        btns_hbox = HIGHBox()
347       
348        # header
349        header_hbox._pack_expand_fill(self.ttitle)
350        #header_hbox._pack_noexpand_nofill(self.umit_logo)
351       
352        # profiles
353        profile_hbox._pack_noexpand_nofill(self.graph_profile_lbl)
354        profile_hbox._pack_noexpand_nofill(self.graph_profile)
355       
356        # horizontal divisors
357        hdivs_hbox._pack_noexpand_nofill(self.hdivs_lbl)
358        hdivs_hbox._pack_noexpand_nofill(self.hdivs)
359       
360        # arc drawing
361        arcdraw_vbox._pack_noexpand_nofill(self.draw_arc_lbl)
362       
363        arcdraw_when = HIGHBox()
364        arcdraw_when._pack_noexpand_nofill(self.draw_arc_onsel)
365        arcdraw_when._pack_noexpand_nofill(self.draw_arc_always)
366
367        arcdraw_where = HIGHBox()
368        arcdraw_where._pack_noexpand_nofill(self.draw_arc_bounds)
369        arcdraw_where._pack_noexpand_nofill(self.draw_arc_allpts)
370               
371        arcdraw_vbox._pack_noexpand_nofill(left_padding(arcdraw_when))
372        arcdraw_vbox._pack_noexpand_nofill(left_padding(arcdraw_where))
373        arcdraw_vbox._pack_noexpand_nofill(left_padding(self.balloons))
374
375        # vertical divisors
376        vdivs_vbox._pack_noexpand_nofill(self.draw_vertdiv)
377       
378        vdivs_kind = HIGHBox()
379        vdivs_kind._pack_noexpand_nofill(self.draw_vertdiv_dash)
380        vdivs_kind._pack_noexpand_nofill(self.draw_vertdiv_solid)
381       
382        vdivs_vbox._pack_noexpand_nofill(left_padding(vdivs_kind))
383       
384        # background fill
385        bgfill_vbox._pack_noexpand_nofill(self.bg_gradient)
386       
387        bgfill_gtype = HIGHBox()
388        bgfill_gtype._pack_noexpand_nofill(self.bg_gradient_vert)
389        bgfill_gtype._pack_noexpand_nofill(self.bg_gradient_horiz)
390       
391        bgfill_vbox._pack_noexpand_nofill(left_padding(bgfill_gtype))
392       
393        # selection fill
394        selectfill_vbox._pack_noexpand_nofill(self.selection_fill)
395       
396        selectfill_kind = HIGHBox()
397        selectfill_kind._pack_noexpand_nofill(self.selection_fill_solid)
398        selectfill_kind._pack_noexpand_nofill(self.selection_fill_gradient)
399       
400        selectfill_vbox._pack_noexpand_nofill(left_padding(selectfill_kind))
401     
402        # bottom buttons
403        btns_hbox.set_homogeneous(True)
404        btns_hbox._pack_expand_fill(self.help)
405        btns_hbox._pack_expand_fill(hig_box_space_holder())
406        btns_hbox._pack_expand_fill(self.apply)
407        btns_hbox._pack_expand_fill(self.cancel)
408        btns_hbox._pack_expand_fill(self.ok)       
409
410       
411        main_vbox._pack_noexpand_nofill(header_hbox)
412        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
413        main_vbox._pack_noexpand_nofill(profile_hbox)
414        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
415        main_vbox._pack_noexpand_nofill(hdivs_hbox)
416        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
417        main_vbox._pack_noexpand_nofill(arcdraw_vbox)
418        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
419        main_vbox._pack_noexpand_nofill(vdivs_vbox)
420        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
421        main_vbox._pack_noexpand_nofill(bgfill_vbox)
422        main_vbox._pack_noexpand_nofill(gtk.HSeparator())
423        main_vbox._pack_noexpand_nofill(selectfill_vbox)
424        main_vbox.pack_end(btns_hbox, False, False, 0)
425        main_vbox.pack_end(gtk.HSeparator(), False, False, 0)
426        self.add(main_vbox)
427       
Note: See TracBrowser for help on using the browser.