← Back to team overview

openerp-dev-web team mailing list archive

lp:~openerp-dev/openobject-client/trunk-Navigationtoolbar_graph_view-rga into lp:openobject-client

 

Ravi Gadhia (OpenERP) has proposed merging lp:~openerp-dev/openobject-client/trunk-Navigationtoolbar_graph_view-rga into lp:openobject-client.

Requested reviews:
  Naresh(OpenERP) (nch-openerp)
Related bugs:
  Bug #623188 in OpenERP GTK Client: "[trunk] graph labels are cut  off at the beginning"
  https://bugs.launchpad.net/openobject-client/+bug/623188

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-client/trunk-Navigationtoolbar_graph_view-rga/+merge/61237
-- 
https://code.launchpad.net/~openerp-dev/openobject-client/trunk-Navigationtoolbar_graph_view-rga/+merge/61237
Your team OpenERP R&D Team is subscribed to branch lp:~openerp-dev/openobject-client/trunk-Navigationtoolbar_graph_view-rga.
=== modified file 'bin/modules/gui/main.py'
--- bin/modules/gui/main.py	2011-05-10 11:40:45 +0000
+++ bin/modules/gui/main.py	2011-05-17 11:57:26 +0000
@@ -711,6 +711,7 @@
         window.connect("destroy", self.sig_quit)
         window.connect("delete_event", self.sig_delete)
         self.window = window
+        self.add_service('main_window','main',self.window)
         self.window.set_icon(common.OPENERP_ICON)
 
         self.notebook = gtk.Notebook()

=== modified file 'bin/service.py'
--- bin/service.py	2010-01-12 09:24:17 +0000
+++ bin/service.py	2011-05-17 11:57:26 +0000
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 ##############################################################################
-#    
+#
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
@@ -15,7 +15,7 @@
 #    GNU Affero General Public License for more details.
 #
 #    You should have received a copy of the GNU Affero General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
@@ -27,6 +27,9 @@
     def exportMethod(self, method):
         pass
 
+    def add_service(self, name, audience="*", service_obj=None):
+        service_dict[name] = (name, audience, service_obj)
+
 def LocalService(name):
     return service_dict[name][2]
 

=== modified file 'bin/widget/view/graph_gtk/graph.py'
--- bin/widget/view/graph_gtk/graph.py	2011-01-06 10:27:51 +0000
+++ bin/widget/view/graph_gtk/graph.py	2011-05-17 11:57:26 +0000
@@ -47,16 +47,21 @@
 matplotlib.rcParams['ytick.labelsize'] = 10
 
 from matplotlib.figure import Figure
-from matplotlib.backends.backend_gtkcairo import FigureCanvasGTKCairo as FigureCanvas
+from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
+from navigationtool import NavigationToolbar2GTK as NavigationToolbar
+import service
 
 class ViewGraph(object):
     def __init__(self, model, axis, fields, axis_data={}, attrs={}):
         self.widget = gtk.HBox()
         self._figure = Figure(figsize=(800,600), dpi=100, facecolor='w')
-        self._subplot = self._figure.add_subplot(111,axisbg='#eeeeee')
+        self._subplot = self._figure.add_subplot(111, axisbg='#eeeeee')
         self._canvas = FigureCanvas(self._figure)
+        main_window = service.LocalService('main_window')
+        toolbar = NavigationToolbar(self._canvas, main_window)
+        toolbar.set_orientation(gtk.ORIENTATION_VERTICAL)
+        self.widget.pack_start(toolbar, False, False)
         self.widget.pack_start(self._canvas, expand=True, fill=True)
-
         if attrs.get('type', 'pie')=='bar':
             if attrs.get('orientation', 'vertical')=='vertical':
                 self._figure.subplots_adjust(left=0.08,right=0.98,bottom=0.25,top=0.98)
@@ -134,11 +139,13 @@
             datas.append(res)
         tinygraph.tinygraph(self._subplot, self.attrs.get('type', 'pie'), self.axis, self.axis_data, datas, axis_group_field=self.axis_group, orientation=self.attrs.get('orientation', 'vertical'))
         # the draw function may generate exception but it is not a problem as it will be redraw latter
+
         try:
             self._subplot.draw(None)
             #XXX it must have some better way to force the redraw but this one works
             self._canvas.queue_resize()
-        except:
+
+        except Exception, e:
             pass
 
     def destroy(self):

