~ubuntu-branches/ubuntu/trusty/pisa/trusty

« back to all changes in this revision

Viewing changes to sx/pisa3/pisa_tables.py

  • Committer: Bazaar Package Importer
  • Author(s): W. Martin Borgert
  • Date: 2010-02-08 22:02:40 UTC
  • Revision ID: james.westby@ubuntu.com-20100208220240-cwsifrpnqeaug5x5
Tags: upstream-3.0.32
ImportĀ upstreamĀ versionĀ 3.0.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: ISO-8859-1 -*-
 
2
#############################################
 
3
## (C)opyright by Dirk Holtwick, 2002-2007 ##
 
4
## All rights reserved                     ##
 
5
#############################################
 
6
 
 
7
__reversion__ = "$Revision: 20 $"
 
8
__author__ = "$Author: holtwick $"
 
9
__date__ = "$Date: 2007-10-09 12:58:24 +0200 (Di, 09 Okt 2007) $"
 
10
 
 
11
from pisa_tags import pisaTag
 
12
from pisa_util import *
 
13
from pisa_reportlab import PmlTable, TableStyle, PmlKeepInFrame
 
14
 
 
15
import copy
 
16
import sys
 
17
 
 
18
import logging
 
19
log = logging.getLogger("ho.pisa")
 
20
            
 
21
def _width(value=None):
 
22
    if value is None:
 
23
        return None
 
24
    value = str(value)
 
25
    if value.endswith("%"):
 
26
        return value
 
27
    return getSize(value)
 
28
 
 
29
class TableData:
 
30
 
 
31
    def __init__(self):
 
32
        self.data = []
 
33
        self.styles = []
 
34
        self.span = []
 
35
        self.mode = ""
 
36
        self.padding = 0
 
37
        self.col = 0
 
38
        # self.c = None
 
39
 
 
40
    def add_cell(self, data=None):
 
41
        self.col += 1
 
42
        self.data[len(self.data) - 1].append(data)
 
43
 
 
44
    def add_style(self, data):
 
45
        # print self.mode, data
 
46
        # Do we have color and 
 
47
        # width = data[3]
 
48
        #if data[0].startswith("LINE"):
 
49
        #    color = data[4]
 
50
        #    if color is None:
 
51
        #        return
 
52
        self.styles.append(copy.copy(data))
 
53
 
 
54
    def add_empty(self, x, y):
 
55
        self.span.append((x, y))
 
56
 
 
57
    def get_data(self):
 
58
        data = self.data
 
59
        for x, y in self.span:
 
60
            try:
 
61
                data[y].insert(x, '')
 
62
            except:
 
63
                pass
 
64
        return data
 
65
   
 
66
    def add_cell_styles(self, c, begin, end, mode="td"):
 
67
        def getColor(a, b): 
 
68
            return a
 
69
        self.mode = mode.upper()
 
70
        if c.frag.backColor and mode != "tr": # XXX Stimmt das so?
 
71
            self.add_style(('BACKGROUND', begin, end, c.frag.backColor))
 
72
            # print 'BACKGROUND', begin, end, c.frag.backColor
 
73
        if 0:
 
74
            log.debug("%r", (
 
75
                begin,
 
76
                end,
 
77
                c.frag.borderTopWidth,
 
78
                c.frag.borderTopStyle,
 
79
                c.frag.borderTopColor,
 
80
                c.frag.borderBottomWidth,
 
81
                c.frag.borderBottomStyle,
 
82
                c.frag.borderBottomColor,
 
83
                c.frag.borderLeftWidth,
 
84
                c.frag.borderLeftStyle,
 
85
                c.frag.borderLeftColor,
 
86
                c.frag.borderRightWidth,
 
87
                c.frag.borderRightStyle,
 
88
                c.frag.borderRightColor,
 
89
                ))
 
90
        if getBorderStyle(c.frag.borderTopStyle) and c.frag.borderTopWidth and c.frag.borderTopColor is not None:
 
91
            self.add_style(('LINEABOVE', begin, (end[0], begin[1]),
 
92
                c.frag.borderTopWidth,
 
93
                c.frag.borderTopColor,
 
94
                "squared"))
 
95
        if getBorderStyle(c.frag.borderLeftStyle) and c.frag.borderLeftWidth and c.frag.borderLeftColor is not None:
 
96
            self.add_style(('LINEBEFORE', begin, (begin[0], end[1]),
 
97
                c.frag.borderLeftWidth,
 
98
                c.frag.borderLeftColor,
 
99
                "squared"))
 
100
        if getBorderStyle(c.frag.borderRightStyle) and c.frag.borderRightWidth and c.frag.borderRightColor is not None:
 
101
            self.add_style(('LINEAFTER', (end[0], begin[1]), end,
 
102
                c.frag.borderRightWidth,
 
103
                c.frag.borderRightColor,
 
104
                "squared"))
 
105
        if getBorderStyle(c.frag.borderBottomStyle) and c.frag.borderBottomWidth and c.frag.borderBottomColor is not None:
 
106
            self.add_style(('LINEBELOW', (begin[0], end[1]), end,
 
107
                c.frag.borderBottomWidth,
 
108
                c.frag.borderBottomColor,
 
109
                "squared"))
 
110
        self.add_style(('LEFTPADDING', begin, end, c.frag.paddingLeft or self.padding))
 
111
        self.add_style(('RIGHTPADDING', begin, end, c.frag.paddingRight or self.padding))
 
112
        self.add_style(('TOPPADDING', begin, end, c.frag.paddingTop or self.padding))
 
113
        self.add_style(('BOTTOMPADDING', begin, end, c.frag.paddingBottom or self.padding))
 
114
 
 
115
class pisaTagTABLE(pisaTag):
 
116
    
 
117
    def start(self, c):
 
118
        c.addPara()
 
119
    
 
120
        attrs = self.attr
 
121
        
 
122
        # Swap table data
 
123
        c.tableData, self.tableData = TableData(), c.tableData
 
124
        tdata = c.tableData
 
125
 
 
126
        # border
 
127
        #tdata.border = attrs.border
 
128
        #tdata.bordercolor = attrs.bordercolor
 
129
 
 
130
        begin = (0, 0)
 
131
        end = (-1, - 1)
 
132
            
 
133
        if attrs.border and attrs.bordercolor:
 
134
            frag = c.frag
 
135
            frag.borderLeftWidth = attrs.border
 
136
            frag.borderLeftColor = attrs.bordercolor
 
137
            frag.borderLeftStyle = "solid"
 
138
            frag.borderRightWidth = attrs.border
 
139
            frag.borderRightColor = attrs.bordercolor
 
140
            frag.borderRightStyle = "solid"
 
141
            frag.borderTopWidth = attrs.border
 
142
            frag.borderTopColor = attrs.bordercolor
 
143
            frag.borderTopStyle = "solid"
 
144
            frag.borderBottomWidth = attrs.border
 
145
            frag.borderBottomColor = attrs.bordercolor
 
146
            frag.borderBottomStyle = "solid"
 
147
 
 
148
            # tdata.add_style(("GRID", begin, end, attrs.border, attrs.bordercolor))
 
149
        
 
150
        tdata.padding = attrs.cellpadding
 
151
        
 
152
        #if 0: #attrs.cellpadding:
 
153
        #    tdata.add_style(('LEFTPADDING', begin, end, attrs.cellpadding))
 
154
        #    tdata.add_style(('RIGHTPADDING', begin, end, attrs.cellpadding))
 
155
        #    tdata.add_style(('TOPPADDING', begin, end, attrs.cellpadding))
 
156
        #    tdata.add_style(('BOTTOMPADDING', begin, end, attrs.cellpadding))
 
157
            
 
158
        # alignment
 
159
        #~ tdata.add_style(('VALIGN', (0,0), (-1,-1), attrs.valign.upper()))
 
160
 
 
161
        # Set Border and padding styles
 
162
        
 
163
        tdata.add_cell_styles(c, (0, 0), (-1, - 1), "table")
 
164
 
 
165
        # bgcolor
 
166
        #if attrs.bgcolor is not None:
 
167
        #    tdata.add_style(('BACKGROUND', (0, 0), (-1, -1), attrs.bgcolor))
 
168
 
 
169
        tdata.align = attrs.align.upper()
 
170
        tdata.col = 0
 
171
        tdata.row = 0        
 
172
        tdata.colw = []
 
173
        tdata.rowh = []
 
174
        tdata.repeat = attrs.repeat
 
175
        tdata.width = _width(attrs.width)
 
