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 #---------------------------------------------------------------------------- 
  17 # 12/10/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
  19 # o 2.5 compatability update. 
  29     def SetPrintFont(self
, font
):      # set the DC font parameters 
  42         fcolour 
= self
.GetFontColour(font
) 
  43         self
.DC
.SetTextForeground(fcolour
) 
  45         setfont 
= wx
.Font(font
["Size"], wx
.SWISS
, set_style
, weight
, underline
) 
  46         setfont
.SetFaceName(font
["Name"]) 
  47         self
.DC
.SetFont(setfont
) 
  49     def GetFontColour(self
, font
): 
  50         fcolour 
= font
["Colour"] 
  51         return wx
.Colour(fcolour
[0], fcolour
[1], fcolour
[2]) 
  53     def OutTextRegion(self
, textout
, txtdraw 
= True): 
  54         textlines 
= textout
.split('\n') 
  55         y 
= copy
.copy(self
.y
) + self
.pt_space_before
 
  56         for text 
in textlines
: 
  59                 vout
, remain 
= self
.SetFlow(text
, self
.region
) 
  60                 if self
.draw 
== True and txtdraw 
== True: 
  61                     test_out 
= self
.TestFull(vout
) 
  62                     if self
.align 
== wx
.ALIGN_LEFT
: 
  63                         self
.DC
.DrawText(test_out
, (self
.indent
+self
.pcell_left_margin
, y
)) 
  65                     elif self
.align 
== wx
.ALIGN_CENTRE
: 
  66                         diff 
= self
.GetCellDiff(test_out
, self
.region
) 
  67                         self
.DC
.DrawText(test_out
, (self
.indent
+diff
/2, y
)) 
  69                     elif self
.align 
== wx
.ALIGN_RIGHT
: 
  70                         diff 
= self
.GetCellDiff(test_out
, self
.region
) 
  71                         self
.DC
.DrawText(test_out
, (self
.indent
+diff
, y
)) 
  74                         self
.DC
.DrawText(test_out
, (self
.indent
+self
.pcell_left_margin
, y
)) 
  77         return y 
- self
.space 
+ self
.pt_space_after
 
  79     def GetCellDiff(self
, text
, width
):      # get the remaining cell size for adjustment 
  80         w
, h 
= self
.DC
.GetTextExtent(text
) 
  86     def TestFull(self
, text_test
): 
  87         w
, h 
= self
.DC
.GetTextExtent(text_test
) 
  88         if w 
> self
.region
:     # trouble fitting into cell 
  89             return self
.SetChar(text_test
, self
.region
)     # fit the text to the cell size 
  93     def SetFlow(self
, ln_text
, width
): 
  94         width 
= width 
- self
.pcell_right_margin
 
  96         split 
= ln_text
.split() 
 101             w
, h 
= self
.DC
.GetTextExtent(" " + split
[0]) 
 109             bword 
= " " + word  
# blank + word 
 112             w
, h 
= self
.DC
.GetTextExtent(text 
+ bword
) 
 117                 remain 
= ' '.join(split
[cnt
:]) 
 121         remain 
= ' '.join(split
[cnt
:]) 
 125     def SetChar(self
, ln_text
, width
):  # truncate string to fit into width 
 126         width 
= width 
- self
.pcell_right_margin 
- self
.pcell_left_margin
 
 129             w
, h 
= self
.DC
.GetTextExtent(text 
+ val
) 
 132                 return text     
# fitted text value 
 136     def OutTextPageWidth(self
, textout
, y_out
, align
, indent
, txtdraw 
= True): 
 137         textlines 
= textout
.split('\n') 
 140         pagew 
= self
.parent
.page_width 
* self
.pwidth        
# full page width 
 141         w
, h 
= self
.DC
.GetTextExtent(textout
) 
 144         for text 
in textlines
: 
 147                 vout
, remain 
= self
.SetFlow(text
, pagew
) 
 148                 if self
.draw 
== True and txtdraw 
== True: 
 150                     if align 
== wx
.ALIGN_LEFT
: 
 151                         self
.DC
.DrawText(test_out
, (indent
, y
)) 
 153                     elif align 
== wx
.ALIGN_CENTRE
: 
 154                         diff 
= self
.GetCellDiff(test_out
, pagew
) 
 155                         self
.DC
.DrawText(test_out
, (indent
+diff
/2, y
)) 
 157                     elif align 
