OSDN Git Service

Improve a modification of ThreadListModel and implement a filtering system.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 18 Aug 2006 06:08:04 +0000 (15:08 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 18 Aug 2006 06:08:04 +0000 (15:08 +0900)
src/Hage1/board_window.py

index 5c3bd22..1f45821 100644 (file)
@@ -42,6 +42,7 @@ class WinWrap:
         self.mainwin = gtk.glade.XML(glade_path)
 
         self.treeview = self.mainwin.get_widget("treeview")
+        self.treeview.set_model(ThreadListModel())
         self.treeview.set_rules_hint(True)
 
         self.popupmenu = self.mainwin.get_widget("popup_menu")
@@ -205,13 +206,7 @@ class WinWrap:
             list_list.append(dic)
 
         model = self.treeview.get_model()
-        if model:
-            sort_column_name, sort_reverse = model.get_sort()
-            model = ThreadListModel(list_list, sort_column_name, sort_reverse)
-        else:
-            model = ThreadListModel(list_list)
-        self.treeview.set_model(model)
-
+        model.set_list(list_list)
         self.reset_sort_indicator()
 
         print "end"
@@ -224,18 +219,62 @@ class ThreadListModel(gtk.GenericTreeModel):
     column_names = ["id", "num", "title", "res", "lineCount",
                     "lastModified", "average"]
 
-    def __init__(self, list, sort_column_name="num", sort_reverse=False):
+    def __init__(self, list=None, sort_column_name="num", sort_reverse=False,
+                 filter_func=None):
         gtk.GenericTreeModel.__init__(self)
-        self.list = list
+        if list:
+            self.original_list = list[:]
+        else:
+            self.original_list = []
+
         self.sort_column_name = sort_column_name
         self.sort_reverse = sort_reverse
+        self.filter_func = filter_func
+
+        self.do_filter()
+        self.do_sort(self.sort_column_name, self.sort_reverse)
+
+    def refilter(self):
+        before_size = len(self.list)
+        self.do_filter()
+        after_size = len(self.list)
         self.do_sort(self.sort_column_name, self.sort_reverse)
+        self.list_modified(before_size, after_size)
+        
+    def set_list(self, newlist):
+        self.original_list = newlist[:]
+        self.refilter()
+
+    def set_filter_func(func):
+        self.filter_func = func
 
     def build_order_dict(self):
         # key: thread id, value: index in self.list
         self.order_dict = dict(
             [(item["id"], index) for index, item in enumerate(self.list)])
+
+    def list_modified(self, before_size, after_size):
+        if before_size > after_size:
+            print "remove", before_size-after_size, "threads"
+            for i in range(before_size - after_size):
+                self.row_deleted(after_size)
+        elif after_size > before_size:
+            print "insert", after_size-before_size, "threads"
+            for i in range(before_size, after_size):
+                self.row_inserted(i, self.get_iter(i))
+        elif after_size != 0:
+            self.row_changed(0, self.get_iter(0))
+
+    def do_filter(self):
+        if not self.filter_func:
+            self.list = self.original_list[:]
+            return
+
+        self.list = []
+        for item in self.original_list:
+            if self.filter_func(self, item):
+                self.list.append(item)
+
     def compare(self, v1, v2, reverse):
         if not reverse:
             if v1 == 0 and v2 == 0:
@@ -282,7 +321,10 @@ class ThreadListModel(gtk.GenericTreeModel):
         return self.column_types[n]
 
     def on_get_iter(self, path):
-        return self.list[path[0]]
+        try:
+            return self.list[path[0]]
+        except IndexError:
+            return None
 
     def on_get_path(self, rowref):
         return self.order_dict[rowref["id"]]