OSDN Git Service

Separate tree view column specification to board_column.py.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 18 Sep 2006 11:31:18 +0000 (20:31 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 18 Sep 2006 11:31:18 +0000 (20:31 +0900)
src/FukuiNoNamari/board_column.py [new file with mode: 0644]
src/FukuiNoNamari/board_window.py

diff --git a/src/FukuiNoNamari/board_column.py b/src/FukuiNoNamari/board_column.py
new file mode 100644 (file)
index 0000000..becea8c
--- /dev/null
@@ -0,0 +1,169 @@
+# Copyright (C) 2006 by Aiwota Programmer
+# aiwotaprog@tetteke.tk
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import pygtk
+pygtk.require('2.0')
+import gtk
+import gobject
+import time
+
+
+class BaseTreeViewColumn(gtk.TreeViewColumn):
+    min_width = 20
+
+    def __init__(self, renderer, treeview):
+        gtk.TreeViewColumn.__init__(self, self.label, renderer)
+        self.treeview = treeview
+        self.setup()
+
+    def setup(self):
+        self.set_resizable(True)
+        self.set_reorderable(True)
+        self.set_clickable(True)
+        self.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
+        self.set_min_width(self.min_width)
+        self.set_fixed_width(self.def_width)
+
+        renderers = self.get_cell_renderers()
+        self.set_cell_data_func(renderers[0], self.on_cell_data)
+
+        self.connect("clicked", self.on_column_clicked)
+        self.treeview.append_column(self)
+
+    def on_sort_column_changed(self):
+        model = self.treeview.get_model()
+        if model:
+            sort_column_name, sort_reverse = model.get_sort()
+            if sort_column_name != self.id:
+                self.set_sort_indicator(False)
+            else:
+                self.set_sort_indicator(True)
+                if sort_reverse:
+                    self.set_sort_order(gtk.SORT_DESCENDING)
+                else:
+                    self.set_sort_order(gtk.SORT_ASCENDING)
+        
+    def on_column_clicked(self, treeviewcolumn):
+        model = self.treeview.get_model()
+        if model:
+            model.sort(self.id)
+            for column in self.treeview.get_columns():
+                column.on_sort_column_changed()
+
+    def on_cell_data(self, column, cell, model, iter):
+        self.updated_thread_highlight(column, cell, model, iter)
+
+        column_num = model.column_names.index(self.name)
+        value = model.get_value(iter, column_num)
+        if model.get_column_type(column_num) \
+               in (gobject.TYPE_INT, gobject.TYPE_DOUBLE):
+            if value == 0:
+                cell.set_property("text", "")
+            else:
+                cell.set_property("text", str(value))
+        else:
+            cell.set_property("text", value)
+
+    def updated_thread_highlight(self, column, cell, model, iter):
+
+        def is_updated_thread():
+            res = model.get_value(
+                iter, model.column_names.index("res"))
+            linecount = model.get_value(
+                iter, model.column_names.index("lineCount"))
+            return res != 0 and linecount != 0 and res > linecount
+
+        if is_updated_thread():
+            cell.set_property("weight", 800)
+        else:
+            cell.set_property("weight", 400)
+
+
+tree_view_column_list = []
+    
+class TreeViewColumn_Number(BaseTreeViewColumn):
+    name = "num"
+    id = "num"
+    label = "No."
+    def_width = 30
+
+    def on_sort_column_changed(self):
+        model = self.treeview.get_model()
+        if model:
+            sort_column_name, sort_reverse = model.get_sort()
+            if sort_column_name == self.id and sort_reverse:
+                self.set_sort_indicator(True)
+                self.set_sort_order(gtk.SORT_DESCENDING)
+            else:
+                self.set_sort_indicator(False)
+        
+tree_view_column_list.append(TreeViewColumn_Number)
+
+
+class TreeViewColumn_Title(BaseTreeViewColumn):
+    name = "title"
+    id = "title"
+    label = "Title"
+    def_width = 330
+
+tree_view_column_list.append(TreeViewColumn_Title)
+
+
+class TreeViewColumn_Res(BaseTreeViewColumn):
+    name = "res"
+    id = "res"
+    label = "Res"
+    def_width = 50
+
+tree_view_column_list.append(TreeViewColumn_Res)
+
+
+class TreeViewColumn_LineCount(BaseTreeViewColumn):
+    name = "lineCount"
+    id = "lineCount"
+    label = "Read"
+    def_width = 50
+
+tree_view_column_list.append(TreeViewColumn_LineCount)
+
+
+class TreeViewColumn_LastModified(BaseTreeViewColumn):
+    id = "lastModified"
+    label = "Last Modified"
+    def_width = 200
+
+    def on_cell_data(self, column, cell, model, iter):
+        self.updated_thread_highlight(column, cell, model, iter)
+
+        lastmod = model.get_value(
+            iter, model.column_names.index("lastModified"))
+        if lastmod == 0:
+            cell.set_property("text", "")
+        else:
+            cell.set_property("text", time.strftime(
+                "%Y/%m/%d(%a) %H:%M:%S", time.localtime(lastmod)))
+
+tree_view_column_list.append(TreeViewColumn_LastModified)
+
+
+class TreeViewColumn_Average(BaseTreeViewColumn):
+    name = "average"
+    id = "average"
+    label = "Average"
+    def_width = 50
+
+tree_view_column_list.append(TreeViewColumn_Average)
index f0745d6..16aa9fd 100644 (file)
@@ -38,6 +38,7 @@ from misc import ThreadInvoker
 import bookmark_list
 import bookmark_window
 import board_plugins
+import board_column
 
 GLADE_FILENAME = "board_window.glade"
 
@@ -82,27 +83,8 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
 
         renderer = gtk.CellRendererText()
 
-        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)
-            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].set_cell_data_func(
-                renderer, self.on_cell_data, column_name)
-            self.treeviewcolumn[column_name].connect(
-                "clicked", self.on_column_clicked, column_name)
-            self.treeviewcolumn[column_name].set_sizing(
-                gtk.TREE_VIEW_COLUMN_FIXED)
-            self.treeviewcolumn[column_name].set_min_width(20)
-            self.treeviewcolumn[column_name].set_fixed_width(
-                ThreadListModel.column_width[i])
-            self.treeview.append_column(self.treeviewcolumn[column_name])
-
-        self.treeviewcolumn["lastModified"].set_cell_data_func(
-            renderer, self.on_data_lastmodified)
+        for column_class in board_column.tree_view_column_list:
+            column_class(renderer, self.treeview)
 
         self.treeview.set_fixed_height_mode(True)
 