176
 
 
177
        # self.tabdata.append(tdata)
 
178
 
 
179
    def end(self, c):
 
180
        tdata = c.tableData
 
181
        data = tdata.get_data()        
 
182
        
 
183
        # Add missing columns so that each row has the same count of columns
 
184
        # This prevents errors in Reportlab table
 
185
        maxcols = max([len(row) for row in data] or [0])
 
186
        for i, row in enumerate(data):
 
187
            data[i] += [''] * (maxcols - len(row))
 
188
 
 
189
        try:
 
190
            if tdata.data:
 
191
                # log.debug("Table sryles %r", tdata.styles)
 
192
                t = PmlTable(
 
193
                    data,
 
194
                    colWidths=tdata.colw,
 
195
                    rowHeights=tdata.rowh,
 
196
                    # totalWidth = tdata.width,
 
197
                    splitByRow=1,
 
198
                    # repeatCols = 1,
 
199
                    repeatRows=tdata.repeat,
 
200
                    hAlign=tdata.align,
 
201
                    vAlign='TOP',
 
202
                    style=TableStyle(tdata.styles))
 
203
                t.totalWidth = _width(tdata.width)
 
204
                t.spaceBefore = c.frag.spaceBefore
 
205
                t.spaceAfter = c.frag.spaceAfter
 
206
                
 
207
                # XXX Maybe we need to copy some more properties?
 
208
                t.keepWithNext = c.frag.keepWithNext
 
209
                # t.hAlign = tdata.align
 
210
                c.addStory(t)
 
211
            else:
 
212
                log.warn(c.warning("<table> is empty"))
 
213
        except:            
 
214
            log.warn(c.warning("<table>"), exc_info=1)
 
215
 
 
216
        # Cleanup and re-swap table data
 
217
        c.clearFrag()
 
218
        c.tableData, self.tableData = self.tableData, None
 
219
    
 
220
class pisaTagTR(pisaTag):
 
221
    
 
222
    def start(self, c):            
 
223
        tdata = c.tableData
 
224
        row = tdata.row
 
225
        begin = (0, row)
 
226
        end = (-1, row)
 
227
        
 
228
        tdata.add_cell_styles(c, begin, end, "tr")       
 
229
        c.frag.vAlign = self.attr.valign or c.frag.vAlign
 
230
          
 
231
        tdata.col = 0
 
232
        tdata.data.append([])
 
233
 
 
234
    def end(self, c):
 
235
        c.tableData.row += 1        
 
236
 
 
237
class pisaTagTD(pisaTag):
 
238
    
 
239
    def start(self, c):
 
240
 
 
241
        if self.attr.align is not None:
 
242
            #print self.attr.align, getAlign(self.attr.align)
 
243
            c.frag.alignment = getAlign(self.attr.align)
 
244
            
 
245
        c.clearFrag()
 
246
        self.story = c.swapStory()
 
247
        # print "#", len(c.story)
 
248
        
 
249
        attrs = self.attr
 
250
        
 
251
        tdata = c.tableData
 
252
 
 
253
        cspan = attrs.colspan
 
254
        rspan = attrs.rowspan
 
255
 
 
256
        row = tdata.row
 
257
        col = tdata.col
 
258
        while 1:
 
259
            for x, y in tdata.span:
 
260
                if x == col and y == row:
 
261
                    col += 1
 
262
                    tdata.col += 1
 
263
            break
 
264
        #cs = 0
 
265
        #rs = 0
 
266
 
 
267
        begin = (col, row)
 
268
        end = (col, row)
 
269
        if cspan:
 
270
            end = (end[0] + cspan - 1, end[1])
 
271
        if rspan:
 
272
            end = (end[0], end[1] + rspan - 1)
 
273
        if begin != end:
 
274
            #~ print begin, end
 
275
            tdata.add_style(('SPAN', begin, end))
 
276
            for x in range(begin[0], end[0] + 1):
 
277
                for y in range(begin[1], end[1] + 1):
 
278
                    if x != begin[0] or y != begin[1]:
 
279
                        tdata.add_empty(x, y)
 
280
 
 
281
        # Set Border and padding styles
 
282
        tdata.add_cell_styles(c, begin, end, "td")
 
283
 
 
284
        # Calculate widths
 
285
        # Add empty placeholders for new columns
 
