reportlab/platypus/doctemplate.py
author rgbecker
Thu, 01 Jun 2000 16:28:35 +0000
changeset 255 ee9e321e747d
parent 253 cfcf8d555a2c
child 268 8414113fa500
permissions -rw-r--r--
pageSize is wrong at present
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 $
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    34
#	Revision 1.14  2000/06/01 16:27:56  rgbecker
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    35
#	pageSize is wrong at present
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    36
#
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    37
#	Revision 1.13  2000/06/01 15:23:06  rgbecker
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    38
#	Platypus re-organisation
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    39
#	
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
    40
#	Revision 1.12  2000/05/26 10:27:37  rgbecker
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
    41
#	Fixed infinite recursion bug
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    42
#	
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    43
#	Revision 1.11  2000/05/17 22:17:38  rgbecker
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    44
#	Renamed BasicFrame to Frame
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
    45
#	
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
    46
#	Revision 1.10  2000/05/17 16:29:40  rgbecker
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
    47
#	Removal of SimpleFrame
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    48
#	
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    49
#	Revision 1.9  2000/05/17 15:37:33  rgbecker
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    50
#	Changes related to removal of SimpleFlowDocument
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
    51
#	
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    52
#	Revision 1.8  2000/05/16 16:15:16  rgbecker
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    53
#	Changes related to removal of SimpleFlowDocument
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    54
#	
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    55
#	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
    56
#	Fixes/Changes to get testplatypus to work with new framework
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    57
#	
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    58
#	Revision 1.6  2000/05/15 15:07:32  rgbecker
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    59
#	Added drawPage
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    60
#	
206
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
    61
#	Revision 1.5  2000/05/13 08:33:53  rgbecker
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
    62
#	fix typo in import
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    63
#	
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
    64
#	Revision 1.4  2000/05/12 16:21:02  rgbecker
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
    65
#	_donothing explicit import
206
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
    66
#	
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
    67
#	Revision 1.3  2000/05/12 14:53:38  rgbecker
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
    68
#	Handle splitting error in build
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
    69
#	
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    70
#	Revision 1.2  2000/05/12 14:45:31  rgbecker
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    71
#	Single actions only in ActionFlowables
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
    72
