SourceForge: thetimelineproj/main: timelinelib/gui/dialogs/categoryeditor.py@f33a8af2e46a
timelinelib/gui/dialogs/categoryeditor.py
author Rickard Lindberg <ricli85@gmail.com>
Sat Jan 02 17:00:43 2010 +0100 (2010-01-02)
changeset 603 f33a8af2e46a
parent 602 8d22d55d0232
child 604 b871ab46d8cd
permissions -rw-r--r--
Removed duplicate import.
     1 # Copyright (C) 2009  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 from timelinelib.db.interface import TimelineIOError
    23 from timelinelib.db.objects import Category
    24 from timelinelib.gui.utils import _display_error_message
    25 from timelinelib.gui.utils import _set_focus_and_select
    26 from timelinelib.gui.utils import BORDER
    27 from timelinelib.gui.utils import ID_ERROR
    28 
    29 
    30 class CategoryEditor(wx.Dialog):
    31     """
    32     Dialog used to edit a category.
    33 
    34     The edited category can be fetched with get_edited_category.
    35     """
    36 
    37     def __init__(self, parent, title, timeline, category):
    38         wx.Dialog.__init__(self, parent, title=title)
    39         self._create_gui()
    40         self.timeline = timeline
    41         self.category = category
    42         if self.category == None:
    43             self.category = Category("", (200, 200, 200), True)
    44         self.txt_name.SetValue(self.category.name)
    45         self.colorpicker.SetColour(self.category.color)
    46 
    47     def get_edited_category(self):
    48         return self.category
    49 
    50     def _create_gui(self):
    51         # The name text box
    52         self.txt_name = wx.TextCtrl(self, size=(150, -1))
    53         # The color chooser
    54         self.colorpicker = colourselect.ColourSelect(self)
    55         # Setup layout
    56         vbox = wx.BoxSizer(wx.VERTICAL)
    57         # Grid for controls
    58         field_grid = wx.FlexGridSizer(2, 2, BORDER, BORDER)
    59         field_grid.Add(wx.StaticText(self, label=_("Name:")),
    60                        flag=wx.ALIGN_CENTER_VERTICAL)
    61         field_grid.Add(self.txt_name)
    62         field_grid.Add(wx.StaticText(self, label=_("Color:")),
    63                        flag=wx.ALIGN_CENTER_VERTICAL)
    64         field_grid.Add(self.colorpicker)
    65         vbox.Add(field_grid, flag=wx.EXPAND|wx.ALL, border=BORDER)
    66         # Buttons
    67         button_box = self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL)
    68         self.Bind(wx.EVT_BUTTON, self._btn_ok_on_click, id=wx.ID_OK)
    69         vbox.Add(button_box, flag=wx.ALL|wx.EXPAND, border=BORDER)
    70         self.SetSizerAndFit(vbox)
    71         _set_focus_and_select(self.txt_name)
    72 
    73     def _btn_ok_on_click(self, e):
    74         try:
    75             name = self.txt_name.GetValue().strip()
    76             if not self._name_valid(name):
    77                 msg = _("Category name '%s' not valid. Must be non-empty.")
    78                 _display_error_message(msg % name, self)
    79                 return
    80             if self._name_in_use(name):
    81                 msg = _("Category name '%s' already in use.")
    82                 _display_error_message(msg % name, self)
    83                 return
    84             self.category.name = name
    85             self.category.color = self.colorpicker.GetColour()
    86             self.timeline.save_category(self.category)
    87             self.EndModal(wx.ID_OK)
    88         except TimelineIOError, e:
    89             _display_error_message(e.message, self)
    90             self.error = e
    91             self.EndModal(ID_ERROR)
    92 
    93     def _name_valid(self, name):
    94         return len(name) > 0
    95 
    96     def _name_in_use(self, name):
    97         for cat in self.timeline.get_categories():
    98             if cat != self.category and cat.name == name:
    99                 return True
   100         return False