4330
|
1 |
#Copyright ReportLab Europe Ltd. 2000-2017
|
2963
|
2 |
#see license.txt for license details
|
|
3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/docs/userguide/ch7_custom.py
|
2966
|
4 |
from tools.docco.rl_doc_utils import *
|
2963
|
5 |
|
|
6 |
heading1("Writing your own $Flowable$ Objects")
|
|
7 |
disc("""
|
|
8 |
Flowables are intended to be an open standard for creating
|
|
9 |
reusable report content, and you can easily create your
|
|
10 |
own objects. We hope that over time we will build up
|
|
11 |
a library of contributions, giving reportlab users a
|
|
12 |
rich selection of charts, graphics and other "report
|
|
13 |
widgets" they can use in their own reports. This section
|
|
14 |
shows you how to create your own flowables.""")
|
|
15 |
|
|
16 |
todo("""we should put the Figure class in the
|
|
17 |
standard library, as it is a very useful base.""")
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
heading2("A very simple $Flowable$")
|
|
23 |
|
|
24 |
disc("""
|
|
25 |
Recall the $hand$ function from the $pdfgen$ section of this user guide which
|
|
26 |
generated a drawing of a hand as a closed figure composed from Bezier curves.
|
|
27 |
""")
|
|
28 |
illust(examples.hand, "a hand")
|
|
29 |
disc("""
|
|
30 |
To embed this or any other drawing in a Platypus flowable we must define a
|
|
31 |
subclass of $Flowable$
|
|
32 |
with at least a $wrap$ method and a $draw$ method.
|
|
33 |
""")
|
|
34 |
eg(examples.testhandannotation)
|
|
35 |
disc("""
|
|
36 |
The $wrap$ method must provide the size of the drawing -- it is used by
|
|
37 |
the Platypus mainloop to decide whether this element fits in the space remaining
|
|
38 |
on the current frame. The $draw$ method performs the drawing of the object after
|
|
39 |
the Platypus mainloop has translated the $(0,0)$ origin to an appropriate location
|
|
40 |
in an appropriate frame.
|
|
41 |
""")
|
|
42 |
disc("""
|
|
43 |
Below are some example uses of the $HandAnnotation$ flowable.
|
|
44 |
""")
|
|
45 |
|
|
46 |
from reportlab.lib.colors import blue, pink, yellow, cyan, brown
|
|
47 |
from reportlab.lib.units import inch
|
|
48 |
|
|
49 |
handnote()
|
|
50 |
|
|
51 |
disc("""The default.""")
|
|
52 |
|
|
53 |
handnote(size=inch)
|
|
54 |
|
|
55 |
disc("""Just one inch high.""")
|
|
56 |
|
|
57 |
handnote(xoffset=3*inch, size=inch, strokecolor=blue, fillcolor=cyan)
|
|
58 |
|
|
59 |
disc("""One inch high and shifted to the left with blue and cyan.""")
|
|
60 |
|
|
61 |
|
|
62 |
heading2("Modifying a Built in $Flowable$")
|
|
63 |
disc("""To modify an existing flowable, you should create a derived class
|
|
64 |
and override the methods you need to change to get the desired behaviour""")
|
|
65 |
disc("""As an example to create a rotated image you need to override the wrap
|
|
66 |
and draw methods of the existing Image class""")
|
|
67 |
import os
|
|
68 |
from reportlab.platypus import *
|
|
69 |
I = '../images/replogo.gif'
|
|
70 |
|
|
71 |
EmbeddedCode("""
|
|
72 |
class RotatedImage(Image):
|
|
73 |
def wrap(self,availWidth,availHeight):
|
|
74 |
h, w = Image.wrap(self,availHeight,availWidth)
|
|
75 |
return w, h
|
|
76 |
def draw(self):
|
|
77 |
self.canv.rotate(90)
|
|
78 |
Image.draw(self)
|
|
79 |
I = RotatedImage('%s')
|
|
80 |
I.hAlign = 'CENTER'
|
2966
|
81 |
""" % I,'I')
|