1 #----------------------------------------------------------------------------
3 # Purpose: preview and printing class -> table/grid printing
5 # Author: Lorne White (email: lorne.white@telusplanet.net)
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
14 # fixed bug for string wider than print region
15 # add index to data list after parsing total pages for paging
16 #----------------------------------------------------------------------------
20 from wxPython
.wx
import *
24 def SetPrintFont(self
, font
): # set the DC font parameters
37 fcolour
= self
.GetFontColour(font
)
38 self
.DC
.SetTextForeground(fcolour
)
40 setfont
= wxFont(font
["Size"], wxSWISS
, set_style
, weight
, underline
)
41 setfont
.SetFaceName(font
["Name"])
42 self
.DC
.SetFont(setfont
)
44 def GetFontColour(self
, font
):
45 fcolour
= font
["Colour"]
46 return wxColour(fcolour
[0], fcolour
[1], fcolour
[2])
48 def OutTextRegion(self
, textout
, txtdraw
= True):
49 textlines
= textout
.split('\n')
50 y
= copy
.copy(self
.y
) + self
.pt_space_before
51 for text
in textlines
:
54 vout
, remain
= self
.SetFlow(text
, self
.region
)
55 if self
.draw
== True and txtdraw
== True:
56 test_out
= self
.TestFull(vout
)
57 if self
.align
== wxALIGN_LEFT
:
58 self
.DC
.DrawText(test_out
, self
.indent
+self
.pcell_left_margin
, y
)
60 elif self
.align
== wxALIGN_CENTRE
:
61 diff
= self
.GetCellDiff(test_out
, self
.region
)
62 self
.DC
.DrawText(test_out
, self
.indent
+diff
/2, y
)
64 elif self
.align
== wxALIGN_RIGHT
:
65 diff
= self
.GetCellDiff(test_out
, self
.region
)
66 self
.DC
.DrawText(test_out
, self
.indent
+diff
, y
)
69 self
.DC
.DrawText(test_out
, self
.indent
+self
.pcell_left_margin
, y
)
72 return y
- self
.space
+ self
.pt_space_after
74 def GetCellDiff(self
, text
, width
): # get the remaining cell size for adjustment
75 w
, h
= self
.DC
.GetTextExtent(text
)
81 def TestFull(self
, text_test
):
82 w
, h
= self
.DC
.GetTextExtent(text_test
)
83 if w
> self
.region
: # trouble fitting into cell
84 return self
.SetChar(text_test
, self
.region
) # fit the text to the cell size
88 def SetFlow(self
, ln_text
, width
):
89 width
= width
- self
.pcell_right_margin
91 split
= ln_text
.split()
96 w
, h
= self
.DC
.GetTextExtent(" " + split
[0])
104 bword
= " " + word
# blank + word
107 w
, h
= self
.DC
.GetTextExtent(text
+ bword
)
112 remain
= ' '.join(split
[cnt
:])
116 remain
= ' '.join(split
[cnt
:])
120 def SetChar(self
, ln_text
, width
): # truncate string to fit into width
121 width
= width
- self
.pcell_right_margin
- self
.pcell_left_margin
124 w
, h
= self
.DC
.GetTextExtent(text
+ val
)
127 return text
# fitted text value
131 def OutTextPageWidth(self
, textout
, y_out
, align
, indent
, txtdraw
= True):
132 textlines
= textout
.split('\n')
135 pagew
= self
.parent
.page_width
* self
.pwidth
# full page width
136 w
, h
= self
.DC
.GetTextExtent(textout
)
139 for text
in textlines
:
142 vout
, remain
= self
.SetFlow(text
, pagew
)
143 if self
.draw
== True and txtdraw
== True:
145 if align
== wxALIGN_LEFT
:
146 self
.DC
.DrawText(test_out
, indent
, y
)
148 elif align
== wxALIGN_CENTRE
:
149 diff
= self
.GetCellDiff(test_out
, pagew
)
150 self
.DC
.DrawText(test_out
, indent
+diff
/2, y
)
152 elif align
== wxALIGN_RIGHT
:
153 diff
= self
.GetCellDiff(test_out
, pagew
)
154 self
.DC
.DrawText(test_out
, indent
+diff
, y
)
157 self
.DC
.DrawText(test_out
, indent
, y_out
)
163 date
, time
= self
.GetNow()
166 def GetDateTime(self
):
167 date
, time
= self
.GetNow()
168 return date
+ ' ' + time
171 full
= str(wxDateTime_Now()) # get the current date and time in print format
177 def SetPreview(self
, preview
):
178 self
.preview
= preview
180 def SetPSize(self
, width
, height
):
181 self
.pwidth
= width
/self
.scale
182 self
.pheight
= height
/self
.scale
184 def SetScale(self
, scale
):
187 def SetPTSize(self
, width
, height
):
189 self
.ptheight
= height
198 class PrintTableDraw(wxScrolledWindow
, PrintBase
):
199 def __init__(self
, parent
, DC
, size
):
202 self
.scale
= parent
.scale
204 self
.height
= size
[1]
207 def SetDefaults(self
):
209 self
.total_pages
= None
211 self
.page_width
= self
.parent
.page_width
212 self
.page_height
= self
.parent
.page_height
214 self
.left_margin
= self
.parent
.left_margin
215 self
.right_margin
= self
.parent
.right_margin
217 self
.top_margin
= self
.parent
.top_margin
218 self
.bottom_margin
= self
.parent
.bottom_margin
219 self
.cell_left_margin
= self
.parent
.cell_left_margin
220 self
.cell_right_margin
= self
.parent
.cell_right_margin
222 self
.label_colour
= self
.parent
.label_colour
224 self
.row_line_colour
= self
.parent
.row_line_colour
225 self
.row_line_size
= self
.parent
.row_line_size
227 self
.row_def_line_colour
= self
.parent
.row_def_line_colour
228 self
.row_def_line_size
= self
.parent
.row_def_line_size
230 self
.column_line_colour
= self
.parent
.column_line_colour
231 self
.column_line_size
= self
.parent
.column_line_size
233 self
.column_def_line_size
= self
.parent
.column_def_line_size
234 self
.column_def_line_colour
= self
.parent
.column_def_line_colour
236 self
.text_font
= self
.parent
.text_font
238 self
.label_font
= self
.parent
.label_font
240 def AdjustValues(self
):
241 self
.vertical_offset
= self
.pheight
* self
.parent
.vertical_offset
242 self
.horizontal_offset
= self
.pheight
* self
.parent
.horizontal_offset
244 self
.pcell_left_margin
= self
.pwidth
* self
.cell_left_margin
245 self
.pcell_right_margin
= self
.pwidth
* self
.cell_right_margin
246 self
.ptop_margin
= self
.pheight
* self
.top_margin
247 self
.pbottom_margin
= self
.pheight
* self
.bottom_margin
249 self
.pheader_margin
= self
.pheight
* self
.parent
.header_margin
250 self
.pfooter_margin
= self
.pheight
* self
.parent
.footer_margin
252 self
.cell_colour
= self
.parent
.set_cell_colour
253 self
.cell_text
= self
.parent
.set_cell_text
256 self
.column_align
= []
257 self
.column_bgcolour
= []
258 self
.column_txtcolour
= []
260 set_column_align
= self
.parent
.set_column_align
261 set_column_bgcolour
= self
.parent
.set_column_bgcolour
262 set_column_txtcolour
= self
.parent
.set_column_txtcolour
264 pos_x
= self
.left_margin
* self
.pwidth
+ self
.horizontal_offset
# left margin
265 self
.column
.append(pos_x
)
267 if self
.set_column
== []:
268 table_width
= self
.page_width
- self
.left_margin
- self
.right_margin
269 width
= table_width
/(len(self
.label
))
270 for val
in self
.label
:
271 column_width
= width
* self
.pwidth
272 pos_x
= pos_x
+ column_width
273 self
.column
.append(pos_x
) # position of each column
275 for val
in self
.set_column
:
276 column_width
= val
* self
.pwidth
277 pos_x
= pos_x
+ column_width
278 self
.column
.append(pos_x
) # position of each column
280 if pos_x
> self
.page_width
* self
.pwidth
: # check if it fits in page
281 print "Warning, Too Wide for Page"
285 if len(self
.column
) -1 != len(self
.label
):
286 print "Column Settings Incorrect", "\nColumn Value: " + str(self
.column
), "\nLabel Value: " + str(self
.label
)
289 first_value
= self
.data
[0]
290 column_total
= len(first_value
)
291 if column_total
!= len(self
.column
) -1:
292 print "Column Settings Incorrect", first_value
, self
.column
296 for col
in range(column_total
):
298 align
= set_column_align
[col
] # check if custom column alignment
301 self
.column_align
.append(align
)
304 colour
= set_column_bgcolour
[col
] # check if custom column background colour
306 colour
= self
.parent
.column_colour
307 self
.column_bgcolour
.append(colour
)
310 colour
= set_column_txtcolour
[col
] # check if custom column text colour
312 colour
= self
.GetFontColour(self
.parent
.text_font
)
314 self
.column_txtcolour
.append(colour
)
318 def SetPointAdjust(self
):
319 f
= wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
) # setup using 10 point
321 f
.SetFaceName(self
.text_font
["Name"])
322 x
, y
= self
.DC
.GetTextExtent("W")
324 self
.label_pt_space_before
= self
.parent
.label_pt_adj_before
* y
/10 # extra spacing for label per point value
325 self
.label_pt_space_after
= self
.parent
.label_pt_adj_after
* y
/10
327 self
.text_pt_space_before
= self
.parent
.text_pt_adj_before
* y
/10 # extra spacing for row text per point value
328 self
.text_pt_space_after
= self
.parent
.text_pt_adj_after
* y
/10
330 def SetPage(self
, page
):
333 def SetColumns(self
, col
):
338 self
.SetPointAdjust()
340 self
.y_start
= self
.ptop_margin
+ self
.vertical_offset
341 self
.y_end
= self
.parent
.page_height
* self
.pheight
- self
.pbottom_margin
+ self
.vertical_offset
343 self
.SetPrintFont(self
.label_font
)
345 x
, y
= self
.DC
.GetTextExtent("W")
348 self
.SetPrintFont(self
.text_font
)
350 x
, y
= self
.DC
.GetTextExtent("W")
353 if self
.total_pages
is None:
354 self
.GetTotalPages() # total pages for display/printing
356 self
.data_cnt
= self
.page_index
[self
.page
-1]
363 def GetTotalPages(self
):
366 self
.page_index
= [0]
370 test
= self
.OutPage()
371 self
.page_index
.append(self
.data_cnt
)
376 self
.total_pages
= cnt
+ 1
379 self
.y
= self
.y_start
380 self
.end_x
= self
.column
[-1]
382 if self
.data_cnt
< len(self
.data
): # if there data for display on the page
383 if self
.label
!= []: # check if header defined
388 for val
in self
.data
:
390 row_val
= self
.data
[self
.data_cnt
]
395 max_y
= self
.PrintRow(row_val
, False) # test to see if row will fit in remaining space
396 test
= max_y
+ self
.space
398 if test
> self
.y_end
:
401 self
.ColourRowCells(max_y
-self
.y
+self
.space
) # colour the row/column
402 max_y
= self
.PrintRow(row_val
, True) # row fits - print text
403 self
.DrawGridLine() # top line of cell
404 self
.y
= max_y
+ self
.space
406 if self
.y
> self
.y_end
:
409 self
.data_cnt
= self
.data_cnt
+ 1
413 if self
.data_cnt
== len(self
.data
): # last value in list
419 def PrintLabel(self
):
420 self
.pt_space_before
= self
.label_pt_space_before
# set the point spacing
421 self
.pt_space_after
= self
.label_pt_space_after
423 self
.LabelColorRow(self
.label_colour
)
424 self
.SetPrintFont(self
.label_font
)
428 for vtxt
in self
.label
:
429 self
.region
= self
.column
[self
.col
+1] - self
.column
[self
.col
]
430 self
.indent
= self
.column
[self
.col
]
432 self
.align
= wxALIGN_LEFT
434 max_out
= self
.OutTextRegion(vtxt
, True)
437 self
.col
= self
.col
+ 1
439 self
.DrawGridLine() # top line of label
440 self
.y
= max_y
+ self
.label_space
442 def PrintHeader(self
): # print the header array
443 if self
.draw
== False:
446 for val
in self
.parent
.header
:
447 self
.SetPrintFont(val
["Font"])
449 header_indent
= val
["Indent"] * self
.pwidth
454 addtext
= self
.GetDate()
455 elif htype
== "Date & Time":
456 addtext
= self
.GetDateTime()
460 self
.OutTextPageWidth(text
+addtext
, self
.pheader_margin
, val
["Align"], header_indent
, True)
462 def PrintFooter(self
): # print the header array
463 if self
.draw
== False:
466 footer_pos
= self
.parent
.page_height
* self
.pheight
- self
.pfooter_margin
+ self
.vertical_offset
467 for val
in self
.parent
.footer
:
468 self
.SetPrintFont(val
["Font"])
470 footer_indent
= val
["Indent"] * self
.pwidth
474 if ftype
== "Pageof":
475 addtext
= "Page " + str(self
.page
) + " of " + str(self
.total_pages
)
476 elif ftype
== "Page":
477 addtext
= "Page " + str(self
.page
)
479 addtext
= str(self
.page
)
480 elif ftype
== "Date":
481 addtext
= self
.GetDate()
482 elif ftype
== "Date & Time":
483 addtext
= self
.GetDateTime()
487 self
.OutTextPageWidth(text
+addtext
, footer_pos
, val
["Align"], footer_indent
, True)
490 def LabelColorRow(self
, colour
):
491 brush
= wxBrush(colour
, wxSOLID
)
492 self
.DC
.SetBrush(brush
)
493 height
= self
.label_space
+ self
.label_pt_space_before
+ self
.label_pt_space_after
494 self
.DC
.DrawRectangle(self
.column
[0], self
.y
, self
.end_x
-self
.column
[0]+1, height
)
496 def ColourRowCells(self
, height
):
497 if self
.draw
== False:
501 for colour
in self
.column_bgcolour
:
502 cellcolour
= self
.GetCellColour(self
.data_cnt
, col
)
503 if cellcolour
is not None:
506 brush
= wxBrush(colour
, wxSOLID
)
507 self
.DC
.SetBrush(brush
)
508 self
.DC
.SetPen(wxPen(wxNamedColour('WHITE'), 0))
510 start_x
= self
.column
[col
]
511 width
= self
.column
[col
+1] - start_x
+ 2
512 self
.DC
.DrawRectangle(start_x
, self
.y
, width
, height
)
515 def PrintRow(self
, row_val
, draw
= True, align
= wxALIGN_LEFT
):
516 self
.SetPrintFont(self
.text_font
)
518 self
.pt_space_before
= self
.text_pt_space_before
# set the point spacing
519 self
.pt_space_after
= self
.text_pt_space_after
524 self
.region
= self
.column
[self
.col
+1] - self
.column
[self
.col
]
525 self
.indent
= self
.column
[self
.col
]
526 self
.align
= self
.column_align
[self
.col
]
528 fcolour
= self
.column_txtcolour
[self
.col
] # set font colour
529 celltext
= self
.GetCellText(self
.data_cnt
, self
.col
)
530 if celltext
is not None:
531 fcolour
= celltext
# override the column colour
533 self
.DC
.SetTextForeground(fcolour
)
535 max_out
= self
.OutTextRegion(vtxt
, draw
)
538 self
.col
= self
.col
+ 1
541 def GetCellColour(self
, row
, col
): # check if custom colour defined for the cell background
543 set = self
.cell_colour
[row
]
552 def GetCellText(self
, row
, col
): # check if custom colour defined for the cell text
554 set = self
.cell_text
[row
]
563 def FinishDraw(self
):
564 self
.DrawGridLine() # draw last row line
565 self
.DrawColumns() # draw all vertical lines
567 def DrawGridLine(self
):
568 if self
.draw
== True:
570 size
= self
.row_line_size
[self
.data_cnt
]
572 size
= self
.row_def_line_size
575 colour
= self
.row_line_colour
[self
.data_cnt
]
577 colour
= self
.row_def_line_colour
579 self
.DC
.SetPen(wxPen(colour
, size
))
582 # y_out = self.y + self.pt_space_before + self.pt_space_after # adjust for extra spacing
583 self
.DC
.DrawLine(self
.column
[0], y_out
, self
.end_x
, y_out
)
585 def DrawColumns(self
):
586 if self
.draw
== True:
588 for val
in self
.column
:
590 size
= self
.column_line_size
[col
]
592 size
= self
.column_def_line_size
595 colour
= self
.column_line_colour
[col
]
597 colour
= self
.column_def_line_colour
601 self
.DC
.SetPen(wxPen(colour
, size
))
602 self
.DC
.DrawLine(indent
, self
.y_start
, indent
, self
.y
)
608 def DoDrawing(self
, DC
):
609 size
= DC
.GetSizeTuple()
616 self
.sizew
= DC
.MaxY()
617 self
.sizeh
= DC
.MaxX()
621 def __init__(self
, parentFrame
=None):
628 self
.set_column_align
= {}
629 self
.set_column_bgcolour
= {}
630 self
.set_column_txtcolour
= {}
631 self
.set_cell_colour
= {}
632 self
.set_cell_text
= {}
633 self
.column_line_size
= {}
634 self
.column_line_colour
= {}
635 self
.row_line_size
= {}
636 self
.row_line_colour
= {}
638 self
.parentFrame
= parentFrame
639 self
.SetPreviewSize()
641 self
.printData
= wxPrintData()
649 self
.SetPrinterOffset()
650 self
.SetHeaderValue()
651 self
.SetFooterValue()
655 def SetPreviewSize(self
, position
= wxPoint(0, 0), size
="Full"):
657 r
= wxGetClientDisplayRect()
658 self
.preview_frame_size
= wxSize(r
.width
, r
.height
)
659 self
.preview_frame_pos
= wxPoint(r
.x
, r
.y
)
661 self
.preview_frame_size
= size
662 self
.preview_frame_pos
= position
664 def SetPaperId(self
, paper
):
665 self
.printData
.SetPaperId(paper
)
667 def SetOrientation(self
, orient
):
668 self
.printData
.SetOrientation(orient
)
671 self
.row_def_line_colour
= wxNamedColour('BLACK')
672 self
.row_def_line_size
= 1
674 self
.column_def_line_colour
= wxNamedColour('BLACK')
675 self
.column_def_line_size
= 1
676 self
.column_colour
= wxNamedColour('WHITE')
678 self
.label_colour
= wxNamedColour('LIGHT GREY')
681 self
.label_font
= { "Name": self.default_font_name, "Size": 12, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
682 self
.text_font
= { "Name": self.default_font_name, "Size": 10, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
684 def TextSpacing(self
):
685 self
.label_pt_adj_before
= 0 # point adjustment before and after the label text
686 self
.label_pt_adj_after
= 0
688 self
.text_pt_adj_before
= 0 # point adjustment before and after the row text
689 self
.text_pt_adj_after
= 0
691 def SetLabelSpacing(self
, before
, after
): # method to set the label space adjustment
692 self
.label_pt_adj_before
= before
693 self
.label_pt_adj_after
= after
695 def SetRowSpacing(self
, before
, after
): # method to set the row space adjustment
696 self
.text_pt_adj_before
= before
697 self
.text_pt_adj_after
= after
699 def SetPrinterOffset(self
): # offset to adjust for printer
700 self
.vertical_offset
= -0.1
701 self
.horizontal_offset
= -0.1
703 def SetHeaderValue(self
):
704 self
.header_margin
= 0.25
705 self
.header_font
= { "Name": self.default_font_name, "Size": 11, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
706 self
.header_align
= wxALIGN_CENTRE
707 self
.header_indent
= 0
708 self
.header_type
= "Text"
710 def SetFooterValue(self
):
711 self
.footer_margin
= 0.7
712 self
.footer_font
= { "Name": self.default_font_name, "Size": 11, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
713 self
.footer_align
= wxALIGN_CENTRE
714 self
.footer_indent
= 0
715 self
.footer_type
= "Pageof"
717 def SetMargins(self
):
718 self
.left_margin
= 1.0
719 self
.right_margin
= 1.0 # only used if no column sizes
721 self
.top_margin
= 0.8
722 self
.bottom_margin
= 1.0
723 self
.cell_left_margin
= 0.1
724 self
.cell_right_margin
= 0.1
726 def SetPortrait(self
):
727 self
.printData
.SetPaperId(wxPAPER_LETTER
)
728 self
.printData
.SetOrientation(wxPORTRAIT
)
729 self
.page_width
= 8.5
730 self
.page_height
= 11.0
732 def SetLandscape(self
):
733 self
.printData
.SetOrientation(wxLANDSCAPE
)
734 self
.page_width
= 11.0
735 self
.page_height
= 8.5
746 self
.default_font_name
= "Arial"
747 self
.default_font
= { "Name": self.default_font_name, "Size": 10, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
749 def SetColAlignment(self
, col
, align
=wxALIGN_LEFT
):
750 self
.set_column_align
[col
] = align
752 def SetColBackgroundColour(self
, col
, colour
):
753 self
.set_column_bgcolour
[col
] = colour
755 def SetColTextColour(self
, col
, colour
):
756 self
.set_column_txtcolour
[col
] = colour
758 def SetCellColour(self
, row
, col
, colour
): # cell background colour
760 set = self
.set_cell_colour
[row
] # test if row already exists
762 set[col
] = colour
# test if column already exists
764 set = { col: colour }
# create the column value
766 set = { col: colour }
# create the column value
768 self
.set_cell_colour
[row
] = set # create dictionary item for colour settings
770 def SetCellText(self
, row
, col
, colour
): # font colour for custom cells
772 set = self
.set_cell_text
[row
] # test if row already exists
774 set[col
] = colour
# test if column already exists
776 set = { col: colour }
# create the column value
778 set = { col: colour }
# create the column value
780 self
.set_cell_text
[row
] = set # create dictionary item for colour settings
782 def SetColumnLineSize(self
, col
, size
): # column line size
783 self
.column_line_size
[col
] = size
# create dictionary item for column line settings
785 def SetColumnLineColour(self
, col
, colour
):
786 self
.column_line_colour
[col
] = colour
788 def SetRowLineSize(self
, row
, size
):
789 self
.row_line_size
[row
] = size
791 def SetRowLineColour(self
, row
, colour
):
792 self
.row_line_colour
[row
] = colour
794 def GetColour(self
, colour
): # returns colours based from wxColour value
797 green
= colour
.Green()
798 return [red
, green
, blue
]
800 def SetHeader(self
, text
= "", type = "Text", font
=None, align
= None, indent
= None, colour
= None, size
= None):
801 set = { "Text": text }
804 set["Font"] = copy
.copy(self
.default_font
)
808 if colour
is not None:
809 setfont
= set["Font"]
810 setfont
["Colour"] = self
.GetColour(colour
)
813 setfont
= set["Font"]
814 setfont
["Size"] = size
817 set["Align"] = self
.header_align
822 set["Indent"] = self
.header_indent
824 set["Indent"] = indent
827 set["Type"] = self
.header_type
831 self
.header
.append(set)
833 def SetFooter(self
, text
= "", type = None, font
=None, align
= None, indent
= None, colour
= None, size
= None):
834 set = { "Text": text }
837 set["Font"] = copy
.copy(self
.default_font
)
841 if colour
is not None:
842 setfont
= set["Font"]
843 setfont
["Colour"] = self
.GetColour(colour
)
846 setfont
= set["Font"]
847 setfont
["Size"] = size
850 set["Align"] = self
.footer_align
855 set["Indent"] = self
.footer_indent
857 set["Indent"] = indent
860 set["Type"] = self
.footer_type
864 self
.footer
.append(set)
867 printout
= SetPrintout(self
)
868 printout2
= SetPrintout(self
)
869 self
.preview
= wxPrintPreview(printout
, printout2
, self
.printData
)
870 if not self
.preview
.Ok():
871 wxMessageBox("There was a problem printing!", "Printing", wxOK
)
874 self
.preview
.SetZoom(60) # initial zoom value
876 frame
= wxPreviewFrame(self
.preview
, self
.parentFrame
, "Print preview")
880 frame
.SetPosition(self
.preview_frame_pos
)
881 frame
.SetSize(self
.preview_frame_size
)
885 pdd
= wxPrintDialogData()
886 pdd
.SetPrintData(self
.printData
)
887 printer
= wxPrinter(pdd
)
888 printout
= SetPrintout(self
)
889 if not printer
.Print(self
.parentFrame
, printout
):
890 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK
)
892 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
895 def DoDrawing(self
, DC
):
896 size
= DC
.GetSizeTuple()
899 table
= PrintTableDraw(self
, DC
, size
)
900 table
.data
= self
.data
901 table
.set_column
= self
.set_column
902 table
.label
= self
.label
903 table
.SetPage(self
.page
)
905 if self
.preview
is None:
906 table
.SetPSize(size
[0]/self
.page_width
, size
[1]/self
.page_height
)
907 table
.SetPTSize(size
[0], size
[1])
908 table
.SetPreview(False)
910 if self
.preview
== 1:
911 table
.scale
= self
.scale
912 table
.SetPSize(size
[0]/self
.page_width
, size
[1]/self
.page_height
)
914 table
.SetPSize(self
.pwidth
, self
.pheight
)
916 table
.SetPTSize(self
.ptwidth
, self
.ptheight
)
917 table
.SetPreview(self
.preview
)
920 self
.page_total
= table
.total_pages
# total display pages
924 self
.ymax
= DC
.MaxY()
925 self
.xmax
= DC
.MaxX()
930 def GetTotalPages(self
):
931 self
.page_total
= 100
932 return self
.page_total
934 def HasPage(self
, page
):
935 if page
<= self
.page_total
:
940 def SetPage(self
, page
):
943 def SetPageSize(self
, width
, height
):
944 self
.pwidth
, self
.pheight
= width
, height
946 def SetTotalSize(self
, width
, height
):
947 self
.ptwidth
, self
.ptheight
= width
, height
949 def SetPreview(self
, preview
, scale
):
950 self
.preview
= preview
953 def SetTotalSize(self
, width
, height
):
955 self
.ptheight
= height
958 def __init__(self
, parent
, grid
, format
= [], total_col
= None, total_row
= None):
959 if total_row
is None:
960 total_row
= grid
.GetNumberRows()
961 if total_col
is None:
962 total_col
= grid
.GetNumberCols()
964 self
.total_row
= total_row
965 self
.total_col
= total_col
969 for row
in range(total_row
):
971 value
= grid
.GetRowLabelValue(row
)
972 row_val
.append(value
)
974 for col
in range(total_col
):
975 value
= grid
.GetCellValue(row
, col
)
976 row_val
.append(value
)
980 for col
in range(total_col
):
981 value
= grid
.GetColLabelValue(col
)
984 self
.table
= PrintTable(parent
)
985 self
.table
.cell_left_margin
= 0.0
986 self
.table
.cell_right_margin
= 0.0
988 self
.table
.label
= label
989 self
.table
.set_column
= format
990 self
.table
.data
= data
995 def SetAttributes(self
):
996 for row
in range(self
.total_row
):
997 for col
in range(self
.total_col
):
998 colour
= self
.grid
.GetCellTextColour(row
, col
-1)
999 self
.table
.SetCellText(row
, col
, colour
)
1001 colour
= self
.grid
.GetCellBackgroundColour(row
, col
-1)
1002 self
.table
.SetCellColour(row
, col
, colour
)
1005 self
.table
.Preview()
1011 class SetPrintout(wxPrintout
):
1012 def __init__(self
, canvas
):
1013 wxPrintout
.__init
__(self
)
1014 self
.canvas
= canvas
1017 def OnBeginDocument(self
, start
, end
):
1018 return self
.base_OnBeginDocument(start
, end
)
1020 def OnEndDocument(self
):
1021 self
.base_OnEndDocument()
1023 def HasPage(self
, page
):
1025 end
= self
.canvas
.HasPage(page
)
1030 def GetPageInfo(self
):
1032 self
.end_pg
= self
.canvas
.GetTotalPages()
1036 end_pg
= self
.end_pg
1038 return (str_pg
, end_pg
, str_pg
, end_pg
)
1040 def OnPreparePrinting(self
):
1041 self
.base_OnPreparePrinting()
1043 def OnBeginPrinting(self
):
1046 self
.preview
= self
.IsPreview()
1048 self
.pixelsPerInch
= self
.GetPPIScreen()
1050 self
.pixelsPerInch
= self
.GetPPIPrinter()
1052 (w
, h
) = dc
.GetSizeTuple()
1053 scaleX
= float(w
) / 1000
1054 scaleY
= float(h
) / 1000
1055 self
.printUserScale
= min(scaleX
, scaleY
)
1057 self
.base_OnBeginPrinting()
1060 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
1061 return self
.psizew
, self
.psizeh
1063 def GetTotalSize(self
):
1064 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
1065 return self
.ptsizew
, self
.ptsizeh
1067 def OnPrintPage(self
, page
):
1069 (w
, h
) = dc
.GetSizeTuple()
1070 scaleX
= float(w
) / 1000
1071 scaleY
= float(h
) / 1000
1072 self
.printUserScale
= min(scaleX
, scaleY
)
1073 dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
)
1075 self
.preview
= self
.IsPreview()
1077 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
1078 self
.canvas
.SetPage(page
)
1080 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
1081 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
1083 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
1084 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
1086 self
.canvas
.DoDrawing(dc
)