#	
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    73
#	Revision 1.1  2000/05/12 12:53:33  rgbecker
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    74
#	Initial try at a document template class
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    75
#	
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    76
__version__=''' $Id: doctemplate.py,v 1.14 2000/06/01 16:27:56 rgbecker Exp $ '''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    77
__doc__="""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    78
More complicated Document model
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    79
"""
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    80
from flowables import *
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    81
from frames import Frame
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    82
from types import *
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    83
import sys
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    84
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    85
def _doNothing(canvas, doc):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    86
	"Dummy callback for onPage"
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    87
	pass
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    88
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    89
class ActionFlowable(Flowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    90
	'''This Flowable is never drawn, it can be used for data driven controls'''
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    91
	def __init__(self,action=[]):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    92
		if type(action) not in (ListType, TupleType):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    93
			action = (action,)
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    94
		self.action = action
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    95
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    96
	def wrap(self, availWidth, availHeight):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    97
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    98
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    99
	def draw(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   100
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   101
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   102
	def apply(self,doc):
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   103
		action = self.action[0]
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   104
		args = tuple(self.action[1:])
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   105
		arn = 'handle_'+action
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   106
		try:
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   107
			apply(getattr(doc,arn), args)
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   108
		except AttributeError, aerr:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   109
			if aerr.args[0]==arn:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   110
				raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   111
			else:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   112
				raise
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   113
		except:
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   114
			t, v, None = sys.exc_info()
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   115
			raise t, "%s\n   handle_%s args=%s"%(v,action,args)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   116
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   117
	def __call__(self):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   118
		return self
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   119
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   120
FrameBreak = ActionFlowable('frameEnd')
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   121
PageBegin = ActionFlowable('pageBegin')
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   122
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   123
class NextPageTemplate(ActionFlowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   124
	def __init__(self,pt):
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   125
		ActionFlowable.__init__(self,('nextPageTemplate',pt))
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   126
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   127
class PageTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   128
	"""
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   129
	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
   130
	of a page when this is selected.
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   131
	derived classes can also implement drawPage if they want
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   132
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   133
	def __init__(self,id=None,frames=[],onPage=None):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   134
		if type(frames) not in (ListType,TupleType): frames = [frames]
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   135
		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
   136
		self.id = id
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   137
		self.frames = frames
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   138
		self.onPage = onPage or _doNothing
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   139
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   140
	def drawPage(self,canv,doc):
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   141
		'''	Override this if you want additional functionality or prefer a class
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   142
			based page routine
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   143
		'''
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   144
		pass
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   145
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   146
class BaseDocTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   147
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   148
	First attempt at defining a document template class.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   149
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   150
	The basic idea is simple.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   151
	0)	The document has a list of data associated with it
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   152
		this data should derive from flowables. We'll have
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   153
		special classes like PageBreak, FrameBreak to do things
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   154
		like forcing a page end etc.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   155
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   156
	1)	The document has one or more page templates.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   157
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   158
	2)	Each page template has one or more frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   159
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   160
	3)	The document class provides base methods for handling the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   161
		story events and some reasonable methods for getting the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   162
		story flowables into the frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   163
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   164
	4)	The document instances can override the base handler routines.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   165
	"""
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   166
	_initArgs = {	'pagesize':DEFAULT_PAGE_SIZE,
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   167
					'pageTemplates':[],
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   168
					'showBoundary':0,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   169
					'leftMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   170
					'rightMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   171
					'topMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   172
					'bottomMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   173
					'allowSplitting':1,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   174
					'title':None,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   175
					'author':None,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   176
					'_pageBreakQuick':1}
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   177
	_invalidInitArgs = ()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   178
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   179
	def __init__(self, filename, **kw):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   180
		self.filename = filename
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   181
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   182
		for k in self._initArgs.keys():
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   183
			if not kw.has_key(k):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   184
				v = self._initArgs[k]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   185
			else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   186
				if k in self._invalidInitArgs:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   187
					raise ValueError, "Invalid argument %s" % k
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   188
				v = kw[k]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   189
			setattr(self,k,v)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   190
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   191
		p = self.pageTemplates
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   192
		self.pageTemplates = []
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   193
		self.addPageTemplates(p)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   194
		self._calc()
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   195
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   196
	def _calc(self):
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   197
		self._rightMargin = self.pagesize[0] - self.rightMargin
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   198
		self._topMargin = self.pagesize[1] - self.topMargin
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   199
		self.width = self._rightMargin - self.leftMargin
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   200
		self.height = self._topMargin - self.bottomMargin
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   201
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   202
	def clean_hanging(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   203
		'handle internal postponed actions'
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   204
		while len(self._hanging):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   205
			self.handle_flowable(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   206
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   207
	def addPageTemplates(self,pageTemplates):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   208
		'add one or a sequence of pageTamplates'
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   209
		if type(pageTemplates) not in (ListType,TupleType):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   210
			pageTemplates = [pageTemplates]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   211
		assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   212
		for t in pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   213
			self.pageTemplates.append(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   214
			
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   215
	def handle_documentBegin(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   216
		'''Hook actions at beginning of document'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   217
		self._hanging = [PageBegin]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   218
		self.pageTemplate = self.pageTemplates[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   219
		self.page = 0
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   220
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   221
	def handle_pageBegin(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   222
		'''Perform actions required at beginning of page.
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   223
		shouldn't normally be called directly'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   224
		self.page = self.page + 1
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   225
		self.pageTemplate.drawPage(self.canv,self)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   226
		self.pageTemplate.onPage(self.canv,self)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   227
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   228
			del self._nextFrameIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   229
		self.frame = self.pageTemplate.frames[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   230
		self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   231
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   232
	def handle_pageEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   233
		'''	show the current page
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   234
			check the next page template
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   235
			hang a page begin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   236
		'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   237
		self.canv.showPage()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   238
		if hasattr(self,'_nextPageTemplateIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   239
			self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   240
			del self._nextPageTemplateIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   241
		self._hanging.append(PageBegin)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   242
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   243
	def handle_pageBreak(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   244
		'''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
   245
		if self._pageBreakQuick:
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   246
			self.handle_pageEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   247
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   248
			n = len(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   249
			while len(self._hanging)==n:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   250
				self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   251
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   252
	def handle_frameBegin(self,*args):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   253
		'''What to do at the beginning of a page'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   254
		self.frame._reset()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   255
		if self.showBoundary or self.frame.showBoundary:
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
   256
			self.frame.drawBoundary(self.canv)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   257
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   258
	def handle_frameEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   259
		'''	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
   260
			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
   261
		'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   262
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   263
			frame = self.pageTemplate.frames[self._nextFrameIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   264
			del self._nextFrameIndex
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   265
			self.handle_frameBegin()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   266
		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
   267
			self.handle_pageEnd()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   268
			self.frame = None
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   269
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   270
			f = self.frame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   271
			self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   272
			self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   273
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   274
	def handle_nextPageTemplate(self,pt):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   275
		'''On endPage chenge to the page template with name or index pt'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   276
		if type(pt) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   277
			for t in self.pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   278
				if t.id == pt:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   279
					self._nextPageTemplateIndex = self.pageTemplates.index(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   280
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   281
			raise ValueError, "can't find template('%s')"%pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   282
		elif type(pt) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   283
			self._nextPageTemplateIndex = pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   284
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   285
			raise TypeError, "argument pt should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   286
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   287
	def handle_nextFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   288
		'''On endFrame chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   289
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   290
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   291
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   292
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   293
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   294
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   295
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   296
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   297
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   298
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   299
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   300
	def handle_currentFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   301
		'''chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   302
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   303
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   304
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   305
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   306
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   307
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   308
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   309
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   310
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   311
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   312
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   313
	def handle_flowable(self,flowables):
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   314
		'''try to handle one flowable from the front of list flowables.'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   315
		f = flowables[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   316
		del flowables[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   317
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   318
		if isinstance(f,PageBreak):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   319
			self.handle_pageBreak()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   320
		elif isinstance(f,ActionFlowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   321
			f.apply(self)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   322
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   323
			#general case we have to do something
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   324
			if not self.frame.add(f, self.canv, trySplit=self.allowSplitting):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   325
				if self.allowSplitting:
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   326
					# see if this is a splittable thing
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   327
					S = self.frame.split(f)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   328
					n = len(S)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   329
				else:
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   330
					n = 0
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   331
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   332
				if n:
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   333
					if not self.frame.add(S[0], self.canv, trySplit=0):
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   334
						raise "LayoutError", "splitting error"
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   335
					del S[0]
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   336
					for f in xrange(n-1):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   337
						flowables.insert(f,S[f])	# put split flowables back on the list
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   338
				else:
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   339
					if hasattr(f,'postponed'):
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   340
						raise "LayoutError", "Flowable too large"
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   341
					f.postponed = 1
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   342
					flowables.insert(0,f)			# put the flowable back
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   343
					self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   344
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   345
	#these are provided so that deriving classes can refer to them
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   346
	_handle_documentBegin = handle_documentBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   347
	_handle_pageBegin = handle_pageBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   348
	_handle_pageEnd = handle_pageEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   349
	_handle_frameBegin = handle_frameBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   350
	_handle_frameEnd = handle_frameEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   351
	_handle_flowable = handle_flowable
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   352
	_handle_nextPageTemplate = handle_nextPageTemplate
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   353
	_handle_currentFrame = handle_currentFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   354
	_handle_nextFrame = handle_nextFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   355
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   356
	def _startBuild(self):
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   357
		self._calc()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   358
		self.canv = canvas.Canvas(self.filename)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   359
		self.handle_documentBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   360
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   361
	def _endBuild(self):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   362
		if self._hanging!=[] and self._hanging[-1] is PageBegin:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   363
			del self._hanging[-1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   364
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   365
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   366
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   367
			self.handle_pageBreak()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   368
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   369
		self.canv.save()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   370
		del self.frame, self.pageTemplate
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   371
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   372
	def build(self, flowables):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   373
		assert filter(lambda x: not isinstance(x,Flowable), flowables)==[], "flowables argument error"
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   374
		self._startBuild()
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   375
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   376
		while len(flowables):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   377
			self.clean_hanging()
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   378
			self.handle_flowable(flowables)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   379
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   380
		self._endBuild()
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   381
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   382
class SimpleDocTemplate(BaseDocTemplate):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   383
	def handle_pageBegin(self):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   384
		self._handle_pageBegin()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   385
		self._handle_nextPageTemplate('Later')
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   386
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   387
	def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing):
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   388
		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
   389
		self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage),
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   390
						PageTemplate(id='Later',frames=frameT, onPage=onLaterPages)])
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   391
		if onFirstPage is _doNothing and hasattr(self,'onFirstPage'):
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   392
			self.pageTemplates[0].drawPage = self.onFirstPage
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   393
		if onLaterPages is _doNothing and hasattr(self,'onLaterPages'):
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   394
			self.pageTemplates[1].drawPage = self.onLaterPages
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   395
		BaseDocTemplate.build(self,flowables)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   396
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   397
if __name__ == '__main__':
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   398
	##########################################################
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   399
	##
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   400
	##	 testing
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   401
	##
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   402
	##########################################################
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   403
	def randomText():
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   404
		#this may or may not be appropriate in your company
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   405
		from random import randint, choice
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   406
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   407
		RANDOMWORDS = ['strategic','direction','proactive',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   408
		'reengineering','forecast','resources',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   409
		'forward-thinking','profit','growth','doubletalk',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   410
		'venture capital','IPO']
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   411
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   412
		sentences = 5
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   413
		output = ""
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   414
		for sentenceno in range(randint(1,5)):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   415
			output = output + 'Blah'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   416
			for wordno in range(randint(10,25)):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   417
				if randint(0,4)==0:
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   418
					word = choice(RANDOMWORDS)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   419
				else:
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   420
					word = 'blah'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   421
				output = output + ' ' +word
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   422
			output = output+'.'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   423
		return output
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   424
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   425
	def myFirstPage(canvas, doc):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   426
		canvas.saveState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   427
		canvas.setStrokeColor(red)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   428
		canvas.setLineWidth(5)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   429
		canvas.line(66,72,66,PAGE_HEIGHT-72)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   430
		canvas.setFont('Times-Bold',24)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   431
		canvas.drawString(108, PAGE_HEIGHT-108, "PLATYPUS")
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   432
		canvas.setFont('Times-Roman',12)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   433
		canvas.drawString(4 * inch, 0.75 * inch, "First Page")
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   434
		canvas.restoreState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   435
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   436
	def myLaterPages(canvas, doc):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   437
		canvas.saveState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   438
		canvas.setStrokeColor(red)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   439
		canvas.setLineWidth(5)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   440
		canvas.line(66,72,66,PAGE_HEIGHT-72)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   441
		canvas.setFont('Times-Roman',12)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   442
		canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   443
		canvas.restoreState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   444
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   445
	def run():
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   446
		objects_to_draw = []
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   447
		from reportlab.lib.styles import ParagraphStyle
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   448
		#from paragraph import Paragraph
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   449
		from doctemplate import SimpleDocTemplate
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   450
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   451
		#need a style
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   452
		normal = ParagraphStyle('normal')
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   453
		normal.firstLineIndent = 18
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   454
		normal.spaceBefore = 6
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   455
		import random
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   456
		for i in range(15):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   457
			height = 0.5 + (2*random.random())
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   458
			box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   459
			objects_to_draw.append(box)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   460
			para = Paragraph(randomText(), normal)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   461
			objects_to_draw.append(para)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   462
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   463
		SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw,
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   464
			onFirstPage=myFirstPage,onLaterPages=myLaterPages)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   465
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   466
	run()