Removed duplicate import.
1 # Copyright (C) 2009 Rickard Lindberg, Roger Lindberg
3 # This file is part of Timeline.
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.
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.
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/>.
20 import wx.lib.colourselect as colourselect
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
30 class CategoryEditor(wx.Dialog):
32 Dialog used to edit a category.
34 The edited category can be fetched with get_edited_category.
37 def __init__(self, parent, title, timeline, category):
38 wx.Dialog.__init__(self, parent, title=title)
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)
47 def get_edited_category(self):
50 def _create_gui(self):
52 self.txt_name = wx.TextCtrl(self, size=(150, -1))
54 self.colorpicker = colourselect.ColourSelect(self)
56 vbox = wx.BoxSizer(wx.VERTICAL)
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)
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)
73 def _btn_ok_on_click(self, e):
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)
80 if self._name_in_use(name):
81 msg = _("Category name '%s' already in use.")
82 _display_error_message(msg % name, self)
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)
91 self.EndModal(ID_ERROR)
93 def _name_valid(self, name):
96 def _name_in_use(self, name):
97 for cat in self.timeline.get_categories():
98 if cat != self.category and cat.name == name: