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

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

     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  import wx.lib.newevent
    21
    22  from timelinelib.canvas.data.exceptions import TimelineIOError
    23  from timelinelib.wxgui.dialogs.editcontainer.view import EditContainerDialog
    24  import timelinelib.wxgui.utils as gui_utils
    25
    26
    27 -class ContainerChoice(wx.Choice):
    28
    29      ContainerChangedEvent, EVT_CONTAINER_CHANGED = wx.lib.newevent.NewEvent()
    30
    31 -    def __init__(self, parent, db, **kwargs):
    32          wx.Choice.__init__(self, parent, **kwargs)
    33          self.db = db
    34          self._clear()
    35          self.Bind(wx.EVT_CHOICE, self._on_choice)
    36
    37 -    def Fill(self, select_container):
    38          self._clear()
    39          self._fill(self.db.get_containers(), select_container)
    40
    41 -    def GetSelectedContainer(self):
    42          selection = self.GetSelection()
    43          if selection != -1:
    44              return self.GetClientData(selection)
    45          else:
    46              return None
    47
    48 -    def _on_choice(self, event):
    49          new_selection_index = event.GetSelection()
    50          if new_selection_index > self.last_real_container_index:
    51              self.SetSelection(self.current_container_selection)
    52              if new_selection_index == self.add_container_item_index:
    53                  self._add_container()
    54          else:
    55              self.current_container_selection = new_selection_index
    56              wx.PostEvent(self, self.ContainerChangedEvent())
    57
    58 -    def _add_container(self):
    59          dialog = EditContainerDialog(self, _("Add Container"), self.db, None)
    60          if dialog.ShowModal() == wx.ID_OK:
    61              self.Fill(dialog.GetEditedContainer())
    62          dialog.Destroy()
    63
    64 -    def _clear(self):
    65          self.last_real_container_index = None
    66          self.add_container_item_index = None
    67          self.current_container_selection = None
    68          self.Clear()
    69
    70 -    def _fill(self, containers, select_container):
    71          self.Append("", None)
    72          selection_set = False
    73          current_item_index = 1
    74          if select_container is not None and select_container not in containers:
    75              self.Append(select_container.text, select_container)
    76              self._select(current_item_index)
    77              current_item_index += 1
    78              selection_set = True
    79          for container in containers:
    80              self.Append(container.text, container)
    81              if not selection_set:
    82                  if container == select_container:
    83                      self._select(current_item_index)
    84                      selection_set = True
    85              current_item_index += 1
    86          self.last_real_container_index = current_item_index - 1
    87          self.add_container_item_index = self.last_real_container_index + 2
    88          self.Append("", None)
    89          self.Append(_("Add new"), None)
    90          if not selection_set:
    91              self._select(0)
    92
    93 -    def _select(self, index):
    94          self.SetSelection(index)
    95          self.current_container_selection = self.GetSelection()
    96          wx.PostEvent(self, self.ContainerChangedEvent())
    97