OSDN Git Service

Enable to get dictionaries in the data store.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / threadlistmodel.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import pygtk
19 pygtk.require('2.0')
20 import gtk
21 import itertools
22
23
24 class ThreadListModel(gtk.GenericTreeModel):
25     column_types = (str, int, str, int, int, int, float)
26     column_names = ["id", "num", "title", "res", "lineCount",
27                     "lastModified", "average"]
28     column_width = [0, 30, 330, 50, 50, 200, 50]
29
30     def __init__(self):
31         gtk.GenericTreeModel.__init__(self)
32         self.original_list = []
33         self.list = []
34         self.order_dict = {}
35         self.sort_column_name = "num"
36         self.sort_reverse = False
37         self.filter_func = None
38         self.compare_func = {}
39
40         self.set_list([])
41
42     def set_compare_func(self, name, func, key=None):
43         self.compare_func[name] = (func, key)
44
45     def del_compare_func(self, name):
46         try:
47             del self.filter_func[name]
48         except:
49             pass
50
51     def get_dict(self, iter):
52         path = self.get_path(iter)
53         return self.list[path[0]]
54
55     def modify_row(self, dict):
56         id = dict["id"]
57         if id in self.order_dict:
58             index = self.order_dict[id]
59             target_dict = self.list[index]
60             for name in self.column_names:
61                 if name in dict:
62                     target_dict[name] = dict[name]
63             self.row_changed(index, self.get_iter(index))
64
65     def refilter(self, filter_func):
66         before_size = len(self.list)
67         self.do_filter(filter_func)
68         after_size = len(self.list)
69         self.do_sort(self.sort_column_name, self.sort_reverse)
70         self.list_modified(before_size, after_size)
71         
72     def set_list(self, newlist):
73         self.original_list = newlist[:]
74         self.refilter(self.filter_func)
75
76     def build_order_dict(self):
77         # key: thread id, value: index in self.list
78         self.order_dict = dict(
79             [(item["id"], index) for index, item in enumerate(self.list)])
80
81     def list_modified(self, before_size, after_size):
82         if before_size > after_size:
83             print "remove", before_size-after_size, "threads"
84             for i in range(before_size - after_size):
85                 self.row_deleted(after_size)
86         elif after_size > before_size:
87             print "insert", after_size-before_size, "threads"
88             for i in range(before_size, after_size):
89                 self.row_inserted(i, self.get_iter(i))
90
91     def do_filter(self, filter_func):
92         # Set filter_func to self.filter_func after successful filtering.
93         # successful means no exception.
94
95         if not filter_func:
96             self.list = self.original_list[:]
97             self.filter_func = filter_func
98             return
99
100         predicate = lambda item: filter_func(self, item)
101         iterable = itertools.ifilter(predicate, self.original_list)
102         self.list = [item for item in iterable]
103         self.filter_func = filter_func
104
105     def compare(self, v1, v2, reverse):
106         if not reverse:
107             if v1 == 0 and v2 == 0:
108                 return 0
109             elif v1 == 0:
110                 return 1
111             elif v2 == 0:
112                 return -1
113         if v1 > v2:
114             return 1
115         elif v1 < v2:
116             return -1
117         else:
118             return 0
119
120     def get_sort(self):
121         return self.sort_column_name, self.sort_reverse
122
123     def do_sort(self, column_name, reverse):
124         sort_list = self.list[:]
125         if column_name in self.compare_func:
126             h = self.compare_func[column_name][0]
127             key = self.compare_func[column_name][1]
128             if not h:
129                 h = self.compare
130             sort_list.sort(lambda x, y: h(x, y, reverse), key, reverse)
131         else:
132             if self.column_types[self.column_names.index(column_name)] == str:
133                 sort_list.sort(None, lambda dic: dic[column_name], reverse)
134             else:
135                 h = lambda x, y: self.compare(x, y, reverse)
136                 sort_list.sort(h, lambda dic: dic[column_name], reverse)
137         self.list = sort_list
138         self.build_order_dict()
139
140     def sort(self, column_name, order_specified=False, reverse=False):
141         old_order_dict = self.order_dict
142
143         if order_specified:
144             sort_reverse = reverse
145         else:
146             if column_name == self.sort_column_name:
147                 # sort reverse
148                 sort_reverse = not self.sort_reverse
149             else:
150                 sort_reverse = False
151
152         self.do_sort(column_name, sort_reverse)
153         self.sort_column_name = column_name
154         self.sort_reverse = sort_reverse
155
156         neworder = [old_order_dict[item["id"]] for item in self.list]
157         self.rows_reordered(None, None, neworder)
158
159     def on_get_flags(self):
160         return gtk.TREE_MODEL_LIST_ONLY
161
162     def on_get_n_columns(self):
163         return len(self.column_types)
164
165     def on_get_column_type(self, n):
166         return self.column_types[n]
167
168     def on_get_iter(self, path):
169         try:
170             return self.list[path[0]]
171         except IndexError:
172             return None
173
174     def on_get_path(self, rowref):
175         return self.order_dict[rowref["id"]]
176
177     def on_get_value(self, rowref, column):
178         return rowref[self.column_names[column]]
179
180     def on_iter_next(self, rowref):
181         try:
182             i = self.order_dict[rowref["id"]] + 1
183             return self.list[i]
184         except IndexError:
185             return None
186
187     def on_iter_children(self, rowref):
188         if rowref:
189             return None
190         return self.list[0]
191
192     def on_iter_has_child(self, rowref):
193         return False
194
195     def on_iter_n_children(self, rowref):
196         if rowref:
197             return 0
198         return len(self.list)
199
200     def on_iter_nth_child(self, rowref, n):
201         if rowref:
202             return None
203         try:
204             return self.list[n]
205         except IndexError:
206             return None
207
208     def on_iter_parent(self, child):
209         return None