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

# Source Code for Module Gnumed.timelinelib.wxgui.dialogs.editevent.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.canvas.data import Event
     22  from timelinelib.canvas.data import Subevent
     23  from timelinelib.canvas.data import TimePeriod
     24  from timelinelib.features.experimental.experimentalfeatures import EXTENDED_CONTAINER_STRATEGY
     25  from timelinelib.utils import ex_msg
     26  from timelinelib.wxgui.framework import Controller
     27
     28
     29 -class EditEventDialogController(Controller):
     30
     31 -    def on_init(self, config, time_type, event_repository, timeline, start, end, event):
     32          self.config = config
     33          self.timeline = timeline
     34          self.time_type = time_type
     35          self.event_repository = event_repository
     36          self._set_values(start, end, event)
     37          self.view.SetFocusOnFirstControl()
     38
     39 -    def on_locked_checkbox_changed(self, event):
     40          self._enable_disable_ends_today()
     41
     42 -    def on_container_changed(self, event):
     43          self._enable_disable_ends_today()
     44          self._enable_disable_locked(EXTENDED_CONTAINER_STRATEGY.enabled())
     45
     46 -    def on_enlarge_click(self, event):
     47          self.reduced_size = self.view.GetSize()
     48          self.reduced_pos = self.view.GetPosition()
     49          screen_width, screen_height = wx.DisplaySize()
     50          dialog_size = (screen_width * 0.9, screen_height * 0.8)
     51          dialog_pos = (screen_width * 0.05, screen_height * 0.05)
     52          self._set_position_and_size(dialog_pos, dialog_size)
     53
     54 -    def on_reduce_click(self, event):
     55          self._set_position_and_size(self.reduced_pos, self.reduced_size)
     56
     57 -    def on_ok_clicked(self, event):
     58          self._create_or_update_event()
     59
     60 -    def on_duplicate(self, evt):
     61          self._create_or_update_event()
     62          if self._done:
     63              self._duplicate_event()
     64
     65 -    def _duplicate_event(self):
     66          dlg = self.view.GetDuplicateEventDialog(self.timeline, self.event)
     67          dlg.ShowModal()
     68          dlg.Destroy()
     69
     70 -    def _create_or_update_event(self):
     71          try:
     72              with self.timeline.transaction("Save event"):
     73                  self._save_event()
     74              if self.view.IsAddMoreChecked():
     75                  self.event = None
     76                  self.view.SetName("")
     77                  self.view.ClearEventData()
     78                  self.view.SetFocusOnFirstControl()
     79                  self._done = False
     80              else:
     81                  if self.opened_from_menu:
     82                      self.config.event_editor_show_period = self.view.GetShowPeriod()
     83                      self.config.event_editor_show_time = self.view.GetShowTime()
     84                  self.view.EndModalOk()
     85                  self._done = True
     86          except ValueError:
     87              self._done = True
     88
     89 -    def _set_values(self, start, end, event):
     90          self.event = event
     91          self.opened_from_menu = self.event is None and start is None
     92          if self.event is None:
     93              self._set_period_in_view(start, end)
     94              self.view.SetName("")
     95              self.view.SetCategory(None)
     96              self.view.SetFuzzy(False)
     97              self.view.SetLocked(False)
     98              self.view.SetEndsToday(False)
     99              self.view.SetContainer(None)
    100              self.view.SetShowAddMoreCheckbox(True)
    101          else:
    102              self._set_period_in_view(
    103                  self.event.get_time_period().start_time,
    104                  self.event.get_time_period().end_time
    105              )
    106              self.view.SetName(self.event.get_text())
    107              self.view.SetCategory(self.event.get_category())
    108              self.view.SetFuzzy(self.event.get_fuzzy())
    109              self.view.SetLocked(self.event.get_locked())
    110              self.view.SetEndsToday(self.event.get_ends_today())
    111              self.view.SetEventData(self.event.data)
    112              if self.event.is_subevent():
    113                  self.view.SetContainer(self.event.container)
    114              else:
    115                  self.view.SetContainer(None)
    116              self.view.SetShowAddMoreCheckbox(False)
    117
    118 -    def _set_period_in_view(self, start, end):
    119          self.view.SetPeriod(self._get_period(start, end))
    120          if self.event:
    121              pass
    122          else:
    123              if self.opened_from_menu:
    124                  self.view.SetShowPeriod(self.config.event_editor_show_period)
    125                  self.view.SetShowTime(self.config.event_editor_show_time)
    126              else:
    127                  pass
    128          if self.config.never_use_time:
    129              self.view.DisableTime()
    130          elif self.event is None and self.config.uncheck_time_for_new_events:
    131              self.view.SetShowTime(False)
    132
    133 -    def _get_period(self, start, end):
    134          if start is None and end is None:
    135              time = self.time_type.now()
    136              return TimePeriod(time, time)
    137          elif end is None:
    138              return TimePeriod(start, start)
    139          else:
    140              return TimePeriod(start, end)
    141
    142 -    def _get_period_from_view(self):
    143          try:
    144              period = self.view.GetPeriod()
    145          except ValueError as ex:
    146              self.view.DisplayInvalidPeriod("%s" % ex_msg(ex))
    147              raise ValueError()
    148          (start, end) = (period.get_start_time(), period.get_end_time())
    149          if self.event is not None and self.view.GetLocked():
    150              self._verify_that_time_has_not_been_changed(start, end)
    151          end = self._adjust_end_if_ends_today(start, end)
    152          TimePeriod(start, end)
    153          return (start, end)
    154
    155 -    def _verify_that_time_has_not_been_changed(self, start, end):
    156          original_start = self.event.get_start_time()
    157          original_end = self.event.get_end_time()
    158          if (original_start != start or
    159              (not self.view.GetEndsToday() and original_end != end)):
    160              self.view.SetPeriod(self.event.get_time_period())
    161              error_message = _("You can't change time when the Event is locked")
    162              self.view.DisplayInvalidPeriod(error_message)
    163              raise ValueError()
    164
    165 -    def _save_event(self):
    166          if self.event is None:
    167              self._create_new_event()
    168          else:
    169              self._update_event()
    170          self.event.data = self.view.GetEventData()
    171          self._save_event_to_db()
    172
    173 -    def _update_event(self):
    174          container_selected = (self.view.GetContainer() is not None)
    175          if container_selected:
    176              self._update_event_when_container_selected()
    177          else:
    178              self._update_event_when_container_not_selected()
    179
    180 -    def _update_event_when_container_selected(self):
    181          if self.event.is_subevent():
    182              self._update_event_when_container_selected_and_event_is_subevent()
    183          else:
    184              self._add_event_to_container()
    185
    186 -    def _update_event_when_container_selected_and_event_is_subevent(self):
    187          if self.event.container == self.view.GetContainer():
    188              (start, end) = self._get_period_from_view()
    189              self.event.update(
    190                  start,
    191                  end,
    192                  self.view.GetName(),
    193                  self.view.GetCategory(),
    194                  self.view.GetFuzzy(),
    195                  self.view.GetLocked(),
    196                  self.view.GetEndsToday()
    197              )
    198          else:
    199              self._change_container()
    200
    201 -    def _update_event_when_container_not_selected(self):
    202          if self.event.is_subevent():
    203              self._remove_event_from_container()
    204          else:
    205              (start, end) = self._get_period_from_view()
    206              self.event.update(
    207                  start,
    208                  end,
    209                  self.view.GetName(),
    210                  self.view.GetCategory(),
    211                  self.view.GetFuzzy(),
    212                  self.view.GetLocked(),
    213                  self.view.GetEndsToday()
    214              )
    215
    216 -    def _remove_event_from_container(self):
    217          self.event.container = None
    218          if self.event.id is not None:
    219              self.timeline.delete_event(self.event)
    220          self._create_new_event()
    221
    222 -    def _add_event_to_container(self):
    223          self.timeline.delete_event(self.event)
    224          self._create_subevent()
    225
    226 -    def _change_container(self):
    227          if self._is_new_container(self.view.GetContainer()):
    228              self._add_new_container()
    229          self._remove_event_from_container()
    230          self.event.container = self.view.GetContainer()
    231
    232 -    def _create_new_event(self):
    233          if self.view.GetContainer() is not None:
    234              self._create_subevent()
    235          else:
    236              (start, end) = self._get_period_from_view()
    237              self.event = Event().update(
    238                  start,
    239                  end,
    240                  self.view.GetName(),
    241                  self.view.GetCategory(),
    242                  self.view.GetFuzzy(),
    243                  self.view.GetLocked(),
    244                  self.view.GetEndsToday()
    245              )
    246
    247 -    def _create_subevent(self):
    248          if self._is_new_container(self.view.GetContainer()):
    249              self._add_new_container()
    250          (start, end) = self._get_period_from_view()
    251          self.event = Subevent().update(
    252              start,
    253              end,
    254              self.view.GetName(),
    255              self.view.GetCategory(),
    256              ends_today=self.view.GetEndsToday()
    257          )
    258          self.event.container = self.view.GetContainer()
    259
    260 -    def _is_new_container(self, container):
    261          return container not in self.timeline.get_containers()
    262
    263 -    def _add_new_container(self):
    264          self._save_container_to_db()
    265
    266 -    def _adjust_end_if_ends_today(self, start, end):
    267          if self.view.GetEndsToday():
    268              end_time = self.time_type.now()
    269          else:
    270              end_time = end
    271          if end_time < start:
    272              self.view.DisplayInvalidPeriod(_("End must be > Start"))
    273              raise ValueError()
    274          return end_time
    275
    276 -    def _save_event_to_db(self):
    277          self.event_repository.save(self.event)
    278
    279 -    def _save_container_to_db(self):
    280          self.event_repository.save(self.view.GetContainer())
    281
    282 -    def _enable_disable_ends_today(self):
    283          enable = ((self._container_not_selected() or self._container_allows_ends_today()) and
    284                    not self.view.GetLocked() and
    285                    self._start_is_in_history())
    286          self.view.EnableEndsToday(enable)
    287
    288 -    def _enable_disable_locked(self, extended_container_strategy_enabled):
    289          if self.event is not None and extended_container_strategy_enabled:
    290              enable = not self.event.is_container()
    291          else:
    292              enable = self._container_not_selected()
    293          self.view.EnableLocked(enable)
    294
    295 -    def _container_not_selected(self):
    296          return self.view.GetContainer() is None
    297
    298 -    def _container_allows_ends_today(self):
    299          container = self.view.GetContainer()
    300          if container is None:
    301              return True
    302          else:
    303              return container.allow_ends_today_on_subevents()
    304
    305 -    def _start_is_in_history(self):
    306          if self.event is None:
    307              return True
    308          if self.event.get_start_time() is None:
    309              return False
    310          return self.event.get_start_time() < self.time_type.now()
    311
    312 -    def _set_position_and_size(self, pos, size):
    313          self.view.SetPosition(pos)
    314          self.view.SetSize(size)
    315