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