From: Eugene Gagarin Date: Thu, 21 May 2009 12:13:13 +0000 (+0400) Subject: Added 'Save to file' dialog to files.out_diabar and files.out_diapie X-Git-Url: http://git.maemo.org/git/?p=findit;a=commitdiff_plain;h=7825d7a6eef3ae64961b2aa7e96461b64e85b363 Added 'Save to file' dialog to files.out_diabar and files.out_diapie --- diff --git a/src/files/out_diabar.py b/src/files/out_diabar.py index 4968b89..296f9b8 100755 --- a/src/files/out_diabar.py +++ b/src/files/out_diabar.py @@ -16,10 +16,25 @@ class Cli_Presentation(object): #============================================================================== class Gtk_Presentation(object): + + def _create_toolbar(self): + toolbar = gtk.Toolbar() + toolbar.set_property('icon-size', 'small-toolbar') + + saveresults_tbtn = gtk.ToolButton('gtk-save') + saveresults_tbtn.connect('clicked', self.save_results) + + toolbar.insert(saveresults_tbtn, -1) + + return toolbar + def __init__(self, results, maxdata=10): import gtk - import cairo; global cairo - import pycha.bar; global pycha + global gtk # for _create_toolbar() + import cairo + global cairo # for expose_event() + import pycha.bar + global pycha # for expose_event() filelist, start_path = results @@ -30,6 +45,10 @@ class Gtk_Presentation(object): # maximum size maximum = max([bsize for bsize, path, size in filelist]) + #==================== + # Diagram + #==================== + # only byte sizes self.data = ( ('sizes', @@ -86,21 +105,47 @@ class Gtk_Presentation(object): drawing = gtk.DrawingArea() drawing.connect('expose-event', self.expose_event) - self.toplevel = drawing + #==================== + # Packing + #==================== + + toolbar = self._create_toolbar() + + vbox = gtk.VBox(False, 0) + vbox.pack_start(drawing, True, True, 0) + vbox.pack_start(toolbar, False, False, 0) + + self.toplevel = vbox def expose_event(self, widget, event): x, y, w, h, _ = widget.window.get_geometry() - surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) + self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) - chart = pycha.bar.HorizontalBarChart(surface, self.options) + chart = pycha.bar.HorizontalBarChart(self.surface, self.options) chart.addDataset(self.data) chart.render() cr = widget.window.cairo_create() # cairo context - cr.set_source_surface(surface, 0, 0) + cr.set_source_surface(self.surface, 0, 0) cr.paint() # outline rectangle cr.set_source_rgb(0, 0, 0) cr.rectangle(0, 0, w, h) cr.stroke() + + def save_results(self, btn): + """Show 'Save to file' dialog.""" + dialog = gtk.FileChooserDialog(title='Save to...', + action='save', + buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK, + gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)) + dialog.show_all() + response = dialog.run() + if response == gtk.RESPONSE_OK: + filepath = dialog.get_filename() + if not filepath.endswith('.png'): + filepath += '.png' + # Saving results in png file + self.surface.write_to_png(filepath) + dialog.destroy() diff --git a/src/files/out_diapie.py b/src/files/out_diapie.py index 75dac27..1289d76 100755 --- a/src/files/out_diapie.py +++ b/src/files/out_diapie.py @@ -14,10 +14,25 @@ class Cli_Presentation(object): #============================================================================== class Gtk_Presentation(object): + + def _create_toolbar(self): + toolbar = gtk.Toolbar() + toolbar.set_property('icon-size', 'small-toolbar') + + saveresults_tbtn = gtk.ToolButton('gtk-save') + saveresults_tbtn.connect('clicked', self.save_results) + + toolbar.insert(saveresults_tbtn, -1) + + return toolbar + def __init__(self, results, maxdata=10): import gtk - import cairo; global cairo - import pycha.pie; global pycha + global gtk # for _create_toolbar() + import cairo + global cairo # for expose_event() + import pycha.pie + global pycha # for expose_event() filelist, start_path = results @@ -25,6 +40,10 @@ class Gtk_Presentation(object): filelist.sort(reverse=True) filelist = filelist[:maxdata] + #==================== + # Diagram + #==================== + # only byte sizes self.data = [(path, [[0, bsize]]) for bsize, path, size in filelist] @@ -55,21 +74,47 @@ class Gtk_Presentation(object): drawing = gtk.DrawingArea() drawing.connect('expose-event', self.expose_event) - self.toplevel = drawing + #==================== + # Packing + #==================== + + toolbar = self._create_toolbar() + + vbox = gtk.VBox(False, 0) + vbox.pack_start(drawing, True, True, 0) + vbox.pack_start(toolbar, False, False, 0) + + self.toplevel = vbox def expose_event(self, widget, event): x, y, w, h, _ = widget.window.get_geometry() - surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) + self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) - chart = pycha.pie.PieChart(surface, self.options) + chart = pycha.pie.PieChart(self.surface, self.options) chart.addDataset(self.data) chart.render() cr = widget.window.cairo_create() # cairo context - cr.set_source_surface(surface, 0, 0) + cr.set_source_surface(self.surface, 0, 0) cr.paint() # outline rectangle cr.set_source_rgb(0, 0, 0) cr.rectangle(0, 0, w, h) cr.stroke() + + def save_results(self, btn): + """Show 'Save to file' dialog.""" + dialog = gtk.FileChooserDialog(title='Save to...', + action='save', + buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK, + gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)) + dialog.show_all() + response = dialog.run() + if response == gtk.RESPONSE_OK: + filepath = dialog.get_filename() + if not filepath.endswith('.png'): + filepath += '.png' + # Saving results in png file + self.surface.write_to_png(filepath) + dialog.destroy()