Gnumed.timelinelib.wxgui.dialogs.categoryeditor

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

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

      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  import wx.lib.colourselect as colourselect
     21
     22  import timelinelib.wxgui.utils as gui_utils
     23  from timelinelib.wxgui.utils import _display_error_message
     24  from timelinelib.wxgui.utils import _set_focus_and_select
     25  from timelinelib.wxgui.utils import BORDER
     26  from timelinelib.editors.category import CategoryEditor
     27  from timelinelib.repositories.dbwrapper import DbWrapperCategoryRepository
     28
     29
     30 -class WxCategoryEdtiorDialog(wx.Dialog):
     31
     32 -    def __init__(self, parent, title, timeline, category):
     33          wx.Dialog.__init__(self, parent, title=title)
     34          self._create_gui()
     35          self.controller = CategoryEditor(self)
     36          self.controller.edit(category, DbWrapperCategoryRepository(timeline))
     37
     38 -    def set_category_tree(self, tree):
     39          def add_tree(tree, indent=""):
     40              for (root, subtree) in tree:
     41                  self.parentlistbox.Append(indent + root.name, root)
     42                  add_tree(subtree, indent + "    ")
     43          self.parentlistbox.Clear()
     44          self.parentlistbox.Append("", None) # No parent
     45          add_tree(tree)
     46          self.SetSizerAndFit(self.vbox)
     47
     48 -    def get_name(self):
     49          return self.txt_name.GetValue().strip()
     50
     51 -    def set_name(self, new_name):
     52          self.txt_name.SetValue(new_name)
     53
     54 -    def get_color(self):
     55          # Convert wx.Color to (r, g, b) tuple
     56          (r, g, b) = self.colorpicker.GetValue()
     57          return (r, g, b)
     58
     59 -    def get_font_color(self):
     60          # Convert wx.Color to (r, g, b) tuple
     61          (r, g, b) = self.fontcolorpicker.GetValue()
     62          return (r, g, b)
     63
     64 -    def set_color(self, new_color):
     65          self.colorpicker.SetValue(new_color)
     66
     67 -    def set_font_color(self, new_color):
     68          self.fontcolorpicker.SetValue(new_color)
     69
     70 -    def get_parent(self):
     71          selection = self.parentlistbox.GetSelection()
     72          if selection == wx.NOT_FOUND:
     73              return None
     74          return self.parentlistbox.GetClientData(selection)
     75
     76 -    def set_parent(self, parent):
     77          no_items = self.parentlistbox.GetCount()
     78          for i in range(0, no_items):
     79              if self.parentlistbox.GetClientData(i) is parent:
     80                  self.parentlistbox.SetSelection(i)
     81                  return
     82
     83 -    def close(self):
     84          self.EndModal(wx.ID_OK)
     85
     86 -    def handle_invalid_name(self, name):
     87          msg = _("Category name '%s' not valid. Must be non-empty.")
     88          _display_error_message(msg % name, self)
     89          _set_focus_and_select(self.txt_name)
     90
     91 -    def handle_used_name(self, name):
     92          msg = _("Category name '%s' already in use.")
     93          _display_error_message(msg % name, self)
     94          _set_focus_and_select(self.txt_name)
     95
     96 -    def handle_db_error(self, e):
     97          gui_utils.handle_db_error_in_dialog(self, e)
     98
     99 -    def get_edited_category(self):
    100          return self.controller.category
    101
    102 -    def _create_gui(self):
    103          self.vbox = wx.BoxSizer(wx.VERTICAL)
    104          field_grid = self._create_field_grid()
    105          button_box = self._create_button_box()
    106          self.vbox.Add(field_grid, flag=wx.ALL|wx.EXPAND, border=BORDER)
    107          self.vbox.Add(button_box, flag=wx.ALL|wx.EXPAND, border=BORDER)
    108          _set_focus_and_select(self.txt_name)
    109
    110 -    def _create_field_grid(self):
    111          self.txt_name = wx.TextCtrl(self, size=(150, -1))
    112          self.colorpicker = colourselect.ColourSelect(self)
    113          self.fontcolorpicker = colourselect.ColourSelect(self)
    114          self.parentlistbox = wx.Choice(self, wx.ID_ANY)
    115          grid = wx.FlexGridSizer(3, 2, BORDER, BORDER)
    116          self._add_ctrl_to_grid(_("Name:"), self.txt_name, grid)
    117          self._add_ctrl_to_grid(_("Color:"), self.colorpicker, grid)
    118          self._add_ctrl_to_grid(_("Font Color:"), self.fontcolorpicker, grid)
    119          self._add_ctrl_to_grid(_("Parent:"), self.parentlistbox, grid)
    120          return grid
    121
    122 -    def _add_ctrl_to_grid(self, name, ctrl, grid):
    123          grid.Add(wx.StaticText(self, label=name), flag=wx.ALIGN_CENTER_VERTICAL)
    124          grid.Add(ctrl)
    125
    126 -    def _create_button_box(self):
    127          button_box = self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL)
    128          self.Bind(wx.EVT_BUTTON, self._btn_ok_on_click, id=wx.ID_OK)
    129          return button_box
    130
    131 -    def _btn_ok_on_click(self, e):
    132          self.controller.save()
    133