== wx
.ALIGN_RIGHT
: 
 158                         diff 
= self
.GetCellDiff(test_out
, pagew
) 
 159                         self
.DC
.DrawText(test_out
, (indent
+diff
, y
)) 
 162                         self
.DC
.DrawText(test_out
, (indent
, y_out
)) 
 168         date
, time 
= self
.GetNow() 
 171     def GetDateTime(self
): 
 172         date
, time 
= self
.GetNow() 
 173         return date 
+ ' ' + time
 
 176         full 
= str(wx
.DateTime_Now())        # get the current date and time in print format 
 182     def SetPreview(self
, preview
): 
 183         self
.preview 
= preview
 
 185     def SetPSize(self
, width
, height
): 
 186         self
.pwidth 
= width
/self
.scale
 
 187         self
.pheight 
= height
/self
.scale
 
 189     def SetScale(self
, scale
): 
 192     def SetPTSize(self
, width
, height
): 
 194         self
.ptheight 
= height
 
 203 class PrintTableDraw(wx
.ScrolledWindow
, PrintBase
): 
 204     def __init__(self
, parent
, DC
, size
): 
 207         self
.scale 
= parent
.scale
 
 209         self
.height 
= size
[1] 
 212     def SetDefaults(self
): 
 214         self
.total_pages 
= None 
 216         self
.page_width 
= self
.parent
.page_width
 
 217         self
.page_height 
= self
.parent
.page_height
 
 219         self
.left_margin 
= self
.parent
.left_margin
 
 220         self
.right_margin 
= self
.parent
.right_margin
 
 222         self
.top_margin  
= self
.parent
.top_margin
 
 223         self
.bottom_margin 
= self
.parent
.bottom_margin
 
 224         self
.cell_left_margin 
= self
.parent
.cell_left_margin
 
 225         self
.cell_right_margin 
= self
.parent
.cell_right_margin
 
 227         self
.label_colour 
= self
.parent
.label_colour
 
 229         self
.row_line_colour 
= self
.parent
.row_line_colour
 
 230         self
.row_line_size 
= self
.parent
.row_line_size
 
 232         self
.row_def_line_colour 
= self
.parent
.row_def_line_colour
 
 233         self
.row_def_line_size 
= self
.parent
.row_def_line_size
 
 235         self
.column_line_colour 
= self
.parent
.column_line_colour
 
 236         self
.column_line_size 
= self
.parent
.column_line_size
 
 238         self
.column_def_line_size 
= self
.parent
.column_def_line_size
 
 239         self
.column_def_line_colour 
= self
.parent
.column_def_line_colour
 
 241         self
.text_font 
= self
.parent
.text_font
 
 243         self
.label_font 
= self
.parent
.label_font
 
 245     def AdjustValues(self
): 
 246         self
.vertical_offset 
= self
.pheight 
* self
.parent
.vertical_offset
 
 247         self
.horizontal_offset 
= self
.pheight 
* self
.parent
.horizontal_offset
 
 249         self
.pcell_left_margin 
= self
.pwidth 
* self
.cell_left_margin
 
 250         self
.pcell_right_margin 
= self
.pwidth 
* self
.cell_right_margin
 
 251         self
.ptop_margin 
= self
.pheight 
* self
.top_margin
 
 252         self
.pbottom_margin 
= self
.pheight 
* self
.bottom_margin
 
 254         self
.pheader_margin 
= self
.pheight 
* self
.parent
.header_margin
 
 255         self
.pfooter_margin 
= self
.pheight 
* self
.parent
.footer_margin
 
 257         self
.cell_colour 
= self
.parent
.set_cell_colour
 
 258         self
.cell_text 
= self
.parent
.set_cell_text
 
 261         self
.column_align 
= [] 
 262         self
.column_bgcolour 
= [] 
 263         self
.column_txtcolour 
= [] 
 265         set_column_align 
= self
.parent
.set_column_align
 
 266         set_column_bgcolour 
= self
.parent
.set_column_bgcolour
 
 267         set_column_txtcolour 
= self
.parent
.set_column_txtcolour
 
 269         pos_x 
= self
.left_margin 
* self
.pwidth 
+ self
.horizontal_offset     
# left margin 
 270         self
.column
.append(pos_x
) 
 272         if self
.set_column 
== []: 
 273             table_width 
= self
.page_width 
- self
.left_margin 
- self
.right_margin
 
 274             width 
