OSDN Git Service

Enable to add custom sort function.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 18 Sep 2006 11:21:57 +0000 (20:21 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 18 Sep 2006 11:21:57 +0000 (20:21 +0900)
src/FukuiNoNamari/threadlistmodel.py

index 1b7cef6..72347f2 100644 (file)
@@ -35,9 +35,19 @@ class ThreadListModel(gtk.GenericTreeModel):
         self.sort_column_name = "num"
         self.sort_reverse = False
         self.filter_func = None
+        self.compare_func = {}
 
         self.set_list([])
 
+    def set_compare_func(self, name, func, key=None):
+        self.compare_func[name] = (func, key)
+
+    def del_compare_func(self, name):
+        try:
+            del self.filter_func[name]
+        except:
+            pass
+
     def modify_row(self, dict):
         id = dict["id"]
         if id in self.order_dict:
@@ -108,11 +118,18 @@ class ThreadListModel(gtk.GenericTreeModel):
 
     def do_sort(self, column_name, reverse):
         sort_list = self.list[:]
-        if self.column_types[self.column_names.index(column_name)] == str:
-            sort_list.sort(None, lambda dic: dic[column_name], reverse)
+        if column_name in self.compare_func:
+            h = self.compare_func[column_name][0]
+            key = self.compare_func[column_name][1]
+            if not h:
+                h = self.compare
+            sort_list.sort(lambda x, y: h(x, y, reverse), key, reverse)
         else:
-            h = lambda x, y: self.compare(x, y, reverse)
-            sort_list.sort(h, lambda dic: dic[column_name], reverse)
+            if self.column_types[self.column_names.index(column_name)] == str:
+                sort_list.sort(None, lambda dic: dic[column_name], reverse)
+            else:
+                h = lambda x, y: self.compare(x, y, reverse)
+                sort_list.sort(h, lambda dic: dic[column_name], reverse)
         self.list = sort_list
         self.build_order_dict()