=== added file 'bin/widget/view/graph_gtk/navigationtool.py'
--- bin/widget/view/graph_gtk/navigationtool.py	1970-01-01 00:00:00 +0000
+++ bin/widget/view/graph_gtk/navigationtool.py	2011-05-17 11:57:26 +0000
@@ -0,0 +1,182 @@
+import gobject
+import gtk; gdk = gtk.gdk
+import pango
+from matplotlib.backend_bases import NavigationToolbar2, cursors
+from matplotlib.backends.backend_gtk import FigureCanvasGTK, FileChooserDialog
+from matplotlib.widgets import SubplotTool
+from matplotlib.figure import Figure
+import os, sys
+import matplotlib
+from matplotlib import verbose
+try:
+
+    if gtk.pygtk_version < (2, 8, 0) or sys.platform == 'win32':
+        icon_filename = 'matplotlib.png'
+    else:
+        icon_filename = 'matplotlib.svg'
+    window_icon = os.path.join(matplotlib.rcParams['datapath'], 'images', icon_filename)
+except:
+    window_icon = None
+    verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1])
+
+
+cursord = {
+    cursors.MOVE          : gdk.Cursor(gdk.FLEUR),
+    cursors.HAND          : gdk.Cursor(gdk.HAND2),
+    cursors.POINTER       : gdk.Cursor(gdk.LEFT_PTR),
+    cursors.SELECT_REGION : gdk.Cursor(gdk.TCROSS),
+    }
+
+class NavigationToolbar2GTK(NavigationToolbar2, gtk.Toolbar):
+    # list of toolitems to add to the toolbar, format is:
+    # text, tooltip_text, image_file, callback(str)
+    toolitems = (
+        ('Home', 'Reset original view', 'home.png', 'home'),
+        ('Back', 'Back to  previous view','back.png', 'back'),
+        ('Forward', 'Forward to next view','forward.png', 'forward'),
+        ('Pan', 'Pan axes with left mouse, zoom with right', 'move.png','pan'),
+        ('Zoom', 'Zoom to rectangle','zoom_to_rect.png', 'zoom'),
+        (None, None, None, None),
+        ('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'),
+        ('Save', 'Save the graph','filesave.png', 'save_figure'),
+        )
+
+    def __init__(self, canvas, window):
+        self.win = window
+        gtk.Toolbar.__init__(self)
+        NavigationToolbar2.__init__(self, canvas)
+        self._idle_draw_id = 0
+
+    def set_message(self, s):
+        pass
+#        if self._idle_draw_id == 0:
+#            print 'eeeeeeeee',s
+#            self.message.set_label(s)
+
+    def set_cursor(self, cursor):
+        self.canvas.window.set_cursor(cursord[cursor])
+
+    def release(self, event):
+        try: del self._imageBack
+        except AttributeError: pass
+
+    def dynamic_update(self):
+        # legacy method; new method is canvas.draw_idle
+        self.canvas.draw_idle()
+
+    def draw_rubberband(self, event, x0, y0, x1, y1):
+        'adapted from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/189744'
+        drawable = self.canvas.window
+        if drawable is None:
+            return
+
+        gc = drawable.new_gc()
+        
+        height = self.canvas.figure.bbox.height
+        y1 = height - y1
+        y0 = height - y0
+
+        w = abs(x1 - x0)
+        h = abs(y1 - y0)
+
+        rect = [int(val)for val in min(x0,x1), min(y0, y1), w, h]
+        try: lastrect, imageBack = self._imageBack
+        except AttributeError:
+            #snap image back
+            if event.inaxes is None:
+                return
+
+            ax = event.inaxes
+            l,b,w,h = [int(val) for val in ax.bbox.bounds]
+            b = int(height)-(b+h)
+            axrect = l,b,w,h
+            self._imageBack = axrect, drawable.get_image(*axrect)
+            drawable.draw_rectangle(gc, False, *rect)
+            self._idle_draw_id = 0
+        else:
+            def idle_draw(*args):
+                drawable.draw_image(gc, imageBack, 0, 0, *lastrect)
+                drawable.draw_rectangle(gc, False, *rect)
+                self._idle_draw_id = 0
+                return False
+            if self._idle_draw_id == 0:
+                self._idle_draw_id = gobject.idle_add(idle_draw)
+
+
+    def _init_toolbar(self):
+        self.set_style(gtk.TOOLBAR_ICONS)
+        self._init_toolbar2_4()
+
+
+    def _init_toolbar2_4(self):
+        basedir = os.path.join(matplotlib.rcParams['datapath'],'images')
+        for text, tooltip_text, image_file, callback in self.toolitems:
+            if text is None:
+                self.insert( gtk.SeparatorToolItem(), -1 )
+                continue
+            fname = os.path.join(basedir, image_file)
+            image = gtk.Image()
+            image.set_from_file(fname)
+            tbutton = gtk.ToolButton(image, text)
+            self.insert(tbutton, -1)
+            tbutton.connect('clicked', getattr(self, callback))
+            tbutton.set_tooltip_text(tooltip_text)
+
+        toolitem = gtk.SeparatorToolItem()
+        self.insert(toolitem, -1)
+        # set_draw() not making separator invisible,
+        # bug #143692 fixed Jun 06 2004, will be in GTK+ 2.6
+        toolitem.set_draw(False)
+        toolitem.set_expand(True)
+
+        toolitem = gtk.ToolItem()
+        self.insert(toolitem, -1)
+        self.message = gtk.Label()
+        toolitem.add(self.message)
+
+        self.show_all()
+
+    def get_filechooser(self):
+        return FileChooserDialog(
+            title='Save the graph',
+            parent=self.win,
+            filetypes=self.canvas.get_supported_filetypes(),
+            default_filetype=self.canvas.get_default_filetype())
+
+    def save_figure(self, button):
+        fname, format = self.get_filechooser().get_filename_from_user()
+        if fname:
+            try:
+                self.canvas.print_figure(fname, format=format)
+            except Exception, e:
+                error_msg_gtk(str(e), parent=self)
+
+    def configure_subplots(self, button):
+        toolfig = Figure(figsize=(6,3))
+        canvas = self._get_canvas(toolfig)
+        toolfig.subplots_adjust(top=0.9)
+        tool =  SubplotTool(self.canvas.figure, toolfig)
+
+        w = int (toolfig.bbox.width)
+        h = int (toolfig.bbox.height)
+
+
+        window = gtk.Window()
+        if (window_icon):
+            try: window.set_icon_from_file(window_icon)
+            except:
+                # we presumably already logged a message on the
+                # failure of the main plot, don't keep reporting
+                pass
+        window.set_title("Subplot Configuration Tool")
+        window.set_default_size(w, h)
+        vbox = gtk.VBox()
+        window.add(vbox)
+        vbox.show()
+
+        canvas.show()
+        vbox.pack_start(canvas, True, True)
+        window.show()
+
+    def _get_canvas(self, fig):
+        return FigureCanvasGTK(fig)
\ No newline at end of file