[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [zanshin] src: Delete all selected items
From:       Mario Bensi <mbensi () ipsquad ! net>
Date:       2011-08-18 16:01:34
Message-ID: 20110818160134.48E32A60A4 () git ! kde ! org
[Download RAW message or body]

Git commit 7523654a21ba459d87545b6ab7f31acf2503d273 by Mario Bensi.
Committed on 18/08/2011 at 17:19.
Pushed by bensi into branch 'master'.

Delete all selected items

REVIEW: 102337

Currently, if you select multiple items and press Key_Delete, it will
delete the current item (the last one selected). This patch makes it
delete all items.

To do that :

- We are started from the Aaron's patch
- We split the item list selected by type ( ProjectTodo, StandardTodo, Category
).
- For type ProjectTodo and Category, we remove the item from the list if the parent \
is  also in the list.
- For type StandardTodo, we remove item from the list if an ancestor
  project is in project list.
- We add removeProjects in TodoHelpers.
- We add removeCategories in CategoryManager.
- We pass the list to the TodoHelpers for projects and todos list and to
  the CategoryManager for categories list.

M  +81   -19   src/actionlisteditor.cpp
M  +31   -5    src/categorymanager.cpp
M  +1    -0    src/actionlisteditorpage.h
M  +1    -0    src/todohelpers.h
M  +5    -0    src/actionlisteditorpage.cpp
M  +37   -6    src/todohelpers.cpp
M  +1    -0    src/categorymanager.h

http://commits.kde.org/zanshin/7523654a21ba459d87545b6ab7f31acf2503d273

diff --git a/src/actionlisteditor.cpp b/src/actionlisteditor.cpp
index 255379c..072b3b6 100644
--- a/src/actionlisteditor.cpp
+++ b/src/actionlisteditor.cpp
@@ -269,39 +269,101 @@ void ActionListEditor::onAddActionRequested()
 
 void ActionListEditor::onRemoveAction()
 {
-    QModelIndex currentIndex = currentPage()->selectionModel()->currentIndex();
+    QModelIndexList currentIndexes = \
currentPage()->selectionModel()->selectedRows();  
-    if (!currentIndex.isValid()) {
+    if (currentIndexes.isEmpty()) {
         return;
     }
 
-    int type = currentIndex.data(Zanshin::ItemTypeRole).toInt();
+    QModelIndexList currentProjects;
+    QModelIndexList currentCategories;
+    QModelIndexList currentTodos;
+
+    foreach (const QModelIndex &index, currentIndexes) {
+        const int type = index.data(Zanshin::ItemTypeRole).toInt();
+        if (type==Zanshin::ProjectTodo) {
+            currentProjects << index;
+        } else if (type==Zanshin::Category) {
+            currentCategories << index;
+        } else if (type==Zanshin::StandardTodo) {
+            currentTodos << index;
+        }
+    }
+
+    // Remove todos and projects already present in selected projects
+    if (!currentProjects.isEmpty()) {
+        QStringList projectUidList;
+        foreach (const QModelIndex project, currentProjects) {
+            projectUidList << project.data(Zanshin::UidRole).toString();
+        }
+
+        QSet<QString> projects = QSet<QString>::fromList(projectUidList);
+
+        foreach (const QModelIndex project, currentProjects) {
+            QSet<QString> ancestors = \
QSet<QString>::fromList(project.data(Zanshin::AncestorsUidRole).toStringList()); +    \
if (!ancestors.intersect(projects).isEmpty()) { +                \
currentProjects.removeOne(project); +            }
+        }
+        foreach (const QModelIndex todo, currentTodos) {
+            QSet<QString> ancestors = \
QSet<QString>::fromList(todo.data(Zanshin::AncestorsUidRole).toStringList()); +       \
if (!ancestors.intersect(projects).isEmpty()) { +                \
currentTodos.removeOne(todo); +            }
+        }
+    }
+
+    // Remove categories if the parent is also in the list
+    if (!currentCategories.isEmpty()) {
+        QStringList categoryList;
+        foreach (const QModelIndex project, currentCategories) {
+            categoryList << project.data(Qt::EditRole).toString();
+        }
+
+        QSet<QString> categories = QSet<QString>::fromList(categoryList);
+
+        foreach (const QModelIndex category, currentCategories) {
+            QStringList pathList = \
category.data(Zanshin::CategoryPathRole).toString().split(CategoryManager::pathSeparator());
 +            pathList.removeLast();
+            QSet<QString> ancestors = QSet<QString>::fromList(pathList);
+            if (!ancestors.intersect(categories).isEmpty()) {
+                currentCategories.removeOne(category);
+            }
+        }
+    }
+
     QModelIndex current;
     QModelIndex mapperIndex;
 
-    if (type==Zanshin::ProjectTodo) {
+    if (!currentProjects.isEmpty()) {
         current = m_projectSelection->currentIndex();
-    } else {
-        current = m_categoriesSelection->currentIndex();
-    }
+        if (current.isValid()) {
+            KModelIndexProxyMapper mapper(current.model(), \
currentProjects[0].model()); +            mapperIndex = \
mapper.mapRightToLeft(currentProjects[0]); +        }
 
-    if (current.isValid()) {
-        KModelIndexProxyMapper mapper(current.model(), currentIndex.model());
-        mapperIndex = mapper.mapRightToLeft(currentIndex);
+        if (TodoHelpers::removeProjects(this, currentProjects)) {
+            if (current==mapperIndex) {
+               m_projectSelection->setCurrentIndex(current.parent(), \
QItemSelectionModel::Select); +            }
+        }
     }
 
-    if (type==Zanshin::ProjectTodo) {
-        if (TodoHelpers::removeProject(this, currentIndex)) {
-            if (type==Zanshin::ProjectTodo && current==mapperIndex) {
-                m_projectSelection->setCurrentIndex(current.parent(), \
                QItemSelectionModel::Select);
-            }
+    if (!currentCategories.isEmpty()) {
+        current = m_categoriesSelection->currentIndex();
+        if (current.isValid()) {
+            KModelIndexProxyMapper mapper(current.model(), \
currentCategories[0].model()); +            mapperIndex = \
mapper.mapRightToLeft(currentCategories[0]);  }
-    } else if (type==Zanshin::Category) {
-        if (CategoryManager::instance().removeCategory(this, currentIndex)) {
+        if (CategoryManager::instance().removeCategories(this, currentCategories)) {
             m_categoriesSelection->setCurrentIndex(current.parent(), \
QItemSelectionModel::Select);  }
-    } else {
-        currentPage()->removeCurrentTodo();
+    }
+
+    if (!currentTodos.isEmpty()) {
+        foreach (QModelIndex index, currentTodos) {
+            currentPage()->removeTodo(index);
+        }
     }
 }
 
diff --git a/src/actionlisteditorpage.cpp b/src/actionlisteditorpage.cpp
index 04b241a..3312a75 100644
--- a/src/actionlisteditorpage.cpp
+++ b/src/actionlisteditorpage.cpp
@@ -336,6 +336,11 @@ void ActionListEditorPage::addNewTodo(const QString &summary)
 void ActionListEditorPage::removeCurrentTodo()
 {
     QModelIndex current = m_treeView->selectionModel()->currentIndex();
+    removeTodo(current);
+}
+
+void ActionListEditorPage::removeTodo(const QModelIndex &current)
+{
     int type = current.data(Zanshin::ItemTypeRole).toInt();
 
     if (!current.isValid() || type!=Zanshin::StandardTodo) {
diff --git a/src/actionlisteditorpage.h b/src/actionlisteditorpage.h
index 11b8d95..fdeafd1 100644
--- a/src/actionlisteditorpage.h
+++ b/src/actionlisteditorpage.h
@@ -69,6 +69,7 @@ public:
 public slots:
     void addNewTodo(const QString &summary);
     void removeCurrentTodo();
+    void removeTodo(const QModelIndex &current);
 
 private slots:
     void onAutoHideColumns();
diff --git a/src/categorymanager.cpp b/src/categorymanager.cpp
index 797c93f..2a83045 100644
--- a/src/categorymanager.cpp
+++ b/src/categorymanager.cpp
@@ -102,20 +102,46 @@ void CategoryManager::addCategory(const QString &categoryPath)
 
 bool CategoryManager::removeCategory(QWidget *parent, const QModelIndex \
&categoryIndex)  {
-    QString categoryName = categoryIndex.data().toString();
-    QString categoryPath = categoryIndex.data(Zanshin::CategoryPathRole).toString();
+    QModelIndexList categories;
+    categories << categoryIndex;
+    return removeCategories(parent, categories);
+}
+
+bool CategoryManager::removeCategories(QWidget *parent, const QModelIndexList \
&categories) +{
+    if (categories.isEmpty()) {
+        return false;
+    }
+
+    QStringList categoryList;
+    foreach (QModelIndex category, categories) {
+        categoryList << category.data().toString();
+    }
+    QString categoryName = categoryList.join(", ");
+
     QString title;
     QString text;
 
-    text = i18n("Do you really want to delete the category '%1'? All actions won't \
                be associated to those categories anymore.", categoryName);
-    title = i18n("Delete Category");
+    if (categories.size() > 1) {
+        text = i18n("Do you really want to delete the category '%1'? All actions \
won't be associated to those categories anymore.", categoryName); +        title = \
i18n("Delete Category"); +    } else {
+        text = i18n("Do you really want to delete the categories '%1'? All actions \
won't be associated to those categories anymore.", categoryName); +        title = \
i18n("Delete Categories"); +    }
 
     int button = KMessageBox::questionYesNo(parent, text, title);
     bool canRemove = (button==KMessageBox::Yes);
 
     if (!canRemove) return false;
 
-    return removeCategory(categoryPath);
+    foreach (QModelIndex category, categories) {
+        QString categoryPath = category.data(Zanshin::CategoryPathRole).toString();
+        if (!removeCategory(categoryPath)) {
+            return false;
+        }
+    }
+    return true;
 }
 
 bool CategoryManager::removeCategory(const QString &categoryPath)
diff --git a/src/categorymanager.h b/src/categorymanager.h
index 890602a..281c183 100644
--- a/src/categorymanager.h
+++ b/src/categorymanager.h
@@ -49,6 +49,7 @@ public:
     void addCategory(const QString &category, const QString &parentCategory);
     void addCategory(const QString &categoryPath);
     bool removeCategory(QWidget *parent, const QModelIndex &categoryIndex);
+    bool removeCategories(QWidget *parent, const QModelIndexList &categoryIndex);
     bool removeTodoFromCategory(const QModelIndex &index, const QString \
                &categoryPath);
     void renameCategory(const QString &oldCategoryPath, const QString \
                &newCategoryPath);
     void moveCategory(const QString &oldCategoryPath, const QString \
                &parentCategoryPath, Zanshin::ItemType parentType);
diff --git a/src/todohelpers.cpp b/src/todohelpers.cpp
index df2db17..46ce7f4 100644
--- a/src/todohelpers.cpp
+++ b/src/todohelpers.cpp
@@ -122,16 +122,44 @@ void removeCurrentTodo(const QModelIndex &project, \
QModelIndexList children, Ako  
 bool TodoHelpers::removeProject(QWidget *parent, const QModelIndex &project)
 {
+    QModelIndexList projects;
+    projects << project;
+    return removeProjects(parent, projects);
+}
+
+bool TodoHelpers::removeProjects(QWidget *parent, const QModelIndexList &projects)
+{
+    if (projects.isEmpty()) {
+        return false;
+    }
+
     bool canRemove = true;
-    QModelIndexList children = \
                project.data(Zanshin::ChildIndexesRole).value<QModelIndexList>();
-    if (!children.isEmpty()) {
-        QString summary = project.data().toString();
+    QString summary;
+    if (projects.size() > 1) {
+        QStringList projectList;
+        foreach (QModelIndex project, projects) {
+            projectList << project.data().toString();
+        }
+        summary = projectList.join(", ");
+    } else {
+        QModelIndexList children = \
projects[0].data(Zanshin::ChildIndexesRole).value<QModelIndexList>(); +        if \
(!children.isEmpty()) { +            summary = projects[0].data().toString();
+        }
+    }
 
+    if (!summary.isEmpty()) {
         QString title;
         QString text;
 
-        text = i18n("Do you really want to delete the project '%1', with all its \
                actions?", summary);
-        title = i18n("Delete Project");
+        if (projects.size() > 1) {
+            title = i18n("Delete Projects");
+            text = i18n("Do you really want to delete the projects '%1', with all \
its actions?", summary); +        } else {
+            title = i18n("Delete Project");
+            text = i18n("Do you really want to delete the project '%1', with all its \
actions?", summary); +        }
+
 
         int button = KMessageBox::questionYesNo(parent, text, title);
         canRemove = (button==KMessageBox::Yes);
@@ -140,7 +168,10 @@ bool TodoHelpers::removeProject(QWidget *parent, const \
QModelIndex &project)  if (!canRemove) return false;
 
     Akonadi::TransactionSequence *sequence = new Akonadi::TransactionSequence();
-    removeCurrentTodo(project, children, sequence);
+    foreach (QModelIndex project, projects) {
+        QModelIndexList children = \
project.data(Zanshin::ChildIndexesRole).value<QModelIndexList>(); +        \
removeCurrentTodo(project, children, sequence); +    }
     sequence->start();
     return true;
 }
diff --git a/src/todohelpers.h b/src/todohelpers.h
index f06e000..b015060 100644
--- a/src/todohelpers.h
+++ b/src/todohelpers.h
@@ -41,6 +41,7 @@ namespace TodoHelpers
     void addProject(const QString &summary, const Akonadi::Collection &collection);
     void addProject(const QString &summary, const Akonadi::Item &parentProject);
     bool removeProject(QWidget *parent, const QModelIndex &project);
+    bool removeProjects(QWidget *parent, const QModelIndexList &projects);
     bool moveTodoToProject(const QModelIndex &todo, const QString &parentUid, const \
                Zanshin::ItemType parentType, const Akonadi::Collection \
                &parentCollection);
     bool moveTodoToProject(const Akonadi::Item &todo, const QString &parentUid, \
const Zanshin::ItemType parentType, const Akonadi::Collection &parentCollection);  \
bool promoteTodo(const QModelIndex &index);


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic