reportlab/platypus/doctemplate.py
author andy_robinson
Tue, 20 Jun 2000 21:56:17 +0000
changeset 294 25d6a8334e99
parent 284 eabeb5f4e851
child 295 1f00bd112cb0
permissions -rw-r--r--
re-synching after sourceforge went weird
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     1
###############################################################################
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     2
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     3
#	ReportLab Public License Version 1.0
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     4
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     5
#   Except for the change of names the spirit and intention of this
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     6
#   license is the same as that of Python
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     7
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     8
#	(C) Copyright ReportLab Inc. 1998-2000.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     9
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    10
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    11
# All Rights Reserved
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    12
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    13
# Permission to use, copy, modify, and distribute this software and its
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    14
# documentation for any purpose and without fee is hereby granted, provided
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    15
# that the above copyright notice appear in all copies and that both that
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    16
# copyright notice and this permission notice appear in supporting
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    17
# documentation, and that the name of ReportLab not be used
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    18
# in advertising or publicity pertaining to distribution of the software
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    19
# without specific, written prior permission. 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    20
# 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    21
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    22
# Disclaimer
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    23
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    24
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    25
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    26
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    27
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    28
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    29
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    30
# PERFORMANCE OF THIS SOFTWARE. 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    31
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    32
###############################################################################
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    33
#	$Log: doctemplate.py,v $
294
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
    34
#	Revision 1.19  2000/06/20 21:56:17  andy_robinson
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
    35
#	re-synching after sourceforge went weird
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
    36
#
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
    37
#	Revision 1.18  2000/06/19 23:51:23  andy_robinson
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
    38
#	Added UserDocTemplate class, and paragraph.getPlainText()
294
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
    39
#	
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    40
#	Revision 1.17  2000/06/19 11:14:03  andy_robinson
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    41
#	Global sequencer put in the 'story builder'.
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
    42
#	
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    43
#	Revision 1.16  2000/06/16 13:49:20  aaron_watters
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    44
#	new build parameters to allow alternate filename and canvas implementation
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    45
#	(in order to support slideshow summary mode, for example, or embedding one
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    46
#	document in another).
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    47
#	
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    48
#	Revision 1.15  2000/06/13 13:03:31  aaron_watters
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    49
#	more documentation changes
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    50
#	
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    51
#	Revision 1.14  2000/06/01 16:27:56  rgbecker
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    52
#	pageSize is wrong at present
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    53
#	
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    54
#	Revision 1.13  2000/06/01 15:23:06  rgbecker
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    55
#	Platypus re-organisation
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    56
#	
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
    57
#	Revision 1.12  2000/05/26 10:27:37  rgbecker
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
    58
#	Fixed infinite recursion bug
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    59
#	
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    60
#	Revision 1.11  2000/05/17 22:17:38  rgbecker
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    61
#	Renamed BasicFrame to Frame
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
    62
#	
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
    63
#	Revision 1.10  2000/05/17 16:29:40  rgbecker
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
    64
#	Removal of SimpleFrame
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    65
#	
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    66
#	Revision 1.9  2000/05/17 15:37:33  rgbecker
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    67
#	Changes related to removal of SimpleFlowDocument
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
    68
#	
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    69
#	Revision 1.8  2000/05/16 16:15:16  rgbecker
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    70
#	Changes related to removal of SimpleFlowDocument
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    71
#	
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    72
#	Revision 1.7  2000/05/16 14:28:55  rgbecker
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    73
#	Fixes/Changes to get testplatypus to work with new framework
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    74
#	
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    75
#	Revision 1.6  2000/05/15 15:07:32  rgbecker
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    76
#	Added drawPage
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    77
#	
206
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
    78
#	Revision 1.5  2000/05/13 08:33:53  rgbecker
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
    79
#	fix typo in import
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    80
#	
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
    81
#	Revision 1.4  2000/05/12 16:21:02  rgbecker
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
    82
#	_donothing explicit import
206
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
    83
#	
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
    84
#	Revision 1.3  2000/05/12 14:53:38  rgbecker
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
    85
#	Handle splitting error in build
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
    86
#	
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    87
#	Revision 1.2  2000/05/12 14:45:31  rgbecker
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    88
#	Single actions only in ActionFlowables
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
    89
#	
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    90
#	Revision 1.1  2000/05/12 12:53:33  rgbecker
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    91
#	Initial try at a document template class
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    92
#	
294
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
    93