@@ -195,43 +177,6 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
         if model:
             model.refilter(None)
 
-    def updated_thread_highlight(self, column, cell, model, iter):
-
-        def is_updated_thread():
-            res = model.get_value(
-                iter, ThreadListModel.column_names.index("res"))
-            linecount = model.get_value(
-                iter, ThreadListModel.column_names.index("lineCount"))
-            return res != 0 and linecount != 0 and res > linecount
-
-        if is_updated_thread():
-            cell.set_property("weight", 800)
-        else:
-            cell.set_property("weight", 400)
-
-    def on_cell_data(self, column, cell, model, iter, column_name):
-        self.updated_thread_highlight(column, cell, model, iter)
-        column_num = ThreadListModel.column_names.index(column_name)
-        value = model.get_value(iter, column_num)
-        if model.get_column_type(column_num) \
-           in (gobject.TYPE_INT, gobject.TYPE_DOUBLE):
-            if value == 0:
-                cell.set_property("text", "")
-            else:
-                cell.set_property("text", str(value))
-        else:
-            cell.set_property("text", value)
-
-    def on_data_lastmodified(self, column, cell, model, iter, user_data=None):
-        self.updated_thread_highlight(column, cell, model, iter)
-        lastmod = model.get_value(
-            iter, ThreadListModel.column_names.index("lastModified"))
-        if lastmod == 0:
-            cell.set_property("text", "")
-        else:
-            cell.set_property("text", time.strftime(
-                "%Y/%m/%d(%a) %H:%M:%S", time.localtime(lastmod)))
-
     def on_board_window_delete_event(self, widget, event):
         self.save()
         return False
@@ -248,27 +193,6 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
     def on_refresh_activate(self, widget):
         self.load(True)
 
-    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()
@@ -358,8 +282,6 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
         # redraw visible area after set list to model
         self.treeview.queue_draw()
 
-        self.reset_sort_indicator()
-
         print "end"
 
     def on_thread_idx_updated(self, thread_uri, idx_dic):
@@ -423,15 +345,13 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
                 order = ""
                 width = ""
                 for column in columns:
-                    for name, kolumn in self.treeviewcolumn.iteritems():
-                        if column == kolumn:
-                            if order:
-                                order += ","
-                            order += name
-                            if width:
-                                width += ","
-                            width += str(column.get_width())
-                            break
+                    if order:
+                        order += ","
+                    order += column.id
+                    if width:
+                        width += ","
+                    width += str(column.get_width())
+
                 sort_column_name, sort_reverse = \
                                   self.treeview.get_model().get_sort()
                 sort_reverse = str(sort_reverse)
@@ -480,13 +400,15 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
             if os.path.exists(states_path):
                 sort_column_name = "num"
                 sort_reverse = False
+                clns = self.treeview.get_columns()
+                treeviewcolumn = dict([(cln.id, cln) for cln in clns])
                 for line in file(states_path):
                     if line.startswith("columns="):
                         line = line[len("columns="):].rstrip("\n")
                         base_column = None
                         for name in line.split(","):
-                            if name in self.treeviewcolumn:
-                                column = self.treeviewcolumn[name]
+                            if name in treeviewcolumn:
+                                column = treeviewcolumn[name]
                                 self.treeview.move_column_after(
                                     column, base_column)
                                 base_column = column
@@ -503,7 +425,7 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
                                     columns[i].set_fixed_width(width)
                     elif line.startswith("sort_column="):
                         kolumn_name = line[len("sort_column="):].rstrip("\n")
-                        if kolumn_name in ThreadListModel.column_names:
+                        if kolumn_name in treeviewcolumn:
                             sort_column_name = kolumn_name
                     elif line.startswith("sort_reverse="):
                         reverse = line[len("sort_reverse="):].rstrip("\n")