Gnumed.timelinelib.wxgui.frames.helpbrowserframe.helpbrowserframe

Package Gnumed :: Package timelinelib :: Package wxgui :: Package frames :: Package helpbrowserframe :: Module helpbrowserframe |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.frames.helpbrowserframe.helpbrowserframe

      1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  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  import webbrowser
     21
     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 HelpBrowserFrame(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, 650), 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.helppagerepository as help
     51              import timelinelib.help.pages as help_pages
     52              self.help_system = help.HelpPageRepository(
     53                  "contents", HELP_RESOURCES_DIR + "/", "page:")
     54              help_pages.install(self.help_system)
     55
     56 -    def show_contents_page(self, e):
     57          self.show_page("contents")
     58
     59 -    def show_contact_page(self, e):
     60          self.show_page("contact")
     61
     62 -    def show_page(self, id, type="page", change_history=True):
     63          """
     64          Where which is a tuple (type, id):
     65
     66            * (page, page_id)
     67            * (search, search_string)
     68          """
     69          if self.help_system is None:
     70              display_error_message(
     71                  _("Could not find markdown Python package. It is needed by the help system."),
     72                  self.GetParent())
     73              return
     74          if change_history:
     75              same_page_as_last = False
     76              if self.current_pos != -1:
     77                  _, current_id = self.history[self.current_pos]
     78                  if id == current_id:
     79                      same_page_as_last = True
     80              if same_page_as_last is False:
     81                  self.history = self.history[:self.current_pos + 1]
     82                  self.history.append((type, id))
     83                  self.current_pos += 1
     84          self._update_buttons()
     85          if type == "page":
     86              self.html_window.SetPage(self._generate_page(id))
     87          elif type == "search":
     88              self.html_window.SetPage(self.help_system.get_search_results_page(id))
     89          self.Show()
     90          self.Raise()
     91
     92 -    def _create_gui(self):
     93          self.Bind(wx.EVT_CLOSE, self._window_on_close)
     94          self.toolbar = self.CreateToolBar()
     95          size = (24, 24)
     96          if 'wxMSW' in wx.PlatformInfo:
     97              home_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "home.png"))
     98              back_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "backward.png"))
     99              forward_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "forward.png"))
    100          else:
    101              home_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR,
    102                                                  size)
    103              back_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR,
    104                                                  size)
    105              forward_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD,
    106                                                     wx.ART_TOOLBAR, size)
    107          self.toolbar.SetToolBitmapSize(size)
    108          # Home
    109          home_str = _("Go to home page")
    110          self.toolbar.AddTool(HelpBrowserFrame.HOME_ID, home_str,
    111                                    home_bmp, shortHelp=home_str)
    112          self.Bind(wx.EVT_TOOL, self._toolbar_on_click, id=HelpBrowserFrame.HOME_ID)
    113          # Separator
    114          self.toolbar.AddSeparator()
    115          # Backward
    116          backward_str = _("Go back one page")
    117          self.toolbar.AddTool(HelpBrowserFrame.BACKWARD_ID, backward_str,
    118                                    back_bmp, shortHelp=backward_str)
    119          self.Bind(wx.EVT_TOOL, self._toolbar_on_click,
    120                    id=HelpBrowserFrame.BACKWARD_ID)
    121          # Forward
    122          forward_str = _("Go forward one page")
    123          self.toolbar.AddTool(HelpBrowserFrame.FORWARD_ID, forward_str,
    124                                    forward_bmp, shortHelp=forward_str)
    125          self.Bind(wx.EVT_TOOL, self._toolbar_on_click,
    126                    id=HelpBrowserFrame.FORWARD_ID)
    127          # Separator
    128          self.toolbar.AddSeparator()
    129          # Search
    130          self.search = wx.SearchCtrl(self.toolbar, size=(150, -1),
    131                                      style=wx.TE_PROCESS_ENTER)
    132          self.Bind(wx.EVT_TEXT_ENTER, self._search_on_text_enter, self.search)
    133          self.toolbar.AddControl(self.search)
    134          self.toolbar.Realize()
    135          # Html window
    136          self.html_window = wx.html.HtmlWindow(self)
    137          self.Bind(wx.html.EVT_HTML_LINK_CLICKED,
    138                    self._html_window_on_link_clicked, self.html_window)
    139          self.html_window.Connect(wx.ID_ANY, wx.ID_ANY, wx.EVT_KEY_DOWN.typeId,
    140                                   self._window_on_key_down)
    141
    142 -    def _window_on_close(self, e):
    143          self.Show(False)
    144
    145 -    def _window_on_key_down(self, evt):
    146          """
    147          Event handler used when a keyboard key has been pressed.
    148
    149          The following keys are handled:
    150          Key         Action
    151          --------    ------------------------------------
    152          Backspace   Go to previous page
    153          """
    154          keycode = evt.GetKeyCode()
    155          if keycode == wx.WXK_BACK:
    156              self._go_back()
    157          evt.Skip()
    158
    159 -    def _toolbar_on_click(self, e):
    160          if e.GetId() == HelpBrowserFrame.HOME_ID:
    161              self._go_home()
    162          elif e.GetId() == HelpBrowserFrame.BACKWARD_ID:
    163              self._go_back()
    164          elif e.GetId() == HelpBrowserFrame.FORWARD_ID:
    165              self._go_forward()
    166
    167 -    def _search_on_text_enter(self, e):
    168          self._search(self.search.GetValue())
    169
    170 -    def _html_window_on_link_clicked(self, e):
    171          url = e.GetLinkInfo().GetHref()
    172          if url.startswith("page:"):
    173              self.show_page(url[5:])
    174          else:
    175              webbrowser.open(url)
    176
    177 -    def _go_home(self):
    178          self.show_page(self.help_system.home_page)
    179
    180 -    def _go_back(self):
    181          if self.current_pos > 0:
    182              self.current_pos -= 1
    183              current_type, current_id = self.history[self.current_pos]
    184              self.show_page(current_id, type=current_type, change_history=False)
    185
    186 -    def _go_forward(self):
    187          if self.current_pos < len(self.history) - 1:
    188              self.current_pos += 1
    189              current_type, current_id = self.history[self.current_pos]
    190              self.show_page(current_id, type=current_type, change_history=False)
    191
    192 -    def _search(self, string):
    193          self.show_page(string, type="search")
    194
    195 -    def _update_buttons(self):
    196          history_len = len(self.history)
    197          enable_backward = history_len > 1 and self.current_pos > 0
    198          enable_forward = history_len > 1 and self.current_pos < history_len - 1
    199          self.toolbar.EnableTool(HelpBrowserFrame.BACKWARD_ID, enable_backward)
    200          self.toolbar.EnableTool(HelpBrowserFrame.FORWARD_ID, enable_forward)
    201
    202 -    def _generate_page(self, page_id):
    203          page = self.help_system.get_page(page_id)
    204          if page is None:
    205              error_page_html = "<h1>%s</h1><p>%s</p>" % (
    206                  _("Page not found"),
    207                  _("Could not find page '%s'.") % page_id)
    208              return self._wrap_in_html(error_page_html)
    209          else:
    210              return self._wrap_in_html(page.render_to_html())
    211
    212 -    def _wrap_in_html(self, content):
    213          HTML_SKELETON = """
    214          <html>
    215          <head>
    216          </head>
    217          <body>
    218          %s
    219          </body>
    220          </html>
    221          """
    222          return HTML_SKELETON % content
    223