OSDN Git Service

board window performance improvement
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / board_window.py
index dcd4136..6086e50 100644 (file)
@@ -29,47 +29,8 @@ GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                          "..", "data")
 GLADE_FILENAME = "board_window.glade"
 
-LIST_ID = 0
-LIST_NUM = 1
-LIST_TITLE = 2
-LIST_RES = 3
-LIST_LINECOUNT = 4
-LIST_LASTMODIFIED = 5
-
 
 class WinWrap:
-    def sort_column(self, model, iter1, iter2, key):
-        v1 = model.get_value(iter1, 1)[key]
-        v2 = model.get_value(iter2, 1)[key]
-        if v1 < v2:
-            return -1
-        elif v1 > v2:
-            return 1
-        else:
-            return 0
-
-    def sort_column_num(self, model, iter1, iter2, key):
-        column_id, order = model.get_sort_column_id()
-        v1 = model.get_value(iter1, 1)[key]
-        v2 = model.get_value(iter2, 1)[key]
-        return self.sort_column_int(v1, v2, order)
-
-    def sort_column_int(self, v1, v2, order):
-        if order == gtk.SORT_ASCENDING:
-            if v1 == board_data.BOARD_DATA_INVALID_VALUE \
-                   and v2 == board_data.BOARD_DATA_INVALID_VALUE:
-                return 0
-            elif v1 == board_data.BOARD_DATA_INVALID_VALUE:
-                return 1
-            elif v2 == board_data.BOARD_DATA_INVALID_VALUE:
-                return -1
-
-        if v1 < v2:
-            return -1
-        elif v1 > v2:
-            return 1
-        else:
-            return 0
 
     def __init__(self, bbs, board):
         self.bbs = bbs
@@ -78,72 +39,24 @@ class WinWrap:
         glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
         self.mainwin = gtk.glade.XML(glade_path)
 
-        # id, num, title, res, lineCount, lastModified
-        self.datastore = gtk.ListStore(str, object)
-        self.datastore.set_sort_func(1000, lambda *args: -1)
-        self.datastore.set_sort_func(LIST_NUM+100, self.sort_column_num,
-                                     "num")
-        self.datastore.set_sort_func(LIST_TITLE+100, self.sort_column,
-                                     "title")
-        self.datastore.set_sort_func(LIST_RES+100, self.sort_column_num,
-                                     "res")
-        self.datastore.set_sort_func(LIST_LINECOUNT+100, self.sort_column_num,
-                                     "lineCount")
-        self.datastore.set_sort_func(LIST_LASTMODIFIED+100, self.sort_column,
-                                     "lastModified")
         self.treeview = self.mainwin.get_widget("treeview")
         self.treeview.set_rules_hint(True)
-        self.treeview.set_model(self.datastore)
-        self.popupmenu = self.mainwin.get_widget("popup_menu")
-
-        def dict_cell_data_func(column, cell, model, iter, key):
-            text = model.get_value(iter, 1)[key]
-            cell.set_property("text", text)
 
-        def dict_cell_data_func_int(column, cell, model, iter, key):
-            integer = model.get_value(iter, 1)[key]
-            if integer > 0:
-                cell.set_property("text", str(integer))
-            else:
-                cell.set_property("text", "")
+        self.popupmenu = self.mainwin.get_widget("popup_menu")
 
         renderer = gtk.CellRendererText()
 
