Bug 167114 - Ability to sort to-do list by completion date
Summary: Ability to sort to-do list by completion date
Status: RESOLVED FIXED
Alias: None
Product: korganizer
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: Ubuntu Linux
: NOR wishlist
Target Milestone: ---
Assignee: kdepim bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-07-21 01:50 UTC by Socceroos
Modified: 2011-01-15 02:35 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Socceroos 2008-07-21 01:50:42 UTC
Version:            (using KDE 4.0.98)
Installed from:    Ubuntu Packages
OS:                Linux

Having the ability to sort the to-do list in Kontact by the completion date is very useful for reporting purposes (ie. Manager wants to know on average how long it takes you to complete tasks in category 'IT problems').

I guess, on that note....it would be sweet to have a simple reporting system. :D
Comment 1 Allen Winter 2008-08-21 16:51:32 UTC
Do you mean sorting by the due date, or the actual date the to-do was completed?
Comment 2 Socceroos 2008-08-22 02:34:25 UTC
Allen, the actual date the to-do was completed.

If you were able to use this information you could create some nifty reports on your efficiency.
Comment 3 Sergio Martins 2011-01-15 02:35:23 UTC
commit 3bcdde2aa6c1043fbd1cb0d18e02ff5f50c63f37
branch master
Author: Sergio Martins <iamsergio@gmail.com>
Date:   Sat Jan 15 01:31:57 2011 +0000

    When sorting by completion, untie completed to-dos by completion date.
    
    Also, "sort completed to-dos separately" only makes sense if we're not sorting by completion date, so I fixed this too.
    BUG: 167114

diff --git a/korganizer/views/todoview/kotodoviewsortfilterproxymodel.cpp b/korganizer/views/todoview/kotodoviewsortfilterproxymodel.cpp
index 0262b2a..81a05f7 100644
--- a/korganizer/views/todoview/kotodoviewsortfilterproxymodel.cpp
+++ b/korganizer/views/todoview/kotodoviewsortfilterproxymodel.cpp
@@ -74,7 +74,7 @@ bool KOTodoViewSortFilterProxyModel::filterAcceptsRow(
 bool KOTodoViewSortFilterProxyModel::lessThan( const QModelIndex &left,
                                                const QModelIndex &right ) const
 {
-  if ( KOPrefs::instance()->sortCompletedTodosSeparately() ) {
+  if ( KOPrefs::instance()->sortCompletedTodosSeparately() && left.column() != KOTodoModel::PercentColumn ) {
     QModelIndex cLeft = left.sibling( left.row(), KOTodoModel::PercentColumn );
     QModelIndex cRight = right.sibling( right.row(), KOTodoModel::PercentColumn );
 
@@ -122,6 +122,11 @@ bool KOTodoViewSortFilterProxyModel::lessThan( const QModelIndex &left,
         return fallbackComparison == 1;
       }
     }
+  } else if ( right.column() == KOTodoModel::PercentColumn ) {
+    const int comparison = compareCompletion( left, right );
+    if ( comparison != 0 ) {
+      return comparison == -1;
+    }
   }
 
   if ( left.data() == right.data() ) {
@@ -192,6 +197,33 @@ int KOTodoViewSortFilterProxyModel::compareDueDates( const QModelIndex &left, co
  *  0 - equal
  *  1 - bigger than
  */
+int KOTodoViewSortFilterProxyModel::compareCompletion( const QModelIndex &left, const QModelIndex &right ) const
+{
+  Q_ASSERT( left.column() == KOTodoModel::PercentColumn );
+  Q_ASSERT( right.column() == KOTodoModel::PercentColumn );
+
+  const int leftValue = sourceModel()->data( left ).toInt();
+  const int rightValue = sourceModel()->data( right ).toInt();
+
+  if ( leftValue == 100 && rightValue == 100 ) {
+    // Untie with the completion date
+    const Todo::Ptr leftTodo = CalendarSupport::todo( left.data( KOTodoModel::TodoRole ).value<Akonadi::Item>() );
+    const Todo::Ptr rightTodo = CalendarSupport::todo( right.data( KOTodoModel::TodoRole ). value<Akonadi::Item>() );
+
+    if ( !leftTodo || !rightTodo ) {
+      return 0;
+    } else {
+      return ( leftTodo->completed() > rightTodo->completed() ) ? -1 : 1;
+    }
+  } else {
+    return ( leftValue < rightValue ) ? -1 : 1;
+  }
+}
+
+/* -1 - less than
+ *  0 - equal
+ *  1 - bigger than
+ */
 int KOTodoViewSortFilterProxyModel::comparePriorities( const QModelIndex &left, const QModelIndex &right ) const
 {
   Q_ASSERT( left.column() == KOTodoModel::PriorityColumn );
diff --git a/korganizer/views/todoview/kotodoviewsortfilterproxymodel.h b/korganizer/views/todoview/kotodoviewsortfilterproxymodel.h
index 8e14ff2..517667c 100644
--- a/korganizer/views/todoview/kotodoviewsortfilterproxymodel.h
+++ b/korganizer/views/todoview/kotodoviewsortfilterproxymodel.h
@@ -51,6 +51,7 @@ class KOTodoViewSortFilterProxyModel : public QSortFilterProxyModel
   private:
     int compareDueDates( const QModelIndex &left, const QModelIndex &right ) const;
     int comparePriorities( const QModelIndex &left, const QModelIndex &right ) const;
+    int compareCompletion( const QModelIndex &left, const QModelIndex &right ) const;
     QStringList mCategories;
     Qt::SortOrder mSortOrder;
 };