~vorlon/ubuntu/saucy/gourmet/trunk

« back to all changes in this revision

Viewing changes to src/lib/exporters/page_drawer.py

  • Committer: Bazaar Package Importer
  • Author(s): Rolf Leggewie
  • Date: 2008-07-26 13:29:41 UTC
  • Revision ID: james.westby@ubuntu.com-20080726132941-6ldd73qmacrzz0bn
Tags: upstream-0.14.0
ImportĀ upstreamĀ versionĀ 0.14.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import gtk
 
2
 
 
3
class PageDrawer (gtk.DrawingArea):
 
4
 
 
5
    def __init__(self, page_width=None, page_height=None,
 
6
                 sub_areas=[],xalign=0.5,yalign=0.5
 
7
                 ):
 
8
        """Draw a page based on page areas given to us.
 
9
 
 
10
        The areas can be given in any scale they like.
 
11
 
 
12
        sub_areas are each (X1,Y1,WIDTH,HEIGHT) where the point defines
 
13
        the upper-left corner of the rectangle.
 
14
        
 
15
        """
 
16
        self.xalign = xalign
 
17
        self.yalign = yalign
 
18
        gtk.DrawingArea.__init__(self)
 
19
        self.gc = None  # initialized in realize-event handler
 
20
        self.width  = 0 # updated in size-allocate handler
 
21
        self.height = 0 # idem
 
22
        if page_width and page_height:
 
23
            self.set_page_area(page_width,page_height,sub_areas)
 
24
        self.connect('size-allocate', self.on_size_allocate)
 
25
        self.connect('expose-event',  self.on_expose_event)
 
26
        self.connect('realize',       self.on_realize)
 
27
 
 
28
    def set_page_area (self, page_width, page_height, sub_areas=[]):
 
29
        self.xy_ratio = page_width/page_height
 
30
        self.areas = []
 
31
        for x1,y1,w,h in sub_areas:
 
32
            width = float(w)/page_width
 
33
            height = float(h)/page_height
 
34
            x = float(x1)/page_width
 
35
            y = float(y1)/page_height
 
36
            self.areas.append(
 
37
                (x,y,width,height)
 
38
                )
 
39
 
 
40
    def on_realize(self, widget):
 
41
        self.gc = widget.window.new_gc()
 
42
        #self.gc.set_line_attributes(3, gtk.gdk.LINE_ON_OFF_DASH,
 
43
        #                            gtk.gdk.CAP_ROUND, gtk.gdk.JOIN_ROUND)
 
44
 
 
45
    def on_size_allocate(self, widget, allocation):
 
46
        self.width = allocation.width
 
47
        self.height = allocation.height
 
48
        
 
49
    def on_expose_event(self, widget, event):
 
50
        if not hasattr(self,'xy_ratio'): return
 
51
        # This is where the drawing takes place
 
52
        if self.xy_ratio * self.height > self.width:
 
53
            width = int(self.width * 0.9)
 
54
            height = int((self.width / self.xy_ratio) * 0.9)
 
55
        else:
 
56
            width = int(self.xy_ratio*self.height*0.9)
 
57
            height = int(self.height*0.9)
 
58
        xpadding = int((self.width - width)*self.xalign)
 
59
        ypadding = int((self.height - height)*self.yalign)
 
60
        self.gc.set_line_attributes(3,
 
61
                               gtk.gdk.LINE_SOLID,
 
62
                               gtk.gdk.CAP_BUTT,
 
63
                               gtk.gdk.JOIN_MITER)
 
64
        widget.window.draw_rectangle(self.gc, False,
 
65
                                     xpadding, ypadding, width, height)
 
66
        self.gc.set_line_attributes(1,
 
67
                                    gtk.gdk.LINE_ON_OFF_DASH,
 
68
                                    gtk.gdk.CAP_BUTT,gtk.gdk.JOIN_MITER)
 
69
        for sub_area in self.areas:
 
70
            x,y,w,h = sub_area
 
71
            self.window.draw_rectangle(
 
72
                self.gc, False,
 
73
                int(xpadding+(x*width)),int(ypadding+(y*height)),int(w*width),int(h*height)
 
74
                )
 
75
        #widget.window.draw_line(self.gc,
 
76
        #                        0, 0, self.width - 1, self.height - 1)
 
77
        #widget.window.draw_line(self.gc,
 
78
        #                        self.width - 1, 0, 0, self.height - 1)
 
79
 
 
80
 
 
81
if __name__ == '__main__':
 
82
    w = gtk.Window()
 
83
    w.add(PageDrawer(8.5,11,[(1,1,3,9.5),
 
84
                             (4.5,1,3,9.5),
 
85
                             ]))
 
86
    w.show_all()
 
87
    w.connect('delete-event',lambda *args: gtk.main_quit())
 
88
    gtk.main()