Add save as png for graph

This commit is contained in:
Cédric Krier committed 2008-04-24 12:56:43 +02:00
1 parent b69ce0faa4
commit cde6605777
2 files changed
+103 -1

No files matched your search

@@ -6,6 +6,7 @@ import datetime
import mx.DateTime
import time
import tryton.rpc as rpc
import cairo
class Popup(object):
@@ -103,6 +104,20 @@ class Graph(gtk.DrawingArea):
self.drawAxis(cr, *self.window.get_size())
self.drawLegend(cr, *self.window.get_size())
def export_png(self, filename, width, height):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cx = cairo.Context(surface)
self.updateArea(cx, width, height)
self.drawBackground(cx, width, height)
self.drawLines(cx, width, height)
self.drawGraph(cx, width, height)
self.drawAxis(cx, width, height)
self.drawLegend(cx, width, height)
surface.write_to_png(filename)
self.queue_draw()
def drawBackground(self, cr, width, height):
# Fill the background with gray
cr.save()
@@ -5,6 +5,8 @@ from graph import Graph
from bar import VerticalBar, HorizontalBar
from line import Line
from pie import Pie
from tryton.config import TRYTON_ICON, CONFIG
import sys, os
GRAPH_TYPE = {
'vbar': VerticalBar,
@@ -13,6 +15,88 @@ GRAPH_TYPE = {
'pie': Pie,
}
def save(widget, graph, window):
dia = gtk.Dialog(_('Tryton - Save As'), window,
gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OK, gtk.RESPONSE_OK))
dia.set_icon(TRYTON_ICON)
dia.set_default_response(gtk.RESPONSE_OK)
dia.vbox.set_spacing(5)
dia.vbox.set_homogeneous(False)
title = gtk.Label('<b>' + _('Image Size') + '</b>')
title.set_alignment(0.0, 0.5)
title.set_use_markup(True)
dia.vbox.pack_start(title)
table = gtk.Table(2, 2)
table.set_col_spacings(3)
table.set_row_spacings(3)
table.set_border_width(1)
table.attach(gtk.Label(_('Width:')), 0, 1, 0, 1, yoptions=False,
xoptions=gtk.FILL)
spinwidth = gtk.SpinButton(gtk.Adjustment(400.0,
0.0, sys.maxint, 1.0, 10.0, 10.0))
spinwidth.set_numeric(True)
spinwidth.set_activates_default(True)
table.attach(spinwidth, 1, 2, 0, 1, yoptions=False, xoptions=gtk.FILL)
table.attach(gtk.Label(_('Height:')), 0, 1, 1, 2, yoptions=False,
xoptions=gtk.FILL)
spinheight = gtk.SpinButton(gtk.Adjustment(200.0,
0.0, sys.maxint, 1.0, 10.0, 10.0))
spinheight.set_numeric(True)
spinheight.set_activates_default(True)
table.attach(spinheight, 1, 2, 1, 2, yoptions=False, xoptions=gtk.FILL)
dia.vbox.pack_start(table)
filechooser = gtk.FileChooserWidget(gtk.FILE_CHOOSER_ACTION_SAVE, None)
filechooser.set_current_folder(CONFIG['client.default_path'])
filter = gtk.FileFilter()
filter.set_name(_('PNG image (*.png)'))
filter.add_mime_type('image/png')
filter.add_pattern('*.png')
filechooser.add_filter(filter)
dia.vbox.pack_start(filechooser)
dia.show_all()
while True:
response = dia.run()
width = spinwidth.get_value_as_int()
height = spinheight.get_value_as_int()
filename = filechooser.get_filename()
if filename:
filename = filename.decode('utf-8')
try:
CONFIG['client.default_path'] = \
os.path.dirname(filepath)
CONFIG.save()
except:
pass
if response == gtk.RESPONSE_OK:
if width and height and filename:
if not filename.endswith('.png'):
filename = filename + '.png'
graph.export_png(filename, width, height)
break
else:
break
window.present()
dia.destroy()
return
def button_press(widget, event, graph, window):
if event.button == 3:
menu = gtk.Menu()
item = gtk.ImageMenuItem('gtk-save-as')
item.connect('activate', save, graph, window)
item.show()
menu.append(item)
menu.popup(None, None, None, event.button, event.time)
return True
class ParserGraph(ParserInterface):
@@ -44,5 +128,8 @@ class ParserGraph(ParserInterface):
yfields.append(yattrs)
widget = GRAPH_TYPE[attrs.get('type', 'vbar')](xfield, yfields, attrs)
event = gtk.EventBox()
event.add(widget)
event.connect('button-press-event', button_press, widget, self.window)
return widget, {'root': widget}, [], ''
return event, {'root': widget}, [], ''