]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/printout.py
bca1aee4a4d81d126be200960b2fe4c21d165c01
[wxWidgets.git] / wxPython / wxPython / lib / printout.py
1 #----------------------------------------------------------------------------
2 # Name: printout.py
3 # Purpose: preview and printing class -> table/grid printing
4 #
5 # Author: Lorne White (email: lorne.white@telusplanet.net)
6 #
7 # Created:
8 # Version 0.72
9 # Date: Sept 8, 2001
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
12
13 import os, sys, string, copy
14
15 from wxPython.wx import *
16 import copy
17
18 class PrintBase:
19 def OutTextRegion(self, textout, txtdraw = TRUE):
20 textlines = string.splitfields(textout, '\n')
21 y = copy.copy(self.y) + self.pt_space_before
22 for text in textlines:
23 remain = 'X'
24 while remain != "":
25 vout, remain = self.SetFlow(text, self.region)
26 if self.draw == TRUE and txtdraw == TRUE:
27 test_out = self.TestFull(vout)
28 if self.align == wxALIGN_LEFT:
29 self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y)
30
31 elif self.align == wxALIGN_CENTRE:
32 diff = self.GetCellDiff(test_out, self.region)
33 self.DC.DrawText(test_out, self.indent+diff/2, y)
34
35 elif self.align == wxALIGN_RIGHT:
36 diff = self.GetCellDiff(test_out, self.region)
37 self.DC.DrawText(test_out, self.indent+diff, y)
38
39 else:
40 self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y)
41 text = remain
42 y = y + self.space
43 return y - self.space + self.pt_space_after
44
45 def GetCellDiff(self, text, width): # get the remaining cell size for adjustment
46 w, h = self.DC.GetTextExtent(text)
47 diff = width - w
48 if diff < 0:
49 diff = 0
50 return diff
51
52 def TestFull(self, text_test):
53 w, h = self.DC.GetTextExtent(text_test)
54 if w > self.region: # trouble fitting into cell
55 return self.SetChar(text_test, self.region) # fit the text to the cell size
56 else:
57 return text_test
58
59 def SetFlow(self, ln_text, width):
60 width = width - self.pcell_right_margin
61 text = ""
62 split = string.split(ln_text)
63 if len(split) == 1:
64 return ln_text, ""
65
66 cnt = 0
67 for word in split:
68 bword = " " + word # blank + word
69 length = len(bword)
70
71 w, h = self.DC.GetTextExtent(text + bword)
72 if w < width:
73 text = text + bword
74 cnt = cnt + 1
75 else:
76 remain = string.joinfields(split[cnt:],' ')
77 text = string.strip(text)
78 return text, remain
79
80 remain = string.joinfields(split[cnt:],' ')
81 vout = string.strip(text)
82 return vout, remain
83
84 def SetChar(self, ln_text, width): # truncate string to fit into width
85 width = width - self.pcell_right_margin - self.pcell_left_margin
86 text = ""
87 for val in ln_text:
88 w, h = self.DC.GetTextExtent(text + val)
89 if w > width:
90 text = text + ".."
91 return text # fitted text value
92 text = text + val
93 return text
94
95 def OutTextPageWidth(self, textout, y_out, align, indent, txtdraw = TRUE):
96 textlines = string.splitfields(textout, '\n')
97 y = copy.copy(y_out)
98
99 pagew = self.parent.page_width * self.pwidth # full page width
100 w, h = self.DC.GetTextExtent(textout)
101 y_line = h
102
103 for text in textlines:
104 remain = 'X'
105 while remain != "":
106 vout, remain = self.SetFlow(text, pagew)
107 if self.draw == TRUE and txtdraw == TRUE:
108 test_out = vout
109 if align == wxALIGN_LEFT:
110 self.DC.DrawText(test_out, indent, y)
111
112 elif align == wxALIGN_CENTRE:
113 diff = self.GetCellDiff(test_out, pagew)
114 self.DC.DrawText(test_out, indent+diff/2, y)
115
116 elif align == wxALIGN_RIGHT:
117 diff = self.GetCellDiff(test_out, pagew)
118 self.DC.DrawText(test_out, indent+diff, y)
119
120 else:
121 self.DC.DrawText(test_out, indent, y_out)
122 text = remain
123 y = y + y_line
124 return y - y_line
125
126 def SetPreview(self, preview):
127 self.preview = preview
128
129 def SetPSize(self, width, height):
130 self.pwidth = width/self.scale
131 self.pheight = height/self.scale
132
133 def SetScale(self, scale):
134 self.scale = scale
135
136 def SetPTSize(self, width, height):
137 self.ptwidth = width
138 self.ptheight = height
139
140 def getWidth(self):
141 return self.sizew
142
143 def getHeight(self):
144 return self.sizeh
145
146
147 class PrintTableDraw(wxScrolledWindow, PrintBase):
148 def __init__(self, parent, DC, size):
149 self.parent = parent
150 self.DC = DC
151 self.scale = parent.scale
152 self.width = size[0]
153 self.height = size[1]
154 self.SetDefaults()
155
156 def SetDefaults(self):
157 self.page = 1
158 self.total_pages = None
159
160 self.page_width = self.parent.page_width
161 self.page_height = self.parent.page_height
162
163 self.left_margin = self.parent.left_margin
164 self.right_margin = self.parent.right_margin
165
166 self.top_margin = self.parent.top_margin
167 self.bottom_margin = self.parent.bottom_margin
168 self.cell_left_margin = self.parent.cell_left_margin
169 self.cell_right_margin = self.parent.cell_right_margin
170
171 self.label_colour = self.parent.label_colour
172
173 self.row_line_colour = self.parent.row_line_colour
174 self.row_line_size = self.parent.row_line_size
175
176 self.row_def_line_colour = self.parent.row_def_line_colour
177 self.row_def_line_size = self.parent.row_def_line_size
178
179 self.column_line_colour = self.parent.column_line_colour
180 self.column_line_size = self.parent.column_line_size
181
182 self.column_def_line_size = self.parent.column_def_line_size
183 self.column_def_line_colour = self.parent.column_def_line_colour
184
185 self.text_font = self.parent.text_font
186 self.text_font_name = self.parent.text_font_name
187 self.text_font_colour = self.parent.text_font_colour
188
189 self.label_font = self.parent.label_font
190 self.label_font_name = self.parent.label_font_name
191 self.label_font_colour = self.parent.label_font_colour
192
193 def AdjustValues(self):
194 self.vertical_offset = self.pheight * self.parent.vertical_offset
195 self.horizontal_offset = self.pheight * self.parent.horizontal_offset
196
197 self.pcell_left_margin = self.pwidth * self.cell_left_margin
198 self.pcell_right_margin = self.pwidth * self.cell_right_margin
199 self.ptop_margin = self.pheight * self.top_margin
200 self.pbottom_margin = self.pheight * self.bottom_margin
201
202 self.pheader_margin = self.pheight * self.parent.header_margin
203 self.pfooter_margin = self.pheight * self.parent.footer_margin
204
205 self.cell_colour = self.parent.set_cell_colour
206 self.cell_text = self.parent.set_cell_text
207
208 self.column = []
209 self.column_align = []
210 self.column_bgcolour = []
211 self.column_txtcolour = []
212
213 set_column_align = self.parent.set_column_align
214 set_column_bgcolour = self.parent.set_column_bgcolour
215 set_column_txtcolour = self.parent.set_column_txtcolour
216
217 pos_x = self.left_margin * self.pwidth + self.horizontal_offset # left margin
218 self.column.append(pos_x)
219
220 if self.set_column == []:
221 table_width = self.page_width - self.left_margin - self.right_margin
222 width = table_width/(len(self.label))
223 for val in self.label:
224 column_width = width * self.pwidth
225 pos_x = pos_x + column_width
226 self.column.append(pos_x) # position of each column
227 else:
228 for val in self.set_column:
229 column_width = val * self.pwidth
230 pos_x = pos_x + column_width
231 self.column.append(pos_x) # position of each column
232
233 if pos_x > self.page_width * self.pwidth: # check if it fits in page
234 print "Warning, Too Wide for Page"
235 return
236
237 if self.label != []:
238 if len(self.column) -1 != len(self.label):
239 print "Column Settings Incorrect", "\nColumn Value: " + str(self.column), "\nLabel Value: " + str(self.label)
240 return
241
242 first_value = self.data[0]
243 column_total = len(first_value)
244 if column_total != len(self.column) -1:
245 print "Column Settings Incorrect", first_value, self.column
246 return
247
248 col = 0
249 for col in range(column_total):
250 try:
251 align = set_column_align[col] # check if custom column alignment
252 except:
253 align = wxALIGN_LEFT
254 self.column_align.append(align)
255
256 try:
257 colour = set_column_bgcolour[col] # check if custom column background colour
258 except:
259 colour = self.parent.column_colour
260 self.column_bgcolour.append(colour)
261
262 try:
263 colour = set_column_txtcolour[col] # check if custom column text colour
264 except:
265 colour = self.parent.text_font_colour
266 self.column_txtcolour.append(colour)
267
268 col = col + 1
269
270 def SetPointAdjust(self):
271 f = wxFont(10, wxSWISS, wxNORMAL, wxNORMAL) # setup using 10 point
272 self.DC.SetFont(f)
273 f.SetFaceName(self.text_font_name)
274 x, y = self.DC.GetTextExtent("W")
275
276 self.label_pt_space_before = self.parent.label_pt_adj_before * y/10 # extra spacing for label per point value
277 self.label_pt_space_after = self.parent.label_pt_adj_after * y/10
278
279 self.text_pt_space_before = self.parent.text_pt_adj_before * y/10 # extra spacing for row text per point value
280 self.text_pt_space_after = self.parent.text_pt_adj_after * y/10
281
282 def SetPage(self, page):
283 self.page = page
284
285 def SetColumns(self, col):
286 self.column = col
287
288 def OutCanvas(self):
289 self.AdjustValues()
290 self.SetPointAdjust()
291
292 self.y_start = self.ptop_margin + self.vertical_offset
293 self.y_end = self.parent.page_height * self.pheight - self.pbottom_margin + self.vertical_offset
294
295 self.text_font.SetFaceName(self.label_font_name)
296 self.DC.SetFont(self.label_font)
297 x, y = self.DC.GetTextExtent("W")
298 self.label_space = y
299
300 self.text_font.SetFaceName(self.text_font_name)
301 self.DC.SetFont(self.text_font)
302 x, y = self.DC.GetTextExtent("W")
303 self.space = y
304
305 if self.total_pages == None:
306 self.GetTotalPages() # total pages for display/printing
307
308 self.data_cnt = 0
309
310 cnt = 1 # get selected page
311 if cnt == self.page:
312 self.draw = TRUE
313 else:
314 self.draw = FALSE
315
316 while cnt < self.page:
317 self.OutPage()
318 cnt = cnt + 1
319
320 self.draw = TRUE
321 self.PrintHeader()
322 self.PrintFooter()
323 self.OutPage()
324
325
326 def GetTotalPages(self):
327 self.data_cnt = 0
328 self.draw = FALSE
329
330 cnt = 0
331 while 1:
332 test = self.OutPage()
333 if test == FALSE:
334 break
335 cnt = cnt + 1
336
337 self.total_pages = cnt + 1
338
339 def OutPage(self):
340 self.y = self.y_start
341 self.end_x = self.column[-1]
342
343 if self.data_cnt < len(self.data)-1: # if there data for display on the page
344 if self.label != []: # check if header defined
345 self.PrintLabel()
346 else:
347 return FALSE
348
349 for val in self.data:
350 try:
351 row_val = self.data[self.data_cnt]
352 except:
353 self.FinishDraw()
354 return FALSE
355
356 max_y = self.PrintRow(row_val, FALSE) # test to see if row will fit in remaining space
357 test = max_y + self.space
358
359 if test > self.y_end:
360 break
361
362 self.ColourRowCells(max_y-self.y+self.space) # colour the row/column
363 max_y = self.PrintRow(row_val, TRUE) # row fits - print text
364 self.DrawGridLine() # top line of cell
365 self.y = max_y + self.space
366
367 if self.y > self.y_end:
368 break
369
370 self.data_cnt = self.data_cnt + 1
371
372 self.FinishDraw()
373
374 if self.data_cnt == len(self.data): # last value in list
375 return FALSE
376
377 return TRUE
378
379
380 def PrintLabel(self):
381 self.pt_space_before = self.label_pt_space_before # set the point spacing
382 self.pt_space_after = self.label_pt_space_after
383
384 self.LabelColorRow(self.label_colour)
385 self.label_font.SetFaceName(self.label_font_name)
386 self.DC.SetFont(self.label_font)
387 self.DC.SetTextForeground(self.label_font_colour)
388
389 self.col = 0
390 max_y = 0
391 for vtxt in self.label:
392 self.region = self.column[self.col+1] - self.column[self.col]
393 self.indent = self.column[self.col]
394
395 self.align = wxALIGN_LEFT
396
397 max_out = self.OutTextRegion(vtxt, TRUE)
398 if max_out > max_y:
399 max_y = max_out
400 self.col = self.col + 1
401
402 self.DrawGridLine() # top line of label
403 self.y = max_y + self.label_space
404
405 def PrintHeader(self): # print the header array
406 if self.draw == FALSE:
407 return
408
409 for val in self.parent.header:
410 f = wxFont(val["Size"], wxSWISS, wxNORMAL, val["Attr"])
411 self.DC.SetFont(f)
412 fontname = val["Name"]
413
414 f.SetFaceName(fontname)
415 self.DC.SetTextForeground(val["Colour"])
416
417 header_indent = val["Indent"] * self.pwidth
418 self.OutTextPageWidth(val["Text"], self.pheader_margin, val["Align"], header_indent, TRUE)
419
420 def PrintFooter(self): # print the header array
421 if self.draw == FALSE:
422 return
423
424 footer_pos = self.parent.page_height * self.pheight - self.pfooter_margin + self.vertical_offset
425 for val in self.parent.footer:
426 f = wxFont(val["Size"], wxSWISS, wxNORMAL, val["Attr"])
427 self.DC.SetFont(f)
428 fontname = val["Name"]
429
430 f.SetFaceName(fontname)
431 self.DC.SetTextForeground(val["Colour"])
432
433 footer_indent = val["Indent"] * self.pwidth
434 ftype = val["Type"]
435 if ftype == "Pageof":
436 text = "Page " + str(self.page) + " of " + str(self.total_pages)
437 elif ftype == "Page":
438 text = "Page " + str(self.page)
439 elif ftype == "Num":
440 text = str(self.page)
441 else:
442 text = ""
443
444 self.OutTextPageWidth(text, footer_pos, val["Align"], footer_indent, TRUE)
445
446 def LabelColorRow(self, colour):
447 brush = wxBrush(colour, wxSOLID)
448 self.DC.SetBrush(brush)
449 height = self.label_space + self.label_pt_space_before + self.label_pt_space_after
450 self.DC.DrawRectangle(self.column[0], self.y, self.end_x-self.column[0]+1, height)
451
452 def ColourRowCells(self, height):
453 if self.draw == FALSE:
454 return
455
456 col = 0
457 for colour in self.column_bgcolour:
458 cellcolour = self.GetCellColour(self.data_cnt, col)
459 if cellcolour != None:
460 colour = cellcolour
461
462 brush = wxBrush(colour, wxSOLID)
463 self.DC.SetBrush(brush)
464 self.DC.SetPen(wxPen(wxNamedColour('WHITE'), 0))
465
466 start_x = self.column[col]
467 width = self.column[col+1] - start_x + 2
468 self.DC.DrawRectangle(start_x, self.y, width, height)
469 col = col + 1
470
471 def PrintRow(self, row_val, draw = TRUE, align = wxALIGN_LEFT):
472 self.text_font.SetFaceName(self.text_font_name)
473 self.DC.SetFont(self.text_font)
474
475 self.pt_space_before = self.text_pt_space_before # set the point spacing
476 self.pt_space_after = self.text_pt_space_after
477
478 self.col = 0
479 max_y = 0
480 self.DC.SetTextForeground(self.text_font_colour)
481 for vtxt in row_val:
482 self.region = self.column[self.col+1] - self.column[self.col]
483 self.indent = self.column[self.col]
484 self.align = self.column_align[self.col]
485
486 fcolour = self.column_txtcolour[self.col] # set font colour
487 celltext = self.GetCellText(self.data_cnt, self.col)
488 if celltext != None:
489 fcolour = celltext # override the column colour
490
491 self.DC.SetTextForeground(fcolour)
492
493 max_out = self.OutTextRegion(vtxt, draw)
494 if max_out > max_y:
495 max_y = max_out
496 self.col = self.col + 1
497 return max_y
498
499 def GetCellColour(self, row, col): # check if custom colour defined for the cell background
500 try:
501 set = self.cell_colour[row]
502 except:
503 return None
504 try:
505 colour = set[col]
506 return colour
507 except:
508 return None
509
510 def GetCellText(self, row, col): # check if custom colour defined for the cell text
511 try:
512 set = self.cell_text[row]
513 except:
514 return None
515 try:
516 colour = set[col]
517 return colour
518 except:
519 return None
520
521 def FinishDraw(self):
522 self.DrawGridLine() # draw last row line
523 self.DrawColumns() # draw all vertical lines
524
525 def DrawGridLine(self):
526 if self.draw == TRUE:
527 try:
528 size = self.row_line_size[self.data_cnt]
529 except:
530 size = self.row_def_line_size
531
532 try:
533 colour = self.row_line_colour[self.data_cnt]
534 except:
535 colour = self.row_def_line_colour
536
537 self.DC.SetPen(wxPen(colour, size))
538
539 y_out = self.y
540 # y_out = self.y + self.pt_space_before + self.pt_space_after # adjust for extra spacing
541 self.DC.DrawLine(self.column[0], y_out, self.end_x, y_out)
542
543 def DrawColumns(self):
544 if self.draw == TRUE:
545 col = 0
546 for val in self.column:
547 try:
548 size = self.column_line_size[col]
549 except:
550 size = self.column_def_line_size
551
552 try:
553 colour = self.column_line_colour[col]
554 except:
555 colour = self.column_def_line_colour
556
557 indent = val
558
559 self.DC.SetPen(wxPen(colour, size))
560 self.DC.DrawLine(indent, self.y_start, indent, self.y)
561 col = col + 1
562
563 def DrawText(self):
564 self.DoRefresh()
565
566 def DoDrawing(self, DC):
567 size = DC.GetSizeTuple()
568 self.DC = DC
569
570 DC.BeginDrawing()
571 self.DrawText()
572 DC.EndDrawing()
573
574 self.sizew = DC.MaxY()
575 self.sizeh = DC.MaxX()
576
577
578 class PrintTable:
579 def __init__(self, parentFrame=None):
580 self.data = []
581 self.set_column = []
582 self.label = []
583 self.header = []
584 self.footer = []
585
586 self.set_column_align = {}
587 self.set_column_bgcolour = {}
588 self.set_column_txtcolour = {}
589 self.set_cell_colour = {}
590 self.set_cell_text = {}
591 self.column_line_size = {}
592 self.column_line_colour = {}
593 self.row_line_size = {}
594 self.row_line_colour = {}
595
596 self.parentFrame = parentFrame
597 self.SetPreviewSize()
598
599 self.printData = wxPrintData()
600 self.scale = 1.0
601
602 self.SetParms()
603 self.SetColors()
604 self.SetFonts()
605 self.TextSpacing()
606
607 self.SetPrinterOffset()
608 self.SetHeaderValue()
609 self.SetFooterValue()
610 self.SetMargins()
611 self.SetPortrait()
612
613 def SetPreviewSize(self, position = wxPoint(0, 0), size="Full"):
614 if size == "Full":
615 screenWidth = int(wx.wxSystemSettings_GetSystemMetric(wx.wxSYS_SCREEN_X))
616 screenHeight = int(wx.wxSystemSettings_GetSystemMetric(wx.wxSYS_SCREEN_Y))
617 self.preview_frame_size = wxSize(screenWidth, screenHeight)
618 self.preview_frame_pos = position
619 else:
620 self.preview_frame_size = size
621 self.preview_frame_pos = position
622
623 def SetPaperId(self, paper):
624 self.printData.SetPaperId(paper)
625
626 def SetOrientation(self, orient):
627 self.printData.SetOrientation(orient)
628
629 def SetColors(self):
630 self.row_def_line_colour = wxNamedColour('BLACK')
631 self.row_def_line_size = 1
632
633 self.column_def_line_colour = wxNamedColour('BLACK')
634 self.column_def_line_size = 1
635 self.column_colour = wxNamedColour('WHITE')
636
637 self.label_colour = wxNamedColour('LIGHT GREY')
638
639 def SetFonts(self):
640 self.label_font_size = 12
641 self.label_font_attr = wxNORMAL
642 self.label_font_name = "Arial"
643 self.label_font_colour = wxNamedColour('BLACK')
644
645 self.text_font_size = 10
646 self.text_font_attr = wxNORMAL
647 self.text_font_name = "Arial"
648 self.text_font_colour = wxNamedColour('BLACK')
649
650 def TextSpacing(self):
651 self.label_pt_adj_before = 0 # point adjustment before and after the label text
652 self.label_pt_adj_after = 0
653
654 self.text_pt_adj_before = 0 # point adjustment before and after the row text
655 self.text_pt_adj_after = 0
656
657 def SetLabelSpacing(self, before, after): # method to set the label space adjustment
658 self.label_pt_adj_before = before
659 self.label_pt_adj_after = after
660
661 def SetRowSpacing(self, before, after): # method to set the row space adjustment
662 self.text_pt_adj_before = before
663 self.text_pt_adj_after = after
664
665 def SetPrinterOffset(self): # offset to adjust for printer
666 self.vertical_offset = -0.1
667 self.horizontal_offset = -0.1
668
669 def SetHeaderValue(self):
670 self.header_margin = 0.25
671 self.header_font_size = 12
672 self.header_font_colour = wxNamedColour('BLACK')
673 self.header_font_attr = wxBOLD
674 self.header_font_name = self.text_font_name
675 self.header_align = wxALIGN_CENTRE
676 self.header_indent = 0
677 self.header_type = None
678
679 def SetFooterValue(self):
680 self.footer_margin = 0.7
681 self.footer_font_size = 10
682 self.footer_font_colour = wxNamedColour('BLACK')
683 self.footer_font_attr = wxNORMAL
684 self.footer_font_name = self.text_font_name
685 self.footer_align = wxALIGN_CENTRE
686 self.footer_indent = 0
687 self.footer_type = "Pageof"
688
689 def SetMargins(self):
690 self.left_margin = 1.0
691 self.right_margin = 1.0 # only used if no column sizes
692
693 self.top_margin = 0.8
694 self.bottom_margin = 1.0
695 self.cell_left_margin = 0.1
696 self.cell_right_margin = 0.1
697
698 def SetPortrait(self):
699 self.printData.SetPaperId(wxPAPER_LETTER)
700 self.printData.SetOrientation(wxPORTRAIT)
701 self.page_width = 8.5
702 self.page_height = 11.0
703
704 def SetLandscape(self):
705 self.printData.SetOrientation(wxLANDSCAPE)
706 self.page_width = 11.0
707 self.page_height = 8.5
708
709 def SetParms(self):
710 self.ymax = 1
711 self.xmax = 1
712 self.page = 1
713 self.total_pg = 100
714
715 self.preview = None
716 self.page = 0
717
718 def SetColAlignment(self, col, align=wxALIGN_LEFT):
719 self.set_column_align[col] = align
720
721 def SetColBackgroundColour(self, col, colour):
722 self.set_column_bgcolour[col] = colour
723
724 def SetColTextColour(self, col, colour):
725 self.set_column_txtcolour[col] = colour
726
727 def SetCellColour(self, row, col, colour): # cell background colour
728 try:
729 set = self.set_cell_colour[row] # test if row already exists
730 try:
731 set[col] = colour # test if column already exists
732 except:
733 set = { col: colour } # create the column value
734 except:
735 set = { col: colour } # create the column value
736
737 self.set_cell_colour[row] = set # create dictionary item for colour settings
738
739 def SetCellText(self, row, col, colour): # font colour for custom cells
740 try:
741 set = self.set_cell_text[row] # test if row already exists
742 try:
743 set[col] = colour # test if column already exists
744 except:
745 set = { col: colour } # create the column value
746 except:
747 set = { col: colour } # create the column value
748
749 self.set_cell_text[row] = set # create dictionary item for colour settings
750
751 def SetColumnLineSize(self, col, size): # column line size
752 self.column_line_size[col] = size # create dictionary item for column line settings
753
754 def SetColumnLineColour(self, col, colour):
755 self.column_line_colour[col] = colour
756
757 def SetRowLineSize(self, row, size):
758 self.row_line_size[row] = size
759
760 def SetRowLineColour(self, row, colour):
761 self.row_line_colour[row] = colour
762
763 def SetHeader(self, text = "", type = None, name=None, size=None, colour = None, align = None, indent = None, attr=None):
764 set = { "Text": text }
765
766 if name == None:
767 set["Name"] = self.header_font_name
768 else:
769 set["Name"] = name
770
771 if size == None:
772 set["Size"] = self.header_font_size
773 else:
774 set["Size"] = size
775
776 if colour == None:
777 set["Colour"] = self.header_font_colour
778 else:
779 set["Colour"] = colour
780
781 if align == None:
782 set["Align"] = self.header_align
783 else:
784 set["Align"] = align
785
786 if indent == None:
787 set["Indent"] = self.header_indent
788 else:
789 set["Indent"] = indent
790
791 if attr == None:
792 set["Attr"] = self.header_font_attr
793 else:
794 set["Attr"] = attr
795
796 if type == None:
797 set["Type"] = self.header_type
798 else:
799 set["Type"] = type
800
801 self.header.append(set)
802
803 def SetFooter(self, text = "", type = None, name=None, size=None, colour = None, align = None, indent = None, attr=None):
804 set = { "Text": text }
805
806 if name == None:
807 set["Name"] = self.footer_font_name
808 else:
809 set["Name"] = name
810
811 if size == None:
812 set["Size"] = self.footer_font_size
813 else:
814 set["Size"] = size
815
816 if colour == None:
817 set["Colour"] = self.footer_font_colour
818 else:
819 set["Colour"] = colour
820
821 if align == None:
822 set["Align"] = self.footer_align
823 else:
824 set["Align"] = align
825
826 if indent == None:
827 set["Indent"] = self.footer_indent
828 else:
829 set["Indent"] = indent
830
831 if type == None:
832 set["Type"] = self.footer_type
833 else:
834 set["Type"] = type
835
836 if attr == None:
837 set["Attr"] = self.footer_font_attr
838 else:
839 set["Attr"] = attr
840
841 self.footer.append(set)
842
843 def Preview(self):
844 printout = SetPrintout(self)
845 printout2 = SetPrintout(self)
846 self.preview = wxPrintPreview(printout, printout2, self.printData)
847 if not self.preview.Ok():
848 wxMessageBox("There was a problem printing!", "Printing", wxOK)
849 return
850
851 self.preview.SetZoom(60) # initial zoom value
852
853 frame = wxPreviewFrame(self.preview, self.parentFrame, "Print preview")
854
855 frame.Initialize()
856 if self.parentFrame:
857 frame.SetPosition(self.preview_frame_pos)
858 frame.SetSize(self.preview_frame_size)
859 frame.Show(true)
860
861
862 def Print(self):
863 pdd = wxPrintDialogData()
864 pdd.SetPrintData(self.printData)
865 printer = wxPrinter(pdd)
866 printout = SetPrintout(self)
867 if not printer.Print(self.parentFrame, printout):
868 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK)
869 else:
870 self.printData = printer.GetPrintDialogData().GetPrintData()
871 printout.Destroy()
872
873 def DoDrawing(self, DC):
874 size = DC.GetSizeTuple()
875 DC.BeginDrawing()
876
877 self.text_font = wxFont(self.text_font_size, wxSWISS, self.text_font_attr, wxNORMAL)
878 self.label_font = wxFont(self.label_font_size, wxSWISS, self.label_font_attr, wxNORMAL)
879
880 table = PrintTableDraw(self, DC, size)
881 table.data = self.data
882 table.set_column = self.set_column
883 table.label = self.label
884 table.SetPage(self.page)
885
886 if self.preview is None:
887 table.SetPSize(size[0]/self.page_width, size[1]/self.page_height)
888 table.SetPTSize(size[0], size[1])
889 table.SetPreview(FALSE)
890 else:
891 if self.preview == 1:
892 table.scale = self.scale
893 table.SetPSize(size[0]/self.page_width, size[1]/self.page_height)
894 else:
895 table.SetPSize(self.pwidth, self.pheight)
896
897 table.SetPTSize(self.ptwidth, self.ptheight)
898 table.SetPreview(self.preview)
899
900 table.OutCanvas()
901 self.page_total = table.total_pages # total display pages
902
903 DC.EndDrawing()
904
905 self.ymax = DC.MaxY()
906 self.xmax = DC.MaxX()
907
908 self.sizeh = size[0]
909 self.sizew = size[1]
910
911 def GetTotalPages(self):
912 self.page_total = 100
913 return self.page_total
914
915 def HasPage(self, page):
916 if page <= self.page_total:
917 return true
918 else:
919 return false
920
921 def SetPage(self, page):
922 self.page = page
923
924 def SetPageSize(self, width, height):
925 self.pwidth, self.pheight = width, height
926
927 def SetTotalSize(self, width, height):
928 self.ptwidth, self.ptheight = width, height
929
930 def SetPreview(self, preview, scale):
931 self.preview = preview
932 self.scale = scale
933
934 def SetTotalSize(self, width, height):
935 self.ptwidth = width
936 self.ptheight = height
937
938 class PrintGrid:
939 def __init__(self, parent, grid, format = [], total_col = None, total_row = None):
940 if total_row == None:
941 total_row = grid.GetNumberRows()
942 if total_col == None:
943 total_col = grid.GetNumberCols()
944
945 self.total_row = total_row
946 self.total_col = total_col
947 self.grid = grid
948
949 data = []
950 for row in range(total_row):
951 row_val = []
952 value = grid.GetRowLabelValue(row)
953 row_val.append(value)
954
955 for col in range(total_col):
956 value = grid.GetCellValue(row, col)
957 row_val.append(value)
958 data.append(row_val)
959
960 label = [""]
961 for col in range(total_col):
962 value = grid.GetColLabelValue(col)
963 label.append(value)
964
965 self.table = PrintTable(parent)
966 self.table.cell_left_margin = 0.0
967 self.table.cell_right_margin = 0.0
968
969 self.table.label = label
970 self.table.set_column = format
971 self.table.data = data
972
973 def GetTable(self):
974 return self.table
975
976 def SetAttributes(self):
977 for row in range(self.total_row):
978 for col in range(self.total_col):
979 colour = self.grid.GetCellTextColour(row, col-1)
980 self.table.SetCellText(row, col, colour)
981
982 colour = self.grid.GetCellBackgroundColour(row, col-1)
983 self.table.SetCellColour(row, col, colour)
984
985 def Preview(self):
986 self.table.Preview()
987
988 def Print(self):
989 self.table.Print()
990
991
992 class SetPrintout(wxPrintout):
993 def __init__(self, canvas):
994 wxPrintout.__init__(self)
995 self.canvas = canvas
996 self.end_pg = 1000
997
998 def OnBeginDocument(self, start, end):
999 return self.base_OnBeginDocument(start, end)
1000
1001 def OnEndDocument(self):
1002 self.base_OnEndDocument()
1003
1004 def HasPage(self, page):
1005 try:
1006 end = self.canvas.HasPage(page)
1007 return end
1008 except:
1009 return true
1010
1011 def GetPageInfo(self):
1012 try:
1013 self.end_pg = self.canvas.GetTotalPages()
1014 except:
1015 pass
1016
1017 end_pg = self.end_pg
1018 str_pg = 1
1019 return (str_pg, end_pg, str_pg, end_pg)
1020
1021 def OnPreparePrinting(self):
1022 self.base_OnPreparePrinting()
1023
1024 def OnBeginPrinting(self):
1025 dc = self.GetDC()
1026
1027 self.preview = self.IsPreview()
1028 if (self.preview):
1029 self.pixelsPerInch = self.GetPPIScreen()
1030 else:
1031 self.pixelsPerInch = self.GetPPIPrinter()
1032
1033 (w, h) = dc.GetSizeTuple()
1034 scaleX = float(w) / 1000
1035 scaleY = float(h) / 1000
1036 self.printUserScale = min(scaleX, scaleY)
1037
1038 self.base_OnBeginPrinting()
1039
1040 def GetSize(self):
1041 self.psizew, self.psizeh = self.GetPPIPrinter()
1042 return self.psizew, self.psizeh
1043
1044 def GetTotalSize(self):
1045 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
1046 return self.ptsizew, self.ptsizeh
1047
1048 def OnPrintPage(self, page):
1049 dc = self.GetDC()
1050 (w, h) = dc.GetSizeTuple()
1051 scaleX = float(w) / 1000
1052 scaleY = float(h) / 1000
1053 self.printUserScale = min(scaleX, scaleY)
1054 dc.SetUserScale(self.printUserScale, self.printUserScale)
1055
1056 self.preview = self.IsPreview()
1057
1058 self.canvas.SetPreview(self.preview, self.printUserScale)
1059 self.canvas.SetPage(page)
1060
1061 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
1062 self.canvas.SetTotalSize(self.ptsizew, self.ptsizeh)
1063
1064 self.psizew, self.psizeh = self.GetPPIPrinter()
1065 self.canvas.SetPageSize(self.psizew, self.psizeh)
1066
1067 self.canvas.DoDrawing(dc)
1068 return true
1069
1070
1071
1072
1073
1074