Package mathbench :: Package lab :: Module library
[hide private]

Source Code for Module mathbench.lab.library

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  Display any help (and code sample) available for the functions and 
  6  libraries that can be used in the shells and scripts. 
  7  """ 
  8   
  9  import sys 
 10   
 11  # Load Python's logging utilities 
 12  import logging 
 13  import logging.handlers 
 14   
 15  # Give access to the system's installed webbrowser 
 16  import webbrowser 
 17   
 18  # wx and its html library 
 19  import wx 
 20  import wx.html as html 
 21  import wx.py.dispatcher as dispatcher 
 22   
 23  import lab_settings 
 24  from mathbench.basement.librarian import LibrarianSingleton 
 25   
 26   
27 -class OokOokHtmlWindow(wx.html.HtmlWindow):
28 """ 29 Catch the links that are cicked and send them to the platform's 30 web browser. 31 """ 32
33 - def __init__(self, parent, id=-1):
34 html.HtmlWindow.__init__(self, parent, id, style=wx.NO_FULL_REPAINT_ON_RESIZE) 35 if "gtk2" in wx.PlatformInfo: 36 self.SetStandardFonts()
37
38 - def OnLinkClicked(self, linkinfo):
39 # Virtuals in the base class have been renamed with base_ on the front. 40 if sys.version_info>=(2,5): 41 webbrowser.open_new_tab(linkinfo.GetHref()) 42 else: 43 webbrowser.open_new(linkinfo.GetHref())
44
45 -class LibraryDeskHTML(wx.Frame):
46 """ 47 The library desk from where all info can be reach. 48 49 Handles search queries, page history, search history... 50 """ 51 52
53 - def _init_html_window(self):
54 """ 55 Create the html widget. 56 57 This widget remains callable through 'self.html'. 58 """ 59 wx.InitAllImageHandlers() 60 self.html = OokOokHtmlWindow(parent=self) 61 if "gtk2" in wx.PlatformInfo: 62 self.html.SetStandardFonts()
63
64 - def _init_navbar(self):
65 """ 66 Create the bar gathering all naviation items. 67 """ 68 self.navbar = wx.BoxSizer(wx.HORIZONTAL) 69 70 # Use the wxFrame internals to create the toolbar and associate it all 71 # in one tidy method call. 72 tb = self.CreateToolBar( wx.TB_HORIZONTAL 73 | wx.NO_BORDER 74 | wx.TB_FLAT 75 | wx.TB_TEXT 76 ) 77 # set default size for icons 78 tb_img_size = (16,16) 79 tb.SetToolBitmapSize(tb_img_size) 80 81 # back 82 img = wx.ArtProvider_GetBitmap(wx.ART_GO_BACK, 83 wx.ART_TOOLBAR, 84 tb_img_size) 85 tb.AddSimpleTool(10,img,"Back","Go to previous page.") 86 self.Bind(wx.EVT_TOOL, self.OnToolClick, id=10) 87 88 # forward 89 img = wx.ArtProvider_GetBitmap(wx.ART_GO_FORWARD, 90 wx.ART_TOOLBAR, 91 tb_img_size) 92 tb.AddSimpleTool(20,img,"Forward","Go to next page.") 93 self.Bind(wx.EVT_TOOL, self.OnToolClick, id=20) 94 95 tb.AddSeparator() 96 97 # home 98 img = wx.ArtProvider_GetBitmap(wx.ART_GO_HOME, 99 wx.ART_TOOLBAR, 100 tb_img_size) 101 tb.AddSimpleTool(50,img,"Home","Go to welcome page.") 102 self.Bind(wx.EVT_TOOL, self.OnToolClick, id=50) 103 104 # search query widget 105 dummylongtext = wx.StaticText(self,-1,"Type your query and press 'Enter'Type your query and press 'Enter'") 106 dummylongtext.Fit() 107 nice_width = dummylongtext.GetBestSize().width 108 dummylongtext.Hide() 109 # self.query_widget = wx.TextCtrl(name='query', 110 # parent=tb, 111 # size=wx.Size(nice_width,-1)) 112 # self.query_widget.Bind(wx.EVT_KEY_DOWN,self.OnQueryKeyDown) 113 self.query_widget = wx.ComboBox( 114 parent=tb, size = wx.Size(nice_width,-1), 115 style=wx.CB_DROPDOWN 116 ) 117 118 self.query_widget.Bind(wx.EVT_TEXT_ENTER, self.OnQueryKeyDown) 119 self.query_widget.SetValue("Type your query and press 'Enter'") 120 self.query_widget.SelectAll() 121 tb.AddControl(self.query_widget) 122 123 # delete button 124 img = wx.ArtProvider_GetBitmap(wx.ART_DELETE, 125 wx.ART_TOOLBAR, 126 tb_img_size) 127 tb.AddSimpleTool(30,img,"Clear","Clear the search form.") 128 self.Bind(wx.EVT_TOOL, self.OnToolClick, id=30) 129 130 # search button 131 img = wx.ArtProvider_GetBitmap(wx.ART_FIND, 132 wx.ART_TOOLBAR, 133 tb_img_size) 134 tb.AddSimpleTool(35,img,"Search","Answer your query.") 135 self.Bind(wx.EVT_TOOL, self.OnToolClick, id=35) 136 137 tb.AddSeparator() 138 139 # Print 140 # -- print settings 141 self.printer = html.HtmlEasyPrinting() 142 # -- print button 143 img = wx.ArtProvider_GetBitmap(wx.ART_PRINT, 144 wx.ART_TOOLBAR, 145 tb_img_size) 146 tb.AddSimpleTool(40,img,"Print","Print current page.") 147 self.Bind(wx.EVT_TOOL, self.OnToolClick, id=40) 148 149 # Final thing to do for a toolbar is call the Realize() method. This 150 # causes it to render (more or less, that is). 151 tb.Realize()
152 153
154 - def __init__(self, title="Library Desk", parent=None):
155 156 wx.Frame.__init__(self, parent, -1, title, size=wx.Size(800,600)) 157 158 self.CreateStatusBar() 159 160 # create the html frame 161 self._init_html_window() 162 # Create the navigation bar 163 self._init_navbar() 164 165 # prepare the layout in the box 166 self.main_sizer = wx.BoxSizer(wx.VERTICAL) 167 self.main_sizer.Add(self.html, proportion=1, flag=wx.GROW) 168 169 self.SetSizer(self.main_sizer) 170 self.SetAutoLayout(True) 171 172 self.Bind(wx.EVT_CLOSE,self.OnClose) 173 # Show the front page of the documentation 174 wx.CallAfter(self.query_widget.SetFocus) 175 self.SetIcon(wx.ArtProvider().GetIcon(wx.ART_HELP_BOOK)) 176 self.Show()
177
178 - def showPage(self,page_txt):
179 """ 180 Show an html page 181 """ 182 self.html.SetPage(page_txt)
183
184 - def addToHistory(self,query_txt):
185 """ 186 Add an element to the search history. 187 """ 188 self.query_widget.Append(query_txt,query_txt.upper())
189 190 #--- EVENTS 191
192 - def OnClose(self,event):
193 """ 194 When the frame is closed 195 """ 196 dispatcher.send(signal='LibraryDeskClosed', sender=self) 197 event.Skip()
198
199 - def OnToolClick(self,event):
200 """ 201 When the user clicks on the toolbar, find which button has 202 been clicked and act ! 203 """ 204 eid = event.GetId() 205 if eid==10: #< Back 206 if not self.html.HistoryBack(): 207 wx.MessageBox("No more items in history!") 208 else: 209 event.Skip() 210 elif eid==20: #< Forward 211 if not self.html.HistoryForward(): 212 wx.MessageBox("No more items in history!") 213 else: 214 event.Skip() 215 elif eid==30: #< Clear 216 self.query_widget.SetValue("") 217 elif eid==40: #< Print 218 self.printer.PrintFile(self.html.GetOpenedPage()) 219 elif eid==50: #< Home 220 LibrarianSingleton.welcome() 221 elif eid==35: #< Search 222 LibrarianSingleton.search(self.query_widget.GetValue()) 223 else: 224 raise Exception("Event from unknown source!")
225
226 - def OnQueryKeyDown(self,event):
227 """ 228 When user clicks on enter while typing in the query widget, 229 then launch the search ! 230 """ 231 LibrarianSingleton.search(self.query_widget.GetValue()) 232 event.Skip() 233 234 235 #--- TEST --------------------------------------------------------------------------------- 236 237 if __name__ == "__main__": 238 app = wx.PySimpleApp()
239 - def createDesk():
240 """ 241 Sample desk factory 242 """ 243 return LibraryDeskHTML("Ooook!")
244 245 LibrarianSingleton.setDeskFactory(createDesk) 246 LibrarianSingleton.welcome() 247 app.MainLoop() 248