Gnumed.timelinelib.wxgui.components.searchbar.controller

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

# Source Code for Module Gnumed.timelinelib.wxgui.components.searchbar.controller

      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 wx
     20
     21  from timelinelib.wxgui.dialogs.eventlist.view import EventListDialog
     22
     23
     24 -class SearchBarController(object):
     25
     26 -    def __init__(self, view):
     27          self._view = view
     28          self._result = []
     29          self._result_index = 0
     30          self._last_search = None
     31          self._last_period = None
     32
     33 -    def set_timeline_canvas(self, timeline_canvas):
     34          self._timeline_canvas = timeline_canvas
     35          self._view.Enable(timeline_canvas is not None)
     36          if timeline_canvas is not None:
     37              self._view.SetPeriodChoices(self._timeline_canvas.GetPeriodChoices())
     38
     39 -    def search(self):
     40          new_search = self._view.GetValue()
     41          new_period = self._view.GetPeriod()
     42          if (
     43              (self._last_search is not None and self._last_search == new_search) and
     44              (self._last_period is not None and self._last_period == new_period)):
     45              self.next()
     46          else:
     47              self._last_search = new_search
     48              self._last_period = new_period
     49              self._search_for_events(new_search, new_period)
     50              self._result_index = 0
     51              self.navigate_to_match()
     52              self._set_result_label()
     53              self._view.UpdateButtons()
     54
     55 -    def _search_for_events(self, new_search, new_period):
     56          if self._timeline_canvas is not None:
     57              self._result = self._timeline_canvas.GetFilteredEvents(new_search, new_period)
     58          else:
     59              self._result = []
     60
     61 -    def _set_result_label(self):
     62          nbr_of_matches = len(self._result)
     63          if nbr_of_matches == 0:
     64              self._view.UpdateNbrOfMatchesLabel(' ' * 4 + _('No matches found'))
     65          elif nbr_of_matches == 1:
     66              self._view.UpdateNbrOfMatchesLabel(' ' * 4 + _('Only one match found'))
     67          else:
     68              self._view.UpdateNbrOfMatchesLabel(' ' * 4 + '%d ' % nbr_of_matches + _('matches found'))
     69
     70 -    def next(self):
     71          if self._on_last_match():
     72              self._result_index = 0
     73          else:
     74              self._result_index += 1
     75          self.navigate_to_match()
     76          self._view.UpdateButtons()
     77
     78 -    def prev(self):
     79          if not self._on_first_match():
     80              self._result_index -= 1
     81              self.navigate_to_match()
     82              self._view.UpdateButtons()
     83
     84 -    def list(self):
     85          event_list = [event.get_label(self._timeline_canvas.GetTimeType()) for event in self._result]
     86          dlg = EventListDialog(self._view, event_list)
     87          if dlg.ShowModal() == wx.ID_OK:
     88              self._result_index = dlg.GetSelectedIndex()
     89              self.navigate_to_match()
     90          dlg.Destroy()
     91
     92 -    def navigate_to_match(self):
     93          if (self._timeline_canvas is not None and self._result_index in range(len(self._result))):
     94              event = self._result[self._result_index]
     95              self._timeline_canvas.Navigate(lambda tp: tp.center(event.mean_time()))
     96              self._timeline_canvas.HighligtEvent(event, clear=True)
     97
     98 -    def enable_backward(self):
     99          return bool(self._result and self._result_index > 0)
    100
    101 -    def enable_forward(self):
    102          return bool(self._result and self._result_index < (len(self._result) - 1))
    103
    104 -    def enable_list(self):
    105          return bool(len(self._result) > 0)
    106
    107 -    def _on_first_match(self):
    108          return self._result and self._result_index == 0
    109
    110 -    def _on_last_match(self):
    111          return self._result and self._result_index == (len(self._result) - 1)
    112