= table_width
/(len(self
.label
)) 
 275             for val 
in self
.label
: 
 276                 column_width 
= width 
* self
.pwidth
 
 277                 pos_x 
= pos_x 
+ column_width
 
 278                 self
.column
.append(pos_x
)   # position of each column 
 280             for val 
in self
.set_column
: 
 281                 column_width 
= val 
* self
.pwidth
 
 282                 pos_x 
= pos_x 
+ column_width
 
 283                 self
.column
.append(pos_x
)   # position of each column 
 285         if pos_x 
> self
.page_width 
* self
.pwidth
:    # check if it fits in page 
 286             print "Warning, Too Wide for Page" 
 290             if len(self
.column
) -1 != len(self
.label
): 
 291                 print "Column Settings Incorrect", "\nColumn Value: " + str(self
.column
), "\nLabel Value: " + str(self
.label
) 
 294         first_value 
= self
.data
[0] 
 295         column_total 
= len(first_value
) 
 296         if column_total 
!= len(self
.column
) -1: 
 297             print "Column Settings Incorrect", first_value
, self
.column
 
 301         for col 
in range(column_total
): 
 303                 align 
= set_column_align
[col
]       # check if custom column alignment 
 305                 align 
= wx
.ALIGN_LEFT
 
 306             self
.column_align
.append(align
) 
 309                 colour 
= set_column_bgcolour
[col
]     # check if custom column background colour 
 311                 colour 
= self
.parent
.column_colour
 
 312             self
.column_bgcolour
.append(colour
) 
 315                 colour 
= set_column_txtcolour
[col
]     # check if custom column text colour 
 317                 colour 
= self
.GetFontColour(self
.parent
.text_font
) 
 319             self
.column_txtcolour
.append(colour
) 
 323     def SetPointAdjust(self
): 
 324         f 
= wx
.Font(10, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
)     # setup using 10 point 
 326         f
.SetFaceName(self
.text_font
["Name"]) 
 327         x
, y 
= self
.DC
.GetTextExtent("W") 
 329         self
.label_pt_space_before 
= self
.parent
.label_pt_adj_before 
* y
/10        # extra spacing for label per point value 
 330         self
.label_pt_space_after 
= self
.parent
.label_pt_adj_after 
* y
/10 
 332         self
.text_pt_space_before 
= self
.parent
.text_pt_adj_before 
* y
/10        # extra spacing for row text per point value 
 333         self
.text_pt_space_after 
= self
.parent
.text_pt_adj_after 
* y
/10 
 335     def SetPage(self
, page
): 
 338     def SetColumns(self
, col
): 
 343         self
.SetPointAdjust() 
 345         self
.y_start 
= self
.ptop_margin 
+ self
.vertical_offset
 
 346         self
.y_end 
= self
.parent
.page_height 
* self
.pheight 
- self
.pbottom_margin 
+ self
.vertical_offset
 
 348         self
.SetPrintFont(self
.label_font
) 
 350         x
, y 
= self
.DC
.GetTextExtent("W") 
 353         self
.SetPrintFont(self
.text_font
) 
 355         x
, y 
= self
.DC
.GetTextExtent("W") 
 358         if self
.total_pages 
is None: 
 359             self
.GetTotalPages()    # total pages for display/printing 
 361         self
.data_cnt 
= self
.page_index
[self
.page
-1] 
 368     def GetTotalPages(self
): 
 371         self
.page_index 
= [0] 
 375             test 
= self
.OutPage() 
 376             self
.page_index
.append(self
.data_cnt
) 
 381         self
.total_pages 
= cnt 
+ 1 
 384         self
.y 
= self
.y_start
 
 385         self
.end_x 
= self
.column
[-1] 
 387         if self
.data_cnt 
< len(self
.data
):  # if there data for display on the page 
 388             if self
.label 
!= []:        # check if header defined 
 393         for val 
in self
.data
: 
 395                 row_val 
= self
.data
[self
.data_cnt
] 
 400             max_y 
= self
.PrintRow(row_val
, False)       # test to see if row will fit in remaining space 
 401             test 
= max_y 
+ self
.space
 
 403             if test 
> self
.y_end
: 
 406             self
.ColourRowCells(max_y
-self
.y
+self
.space
)       # colour the row/column 
 407             max_y 
= self
.PrintRow(row_val
, True)      # row fits - print text 
 408             self
.DrawGridLine()     # top line of cell 
 409             self
