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

# Source Code for Module Gnumed.timelinelib.wxgui.dialogs.categorieseditor

      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 wx
     20
     21  import timelinelib.wxgui.utils as gui_utils
     22  from timelinelib.wxgui.utils import BORDER
     23  from timelinelib.wxgui.components.cattree import CategoriesTree
     24  from timelinelib.wxgui.components.cattree import add_category
     25  from timelinelib.wxgui.components.cattree import edit_category
     26  from timelinelib.wxgui.components.cattree import delete_category
     27
     28
     29 -class CategoriesEditor(wx.Dialog):
     30      """
     31      Dialog used to edit categories of a timeline.
     32
     33      The edits happen immediately. In other words: when the dialog is closing
     34      all edits have been applied already.
     35      """
     36
     37 -    def __init__(self, parent, timeline):
     38          wx.Dialog.__init__(self, parent, title=_("Edit Categories"))
     39          self.db = timeline
     40          self.cat_tree = self._create_gui()
     41          self._fill_controls_with_data()
     42
     43 -    def _fill_controls_with_data(self):
     44          self.cat_tree.initialize_from_db(self.db)
     45
     46 -    def _create_gui(self):
     47          vbox = wx.BoxSizer(wx.VERTICAL)
     48          cat_tree = self._create_cat_tree(vbox)
     49          self._create_buttons(vbox)
     50          self.SetSizerAndFit(vbox)
     51          self.Bind(wx.EVT_CLOSE, self._window_on_close)
     52          return cat_tree
     53
     54 -    def _window_on_close(self, e):
     55          self.cat_tree.destroy()
     56          self.EndModal(wx.ID_CLOSE)
     57
     58 -    def _create_cat_tree(self, vbox):
     59          cat_tree = CategoriesTree(self, self.db_error_handler)
     60          cat_tree.SetMinSize((-1, 200))
     61          self.Bind(wx.EVT_TREE_SEL_CHANGED, self._cat_tree_on_sel_changed,
     62                    cat_tree)
     63          vbox.Add(cat_tree, flag=wx.ALL|wx.EXPAND, border=BORDER)
     64          return cat_tree
     65
     66 -    def _cat_tree_on_sel_changed(self, e):
     67          self._updateButtons()
     68
     69 -    def _create_buttons(self, vbox):
     70          button_box = wx.BoxSizer(wx.HORIZONTAL)
     71          self.btn_edit = self._create_edit_button(button_box)
     72          self._create_add_button(button_box)
     73          self.btn_del = self._create_delete_button(button_box)
     74          self._create_close_button(button_box)
     75          vbox.Add(button_box, flag=wx.ALL|wx.EXPAND, border=BORDER)
     76
     77 -    def _create_edit_button(self, button_box):
     78          btn = wx.Button(self, wx.ID_EDIT)
     79          btn.Disable()
     80          self.Bind(wx.EVT_BUTTON, self._btn_edit_on_click, btn)
     81          button_box.Add(btn, flag=wx.RIGHT, border=BORDER)
     82          return btn
     83
     84 -    def _btn_edit_on_click(self, e):
     85          selected_category = self.cat_tree.get_selected_category()
     86          if selected_category is not None:
     87              edit_category(self, self.db, selected_category,
     88                            self.db_error_handler)
     89              self._updateButtons()
     90
     91 -    def _create_add_button(self, button_box):
     92          btn = wx.Button(self, wx.ID_ADD)
     93          self.Bind(wx.EVT_BUTTON, self._btn_add_on_click, btn)
     94          button_box.Add(btn, flag=wx.RIGHT, border=BORDER)
     95          return btn
     96
     97 -    def _btn_add_on_click(self, e):
     98          add_category(self, self.db, self.db_error_handler)
     99          self._updateButtons()
    100
    101 -    def _create_delete_button(self, button_box):
    102          btn = wx.Button(self, wx.ID_DELETE)
    103          btn.Disable()
    104          self.Bind(wx.EVT_BUTTON, self._btn_del_on_click, btn)
    105          button_box.Add(btn, flag=wx.RIGHT, border=BORDER)
    106          return btn
    107
    108 -    def _btn_del_on_click(self, e):
    109          selected_category = self.cat_tree.get_selected_category()
    110          if selected_category is not None:
    111              delete_category(self, self.db, selected_category,
    112                              self.db_error_handler)
    113              self._updateButtons()
    114
    115 -    def _create_close_button(self, button_box):
    116          btn = wx.Button(self, wx.ID_CLOSE)
    117          self.Bind(wx.EVT_BUTTON, self._btn_close_on_click, btn)
    118          button_box.Add(btn, flag=wx.LEFT, border=BORDER)
    119          return btn
    120
    121 -    def _btn_close_on_click(self, e):
    122          self.Close()
    123
    124 -    def db_error_handler(self, e):
    125          gui_utils.handle_db_error_in_dialog(self, e)
    126
    127 -    def _updateButtons(self):
    128          selected_category = self.cat_tree.get_selected_category() is not None
    129          self.btn_edit.Enable(selected_category)
    130          self.btn_del.Enable(selected_category)
    131