286
        if (col + 1) > len(tdata.colw):
 
287
            tdata.colw = tdata.colw + ((col + 1 - len(tdata.colw)) * [_width()])
 
288
        # Get value of with, if no spanning
 
289
        if not cspan:
 
290
            # print c.frag.width
 
291
            width = c.frag.width or self.attr.width #self._getStyle(None, attrs, "width", "width", mode)
 
292
            # If is value, the set it in the right place in the arry
 
293
            # print width, _width(width)
 
294
            if width is not None:
 
295
                tdata.colw[col] = _width(width)
 
296
 
 
297
        # Calculate heights
 
298
        if row + 1 > len(tdata.rowh):
 
299
            tdata.rowh = tdata.rowh + ((row + 1 - len(tdata.rowh)) * [_width()])
 
300
        if not rspan:
 
301
            height = None #self._getStyle(None, attrs, "height", "height", mode)
 
302
            if height is not None:
 
303
                tdata.rowh[row] = _width(height)
 
304
                tdata.add_style(('FONTSIZE', begin, end, 1.0))
 
305
                tdata.add_style(('LEADING', begin, end, 1.0))
 
306
 
 
307
        # Vertical align      
 
308
        valign = self.attr.valign or c.frag.vAlign
 
309
        if valign is not None:
 
310
            tdata.add_style(('VALIGN', begin, end, valign.upper()))
 
311
 
 
312
        # Reset border, otherwise the paragraph block will have borders too
 
313
        frag = c.frag
 
314
        frag.borderLeftWidth = 0
 
315
        frag.borderLeftColor = None
 
316
        frag.borderLeftStyle = None
 
317
        frag.borderRightWidth = 0
 
318
        frag.borderRightColor = None
 
319
        frag.borderRightStyle = None
 
320
        frag.borderTopWidth = 0
 
321
        frag.borderTopColor = None
 
322
        frag.borderTopStyle = None
 
323
        frag.borderBottomWidth = 0
 
324
        frag.borderBottomColor = None
 
325
        frag.borderBottomStyle = None
 
326
 
 
327
    def end(self, c):
 
328
        tdata = c.tableData
 
329
        
 
330
        c.addPara()
 
331
        cell = c.story
 
332
        
 
333
        # Handle empty cells, they otherwise collapse
 
334
        if not cell:
 
335
            cell = ' '        
 
336
            
 
337
        # Keep in frame if needed since Reportlab does no split inside of cells
 
338
        elif (not c.frag.insideStaticFrame) and (c.frag.keepInFrameMode is not None):
 
339
            
 
340
            # tdata.keepinframe["content"] = cell
 
341
            cell = PmlKeepInFrame(
 
342
                maxWidth=0,
 
343
                maxHeight=0,
 
344
                mode=c.frag.keepInFrameMode,
 
345
                content=cell)
 
346
 
 
347
        c.swapStory(self.story)
 
348
              
 
349
        tdata.add_cell(cell)
 
350
        
 
351
class pisaTagTH(pisaTagTD):
 
352
    pass
 
353
 
 
354
'''
 
355
    end_th = end_td
 
356
 
 
357
    def start_keeptogether(self, attrs):
 
358
        self.story.append([])
 
359
        self.next_para()
 
360
 
 
361
    def end_keeptogether(self):
 
362
        if not self.story[-1]:
 
363
            self.add_noop()
 
364
        self.next_para()
 
365
        s = self.story.pop()
 
366
        self.add_story(KeepTogether(s))
 
367
 
 
368
    def start_keepinframe(self, attrs):
 
369
        self.story.append([])
 
370
        self.keepinframe = {
 
371
            "maxWidth": attrs["maxwidth"],
 
372
            "maxHeight": attrs["maxheight"],
 
373
            "mode": attrs["mode"],
 
374
            "name": attrs["name"],
 
375
            "mergeSpace": attrs["mergespace"]
 
376
            }
 
377
        # print self.keepinframe
 
378
        self.next_para()
 
379
 
 
380
    def end_keepinframe(self):
 
381
        if not self.story[-1]:
 
382
            self.add_noop()
 
383
        self.next_para()
 
384
        self.keepinframe["content"] = self.story.pop()
 
385
        self.add_story(KeepInFrame(**self.keepinframe))
 
386
'''
 
 
b'\\ No newline at end of file'