.y 
= max_y 
+ self
.space
 
 411             if self
.y 
> self
.y_end
: 
 414             self
.data_cnt 
= self
.data_cnt 
+ 1 
 418         if self
.data_cnt 
== len(self
.data
):    # last value in list 
 424     def PrintLabel(self
): 
 425         self
.pt_space_before 
= self
.label_pt_space_before   
# set the point spacing 
 426         self
.pt_space_after 
= self
.label_pt_space_after
 
 428         self
.LabelColorRow(self
.label_colour
) 
 429         self
.SetPrintFont(self
.label_font
) 
 433         for vtxt 
in self
.label
: 
 434             self
.region 
= self
.column
[self
.col
+1] - self
.column
[self
.col
] 
 435             self
.indent 
= self
.column
[self
.col
] 
 437             self
.align 
= wx
.ALIGN_LEFT
 
 439             max_out 
= self
.OutTextRegion(vtxt
, True) 
 442             self
.col 
= self
.col 
+ 1 
 444         self
.DrawGridLine()     # top line of label 
 445         self
.y 
= max_y 
+ self
.label_space
 
 447     def PrintHeader(self
):      # print the header array 
 448         if self
.draw 
== False: 
 451         for val 
in self
.parent
.header
: 
 452             self
.SetPrintFont(val
["Font"]) 
 454             header_indent 
= val
["Indent"] * self
.pwidth
 
 459                 addtext 
= self
.GetDate() 
 460             elif htype 
== "Date & Time": 
 461                 addtext 
= self
.GetDateTime() 
 465             self
.OutTextPageWidth(text
+addtext
, self
.pheader_margin
, val
["Align"], header_indent
, True) 
 467     def PrintFooter(self
):      # print the header array 
 468         if self
.draw 
== False: 
 471         footer_pos 
= self
.parent
.page_height 
* self
.pheight 
- self
.pfooter_margin 
+ self
.vertical_offset
 
 472         for val 
in self
.parent
.footer
: 
 473             self
.SetPrintFont(val
["Font"]) 
 475             footer_indent 
= val
["Indent"] * self
.pwidth
 
 479             if ftype 
== "Pageof": 
 480                 addtext 
= "Page " + str(self
.page
) + " of " + str(self
.total_pages
) 
 481             elif ftype 
== "Page": 
 482                 addtext 
= "Page " + str(self
.page
) 
 484                 addtext 
= str(self
.page
) 
 485             elif ftype 
== "Date": 
 486                 addtext 
= self
.GetDate() 
 487             elif ftype 
== "Date & Time": 
 488                 addtext 
= self
.GetDateTime() 
 492             self
.OutTextPageWidth(text
+addtext
, footer_pos
, val
["Align"], footer_indent
, True) 
 495     def LabelColorRow(self
, colour
): 
 496         brush 
= wx
.Brush(colour
, wx
.SOLID
) 
 497         self
.DC
.SetBrush(brush
) 
 498         height 
= self
.label_space 
+ self
.label_pt_space_before 
+ self
.label_pt_space_after
 
 499         self
.DC
.DrawRectangle((self
.column
[0], self
.y
), 
 500                               (self
.end_x
-self
.column
[0]+1, height
)) 
 502     def ColourRowCells(self
, height
): 
 503         if self
.draw 
== False: 
 507         for colour 
in self
.column_bgcolour
: 
 508             cellcolour 
= self
.GetCellColour(self
.data_cnt
, col
) 
 509             if cellcolour 
is not None: 
 512             brush 
= wx
.Brush(colour
, wx
.SOLID
) 
 513             self
.DC
.SetBrush(brush
) 
 514             self
.DC
.SetPen(wx
.Pen(wx
.NamedColour('WHITE'), 0)) 
 516             start_x 
= self
.column
[col
] 
 517             width 
= self
.column
[col
+1] - start_x 
+ 2 
 518             self
.DC
.DrawRectangle((start_x
, self
.y
), (width
, height
)) 
 521     def PrintRow(self
, row_val
, draw 
= True, align 
= wx
.ALIGN_LEFT
): 
 522         self
.SetPrintFont(self
.text_font
) 
 524         self
.pt_space_before 
= self
.text_pt_space_before   
# set the point spacing 
 525         self
.pt_space_after 
= self
.text_pt_space_after
 
 530             self
.region 
= self
.column
[self
.col
+1] - self
.column
[self
.col
] 
 531             self
