1 """ CategorySelector.py
3 Module for selecting categories
5 __copyright__
= "Copyright (c) 2002-2005 Free Software Foundation, Inc."
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
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. """
25 import FeedCategoryList
33 class CategoryView(MVP
.WidgetView
):
38 def _initialize(self
):
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
)
48 self
._widget
.set_model(self
._model
)
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
)
69 def _sort_func(self
, model
, a
, b
):
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
77 fclist
= FeedCategoryList
.get_instance()
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
:
84 elif cat_b
in fclist
.pseudo_categories
:
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
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
)
104 def _get_selected_category(self
):
107 active
= self
._view
.get_active()
108 category
= self
._model
[active
][Column
.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
)
119 def _category_removed_cb(self
, signal
):
120 self
._model
.foreach(self
._foreach
_remove
_cat
,
122 self
._update
_selection
()
125 def _foreach_remove_cat(self
, model
, path
, iter, category
):
126 cat
= model
[path
][Column
.CATEGORY
]
132 def _category_changed_cb(self
, signal
):
133 self
._model
.foreach(self
._foreach
_category
_changed
,
135 self
._update
_selection
()
138 def _foreach_category_changed(self
, model
, path
, iter, category
):
139 cat
= model
[path
][Column
.CATEGORY
]
142 Column
.TITLE
, category
.title
,
143 Column
.CATEGORY
, category
)
147 def _display_categories(self
, signal
):
148 fclist
= signal
.sender
150 for category
in fclist
.pseudo_categories
:
151 it
= self
._model
.append()
153 Column
.TITLE
, category
.title
,
154 Column
.CATEGORY
, category
)
155 for category
in fclist
.user_categories
:
156 it
= self
._model
.append()
158 Column
.TITLE
, category
.title
,
159 Column
.CATEGORY
, category
)
160 self
._update
_selection
()
163 def _update_selection(self
):
164 active
= self
._view
.get_active()
166 self
._view
.set_active_iter(self
._model
.get_iter_first())
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
))
175 def category_selected(self
, category
):
176 self
._model
.foreach(self
._foreach
_category
_selected
,
178 self
._update
_selection
()
180 def _foreach_category_selected(self
, model
, path
, iter, category
):
181 cat
= model
[path
][Column
.CATEGORY
]
183 self
._view
.set_active_iter(iter)