📄 printout.py
字号:
#----------------------------------------------------------------------------
# Name: printout.py
# Purpose: preview and printing class -> table/grid printing
#
# Author: Lorne White (email: lorne.white@telusplanet.net)
#
# Created:
# Version 0.75
# Date: May 15, 2002
# Licence: wxWindows license
#----------------------------------------------------------------------------
# Release Notes
# fixed bug for string wider than print region
# add index to data list after parsing total pages for paging
#----------------------------------------------------------------------------
# 12/10/2003 - Jeff Grimmett (grimmtooth@softhome.net)
# o 2.5 compatability update.
#----------------------------------------------------------------------------
# 11/23/2004 - Vernon Cole (wnvcole@peppermillcas.com)
# o Generalize for non-2-dimensional sequences and non-text data
# (can use as a simple text printer by supplying a list of strings.)
# o Add a small _main_ for self test
import copy
import types
import wx
class PrintBase:
def SetPrintFont(self, font): # set the DC font parameters
fattr = font["Attr"]
if fattr[0] == 1:
weight = wx.BOLD
else:
weight = wx.NORMAL
if fattr[1] == 1:
set_style = wx.ITALIC
else:
set_style = wx.NORMAL
underline = fattr[2]
fcolour = self.GetFontColour(font)
self.DC.SetTextForeground(fcolour)
setfont = wx.Font(font["Size"], wx.SWISS, set_style, weight, underline)
setfont.SetFaceName(font["Name"])
self.DC.SetFont(setfont)
def GetFontColour(self, font):
fcolour = font["Colour"]
return wx.Colour(fcolour[0], fcolour[1], fcolour[2])
def OutTextRegion(self, textout, txtdraw = True):
textlines = textout.split('\n')
y = copy.copy(self.y) + self.pt_space_before
for text in textlines:
remain = 'X'
while remain != "":
vout, remain = self.SetFlow(text, self.region)
if self.draw == True and txtdraw == True:
test_out = self.TestFull(vout)
if self.align == wx.ALIGN_LEFT:
self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y)
elif self.align == wx.ALIGN_CENTRE:
diff = self.GetCellDiff(test_out, self.region)
self.DC.DrawText(test_out, self.indent+diff/2, y)
elif self.align == wx.ALIGN_RIGHT:
diff = self.GetCellDiff(test_out, self.region)
self.DC.DrawText(test_out, self.indent+diff, y)
else:
self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y)
text = remain
y = y + self.space
return y - self.space + self.pt_space_after
def GetCellDiff(self, text, width): # get the remaining cell size for adjustment
w, h = self.DC.GetTextExtent(text)
diff = width - w
if diff < 0:
diff = 0
return diff
def TestFull(self, text_test):
w, h = self.DC.GetTextExtent(text_test)
if w > self.region: # trouble fitting into cell
return self.SetChar(text_test, self.region) # fit the text to the cell size
else:
return text_test
def SetFlow(self, ln_text, width):
width = width - self.pcell_right_margin
text = ""
split = ln_text.split()
if len(split) == 1:
return ln_text, ""
try:
w, h = self.DC.GetTextExtent(" " + split[0])
if w >= width:
return ln_text, ""
except:
pass
cnt = 0
for word in split:
bword = " " + word # blank + word
length = len(bword)
w, h = self.DC.GetTextExtent(text + bword)
if w < width:
text = text + bword
cnt = cnt + 1
else:
remain = ' '.join(split[cnt:])
text = text.strip()
return text, remain
remain = ' '.join(split[cnt:])
vout = text.strip()
return vout, remain
def SetChar(self, ln_text, width): # truncate string to fit into width
width = width - self.pcell_right_margin - self.pcell_left_margin
text = ""
for val in ln_text:
w, h = self.DC.GetTextExtent(text + val)
if w > width:
text = text + ".."
return text # fitted text value
text = text + val
return text
def OutTextPageWidth(self, textout, y_out, align, indent, txtdraw = True):
textlines = textout.split('\n')
y = copy.copy(y_out)
pagew = self.parent.page_width * self.pwidth # full page width
w, h = self.DC.GetTextExtent(textout)
y_line = h
for text in textlines:
remain = 'X'
while remain != "":
vout, remain = self.SetFlow(text, pagew)
if self.draw == True and txtdraw == True:
test_out = vout
if align == wx.ALIGN_LEFT:
self.DC.DrawText(test_out, indent, y)
elif align == wx.ALIGN_CENTRE:
diff = self.GetCellDiff(test_out, pagew)
self.DC.DrawText(test_out, indent+diff/2, y)
elif align == wx.ALIGN_RIGHT:
diff = self.GetCellDiff(test_out, pagew)
self.DC.DrawText(test_out, indent+diff, y)
else:
self.DC.DrawText(test_out, indent, y_out)
text = remain
y = y + y_line
return y - y_line
def GetDate(self):
date, time = self.GetNow()
return date
def GetDateTime(self):
date, time = self.GetNow()
return date + ' ' + time
def GetNow(self):
now = wx.DateTime.Now()
date = now.FormatDate()
time = now.FormatTime()
return date, time
def SetPreview(self, preview):
self.preview = preview
def SetPSize(self, width, height):
self.pwidth = width/self.scale
self.pheight = height/self.scale
def SetScale(self, scale):
self.scale = scale
def SetPTSize(self, width, height):
self.ptwidth = width
self.ptheight = height
def getWidth(self):
return self.sizew
def getHeight(self):
return self.sizeh
class PrintTableDraw(wx.ScrolledWindow, PrintBase):
def __init__(self, parent, DC, size):
self.parent = parent
self.DC = DC
self.scale = parent.scale
self.width = size[0]
self.height = size[1]
self.SetDefaults()
def SetDefaults(self):
self.page = 1
self.total_pages = None
self.page_width = self.parent.page_width
self.page_height = self.parent.page_height
self.left_margin = self.parent.left_margin
self.right_margin = self.parent.right_margin
self.top_margin = self.parent.top_margin
self.bottom_margin = self.parent.bottom_margin
self.cell_left_margin = self.parent.cell_left_margin
self.cell_right_margin = self.parent.cell_right_margin
self.label_colour = self.parent.label_colour
self.row_line_colour = self.parent.row_line_colour
self.row_line_size = self.parent.row_line_size
self.row_def_line_colour = self.parent.row_def_line_colour
self.row_def_line_size = self.parent.row_def_line_size
self.column_line_colour = self.parent.column_line_colour
self.column_line_size = self.parent.column_line_size
self.column_def_line_size = self.parent.column_def_line_size
self.column_def_line_colour = self.parent.column_def_line_colour
self.text_font = self.parent.text_font
self.label_font = self.parent.label_font
def AdjustValues(self):
self.vertical_offset = self.pheight * self.parent.vertical_offset
self.horizontal_offset = self.pheight * self.parent.horizontal_offset
self.pcell_left_margin = self.pwidth * self.cell_left_margin
self.pcell_right_margin = self.pwidth * self.cell_right_margin
self.ptop_margin = self.pheight * self.top_margin
self.pbottom_margin = self.pheight * self.bottom_margin
self.pheader_margin = self.pheight * self.parent.header_margin
self.pfooter_margin = self.pheight * self.parent.footer_margin
self.cell_colour = self.parent.set_cell_colour
self.cell_text = self.parent.set_cell_text
self.column = []
self.column_align = []
self.column_bgcolour = []
self.column_txtcolour = []
set_column_align = self.parent.set_column_align
set_column_bgcolour = self.parent.set_column_bgcolour
set_column_txtcolour = self.parent.set_column_txtcolour
pos_x = self.left_margin * self.pwidth + self.horizontal_offset # left margin
self.column.append(pos_x)
#module logic expects two dimensional data -- fix input if needed
if isinstance(self.data,types.StringTypes):
self.data = [[copy.copy(self.data)]] # a string becomes a single cell
try:
rows = len(self.data)
except TypeError:
self.data = [[str(self.data)]] # a non-iterable becomes a single cell
rows = 1
first_value = self.data[0]
if isinstance(first_value, types.StringTypes): # a sequence of strings
if self.label == [] and self.set_column == []:
data = []
for x in self.data: #becomes one column
data.append([x])
else:
data = [self.data] #becames one row
self.data = data
first_value = data[0]
try:
column_total = len(first_value)
except TypeError: # a sequence of non-iterables
if self.label == [] and self.set_column == []:
data = [] #becomes one column
for x in self.data:
data.append([str(x)])
column_total = 1
else:
data = [self.data] #becomes one row
column_total = len(self.data)
self.data = data
first_value = data[0]
if self.set_column == []:
table_width = self.page_width - self.left_margin - self.right_margin
if self.label == []:
temp = first_value
else:
temp = self.label
width = table_width/(len(temp))
for val in temp:
column_width = width * self.pwidth
pos_x = pos_x + column_width
self.column.append(pos_x) # position of each column
else:
for val in self.set_column:
column_width = val * self.pwidth
pos_x = pos_x + column_width
self.column.append(pos_x) # position of each column
if pos_x > self.page_width * self.pwidth: # check if it fits in page
print "Warning, Too Wide for Page"
return
if self.label != []:
if len(self.column) -1 != len(self.label):
print "Column Settings Incorrect", "\nColumn Value: " + str(self.column), "\nLabel Value: " + str(self.label)
return
if column_total != len(self.column) -1:
print "Cannot fit", first_value, 'in', len(self.column)-1, 'columns.'
return
for col in range(column_total):
try:
align = set_column_align[col] # check if custom column alignment
except:
align = wx.ALIGN_LEFT
self.column_align.append(align)
try:
colour = set_column_bgcolour[col] # check if custom column background colour
except:
colour = self.parent.column_colour
self.column_bgcolour.append(colour)
try:
colour = set_column_txtcolour[col] # check if custom column text colour
except:
colour = self.GetFontColour(self.parent.text_font)
self.column_txtcolour.append(colour)
def SetPointAdjust(self):
f = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL) # setup using 10 point
self.DC.SetFont(f)
f.SetFaceName(self.text_font["Name"])
x, y = self.DC.GetTextExtent("W")
self.label_pt_space_before = self.parent.label_pt_adj_before * y/10 # extra spacing for label per point value
self.label_pt_space_after = self.parent.label_pt_adj_after * y/10
self.text_pt_space_before = self.parent.text_pt_adj_before * y/10 # extra spacing for row text per point value
self.text_pt_space_after = self.parent.text_pt_adj_after * y/10
def SetPage(self, page):
self.page = page
def SetColumns(self, col):
self.column = col
def OutCanvas(self):
self.AdjustValues()
self.SetPointAdjust()
self.y_start = self.ptop_margin + self.vertical_offset
self.y_end = self.parent.page_height * self.pheight - self.pbottom_margin + self.vertical_offset
self.SetPrintFont(self.label_font)
x, y = self.DC.GetTextExtent("W")
self.label_space = y
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -