Updated Arabic Translation by Djihed Afifi.
[straw.git] / src / lib / CategorySelector.py
blob145ef25eb26b6f91b78cacf1f394a8fb6aa48082
1 """ CategorySelector.py
3 Module for selecting categories
4 """
5 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__ = """
7 Straw is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
12 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA. """
20 import locale
21 import pygtk
22 pygtk.require('2.0')
23 import gobject
24 import gtk
25 import FeedCategoryList
26 import MVP
27 import Event
29 class Column:
30 TITLE=0
31 CATEGORY=1
33 class CategoryView(MVP.WidgetView):
34 """
35 Widget: gtk.ComboBox
36 Model: ListStore
37 """
38 def _initialize(self):
39 self._popup = None
40 self._widget.set_wrap_width(1)
41 title = gtk.CellRendererText()
42 self._widget.pack_start(title,False)
43 self._widget.add_attribute(title,'text',Column.TITLE)
44 self._widget.connect('changed', self._selection_changed)
45 return
47 def _model_set(self):
48 self._widget.set_model(self._model)
49 return
51 def _selection_changed(self, combo, data=None):
52 self._presenter.selection_changed(combo, data)
54 def set_active(self, index): self._widget.set_active(index)
55 def get_active(self): return self._widget.get_active()
57 def get_active_iter(self): return self._widget.get_active_iter()
58 def set_active_iter(self, it): self._widget.set_active_iter(it)
60 class CategoryPresenter(MVP.BasicPresenter):
61 def _initialize(self):
62 self.initialize_slots(Event.CategorySelectionChangedSignal)
63 self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
64 self.model.set_sort_func(Column.TITLE, self._sort_func)
65 self.model.set_sort_column_id(Column.TITLE, gtk.SORT_ASCENDING)
66 self._init_signals()
67 return
69 def _sort_func(self, model, a, b):
70 """
71 From the gtk.TreeSortable.set_sort_func doc:
73 The comparison callback should return -1 if the iter1 row should come before
74 the iter2 row, 0 if the rows are equal, or 1 if the iter1 row should come
75 after the iter2 row.
76 """
77 fclist = FeedCategoryList.get_instance()
78 retval = 0
79 cat_a = model.get_value(a, Column.CATEGORY)
80 cat_b = model.get_value(b, Column.CATEGORY)
82 if cat_a in fclist.pseudo_categories:
83 retval = -1
84 elif cat_b in fclist.pseudo_categories:
85 retval = 1
86 elif cat_a is not None and cat_b is not None:
87 retval = locale.strcoll(cat_a.title, cat_b.title)
88 elif cat_a is not None: retval = -1
89 elif cat_b is not None: retval = 1
90 return retval
92 def _init_signals(self):
93 fclist = FeedCategoryList.get_instance()
94 fclist.signal_connect(Event.FeedCategoryListLoadedSignal,
95 self._display_categories)
96 fclist.signal_connect(Event.FeedCategoryAddedSignal,
97 self._category_added_cb)
98 fclist.signal_connect(Event.FeedCategoryRemovedSignal,
99 self._category_removed_cb)
100 fclist.signal_connect(Event.FeedCategoryChangedSignal,
101 self._category_changed_cb)
102 return
104 def _get_selected_category(self):
105 category = None
106 if len(self._model):
107 active = self._view.get_active()
108 category = self._model[active][Column.CATEGORY]
109 return category
111 def _category_added_cb(self, signal):
112 newcat = signal.category
113 treeiter = self._model.append()
114 self._model.set(treeiter,
115 Column.TITLE, newcat.title,
116 Column.CATEGORY, newcat)
117 return
119 def _category_removed_cb(self, signal):
120 self._model.foreach(self._foreach_remove_cat,
121 signal.category)
122 self._update_selection()
123 return
125 def _foreach_remove_cat(self, model, path, iter, category):
126 cat = model[path][Column.CATEGORY]
127 if cat is category:
128 model.remove(iter)
129 return True
130 return False
132 def _category_changed_cb(self, signal):
133 self._model.foreach(self._foreach_category_changed,
134 signal.sender)
135 self._update_selection()
136 return
138 def _foreach_category_changed(self, model, path, iter, category):
139 cat = model[path][Column.CATEGORY]
140 if cat is category:
141 model.set(iter,
142 Column.TITLE, category.title,
143 Column.CATEGORY, category)
144 return True
145 return False
147 def _display_categories(self, signal):
148 fclist = signal.sender
149 self._model.clear()
150 for category in fclist.pseudo_categories:
151 it = self._model.append()
152 self._model.set(it,
153 Column.TITLE, category.title,
154 Column.CATEGORY, category)
155 for category in fclist.user_categories:
156 it = self._model.append()
157 self._model.set(it,
158 Column.TITLE, category.title,
159 Column.CATEGORY, category)
160 self._update_selection()
161 return
163 def _update_selection(self):
164 active = self._view.get_active()
165 if active < 0:
166 self._view.set_active_iter(self._model.get_iter_first())
167 return
169 def selection_changed(self, combo, data=None):
170 model = combo.get_model()
171 newitem = model[combo.get_active()][1]
172 self.emit_signal(Event.CategorySelectionChangedSignal(self,newitem))
173 return
175 def category_selected(self, category):
176 self._model.foreach(self._foreach_category_selected,
177 category)
178 self._update_selection()
180 def _foreach_category_selected(self, model, path, iter, category):
181 cat = model[path][Column.CATEGORY]
182 if cat is category:
183 self._view.set_active_iter(iter)
184 return True
185 return False