Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Module search |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.components.search

      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
     23  from timelinelib.config.paths import ICONS_DIR
     24
     25
     26 -class SearchBar(wx.ToolBar):
     27
     28 -    def __init__(self, parent, close_fn):
     29          wx.ToolBar.__init__(self, parent, style=wx.TB_HORIZONTAL|wx.TB_BOTTOM)
     30          self.last_search = None
     31          self.result = []
     32          self.result_index = 0
     33          self.view = None
     34          self.close_fn = close_fn
     35          self._create_gui()
     36          self._update_buttons()
     37
     38 -    def set_view(self, view):
     39          self.view = view
     40          self.Enable(view is not None)
     41
     42 -    def _create_gui(self):
     43          icon_size = (16, 16)
     44          # Close button
     45          if 'wxMSW' in wx.PlatformInfo:
     46              close_bmp = wx.Bitmap(os.path.join(ICONS_DIR, "close.png"))
     47          else:
     48              close_bmp = wx.ArtProvider.GetBitmap(wx.ART_CROSS_MARK, wx.ART_TOOLBAR,
     49                                               icon_size)
     50          self.AddLabelTool(wx.ID_CLOSE, "", close_bmp, shortHelp="")
     51          self.Bind(wx.EVT_TOOL, self._btn_close_on_click, id=wx.ID_CLOSE)
     52          # Search box
     53          self.search = wx.SearchCtrl(self, size=(150, -1),
     54                                      style=wx.TE_PROCESS_ENTER)
     55          self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN,
     56                    self._search_on_search_btn, self.search)
     57          self.Bind(wx.EVT_TEXT_ENTER, self._search_on_text_enter, self.search)
     58          self.AddControl(self.search)
     59          # Prev button
     60          prev_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR,
     61                                              icon_size)
     62          self.AddLabelTool(wx.ID_BACKWARD, "", prev_bmp, shortHelp="")
     63          self.Bind(wx.EVT_TOOL, self._btn_prev_on_click, id=wx.ID_BACKWARD)
     64          # Next button
     65          next_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_TOOLBAR,
     66                                              icon_size)
     67          self.AddLabelTool(wx.ID_FORWARD, "", next_bmp, shortHelp="")
     68          self.Bind(wx.EVT_TOOL, self._btn_next_on_click, id=wx.ID_FORWARD)
     69          # No match label
     70          self.lbl_no_match = wx.StaticText(self, label=_("No match"))
     71          self.lbl_no_match.Show(False)
     72          self.AddControl(self.lbl_no_match)
     73          # Single match label
     74          self.lbl_single_match = wx.StaticText(self, label=_("Only one match"))
     75          self.lbl_single_match.Show(False)
     76          self.AddControl(self.lbl_single_match)
     77          # Finish it up
     78          self.Realize()
     79
     80 -    def _btn_close_on_click(self, e):
     81          self.close_fn()
     82
     83 -    def _search_on_search_btn(self, e):
     84          self._search()
     85
     86 -    def _search_on_text_enter(self, e):
     87          self._search()
     88
     89 -    def _btn_prev_on_click(self, e):
     90          self._prev()
     91
     92 -    def _btn_next_on_click(self, e):
     93          self._next()
     94
     95 -    def _search(self):
     96          new_search = self.search.GetValue()
     97          if self.last_search is not None and self.last_search == new_search:
     98              self._next()
     99          else:
    100              self.last_search = new_search
    101              if self.view is not None:
    102                  events = self.view.get_timeline().search(self.last_search)
    103                  filtered_events = self.view.get_view_properties().filter_events(events)
    104                  self.result = filtered_events
    105              else:
    106                  self.result = []
    107              self.result_index = 0
    108              self._navigate_to_match()
    109              self.lbl_no_match.Show(len(self.result) == 0)
    110              self.lbl_single_match.Show(len(self.result) == 1)
    111          self._update_buttons()
    112
    113 -    def _update_buttons(self):
    114          enable_backward = bool(self.result and self.result_index > 0)
    115          self.EnableTool(wx.ID_BACKWARD, enable_backward)
    116          enable_forward = bool(self.result and
    117                                self.result_index < (len(self.result) - 1))
    118          self.EnableTool(wx.ID_FORWARD, enable_forward)
    119
    120 -    def _next(self):
    121          if self.result > 0 and self.result_index < (len(self.result) - 1):
    122              self.result_index += 1
    123              self._navigate_to_match()
    124              self._update_buttons()
    125
    126 -    def _prev(self):
    127          if self.result > 0 and self.result_index > 0:
    128              self.result_index -= 1
    129              self._navigate_to_match()
    130              self._update_buttons()
    131
    132 -    def _navigate_to_match(self):
    133          if (self.view is not None and
    134              self.result_index in range(len(self.result))):
    135              event = self.result[self.result_index]
    136              self.view.navigate_timeline(lambda tp: tp.center(event.mean_time()))
    137