Package Gnumed :: Package timelinelib :: Package wxgui :: Package dialogs :: Module helpbrowser
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.wxgui.dialogs.helpbrowser

  1  # Copyright (C) 2009, 2010, 2011  Rickard Lindberg, Roger Lindberg 
  2  # 
  3  # This file is part of Timeline. 
  4  # 
  5  # Timeline is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Timeline is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>. 
 17   
 18   
 19  import os.path 
 20   
 21  import wx 
 22  import wx.html 
 23   
 24  from timelinelib.config.paths import HELP_RESOURCES_DIR 
 25  from timelinelib.config.paths import ICONS_DIR 
 26  from timelinelib.wxgui.utils import _display_error_message 
 27   
 28   
29 -class HelpBrowser(wx.Frame):
30 31 HOME_ID = 10 32 BACKWARD_ID = 20 33 FORWARD_ID = 30 34
35 - def __init__(self, parent):
36 wx.Frame.__init__(self, parent, title=_("Help"), 37 size=(600, 550), style=wx.DEFAULT_FRAME_STYLE) 38 self.history = [] 39 self.current_pos = -1 40 self._create_help_system() 41 self._create_gui() 42 self._update_buttons()
43
44 - def _create_help_system(self):
45 try: 46 import markdown 47 except ImportError: 48 self.help_system = None 49 else: 50 import timelinelib.help.system as help 51 import timelinelib.help.pages as help_pages 52 self.help_system = help.HelpSystem( 53 "contents", HELP_RESOURCES_DIR + "/", "page:") 54 help_pages.install(self.help_system)
55
56 - def show_page(self, id, type="page", change_history=True):
57 """ 58 Where which is a tuple (type, id): 59 60 * (page, page_id) 61 * (search, search_string) 62 """ 63 if self.help_system is None: 64 _display_error_message( 65 _("Could not find markdown Python package. It is needed by the help system. See the Timeline website or the INSTALL file for instructions how to install it."), 66 self.GetParent()) 67 return 68 if change_history: 69 same_page_as_last = False 70 if self.current_pos != -1: 71 current_type, current_id = self.history[self.current_pos] 72 if id == current_id: 73 same_page_as_last = True 74 if same_page_as_last == False: 75 self.history = self.history[:self.current_pos + 1] 76 self.history.append((type, id)) 77 self.current_pos += 1 78 self._update_buttons() 79 if type == "page": 80 self.html_window.SetPage(self._generate_page(id)) 81 elif type == "search": 82 self.html_window.SetPage(self.help_system.get_search_results_page(id)) 83 self.Show() 84 self.Raise()
85
86 - def _create_gui(self):
87 self.Bind(wx.EVT_CLOSE, self._window_on_close) 88 self.toolbar = self.CreateToolBar() 89 size = (24, 24) 90 if 'wxMSW' in wx.PlatformInfo: 91 home_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "home.png")) 92 back_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "backward.png")) 93 forward_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "forward.png")) 94 else: 95 home_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR, 96 size) 97 back_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR, 98 size) 99 forward_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, 100 wx.ART_TOOLBAR, size) 101 self.toolbar.SetToolBitmapSize(size) 102 # Home 103 home_str = _("Go to home page") 104 self.toolbar.AddLabelTool(HelpBrowser.HOME_ID, home_str, 105 home_bmp, shortHelp=home_str) 106 self.Bind(wx.EVT_TOOL, self._toolbar_on_click, id=HelpBrowser.HOME_ID) 107 # Separator 108 self.toolbar.AddSeparator() 109 # Backward 110 backward_str = _("Go back one page") 111 self.toolbar.AddLabelTool(HelpBrowser.BACKWARD_ID, backward_str, 112 back_bmp, shortHelp=backward_str) 113 self.Bind(wx.EVT_TOOL, self._toolbar_on_click, 114 id=HelpBrowser.BACKWARD_ID) 115 # Forward 116 forward_str = _("Go forward one page") 117 self.toolbar.AddLabelTool(HelpBrowser.FORWARD_ID, forward_str, 118 forward_bmp, shortHelp=forward_str) 119 self.Bind(wx.EVT_TOOL, self._toolbar_on_click, 120 id=HelpBrowser.FORWARD_ID) 121 # Separator 122 self.toolbar.AddSeparator() 123 # Search 124 self.search = wx.SearchCtrl(self.toolbar, size=(150, -1), 125 style=wx.TE_PROCESS_ENTER) 126 self.Bind(wx.EVT_TEXT_ENTER, self._search_on_text_enter, self.search) 127 self.toolbar.AddControl(self.search) 128 self.toolbar.Realize() 129 # Html window 130 self.html_window = wx.html.HtmlWindow(self) 131 self.Bind(wx.html.EVT_HTML_LINK_CLICKED, 132 self._html_window_on_link_clicked, self.html_window) 133 self.html_window.Connect(wx.ID_ANY, wx.ID_ANY, wx.EVT_KEY_DOWN.typeId, 134 self._window_on_key_down)
135
136 - def _window_on_close(self, e):
137 self.Show(False)
138
139 - def _window_on_key_down(self, evt):
140 """ 141 Event handler used when a keyboard key has been pressed. 142 143 The following keys are handled: 144 Key Action 145 -------- ------------------------------------ 146 Backspace Go to previous page 147 """ 148 keycode = evt.GetKeyCode() 149 if keycode == wx.WXK_BACK: 150 self._go_back() 151 evt.Skip()
152
153 - def _toolbar_on_click(self, e):
154 if e.GetId() == HelpBrowser.HOME_ID: 155 self._go_home() 156 elif e.GetId() == HelpBrowser.BACKWARD_ID: 157 self._go_back() 158 elif e.GetId() == HelpBrowser.FORWARD_ID: 159 self._go_forward()
160
161 - def _search_on_text_enter(self, e):
162 self._search(self.search.GetValue())
163 170 # open in broswer 171
172 - def _go_home(self):
173 self.show_page(self.help_system.home_page)
174
175 - def _go_back(self):
176 if self.current_pos > 0: 177 self.current_pos -= 1 178 current_type, current_id = self.history[self.current_pos] 179 self.show_page(current_id, type=current_type, change_history=False)
180
181 - def _go_forward(self):
182 if self.current_pos < len(self.history) - 1: 183 self.current_pos += 1 184 current_type, current_id = self.history[self.current_pos] 185 self.show_page(current_id, type=current_type, change_history=False)
186
187 - def _search(self, string):
188 self.show_page(string, type="search")
189
190 - def _update_buttons(self):
191 history_len = len(self.history) 192 enable_backward = history_len > 1 and self.current_pos > 0 193 enable_forward = history_len > 1 and self.current_pos < history_len - 1 194 self.toolbar.EnableTool(HelpBrowser.BACKWARD_ID, enable_backward) 195 self.toolbar.EnableTool(HelpBrowser.FORWARD_ID, enable_forward)
196
197 - def _generate_page(self, id):
198 page = self.help_system.get_page(id) 199 if page == None: 200 error_page_html = "<h1>%s</h1><p>%s</p>" % ( 201 _("Page not found"), 202 _("Could not find page '%s'.") % id) 203 return self._wrap_in_html(error_page_html) 204 else: 205 return self._wrap_in_html(page.render_to_html())
206
207 - def _wrap_in_html(self, content):
208 HTML_SKELETON = """ 209 <html> 210 <head> 211 </head> 212 <body> 213 %s 214 </body> 215 </html> 216 """ 217 return HTML_SKELETON % content
218