Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / taganddescriptiondialog.py
1 import gtk
2 import re
3 from kofoto.gkofoto.environment import env
4 from kofoto.common import UnimplementedError
5
6 class TagAndDescriptionDialog:
7     def __init__(self, title, tagText=u"", descText=u""):
8         env.assertUnicode(tagText)
9         env.assertUnicode(descText)
10         self._widgets = gtk.glade.XML(env.gladeFile, "tagAndDescriptionDialog")
11         self._dialog = self._widgets.get_widget("tagAndDescriptionDialog")
12         self._dialog.set_title(title)
13         self._tagWidget = self._widgets.get_widget("tag")
14         self._tagWidget.set_text(tagText)
15         self._descWidget = self._widgets.get_widget("description")
16         self._descWidget.set_text(descText)
17         self._descWidget.connect("changed", self._descriptionChanged, self._tagWidget)
18         okbutton = self._widgets.get_widget("okbutton")
19         self._tagWidget.connect("changed", self._tagChanged, okbutton)
20         self.__descText = descText
21         okbutton.set_sensitive(self._isTagOkay(tagText))
22
23     def run(self, ok=None, data=None):
24         result = self._dialog.run()
25         tag = self._tagWidget.get_text().decode("utf-8")
26         desc = self._descWidget.get_text().decode("utf-8")
27         self._dialog.destroy()
28         if result == gtk.RESPONSE_OK:
29             if ok == None:
30                 return None
31             else:
32                 if data:
33                     return ok(tag, desc, data)
34                 else:
35                     return ok(tag, desc)
36         else:
37             return None
38
39     def _isTagOkay(self, tag):
40         raise UnimplementedError
41
42     def __generateTagName(self, descText):
43         env.assertUnicode(descText)
44         return re.sub(r"(?u)\W", "", descText).lower()
45
46     def _descriptionChanged(self, description, tag):
47         newDescText = description.get_text().decode("utf-8")
48         currentTagText = self._tagWidget.get_text()
49         if currentTagText == self.__generateTagName(self.__descText):
50             tag.set_text(self.__generateTagName(newDescText))
51         self.__descText = newDescText
52
53     def _tagChanged(self, tag, button):
54         tagString = tag.get_text().decode("utf-8")
55         button.set_sensitive(self._isTagOkay(tagString))