.indent 
= self
.column
[self
.col
] 
 532             self
.align 
= self
.column_align
[self
.col
] 
 534             fcolour 
= self
.column_txtcolour
[self
.col
]       # set font colour 
 535             celltext 
= self
.GetCellText(self
.data_cnt
, self
.col
) 
 536             if celltext 
is not None: 
 537                 fcolour 
= celltext      
# override the column colour 
 539             self
.DC
.SetTextForeground(fcolour
) 
 541             max_out 
= self
.OutTextRegion(vtxt
, draw
) 
 544             self
.col 
= self
.col 
+ 1 
 547     def GetCellColour(self
, row
, col
):      # check if custom colour defined for the cell background 
 549             set = self
.cell_colour
[row
] 
 558     def GetCellText(self
, row
, col
):      # check if custom colour defined for the cell text 
 560             set = self
.cell_text
[row
] 
 569     def FinishDraw(self
): 
 570         self
.DrawGridLine()     # draw last row line 
 571         self
.DrawColumns()      # draw all vertical lines 
 573     def DrawGridLine(self
): 
 574         if self
.draw 
== True: 
 576                 size 
= self
.row_line_size
[self
.data_cnt
] 
 578                 size 
= self
.row_def_line_size
 
 581                 colour 
= self
.row_line_colour
[self
.data_cnt
] 
 583                 colour 
= self
.row_def_line_colour
 
 585             self
.DC
.SetPen(wx
.Pen(colour
, size
)) 
 588 #            y_out = self.y + self.pt_space_before + self.pt_space_after     # adjust for extra spacing 
 589             self
.DC
.DrawLine((self
.column
[0], y_out
), (self
.end_x
, y_out
)) 
 591     def DrawColumns(self
): 
 592         if self
.draw 
== True: 
 594             for val 
in self
.column
: 
 596                     size 
= self
.column_line_size
[col
] 
 598                     size 
= self
.column_def_line_size
 
 601                     colour 
= self
.column_line_colour
[col
] 
 603                     colour 
= self
.column_def_line_colour
 
 607                 self
.DC
.SetPen(wx
.Pen(colour
, size
)) 
 608                 self
.DC
.DrawLine((indent
, self
.y_start
), (indent
, self
.y
)) 
 614     def DoDrawing(self
, DC
): 
 622         self
.sizew 
= DC
.MaxY() 
 623         self
.sizeh 
= DC
.MaxX() 
 627     def __init__(self
, parentFrame
=None): 
 634         self
.set_column_align 
= {} 
 635         self
.set_column_bgcolour 
= {} 
 636         self
.set_column_txtcolour 
= {} 
 637         self
.set_cell_colour 
= {} 
 638         self
.set_cell_text 
= {} 
 639         self
.column_line_size 
= {} 
 640         self
.column_line_colour 
= {} 
 641         self
.row_line_size 
= {} 
 642         self
.row_line_colour 
= {} 
 644         self
.parentFrame 
= parentFrame
 
 645         self
.SetPreviewSize() 
 647         self
.printData 
= wx
.PrintData() 
 655         self
.SetPrinterOffset() 
 656         self
.SetHeaderValue() 
 657         self
.SetFooterValue() 
 661     def SetPreviewSize(self
, position 
= wx
.Point(0, 0), size
="Full"): 
 663             r 
= wx
.GetClientDisplayRect() 
 664             self
.preview_frame_size 
= r
.GetSize() 
 665             self
.preview_frame_pos 
= r
.GetPosition() 
 667             self
.preview_frame_size 
= size
 
 668             self
.preview_frame_pos 
= position
 
 670     def SetPaperId(self
, paper
): 
 671         self
.printData
.SetPaperId(paper
) 
 673     def SetOrientation(self
, orient
): 
 674         self
.printData
.SetOrientation(orient
) 
 677         self
.row_def_line_colour 
= wx
.NamedColour('BLACK') 
 678         self
.row_def_line_size 
= 1 
 680         self
.column_def_line_colour 
= wx
.NamedColour('BLACK') 
 681         self
.column_def_line_size 
= 1 
 682         self
.column_colour 
= wx
.NamedColour('WHITE') 
 684         self
.label_colour 
= wx
.NamedColour('LIGHT GREY') 
 687         self
.label_font 
= { "Name": self.default_font_name, "Size": 12, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
 
 688         self
.text_font 
= { "Name": self.default_font_name, "Size": 10, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
 
 690     def TextSpacing(self
): 
 691         self
.label_pt_adj_before 
= 0     # point adjustment before and after the label text 
 692         self
.label_pt_adj_after 
= 0 
 694         self
.text_pt_adj_before 
= 0     # point adjustment before and after the row text 
 695         self
.text_pt_adj_after 
= 0 
 697     def SetLabelSpacing(self
, before
, after
):        # method to set the label space adjustment 
 698         self
.label_pt_adj_before 
= before
 
 699         self
.label_pt_adj_after 
= after
 
 701     def SetRowSpacing(self
, before
, after
):         # method to set the row space adjustment 
 702         self
.text_pt_adj_before 
= before
 
 703         self
.text_pt_adj_after 
= after
 
 705     def SetPrinterOffset(self
):        # offset to adjust for printer 
 706         self
.vertical_offset 
= -0.1 
 707         self
.horizontal_offset 
= -0.1 
 709     def SetHeaderValue(self
): 
 710         self
.header_margin 
= 0.25 
 711         self
.header_font 
= { "Name": self.default_font_name, "Size": 11, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
 
 712         self
.header_align 
= wx
.ALIGN_CENTRE
 
 713         self
.header_indent 
= 0 
 714         self
.header_type 
= "Text" 
 716     def SetFooterValue(self
): 
 717         self
.footer_margin 
= 0.7 
 718         self
.footer_font 
= { "Name": self.default_font_name, "Size": 11, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
 
 719         self
.footer_align 
= wx
.ALIGN_CENTRE
 
 720         self
.footer_indent 
= 0 
 721         self
.footer_type 
= "Pageof" 
 723     def SetMargins(self
): 
 724         self
.left_margin 
= 1.0 
 725         self
.right_margin 
= 1.0     # only used if no column sizes 
 727         self
.top_margin  
= 0.8 
 728         self
.bottom_margin 
= 1.0 
 729         self
.cell_left_margin 
= 0.1 
 730         self
.cell_right_margin 
= 0.1 
 732     def SetPortrait(self
): 
 733         self
.printData
.SetPaperId(wx
.PAPER_LETTER
) 
 734         self
.printData
.SetOrientation(wx
.PORTRAIT
) 
 735         self
.page_width 
= 8.5 
 736         self
.page_height 
= 11.0 
 738     def SetLandscape(self
): 
 739         self
.printData
.SetOrientation(wx
.LANDSCAPE
) 
 740         self
.page_width 
= 11.0 
 741         self
.page_height 
= 8.5 
 752         self
.default_font_name 
= "Arial" 
 753         self
.default_font 
= { "Name": self.default_font_name, "Size": 10, "Colour": [0, 0, 0], "Attr": [0, 0, 0] }
 
 755     def SetColAlignment(self
, col
, align
=wx
.ALIGN_LEFT
): 
 756         self
.set_column_align
[col
] = align
 
 758     def SetColBackgroundColour(self
, col
, colour
): 
 759         self
.set_column_bgcolour
[col
] = colour
 
 761     def SetColTextColour(self
, col
, colour
): 
 762         self
.set_column_txtcolour
[col
] = colour
 
 764     def SetCellColour(self
, row
, col
, colour
):      # cell background colour 
 766             set = self
.set_cell_colour
[row
]     # test if row already exists 
 768                 set[col
] = colour       
# test if column already exists 
 770                 set = { col: colour }       
# create the column value 
 772             set = { col: colour }           
# create the column value 
 774         self
.set_cell_colour
[row
] = set    # create dictionary item for colour settings 
 776     def SetCellText(self
, row
, col
, colour
):        # font colour for custom cells 
 778             set = self
.set_cell_text
[row
]     # test if row already exists 
 780                 set[col
] = colour       
# test if column already exists 
 782                 set = { col: colour }       
# create the column value 
 784             set = { col: colour }           
# create the column value 
 786         self
.set_cell_text
[row
] = set    # create dictionary item for colour settings 
 788     def SetColumnLineSize(self
, col
, size
):      # column line size 
 789         self
.column_line_size
[col
] = size    
# create dictionary item for column line settings 
 791     def SetColumnLineColour(self
, col
, colour
): 
 792         self
.column_line_colour
[col
] = colour
 
 794     def SetRowLineSize(self
, row
, size
): 
 795         self
.row_line_size
[row
] = size
 
 797     def SetRowLineColour(self
, row
, colour
): 
 798         self
.row_line_colour
[row
] = colour
 
 800     def GetColour(self
, colour
):        # returns colours based from wxColour value 
 803         green 
= colour
.Green() 
 804         return [red
, green
, blue 
] 
 806     def SetHeader(self
, text 
= "", type = "Text", font
=None, align 
= None, indent 
= None, colour 
= None, size 
= None): 
 807         set = { "Text": text }
 
 810             set["Font"] = copy
.copy(self
.default_font
) 
 814         if colour 
is not None: 
 815             setfont 
= set["Font"] 
 816             setfont
["Colour"] = self
.GetColour(colour
) 
 819             setfont 
= set["Font"] 
 820             setfont
["Size"] = size
 
 823             set["Align"] = self
.header_align
 
 828             set["Indent"] = self
.header_indent
 
 830             set["Indent"] = indent
 
 833             set["Type"] = self
.header_type
 
 837         self
.header
.append(set) 
 839     def SetFooter(self
, text 
= "", type = None, font
=None, align 
= None, indent 
= None, colour 
= None, size 
= None): 
 840         set = { "Text": text }
 
 843             set["Font"] = copy
.copy(self
.default_font
) 
 847         if colour 
is not None: 
 848             setfont 
= set["Font"] 
 849             setfont
["Colour"] = self
.GetColour(colour
) 
 852             setfont 
= set["Font"] 
 853             setfont
["Size"] = size
 
 856             set["Align"] = self
.footer_align
 
 861             set["Indent"] = self
.footer_indent
 
 863             set["Indent"] = indent
 
 866             set["Type"] = self
.footer_type
 
 870         self
.footer
.append(set) 
 873         printout 
= SetPrintout(self
) 
 874         printout2 
= SetPrintout(self
) 
 875         self
.preview 
= wx
.PrintPreview(printout
, printout2
, self
.printData
) 
 876         if not self
.preview
.Ok(): 
 877             wxMessageBox("There was a problem printing!", "Printing", wx
.OK
) 
 880         self
.preview
.SetZoom(60)        # initial zoom value 
 882         frame 
= wx
.PreviewFrame(self
.preview
, self
.parentFrame
, "Print preview") 
 886             frame
.SetPosition(self
.preview_frame_pos
) 
 887             frame
.SetSize(self
.preview_frame_size
) 
 891         pdd 
= wx
.PrintDialogData() 
 892         pdd
.SetPrintData(self
.printData
) 
 893         printer 
= wx
.Printer(pdd
) 
 894         printout 
= SetPrintout(self
) 
 895         if not printer
.Print(self
.parentFrame
, printout
): 
 896             wx
.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx
.OK
) 
 898             self
.printData 
= printer
.GetPrintDialogData().GetPrintData() 
 901     def DoDrawing(self
, DC
): 
 905         table 
= PrintTableDraw(self
, DC
, size
) 
 906         table
.data 
= self
.data
 
 907         table
.set_column 
= self
.set_column
 
 908         table
.label 
= self
.label
 
 909         table
.SetPage(self
.page
) 
 911         if self
.preview 
is None: 
 912             table
.SetPSize(size
[0]/self
.page_width
, size
[1]/self
.page_height
) 
 913             table
.SetPTSize(size
[0], size
[1]) 
 914             table
.SetPreview(False) 
 916             if self
.preview 
== 1: 
 917                 table
.scale 
= self
.scale
 
 918                 table
.SetPSize(size
[0]/self
.page_width
, size
[1]/self
.page_height
) 
 920                 table
.SetPSize(self
.pwidth
, self
.pheight
) 
 922             table
.SetPTSize(self
.ptwidth
, self
.ptheight
) 
 923             table
.SetPreview(self
.preview
) 
 926         self
.page_total 
= table
.total_pages     
# total display pages 
 930         self
.ymax 
= DC
.MaxY() 
 931         self
.xmax 
= DC
.MaxX() 
 936     def GetTotalPages(self
): 
 937         self
.page_total 
= 100 
 938         return self
.page_total
 
 940     def HasPage(self
, page
): 
 941         if page 
<= self
.page_total
: 
 946     def SetPage(self
, page
): 
 949     def SetPageSize(self
, width
, height
): 
 950         self
.pwidth
, self
.pheight 
= width
, height
 
 952     def SetTotalSize(self
, width
, height
): 
 953         self
.ptwidth
, self
.ptheight 
= width
, height
 
 955     def SetPreview(self
, preview
, scale
): 
 956         self
.preview 
= preview
 
 959     def SetTotalSize(self
, width
, height
): 
 961         self
.ptheight 
= height
 
 964     def __init__(self
, parent
, grid
, format 
= [], total_col 
= None, total_row 
= None): 
 965         if total_row 
is None: 
 966             total_row 
= grid
.GetNumberRows() 
 967         if total_col 
is None: 
 968             total_col 
= grid
.GetNumberCols() 
 970         self
.total_row 
= total_row
 
 971         self
.total_col 
= total_col
 
 975         for row 
in range(total_row
): 
 977             value 
= grid
.GetRowLabelValue(row
) 
 978             row_val
.append(value
) 
 980             for col 
in range(total_col
): 
 981                 value 
= grid
.GetCellValue(row
, col
) 
 982                 row_val
.append(value
) 
 986         for col 
in range(total_col
): 
 987             value 
= grid
.GetColLabelValue(col
) 
 990         self
.table 
= PrintTable(parent
) 
 991         self
.table
.cell_left_margin 
= 0.0 
 992         self
.table
.cell_right_margin 
= 0.0 
 994         self
.table
.label 
= label
 
 995         self
.table
.set_column 
= format
 
 996         self
.table
.data 
= data
 
1001     def SetAttributes(self
): 
1002         for row 
in range(self
.total_row
): 
1003             for col 
in range(self
.total_col
): 
1004                 colour 
= self
.grid
.GetCellTextColour(row
, col
-1) 
1005                 self
.table
.SetCellText(row
, col
, colour
) 
1007                 colour 
= self
.grid
.GetCellBackgroundColour(row
, col
-1) 
1008                 self
.table
.SetCellColour(row
, col
, colour
) 
1011         self
.table
.Preview() 
1017 class SetPrintout(wx
.Printout
): 
1018     def __init__(self
, canvas
): 
1019         wx
.Printout
.__init
__(self
) 
1020         self
.canvas 
= canvas
 
1023     def OnBeginDocument(self
, start
, end
): 
1024         return self
.base_OnBeginDocument(start
, end
) 
1026     def OnEndDocument(self
): 
1027         self
.base_OnEndDocument() 
1029     def HasPage(self
, page
): 
1031             end 
= self
.canvas
.HasPage(page
) 
1036     def GetPageInfo(self
): 
1038             self
.end_pg 
= self
.canvas
.GetTotalPages() 
1042         end_pg 
= self
.end_pg
 
1044         return (str_pg
, end_pg
, str_pg
, end_pg
) 
1046     def OnPreparePrinting(self
): 
1047         self
.base_OnPreparePrinting() 
1049     def OnBeginPrinting(self
): 
1052         self
.preview 
= self
.IsPreview() 
1054             self
.pixelsPerInch 
= self
.GetPPIScreen() 
1056             self
.pixelsPerInch 
= self
.GetPPIPrinter() 
1058         (w
, h
) = dc
.GetSize() 
1059         scaleX 
= float(w
) / 1000 
1060         scaleY 
= float(h
) / 1000 
1061         self
.printUserScale 
= min(scaleX
, scaleY
) 
1063         self
.base_OnBeginPrinting() 
1066         self
.psizew
, self
.psizeh 
= self
.GetPPIPrinter() 
1067         return self
.psizew
, self
.psizeh
 
1069     def GetTotalSize(self
): 
1070         self
.ptsizew
, self
.ptsizeh 
= self
.GetPageSizePixels() 
1071         return self
.ptsizew
, self
.ptsizeh
 
1073     def OnPrintPage(self
, page
): 
1075         (w
, h
) = dc
.GetSize() 
1076         scaleX 
= float(w
) / 1000 
1077         scaleY 
= float(h
) / 1000 
1078         self
.printUserScale 
= min(scaleX
, scaleY
) 
1079         dc
.SetUserScale(self
.printUserScale
, self
.printUserScale
) 
1081         self
.preview 
= self
.IsPreview() 
1083         self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
) 
1084         self
.canvas
.SetPage(page
) 
1086         self
.ptsizew
, self
.ptsizeh 
= self
.GetPageSizePixels() 
1087         self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
) 
1089         self
.psizew
, self
.psizeh 
= self
.GetPPIPrinter() 
1090         self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
) 
1092         self
.canvas
.DoDrawing(dc
)