source: sasview/src/sas/sasgui/guiframe/pdfview.py @ 280f929

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 280f929 was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Remaining modules refactored

  • Property mode set to 100644
File size: 5.1 KB
Line 
1# Read PDF files by embeding the Adobe Acrobat Reader
2# wx.activex module uses class ActiveX control
3
4import  wx
5import os
6if wx.Platform == '__WXMSW__':
7    from wx.lib.pdfwin import PDFWindow
8
9from wx.lib.scrolledpanel import ScrolledPanel
10STYLE = wx.TE_MULTILINE|wx.TE_READONLY|wx.SUNKEN_BORDER|wx.HSCROLL
11
12class TextPanel(ScrolledPanel):
13    """
14    Panel that contains the text
15    """
16    def __init__(self, parent, text=None):
17        """
18        """
19        ScrolledPanel.__init__(self, parent, id=-1)
20        self.SetupScrolling()
21        self.parent = parent
22        self.text = text
23        sizer = wx.BoxSizer(wx.VERTICAL)
24        self.textctl = wx.TextCtrl(self, -1, size=(-1, -1), style=STYLE)
25        self.textctl.SetValue(self.text)
26        sizer.Add(self.textctl, proportion=1, flag=wx.EXPAND)
27        self.SetSizer(sizer)
28        self.SetAutoLayout(True)
29        wx.EVT_CLOSE(self.parent, self.OnClose)
30               
31    def OnClose(self, event):
32        """
33        Close panel
34        """
35        self.parent.Destroy()
36       
37class TextFrame(wx.Frame):
38    """
39    Frame for PDF panel
40    """
41    def __init__(self, parent, id, title, text):
42        """
43        Init
44       
45        :param parent: parent panel/container
46        :param path: full path of the pdf file
47        """
48        # Initialize the Frame object
49        wx.Frame.__init__(self, parent, id, title,
50                          wx.DefaultPosition, wx.Size(600, 830))
51        # make an instance of the class
52        TextPanel(self, text) 
53        self.SetFocus()
54       
55class PDFPanel(wx.Panel):
56    """
57    Panel that contains the pdf reader
58    """
59    def __init__(self, parent, path=None):
60        """
61        """
62        wx.Panel.__init__(self, parent, id=-1)
63       
64        self.parent = parent
65        self.path = path
66        sizer = wx.BoxSizer(wx.VERTICAL)
67        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
68       
69        self.pdf = PDFWindow(self, style=wx.SUNKEN_BORDER)
70       
71        sizer.Add(self.pdf, proportion=1, flag=wx.EXPAND)
72       
73        btn = wx.Button(self, wx.NewId(), "Open PDF File")
74        self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
75        btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
76
77        self.pdf.LoadFile(self.path)
78        btn = wx.Button(self, wx.NewId(), "Previous Page")
79        self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
80        btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
81
82        btn = wx.Button(self, wx.NewId(), "Next Page")
83        self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
84        btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
85       
86        btn = wx.Button(self, wx.NewId(), "Close")
87        self.Bind(wx.EVT_BUTTON, self.OnClose, btn)
88        btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
89        btnSizer.Add((50,-1), proportion=2, flag=wx.EXPAND)
90        sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND)
91
92        self.SetSizer(sizer)
93        self.SetAutoLayout(True)
94        wx.EVT_CLOSE(self.parent, self.OnClose)
95       
96    def OnOpenButton(self, event):
97        """
98        Open file button
99        """
100        # make sure you have PDF files available on your drive
101        dlg = wx.FileDialog(self, wildcard="*.pdf")
102        dlg.SetDirectory(os.path.dirname(self.path))
103        if dlg.ShowModal() == wx.ID_OK:
104            wx.BeginBusyCursor()
105            file = dlg.GetPath()
106            self.pdf.LoadFile(file)
107            self.parent.SetTitle(os.path.basename(file.split('.')[0]))
108            wx.EndBusyCursor()
109        dlg.Destroy()
110        # Let Panel know the file changed: Avoiding C++ error
111        self.Update()
112       
113    def OnLoad(self, event=None, path=None):
114        """
115        Load a pdf file
116       
117        : Param path: full path to the file
118        """
119        self.pdf.LoadFile(path)
120
121       
122    def OnPrevPageButton(self, event):
123        """
124        Goes to Previous page
125        """
126        self.pdf.gotoPreviousPage()
127
128    def OnNextPageButton(self, event):
129        """
130        Goes to Next page
131        """
132        self.pdf.gotoNextPage()
133       
134    def OnClose(self, event):
135        """
136        Close panel
137        """
138        self.parent.Destroy()
139
140class PDFFrame(wx.Frame):
141    """
142    Frame for PDF panel
143    """
144    def __init__(self, parent, id, title, path):
145        """
146        Init
147       
148        :param parent: parent panel/container
149        :param path: full path of the pdf file
150        """
151        # Initialize the Frame object
152        wx.Frame.__init__(self, parent, id, title,
153                          wx.DefaultPosition, wx.Size(600, 830))
154        # make an instance of the class
155        PDFPanel(self, path) 
156       
157class ViewApp(wx.App):
158    def OnInit(self):
159        path = None
160        frame = PDFFrame(None, -1, "PDFView", path=path) 
161         
162        frame.Show(True)
163        #self.SetTopWindow(frame)
164       
165        return True
166               
167if __name__ == "__main__": 
168    app = ViewApp(0)
169    app.MainLoop()     
Note: See TracBrowser for help on using the repository browser.