From 5f27f4242983b5a29bf2db0892765a6b766b7c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fauconnier?= Date: Fri, 16 Nov 2018 13:30:32 +0100 Subject: [PATCH] improvements on multiple selected documents handling --- src/document/UBDocumentContainer.cpp | 11 +- src/document/UBDocumentController.cpp | 239 +++++++++++++++----------- src/document/UBDocumentController.h | 2 + 3 files changed, 154 insertions(+), 98 deletions(-) diff --git a/src/document/UBDocumentContainer.cpp b/src/document/UBDocumentContainer.cpp index ca00d9d2..456c37a7 100644 --- a/src/document/UBDocumentContainer.cpp +++ b/src/document/UBDocumentContainer.cpp @@ -137,8 +137,15 @@ void UBDocumentContainer::deleteThumbPage(int index) void UBDocumentContainer::updateThumbPage(int index) { - mDocumentThumbs[index] = UBThumbnailAdaptor::get(mCurrentDocument, index); - emit documentPageUpdated(index); + if (mDocumentThumbs.size() > index) + { + mDocumentThumbs[index] = UBThumbnailAdaptor::get(mCurrentDocument, index); + emit documentPageUpdated(index); + } + else + { + qDebug() << "error [updateThumbPage] : index > mDocumentThumbs' size."; + } } void UBDocumentContainer::insertThumbPage(int index) diff --git a/src/document/UBDocumentController.cpp b/src/document/UBDocumentController.cpp index fd7d7f93..0267a72c 100644 --- a/src/document/UBDocumentController.cpp +++ b/src/document/UBDocumentController.cpp @@ -1803,73 +1803,37 @@ void UBDocumentController::TreeViewSelectionChanged(const QItemSelection &select int nbIndexes = selected.indexes().count(); if (nbIndexes) { - //N/C - NNE - 20140408 - QModelIndexList list = mDocumentUI->documentTreeView->selectionModel()->selectedRows(); //if multi-selection - if(list.count() > 1){ - //selectOnlyTopLevelItems(mDocumentUI->documentTreeView->selectionModel()->selectedRows()); - - //check if the selection is in the same top-level folder - QModelIndex sourceIndex1 = mapIndexToSource(list.at(list.count()-1)); - QModelIndex sourceIndex2 = mapIndexToSource(list.at(list.count()-2)); + if(list.count() > 1) + { + for (int i=0; i < list.count() ; i++) + { + QModelIndex newSelectedRow = list.at(i); + QModelIndex parent = newSelectedRow.parent(); + bool isParentIsSelected = false; + while(parent.isValid()) + { + isParentIsSelected |= (list.indexOf(parent) != -1); - UBDocumentTreeModel *model = UBPersistenceManager::persistenceManager()->mDocumentTreeStructureModel; + if(isParentIsSelected) + break; - bool sameFolder = (model->inTrash(sourceIndex1) && model->inTrash(sourceIndex2)); + parent = parent.parent(); + } - sameFolder |= (model->inMyDocuments(sourceIndex1) && model->inMyDocuments(sourceIndex2)); + if(!isParentIsSelected) + TreeViewSelectionChanged(newSelectedRow, QModelIndex()); + else + mDocumentUI->documentTreeView->selectionModel()->select(newSelectedRow, QItemSelectionModel::Deselect | QItemSelectionModel::Rows); - if(!sameFolder){ - mDocumentUI->documentTreeView->clearSelection(); - mDocumentUI->documentTreeView->selectionModel()->select(list.at(list.count()-1), QItemSelectionModel::Select | QItemSelectionModel::Rows); } } - //N/C - NNE - 20140408 : END - - //if the parent has been already select, don't select its children - QModelIndex newSelectedRow = selected.indexes().first(); - QModelIndex parent = newSelectedRow.parent(); - bool isParentIsSelected = false; - while(parent.isValid()){ - isParentIsSelected |= (list.indexOf(parent) != -1); - - if(isParentIsSelected) - break; - - parent = parent.parent(); - } - - if(!isParentIsSelected) - TreeViewSelectionChanged(newSelectedRow, QModelIndex()); else - mDocumentUI->documentTreeView->selectionModel()->select(newSelectedRow, QItemSelectionModel::Deselect | QItemSelectionModel::Rows); - - //if he newSelectedRow have children deselect them - /* - UBDocumentTreeModel *model = UBPersistenceManager::persistenceManager()->mDocumentTreeStructureModel; - - if(model->rowCount(newSelectedRow) > 0){ - QModelIndexList children; - QModelIndex parent = newSelectedRow; - - //init children list - for(int i = 0; i < model->rowCount(parent); i++){ - children.push_back(parent.child(i, 0)); - } - - for(int i = 0; i < children.size(); i++){ - //deselect the element - mDocumentUI->documentTreeView->selectionModel()->select(children.at(0), QItemSelectionModel::Deselect); - - //add its children if any - for(int k = 0; k < model->rowCount(children.at(i)); k++){ - children.push_back(children.at(i).child(k, 0)); - } - } + { + TreeViewSelectionChanged(selected.indexes().at(0), QModelIndex()); } - */ } } @@ -2229,38 +2193,48 @@ void UBDocumentController::deleteSelectedItem() if (!UBApplication::mainWindow->yesNoQuestion(tr("Remove Item"), tr("Are you sure you want to remove the selected item(s) ?"))) return; - //we iterate in backward because if we iterate in normal order, - //indexes are invalid after the first deletion - for(int i = indexes.size()-1; i >= 0; i--){ - QModelIndex currentIndex = indexes.at(i); - DeletionType deletionForSelection = deletionTypeForSelection(mSelectionType, currentIndex, docModel); + if (indexes.size() > 1) + { + deleteMultipleItems(indexes, docModel); + } + else + { + deleteSingleItem(indexes.at(0), docModel); + } - //N/C - NNE - 20140408 : if parent is selected, continue, because it will be deleted when the parent will be deleted - if(mDocumentUI->documentTreeView->selectionModel()->isSelected(currentIndex.parent())){ - continue; - } - //N/C - NNE - 20140408 : END + updateActions(); +} - switch (deletionForSelection) { - case DeletePage: - { - deletePages(mDocumentUI->thumbnailWidget->selectedItems()); - break; - } - case MoveToTrash: - { - moveToTrash(currentIndex, docModel); +void UBDocumentController::deleteMultipleItems(QModelIndexList indexes, UBDocumentTreeModel* docModel) +{ + DeletionType deletionForSelection = deletionTypeForSelection(mSelectionType, indexes.at(0), docModel); - break; - } - case CompleteDelete: + switch (deletionForSelection) + { + case DeletePage: + { + deletePages(mDocumentUI->thumbnailWidget->selectedItems()); + break; + } + case MoveToTrash: + { + moveIndexesToTrash(indexes, docModel); + break; + } + case CompleteDelete: + { + for (int i =0; i < indexes.size(); i++) { - deleteIndexAndAssociatedData(currentIndex); + deleteIndexAndAssociatedData(indexes.at(i)); emit documentThumbnailsUpdated(this); - break; } - case EmptyFolder: + break; + } + case EmptyFolder: + { + for (int i =0; i < indexes.size(); i++) { + QModelIndex currentIndex = indexes.at(i); if (currentIndex == docModel->myDocumentsIndex()) { //Emptying "My documents". Keeping Untitled Documents int startInd = 0; while (docModel->rowCount(currentIndex)) { @@ -2288,14 +2262,81 @@ void UBDocumentController::deleteSelectedItem() if(deleteCurrentScene) createNewDocumentInUntitledFolder(); } - - break; } - case EmptyTrash: + + break; + } + case EmptyTrash: + { + for (int i=0; i < indexes.size(); i++) { - emptyFolder(currentIndex, CompleteDelete); // Empty trash folder - break; + emptyFolder(indexes.at(i), CompleteDelete); // Empty trash folder + } + + break; + } + } + updateActions(); +} + +void UBDocumentController::deleteSingleItem(QModelIndex currentIndex, UBDocumentTreeModel* docModel) +{ + DeletionType deletionForSelection = deletionTypeForSelection(mSelectionType, currentIndex, docModel); + + switch (deletionForSelection) + { + case DeletePage: + { + deletePages(mDocumentUI->thumbnailWidget->selectedItems()); + break; + } + case MoveToTrash: + { + moveToTrash(currentIndex, docModel); + break; + } + case CompleteDelete: + { + deleteIndexAndAssociatedData(currentIndex); + emit documentThumbnailsUpdated(this); + break; + } + case EmptyFolder: + { + if (currentIndex == docModel->myDocumentsIndex()) { //Emptying "My documents". Keeping Untitled Documents + int startInd = 0; + while (docModel->rowCount(currentIndex)) { + QModelIndex testSubINdecurrentIndex = docModel->index(startInd, 0, currentIndex); + if (testSubINdecurrentIndex == docModel->untitledDocumentsIndex()) { + emptyFolder(testSubINdecurrentIndex, MoveToTrash); + startInd++; + continue; + } + if (!testSubINdecurrentIndex.isValid()) { + break; + } + docModel->moveIndex(testSubINdecurrentIndex, docModel->trashIndex()); + } + //issue 1629 - NNE - 20131105 + //Here, we are sure that the current scene has been deleted + createNewDocumentInUntitledFolder(); + } else { + //issue 1629 - NNE - 20131105 + //Check if we will delete the current scene + UBDocumentTreeNode *currentNode = docModel->nodeFromIndex(currentIndex); + bool deleteCurrentScene = currentNode->findNode(docModel->nodeFromIndex(docModel->currentIndex())); + + emptyFolder(currentIndex, MoveToTrash); //Empty constant folder + + if(deleteCurrentScene) createNewDocumentInUntitledFolder(); } + + break; + } + case EmptyTrash: + { + emptyFolder(currentIndex, CompleteDelete); // Empty trash folder + break; } } updateActions(); @@ -2332,14 +2373,17 @@ void UBDocumentController::moveIndexesToTrash(const QModelIndexList &list, UBDoc UBDocumentProxy *proxy = docModel->proxyForIndex(sourceSibling); - setDocument(proxy); - UBApplication::boardController->setActiveDocumentScene(proxy,0,true); - docModel->setCurrentDocument(proxy); + if (proxy) + { + setDocument(proxy); + UBApplication::boardController->setActiveDocumentScene(proxy,0,true); + docModel->setCurrentDocument(proxy); - selectionModel->select(sibling, QItemSelectionModel::ClearAndSelect); + selectionModel->select(sibling, QItemSelectionModel::ClearAndSelect); - deleteCurrentScene = false; + deleteCurrentScene = false; + } }else{ sibling = findNextSiblingNotSelected(proxyMapCurentScene, selectionModel); @@ -2348,13 +2392,16 @@ void UBDocumentController::moveIndexesToTrash(const QModelIndexList &list, UBDoc UBDocumentProxy *proxy = docModel->proxyForIndex(sourceSibling); - setDocument(proxy); - UBApplication::boardController->setActiveDocumentScene(proxy,0,true); - docModel->setCurrentDocument(proxy); + if (proxy) + { + setDocument(proxy); + UBApplication::boardController->setActiveDocumentScene(proxy,0,true); + docModel->setCurrentDocument(proxy); - selectionModel->select(sibling, QItemSelectionModel::ClearAndSelect); + selectionModel->select(sibling, QItemSelectionModel::ClearAndSelect); - deleteCurrentScene = false; + deleteCurrentScene = false; + } } } } diff --git a/src/document/UBDocumentController.h b/src/document/UBDocumentController.h index 7770a81b..c05a951c 100644 --- a/src/document/UBDocumentController.h +++ b/src/document/UBDocumentController.h @@ -434,6 +434,8 @@ class UBDocumentController : public UBDocumentContainer void createNewDocumentGroup(); void deleteSelectedItem(); + void deleteSingleItem(QModelIndex index, UBDocumentTreeModel *docModel); + void deleteMultipleItems(QModelIndexList indexes, UBDocumentTreeModel *docModel); void emptyFolder(const QModelIndex &index, DeletionType pDeletionType = MoveToTrash); void deleteIndexAndAssociatedData(const QModelIndex &pIndex); void renameSelectedItem();