-        column = gtk.TreeViewColumn("num", renderer)
-        column.set_cell_data_func(renderer, dict_cell_data_func_int, "num")
-        column.set_sort_column_id(LIST_NUM+100)
-        column.set_resizable(True)
-        column.set_reorderable(True)
-        self.treeview.append_column(column)
-
-        column = gtk.TreeViewColumn("title", renderer)
-        column.set_cell_data_func(renderer, dict_cell_data_func, "title")
-        column.set_sort_column_id(LIST_TITLE+100)
-        column.set_resizable(True)
-        column.set_reorderable(True)
-        self.treeview.append_column(column)
-
-        column = gtk.TreeViewColumn("res", renderer)
-        column.set_cell_data_func(renderer, dict_cell_data_func_int, "res")
-        column.set_sort_column_id(LIST_RES+100)
-        column.set_resizable(True)
-        column.set_reorderable(True)
-        self.treeview.append_column(column)
-        
-        column = gtk.TreeViewColumn("read", renderer)
-        column.set_cell_data_func(renderer, dict_cell_data_func_int,
-                                  "lineCount")
-        column.set_sort_column_id(LIST_LINECOUNT+100)
-        column.set_resizable(True)
-        column.set_reorderable(True)
-        self.treeview.append_column(column)
-        
-        column = gtk.TreeViewColumn("lastModified", renderer)
-        column.set_cell_data_func(renderer, dict_cell_data_func, "lastModified")
-        column.set_sort_column_id(LIST_LASTMODIFIED+100)
-        column.set_resizable(True)
-        column.set_reorderable(True)
-        self.treeview.append_column(column)
+        self.treeviewcolumn = {}
+        for i in range(1, len(ThreadListModel.column_names)):
+            column_name = ThreadListModel.column_names[i]
+            self.treeviewcolumn[column_name] = gtk.TreeViewColumn(
+                column_name, renderer, text=i)
+            self.treeviewcolumn[column_name].set_resizable(True)
+            self.treeviewcolumn[column_name].set_reorderable(True)
+            self.treeviewcolumn[column_name].set_clickable(True)
+            self.treeviewcolumn[column_name].connect(
+                "clicked", self.on_column_clicked, column_name)
+            self.treeview.append_column(self.treeviewcolumn[column_name])
 
         sigdic = {"on_board_window_destroy": self.on_board_window_destroy,
                   "on_quit_activate": self.on_quit_activate,
@@ -170,14 +83,36 @@ class WinWrap:
         t = board_data.GetRemote(self.bbs, self.board, self.update_datastore)
         t.start()
 
+    def on_column_clicked(self, treeviewcolumn, column_name):
+        model = self.treeview.get_model()
+        if model:
+            model.sort(column_name)
+            self.reset_sort_indicator()
+
+    def reset_sort_indicator(self):
+        model = self.treeview.get_model()
+        if model:
+            sort_column_name, sort_reverse = model.get_sort()
+            for name,column in self.treeviewcolumn.iteritems():
+                column.set_sort_indicator(False)
+            if sort_column_name != "num" or sort_reverse:
+                self.treeviewcolumn[sort_column_name].set_sort_indicator(True)
+                if sort_reverse:
+                    self.treeviewcolumn[sort_column_name].set_sort_order(
+                        gtk.SORT_DESCENDING)
+                else:
+                    self.treeviewcolumn[sort_column_name].set_sort_order(
+                        gtk.SORT_ASCENDING)
+        
     def on_open_thread(self, widget):
         treeselection = self.treeview.get_selection()
         model, iter = treeselection.get_selected()
         if not iter:
             return
 
-        thread = model.get_value(iter, 0)
-        title = model.get_value(iter, 1)["title"]
+        thread = model.get_value(iter, ThreadListModel.column_names.index("id"))
+        title = model.get_value(
+            iter, ThreadListModel.column_names.index("title"))
         print thread + ':"' + title + '"', "activated"
 
         thread_window.WinWrap(self.bbs, self.board, thread)
@@ -196,20 +131,126 @@ class WinWrap:
             return 1
 
     def update_datastore(self, datalist):
-        print "clear datastore"
-        self.datastore.clear()
-        column_id, order = self.datastore.get_sort_column_id()
-        self.datastore.set_sort_column_id(1000, gtk.SORT_ASCENDING)
         print "reflesh datastore"
 
         time_start = time.time()
+        list_list = []
         for id, dic in datalist.iteritems():
-            self.datastore.append([id, dic])
+            dic["id"] = id
+            list_list.append(dic)
 
-        if column_id is not None and order is not None:
-            self.datastore.set_sort_column_id(column_id, order)
+        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:
-            self.datastore.set_sort_column_id(LIST_NUM+100, gtk.SORT_ASCENDING)
+            model = ThreadListModel(list_list)
+        self.treeview.set_model(model)
+
+        self.reset_sort_indicator()
+
         print "end"
         time_end = time.time()
         print time_end - time_start
+
+
+class ThreadListModel(gtk.GenericTreeModel):
+    column_types = (str, int, str, int, int, str)
+    column_names = ["id", "num", "title", "res", "lineCount", "lastModified"]
+
+    def __init__(self, list, sort_column_name="num", sort_reverse=False):
+        gtk.GenericTreeModel.__init__(self)
+        self.list = list
+        self.sort_column_name = sort_column_name
+        self.sort_reverse = sort_reverse
+        self.do_sort(self.sort_column_name, self.sort_reverse)
+
+    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 compare(self, v1, v2, reverse):
+        if not reverse:
+            if v1 == 0 and v2 == 0:
+                return 0
+            elif v1 == 0:
+                return 1
+            elif v2 == 0:
+                return -1
+        return v1 - v2
+
+    def get_sort(self):
+        return self.sort_column_name, self.sort_reverse
+
+    def do_sort(self, column_name, reverse):
+        if self.column_types[self.column_names.index(column_name)] == str:
+            self.list.sort(None, lambda dic: dic[column_name], reverse)
+        else:
+            h = lambda x, y: self.compare(x, y, reverse)
+            self.list.sort(h, lambda dic: dic[column_name], reverse)
+        self.build_order_dict()
+
+    def sort(self, column_name):
+        old_order_dict = self.order_dict
+
+        if column_name == self.sort_column_name:
+            # sort reverse
+            self.sort_reverse = not self.sort_reverse
+        else:
+            self.sort_column_name = column_name
+            self.sort_reverse = False
+
+        self.do_sort(self.sort_column_name, self.sort_reverse)
+
+        neworder = [old_order_dict[item["id"]] for item in self.list]
+        self.rows_reordered(None, None, neworder)
+
+    def on_get_flags(self):
+        return gtk.TREE_MODEL_LIST_ONLY
+
+    def on_get_n_columns(self):
+        return len(self.column_types)
+
+    def on_get_column_type(self, n):
+        return self.column_types[n]
+
+    def on_get_iter(self, path):
+        return self.list[path[0]]
+
+    def on_get_path(self, rowref):
+        return self.order_dict[rowref["id"]]
+
+    def on_get_value(self, rowref, column):
+        return rowref[self.column_names[column]]
+
+    def on_iter_next(self, rowref):
+        try:
+            i = self.order_dict[rowref["id"]] + 1
+            return self.list[i]
+        except IndexError:
+            return None
+
+    def on_iter_children(self, rowref):
+        if rowref:
+            return None
+        return self.list[0]
+
+    def on_iter_has_child(self, rowref):
+        return False
+
+    def on_iter_n_children(self, rowref):
+        if rowref:
+            return 0
+        return len(self.list)
+
+    def on_iter_nth_child(self, rowref, n):
+        if rowref:
+            return None
+        try:
+            return self.list[n]
+        except IndexError:
+            return None
+
+    def on_iter_parent(child):
+        return None