__version__=''' $Id: doctemplate.py,v 1.19 2000/06/20 21:56:17 andy_robinson Exp $ '''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    94
__doc__="""
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    95
This module contains the core structure of platypus.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    96
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    97
Platypus constructs documents.  Document styles are determined by DocumentTemplates.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    98
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    99
Each DocumentTemplate contains one or more PageTemplates which defines the look of the
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   100
pages of the document.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   101
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   102
Each PageTemplate has a procedure for drawing the "non-flowing" part of the page
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   103
(for example the header, footer, page number, fixed logo graphic, watermark, etcetera) and
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   104
a set of Frames which enclose the flowing part of the page (for example the paragraphs,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   105
tables, or non-fixed diagrams of the text).
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   106
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   107
A document is built when a DocumentTemplate is fed a sequence of Flowables.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   108
The action of the build consumes the flowables in order and places them onto
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   109
frames on pages as space allows.  When a frame runs out of space the next frame
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   110
of the page is used.  If no frame remains a new page is created.  A new page
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   111
can also be created if a page break is forced.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   112
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   113
The special invisible flowable NextPageTemplate can be used to specify
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   114
the page template for the next page (which by default is the one being used
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   115
for the current frame).
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   116
"""
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   117
from reportlab.platypus.flowables import *
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   118
from reportlab.platypus.paragraph import Paragraph
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   119
from reportlab.platypus.frames import Frame
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   120
import reportlab.lib.sequencer
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   121
from types import *
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   122
import sys
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   123
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   124
def _doNothing(canvas, doc):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   125
	"Dummy callback for onPage"
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   126
	pass
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   127
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   128
class ActionFlowable(Flowable):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   129
	'''This Flowable is never drawn, it can be used for data driven controls
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   130
	   For example to change a page template (from one column to two, for example)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   131
	   use NextPageTemplate which creates an ActionFlowable.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   132
	'''
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   133
	def __init__(self,action=[]):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   134
		if type(action) not in (ListType, TupleType):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   135
			action = (action,)
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   136
		self.action = action
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   137
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   138
	def wrap(self, availWidth, availHeight):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   139
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   140
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   141
	def draw(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   142
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   143
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   144
	def apply(self,doc):
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   145
		action = self.action[0]
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   146
		args = tuple(self.action[1:])
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   147
		arn = 'handle_'+action
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   148
		try:
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   149
			apply(getattr(doc,arn), args)
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   150
		except AttributeError, aerr:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   151
			if aerr.args[0]==arn:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   152
				raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   153
			else:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   154
				raise
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   155
		except "bogus":
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   156
			t, v, None = sys.exc_info()
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   157
			raise t, "%s\n   handle_%s args=%s"%(v,action,args)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   158
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   159
	def __call__(self):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   160
		return self
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   161
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   162
FrameBreak = ActionFlowable('frameEnd')
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   163
PageBegin = ActionFlowable('pageBegin')
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   164
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   165
class NextPageTemplate(ActionFlowable):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   166
	"""When you get to the next page, use the template specified (change to two column, for example)  """
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   167
	def __init__(self,pt):
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   168
		ActionFlowable.__init__(self,('nextPageTemplate',pt))
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   169
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   170
class PageTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   171
	"""
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   172
	essentially a list of Frames and an onPage routine to call at the start
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   173
	of a page when this is selected.
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   174
	derived classes can also implement drawPage if they want
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   175
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   176
	def __init__(self,id=None,frames=[],onPage=None):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   177
		if type(frames) not in (ListType,TupleType): frames = [frames]
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   178
		assert filter(lambda x: not isinstance(x,Frame), frames)==[], "frames argument error"
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   179
		self.id = id
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   180
		self.frames = frames
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   181
		self.onPage = onPage or _doNothing
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   182
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   183
	def beforeDrawPage(self,canv,doc):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   184
		"""Override this if you want additional functionality or prefer
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   185
		a class based page routine.  Called before any flowables for
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   186
		this page are processed."""
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   187
		pass
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   188
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   189
	def afterDrawPage(self, canv, doc):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   190
		"""This is called after the last flowable for the page has
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   191
		been processed.  You might use this if the page header or
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   192
		footer needed knowledge of what flowables were drawn on
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   193
		this page."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   194
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   195
		
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   196
class BaseDocTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   197
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   198
	First attempt at defining a document template class.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   199
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   200
	The basic idea is simple.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   201
	0)	The document has a list of data associated with it
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   202
		this data should derive from flowables. We'll have
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   203
		special classes like PageBreak, FrameBreak to do things
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   204
		like forcing a page end etc.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   205
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   206
	1)	The document has one or more page templates.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   207
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   208
	2)	Each page template has one or more frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   209
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   210
	3)	The document class provides base methods for handling the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   211
		story events and some reasonable methods for getting the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   212
		story flowables into the frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   213
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   214
	4)	The document instances can override the base handler routines.
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   215
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   216
	Most of the methods for this class are not called directly by the user,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   217
	but in some advanced usages they may need to be overridden via subclassing.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   218
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   219
	EXCEPTION: doctemplate.build(...) must be called for most reasonable uses
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   220
	since it builds a document using the page template.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   221
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   222
        Each document template builds exactly one document into a file specified
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   223
        by the filename argument on initialization.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   224
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   225
	Possible keyword arguments for the initialization:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   226
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   227
	pageTemplates: A list of templates.  Must be nonempty.  Names
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   228
	  assigned to the templates are used for referring to them so no two used
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   229
	  templates should have the same name.  For example you might want one template
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   230
	  for a title page, one for a section first page, one for a first page of
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   231
	  a chapter and two more for the interior of a chapter on odd and even pages.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   232
	  If this argument is omitted then at least one pageTemplate should be provided
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   233
	  using the addPageTemplates method before the document is built.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   234
	showBoundary: (for debugging) if set draw a box around the frame boundaries.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   235
	leftMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   236
	rightMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   237
	topMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   238
	bottomMargin:  Margin sizes in points (default 1 inch)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   239
	  These margins may be overridden by the pageTemplates.  They are primarily of interest
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   240
	  for the SimpleDocumentTemplate subclass.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   241
	allowSplitting:  If set flowables (eg, paragraphs) may be split across frames or pages
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   242
	  (default: 1)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   243
	title: Internal title for document (does not automatically display on any page)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   244
	author: Internal author for document (does not automatically display on any page)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   245
	"""
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   246
	_initArgs = {	'pagesize':DEFAULT_PAGE_SIZE,
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   247
					'pageTemplates':[],
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   248
					'showBoundary':0,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   249
					'leftMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   250
					'rightMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   251
					'topMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   252
					'bottomMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   253
					'allowSplitting':1,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   254
					'title':None,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   255
					'author':None,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   256
					'_pageBreakQuick':1}
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   257
	_invalidInitArgs = ()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   258
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   259
	def __init__(self, filename, **kw):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   260
		"""create a document template bound to a filename (see class documentation for keyword arguments)"""
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   261
		self.filename = filename
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   262
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   263
		for k in self._initArgs.keys():
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   264
			if not kw.has_key(k):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   265
				v = self._initArgs[k]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   266
			else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   267
				if k in self._invalidInitArgs:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   268
					raise ValueError, "Invalid argument %s" % k
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   269
				v = kw[k]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   270
			setattr(self,k,v)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   271
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   272
		p = self.pageTemplates
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   273
		self.pageTemplates = []
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   274
		self.addPageTemplates(p)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   275
		self._calc()
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   276
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   277
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   278
	def _calc(self):
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   279
		self._rightMargin = self.pagesize[0] - self.rightMargin
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   280
		self._topMargin = self.pagesize[1] - self.topMargin
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   281
		self.width = self._rightMargin - self.leftMargin
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   282
		self.height = self._topMargin - self.bottomMargin
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   283
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   284
	def clean_hanging(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   285
		'handle internal postponed actions'
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   286
		while len(self._hanging):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   287
			self.handle_flowable(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   288
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   289
	def addPageTemplates(self,pageTemplates):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   290
		'add one or a sequence of pageTemplates'
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   291
		if type(pageTemplates) not in (ListType,TupleType):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   292
			pageTemplates = [pageTemplates]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   293
		assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   294
		for t in pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   295
			self.pageTemplates.append(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   296
			
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   297
	def handle_documentBegin(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   298
		'''Hook actions at beginning of document'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   299
		self._hanging = [PageBegin]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   300
		self.pageTemplate = self.pageTemplates[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   301
		self.page = 0
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   302
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   303
	def handle_pageBegin(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   304
		'''Perform actions required at beginning of page.
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   305
		shouldn't normally be called directly'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   306
		self.page = self.page + 1
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   307
		self.pageTemplate.beforeDrawPage(self.canv,self)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   308
		self.pageTemplate.onPage(self.canv,self)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   309
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   310
			del self._nextFrameIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   311
		self.frame = self.pageTemplate.frames[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   312
		self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   313
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   314
	def handle_pageEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   315
		'''	show the current page
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   316
			check the next page template
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   317
			hang a page begin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   318
		'''
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   319
		self.pageTemplate.afterDrawPage(self.canv, self)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   320
		self.canv.showPage()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   321
		if hasattr(self,'_nextPageTemplateIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   322
			self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   323
			del self._nextPageTemplateIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   324
		self._hanging.append(PageBegin)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   325
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   326
	def handle_pageBreak(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   327
		'''some might choose not to end all the frames'''
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   328
		if self._pageBreakQuick:
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   329
			self.handle_pageEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   330
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   331
			n = len(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   332
			while len(self._hanging)==n:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   333
				self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   334
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   335
	def handle_frameBegin(self,*args):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   336
		'''What to do at the beginning of a page'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   337
		self.frame._reset()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   338
		if self.showBoundary or self.frame.showBoundary:
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
   339
			self.frame.drawBoundary(self.canv)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   340
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   341
	def handle_frameEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   342
		'''	Handles the semantics of the end of a frame. This includes the selection of
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   343
			the next frame or if this is the last frame then invoke pageEnd.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   344
		'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   345
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   346
			frame = self.pageTemplate.frames[self._nextFrameIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   347
			del self._nextFrameIndex
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   348
			self.handle_frameBegin()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   349
		elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   350
			self.handle_pageEnd()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   351
			self.frame = None
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   352
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   353
			f = self.frame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   354
			self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   355
			self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   356
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   357
	def handle_nextPageTemplate(self,pt):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   358
		'''On endPage chenge to the page template with name or index pt'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   359
		if type(pt) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   360
			for t in self.pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   361
				if t.id == pt:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   362
					self._nextPageTemplateIndex = self.pageTemplates.index(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   363
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   364
			raise ValueError, "can't find template('%s')"%pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   365
		elif type(pt) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   366
			self._nextPageTemplateIndex = pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   367
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   368
			raise TypeError, "argument pt should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   369
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   370
	def handle_nextFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   371
		'''On endFrame chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   372
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   373
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   374
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   375
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   376
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   377
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   378
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   379
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   380
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   381
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   382
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   383
	def handle_currentFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   384
		'''chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   385
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   386
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   387
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   388
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   389
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   390
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   391
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   392
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   393
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   394
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   395
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   396
	def handle_flowable(self,flowables):
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   397
		'''try to handle one flowable from the front of list flowables.'''
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   398
		
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   399
		f = flowables[0]
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   400
		#allow document a chance to look at, modify or ignore
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   401
		#the object about to be processed
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   402
		f = self.handle_beforeFlowable(f)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   403
		del flowables[0]
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   404
		if f is None:
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   405
			return
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   406
		
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   407
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   408
		if isinstance(f,PageBreak):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   409
			self.handle_pageBreak()
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   410
			self.handle_afterFlowable(f)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   411
		elif isinstance(f,ActionFlowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   412
			f.apply(self)
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   413
			self.handle_afterFlowable(f)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   414
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   415
			#general case we have to do something
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   416
			if self.frame.add(f, self.canv, trySplit=self.allowSplitting):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   417
				self.handle_afterFlowable(f)
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   418
			else:
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   419
				if self.allowSplitting:
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   420
					# see if this is a splittable thing
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   421
					S = self.frame.split(f)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   422
					n = len(S)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   423
				else:
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   424
					n = 0
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   425
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   426
				if n:
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   427
					if self.frame.add(S[0], self.canv, trySplit=0):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   428
						self.handle_afterFlowable(f)
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   429
					else:
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   430
						raise "LayoutError", "splitting error"
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   431
					del S[0]
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   432
					for f in xrange(n-1):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   433
						flowables.insert(f,S[f])	# put split flowables back on the list
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   434
				else:
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   435
					if hasattr(f,'postponed'):
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   436
						raise "LayoutError", "Flowable too large"
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   437
					f.postponed = 1
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   438
					flowables.insert(0,f)			# put the flowable back
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   439
					self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   440
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   441
	def handle_beforeFlowable(self, flowable):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   442
		return flowable
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   443
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   444
	def handle_afterFlowable(self, flowable):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   445
		pass
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   446
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   447
	#these are provided so that deriving classes can refer to them
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   448
	_handle_documentBegin = handle_documentBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   449
	_handle_pageBegin = handle_pageBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   450
	_handle_pageEnd = handle_pageEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   451
	_handle_frameBegin = handle_frameBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   452
	_handle_frameEnd = handle_frameEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   453
	_handle_flowable = handle_flowable
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   454
	_handle_nextPageTemplate = handle_nextPageTemplate
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   455
	_handle_currentFrame = handle_currentFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   456
	_handle_nextFrame = handle_nextFrame
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   457
	_handle_beforeFlowable = handle_beforeFlowable
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   458
	_handle_afterFlowable = handle_afterFlowable
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   459
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   460
	def _startBuild(self, canvasmaker=None, filename=None):
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   461
		if canvasmaker is None: canvasmaker = canvas.Canvas
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   462
		if filename is None: filename = self.filename
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   463
		self._calc()
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   464
		self.canv = canvasmaker(filename) #canvas.Canvas(self.filename)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   465
		self.handle_documentBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   466
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   467
	def _endBuild(self):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   468
		if self._hanging!=[] and self._hanging[-1] is PageBegin:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   469
			del self._hanging[-1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   470
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   471
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   472
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   473
			self.handle_pageBreak()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   474
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   475
		self.canv.save()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   476
		del self.frame, self.pageTemplate
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   477
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   478
	def build(self, flowables, filename=None, canvasmaker=canvas.Canvas):
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   479
		"""Build the document from a list of flowables.
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   480
		   If the filename argument is provided then that filename is used
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   481
		   rather than the one provided upon initialization.
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   482
		   If the canvasmaker argument is provided then it will be used
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   483
		   instead of the default.  For example a slideshow might use
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   484
		   an alternate canvas which places 6 slides on a page (by
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   485
		   doing translations, scalings and redefining the page break
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   486
		   operations).
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   487
		"""
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   488
		#assert filter(lambda x: not isinstance(x,Flowable), flowables)==[], "flowables argument error"
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   489
		if filename is None:
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   490
		    filename = self.filename
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   491
		self._startBuild(canvasmaker, filename)
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   492
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   493
		while len(flowables):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   494
			self.clean_hanging()
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   495
			self.handle_flowable(flowables)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   496
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   497
		self._endBuild()
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   498
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   499
class UserDocTemplate(BaseDocTemplate):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   500
	"""This is a descendant of BaseDocTemplate
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   501
	which provides easy hooks for straightforward
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   502
	document processing. Use this as a base class.
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   503
	The methods are mostly 'contract-free' - they
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   504
	require that the programmer do little or nothing.
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   505
	To distinguish them, they are generally called
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   506
	'beforeEvent' or 'afterEvent'."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   507
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   508
	#-----these methods are redefined to call the user hooks---
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   509
	def __init__(self, filename, **kw):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   510
		apply(BaseDocTemplate.__init__,(self,filename),kw)
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   511
		self.afterInit()
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   512
		
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   513
	def handle_documentBegin(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   514
		self._handle_documentBegin()
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   515
		self.beforeDocument()
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   516
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   517
	def handle_pageBegin(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   518
		self._handle_pageBegin()
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   519
		self.beforePage()
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   520
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   521
	def handle_pageEnd(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   522
		#trickier, need to rewrite
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   523
		self.afterPage()
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   524
		self._handle_pageEnd()
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   525
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   526
	def handle_beforeFlowable(self, flowable):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   527
		return self.beforeFlowable(flowable)
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   528
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   529
	def handle_afterFlowable(self, flowable):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   530
		self.afterFlowable(flowable)
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   531
		
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   532
	#-----these are the default implementation of the user hooks
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   533
	def afterInit(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   534
		"""This is called after initialisation.
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   535
		Hooking into this rather than overriding
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   536
		__init__ means that one need not worry
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   537
		about passing a complex sequence of arguments
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   538
		to the base class."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   539
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   540
	
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   541
	def beforeDocument(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   542
		"""This is called before any processing is
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   543
		done on the document."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   544
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   545
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   546
	def beforePage(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   547
		"""This is called at the beginning of page
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   548
		processing, and immediately before the
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   549
		beforeDrawPage method of the current page
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   550
		template."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   551
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   552
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   553
	def afterPage(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   554
		"""This is called after page processing, and
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   555
		immediately after the afterDrawPage method
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   556
		of the current page template."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   557
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   558
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   559
	def beforeFlowable(self, flowable):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   560
		"""This is called before a flowable is processed.
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   561
		It may modify it or substitute another flowable,
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   562
		or even return None to signify that it should
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   563
		not be processed.  If you just want to examine
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   564
		the object, be sure to return it."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   565
		return flowable
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   566
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   567
	def afterFlowable(self, flowable):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   568
		"""This is called after a flowable is processed.
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   569
		"""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   570
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   571
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   572
class SimpleDocTemplate(BaseDocTemplate):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   573
	"""A special case document template that will handle many simple documents.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   574
	   See documentation for BaseDocTemplate.  No pageTemplates are required 
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   575
	   for this special case.   A page templates are inferred from the
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   576
	   margin information and the onFirstPage, onLaterPages arguments to the build method.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   577
	   
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   578
	   A document which has all pages with the same look except for the first
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   579
	   page may can be built using this special approach.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   580
	   """
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   581
	def handle_pageBegin(self):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   582
		self._handle_pageBegin()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   583
		self._handle_nextPageTemplate('Later')
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   584
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   585
	def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   586
	        """build the document using the flowables.  Annotate the first page using the onFirstPage
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   587
	           function and later pages using the onLaterPages function.  The onXXX pages should follow
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   588
	           the signature
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   589
	           
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   590
	              def myOnFirstPage(canvas, document):
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   591
	                  # do annotations and modify the document
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   592
	                  ...
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   593
	                  
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   594
	           The functions can do thing like draw logos, page numbers,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   595
	           footers, etcetera. They can use external variables to vary
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   596
	           the look (for example providing page numbering or section names).
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   597
	        """
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   598
		frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal')
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   599
		self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage),
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   600
						PageTemplate(id='Later',frames=frameT, onPage=onLaterPages)])
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   601
		if onFirstPage is _doNothing and hasattr(self,'onFirstPage'):
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   602
			self.pageTemplates[0].beforeDrawPage = self.onFirstPage
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   603
		if onLaterPages is _doNothing and hasattr(self,'onLaterPages'):
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   604
			self.pageTemplates[1].beforeDrawPage = self.onLaterPages
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   605
		BaseDocTemplate.build(self,flowables)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   606
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   607
if __name__ == '__main__':
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   608
	##########################################################
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   609
	##
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   610
	##	 testing
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   611
	##
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   612
	##########################################################
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   613
	def randomText():
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   614
		#this may or may not be appropriate in your company
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   615
		from random import randint, choice
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   616
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   617
		RANDOMWORDS = ['strategic','direction','proactive',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   618
		'reengineering','forecast','resources',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   619
		'forward-thinking','profit','growth','doubletalk',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   620
		'venture capital','IPO']
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   621
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   622
		sentences = 5
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   623
		output = ""
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   624
		for sentenceno in range(randint(1,5)):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   625
			output = output + 'Blah'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   626
			for wordno in range(randint(10,25)):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   627
				if randint(0,4)==0:
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   628
					word = choice(RANDOMWORDS)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   629
				else:
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   630
					word = 'blah'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   631
				output = output + ' ' +word
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   632
			output = output+'.'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   633
		return output
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   634
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   635
	def myFirstPage(canvas, doc):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   636
		canvas.saveState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   637
		canvas.setStrokeColor(red)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   638
		canvas.setLineWidth(5)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   639
		canvas.line(66,72,66,PAGE_HEIGHT-72)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   640
		canvas.setFont('Times-Bold',24)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   641
		canvas.drawString(108, PAGE_HEIGHT-108, "PLATYPUS")
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   642
		canvas.setFont('Times-Roman',12)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   643
		canvas.drawString(4 * inch, 0.75 * inch, "First Page")
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   644
		canvas.restoreState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   645
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   646
	def myLaterPages(canvas, doc):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   647
		canvas.saveState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   648
		canvas.setStrokeColor(red)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   649
		canvas.setLineWidth(5)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   650
		canvas.line(66,72,66,PAGE_HEIGHT-72)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   651
		canvas.setFont('Times-Roman',12)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   652
		canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   653
		canvas.restoreState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   654
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   655
	def run():
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   656
		objects_to_draw = []
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   657
		from reportlab.lib.styles import ParagraphStyle
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   658
		#from paragraph import Paragraph
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   659
		from doctemplate import SimpleDocTemplate
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   660
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   661
		#need a style
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   662
		normal = ParagraphStyle('normal')
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   663
		normal.firstLineIndent = 18
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   664
		normal.spaceBefore = 6
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   665
		import random
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   666
		for i in range(15):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   667
			height = 0.5 + (2*random.random())
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   668
			box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   669
			objects_to_draw.append(box)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   670
			para = Paragraph(randomText(), normal)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   671
			objects_to_draw.append(para)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   672
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   673
		SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw,
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   674
			onFirstPage=myFirstPage,onLaterPages=myLaterPages)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   675
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   676
	run()
294
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
   677