OSDN Git Service

Board treeview is sortable.
[fukui-no-namari/dialektos.git] / src / thread_list_model.cxx
index da9b8bb..cb7f8f6 100644 (file)
@@ -27,7 +27,7 @@
 #include <boost/mpl/find.hpp>
 #include <boost/mpl/pop_front.hpp>
 #include <boost/foreach.hpp>
-#include <boost/integer_traits.hpp>
+#include <boost/function.hpp>
 
 
 namespace dialektos {
@@ -80,20 +80,6 @@ struct GetColumnGType {
 
 } // namespace model_column
 
-namespace {
-struct CompareNumber {
-  bool operator()(const ModelColumns& lhs, const ModelColumns& rhs) const {
-    int left_num = model_column::field<model_column::Number>(lhs);
-    int right_num = model_column::field<model_column::Number>(rhs);
-
-    if (left_num == 0) left_num = boost::integer_traits<int>::const_max;
-    if (right_num == 0) right_num = boost::integer_traits<int>::const_max;
-
-    return left_num < right_num;
-  }
-};
-}
-
 
 Glib::RefPtr<ThreadListModel> ThreadListModel::create() {
   return Glib::RefPtr<ThreadListModel>(new ThreadListModel());
@@ -102,7 +88,7 @@ Glib::RefPtr<ThreadListModel> ThreadListModel::create() {
 ThreadListModel::ThreadListModel():
   Glib::ObjectBase(typeid(ThreadListModel)),
   Glib::Object(),
-  list_(), order_() {
+  list_(), order_(), sort_function_(Compare<model_column::Number>()) {
 }
 
 ThreadListModel::~ThreadListModel() {}
@@ -148,25 +134,28 @@ void ThreadListModel::set_buffer(
     append(cols);
   }
 
-  OrderType old_order = order_;
-
-  std::sort(list_.begin(), list_.end(), CompareNumber());
+  sort();
+}
 
-  for (size_t i = 0; i != list_.size(); i++) {
-    const model_column::ID::type& id =
-      model_column::field<model_column::ID>(list_[i]);
-    order_[id] = i;
-  }
+void ThreadListModel::sort() {
+  std::sort(list_.begin(), list_.end(), sort_function_);
+  do_after_sort();
+}
 
+void ThreadListModel::do_after_sort() {
+  // for informing to the tree view.
   std::vector<int> new_order;
   new_order.reserve(order_.size());
 
-  BOOST_FOREACH(const ModelColumns& record, list_) {
+  // set new order.
+  for (size_t i = 0; i != list_.size(); i++) {
     const model_column::ID::type& id =
-      model_column::field<model_column::ID>(record);
-    new_order.push_back(old_order[id]);
+      model_column::field<model_column::ID>(list_[i]);
+    new_order.push_back(order_[id]);
+    order_[id] = i;
   }
 
+  // inform new order to the tree view.
   if (!new_order.empty())
     rows_reordered(Gtk::TreeModel::Path(), &(new_order[0]));
 }