~launchpad-p-s/sofastatistics/main

« back to all changes in this revision

Viewing changes to showhtml.py

  • Committer: Grant Paton-Simpson
  • Date: 2009-05-19 04:21:43 UTC
  • Revision ID: g@ubuntu-20090519042143-p561mbokz3inefvd
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import wx
 
2
import os
 
3
 
 
4
import full_html
 
5
 
 
6
def get_html(title, content, template, root="", file_name="", print_folder=""):
 
7
    """
 
8
    Returns HTML with embedded CSS.
 
9
    %title% is replaced with title
 
10
    %content% is replaced with the content
 
11
    """
 
12
    #get html content
 
13
    fil = file(template, "r")
 
14
    html = fil.read()
 
15
    html = html.replace("%title%", title)
 
16
    html = html.replace("%content%", content)
 
17
    html = html.replace("%root%", "file://" + root)
 
18
    fil.close()
 
19
    #save copy of html content (for printing)
 
20
    if print_folder:
 
21
        fil = file(os.path.join(print_folder, file_name), "w")
 
22
        fil.write(html)
 
23
        fil.close()
 
24
    return html
 
25
 
 
26
def get_html_header(title, header_template):
 
27
    "Get the HTML down as far as (and including) <body>"
 
28
    fil = file(header_template, "r")
 
29
    hdr = fil.read()
 
30
    hdr = hdr.replace("%title%", title)
 
31
    fil.close()
 
32
    return hdr
 
33
 
 
34
class ShowHTML(wx.Dialog):
 
35
    "Show HTML window with content displayed"    
 
36
    
 
37
    def __init__(self, parent, content, file_name, title, print_folder):
 
38
        """
 
39
        content - html ready to display.
 
40
        file_name - excludes any path information. Needed for printing.
 
41
        title - dialog title.
 
42
        print_folder - needs to be a subfolder of the current folder.
 
43
        """
 
44
        wx.Dialog.__init__(self, parent=parent, id=-1, title=title, 
 
45
            pos=(0,0),
 
46
            size=(900,700),
 
47
            style=wx.RESIZE_BORDER|wx.DEFAULT_DIALOG_STYLE|wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX)
 
48
        
 
49
        self.file_name = file_name
 
50
        self.print_folder = print_folder
 
51
        
 
52
        html = full_html.FullHTML(self, size=wx.DefaultSize)
 
53
        html.ShowHTML(content)
 
54
        
 
55
        btnPrint = wx.Button(self, -1, "Print")
 
56
        btnPrint.Bind(wx.EVT_BUTTON, self.OnPrint)
 
57
        
 
58
        btnClose = wx.Button(self, wx.ID_CLOSE, "Close")
 
59
        btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
 
60
        
 
61
        szrMain = wx.BoxSizer(wx.VERTICAL)
 
62
        szrMain.Add(html,1,wx.GROW|wx.ALL, 5)
 
63
        
 
64
        szrButtons = wx.FlexGridSizer(rows=1, cols=2, hgap=5, vgap=5)
 
65
        szrButtons.AddGrowableCol(1,2)
 
66
        szrButtons.Add(btnPrint, 0, wx.ALL, 5)
 
67
        szrButtons.Add(btnClose, 0, wx.ALIGN_RIGHT|wx.ALL, 5)
 
68
        
 
69
        szrMain.Add(szrButtons,0, wx.GROW)
 
70
        
 
71
        self.SetSizer(szrMain)
 
72
        self.Layout()
 
73
        
 
74
    def OnPrint(self, event):
 
75
        "Print page"
 
76
        #printer = wx.html.HtmlEasyPrinting("Printing output", None)
 
77
        #printer.PrintFile(self.file_name) #horrible printing - large H1s, no CSS etc
 
78
        
 
79
        full_file = os.path.join(os.getcwd(), self.print_folder, self.file_name)
 
80
        os.system("rundll32.exe MSHTML.DLL,PrintHTML \"%s\"" % full_file)
 
81
    
 
82
    def OnClose(self, event):
 
83
        "Close Viewer"        
 
84
        self.Destroy()