From 425129ff2f18d3dd97045eed8d376ad31980014a Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Sun, 14 May 2017 12:07:56 -0400 Subject: [PATCH] Document mode: improved doc. selection after deleting documents This mainly changes document mode behaviour in two ways: 1) When deleting 2+ items, a new document was selected in the list. Now, the current document is selected, or if it has been deleted, a the first document in the list is set as current document. 2) When deleting the last item in the trash, no document was selected. Now, the current document is selected instead. --- src/document/UBDocumentController.cpp | 70 ++++++++++++++++++++------- src/document/UBDocumentController.h | 3 +- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/document/UBDocumentController.cpp b/src/document/UBDocumentController.cpp index 2910e785..088544f8 100644 --- a/src/document/UBDocumentController.cpp +++ b/src/document/UBDocumentController.cpp @@ -546,37 +546,66 @@ void UBDocumentController::duplicateSelectedItem() } /** - * @brief When deleting multiple documents, find a new document and select it + * @brief Set the first document in the list as current document * - * This method simply selects the first un-selected document + * If there are no documents, a new one is created. */ -void UBDocumentController::selectADocumentOnMultipleTrashing() +void UBDocumentController::selectFirstDocumentInList() { - // Loop through all folders, and each document in those folders, until we find - // a document that is not in the current selection (which is being deleted) + // Loop through all folders until we find one that is not the trash and not empty, + // and select the first document in that folder for (int i(0); i < mDocumentUI->documentTreeWidget->topLevelItemCount(); ++i) { QTreeWidgetItem* item = mDocumentUI->documentTreeWidget->topLevelItem(i); UBDocumentGroupTreeItem* groupItem = dynamic_cast(item); - if (!groupItem->isTrashFolder()) { - for (int j(0); j < groupItem->childCount(); ++j) { - if (!mCurrentSelection.contains( groupItem->child(j) )) { - selectDocument(((UBDocumentProxyTreeItem*)groupItem->child(j))->proxy(), true); - return; - } - } + if (!groupItem->isTrashFolder() && groupItem->childCount() > 0) { + selectDocument(((UBDocumentProxyTreeItem*)groupItem->child(0))->proxy(), true); + groupItem->child(0)->setSelected(true); + return; } } - - // No document found => create a new one UBDocumentGroupTreeItem* topFolder = dynamic_cast(mDocumentUI->documentTreeWidget->topLevelItem(0)); UBDocumentProxy* document = UBPersistenceManager::persistenceManager()->createDocument(topFolder->groupName()); selectDocument(document, true); +} + +/** + * @brief Find the current document, and select it in the list + * + * If selectNewCurrentDocument is true, the first document in the list is selected and set as + * current document. + */ +void UBDocumentController::selectATreeItemOnMultipleTrashing(bool selectNewCurrentDocument) +{ + mCurrentSelection.clear(); + + if (selectNewCurrentDocument) { + selectFirstDocumentInList(); + return; + } + + // Find the currently selected document, and select its corresponding tree item + // If it isn't found, then the first document in the list is selected and set as current document + + for (int i(0); i < mDocumentUI->documentTreeWidget->topLevelItemCount(); i++) { + QTreeWidgetItem* item = mDocumentUI->documentTreeWidget->topLevelItem(i); + UBDocumentGroupTreeItem* groupItem = dynamic_cast(item); + if (!groupItem->isTrashFolder()) { + for (int j(0); j < groupItem->childCount(); j++) { + if (((UBDocumentProxyTreeItem*)groupItem->child(j))->proxy() == mBoardController->selectedDocument()) { + ((UBDocumentProxyTreeItem*)groupItem->child(j))->setSelected(true); + return; + } + } + } + } + + selectFirstDocumentInList(); } void UBDocumentController::selectADocumentOnTrashingSelectedOne(UBDocumentGroupTreeItem* groupTi,UBDocumentProxyTreeItem *proxyTi) @@ -807,7 +836,7 @@ void UBDocumentController::deleteTreeItem(QTreeWidgetItem * item, bool showConfi if (selectNewDocument) { if (mTrashTi->childCount()==0) - selectDocument(NULL); + selectATreeItemOnMultipleTrashing(false); else selectDocument(((UBDocumentProxyTreeItem*)mTrashTi->child(0))->proxy(), false); } @@ -849,11 +878,18 @@ void UBDocumentController::deleteSelectedItem() QList foldersToDelete; + bool currentDocumentDeleted = false; + foreach (QTreeWidgetItem * item, mCurrentSelection) { LastSelectedElementType type = itemType(item); - if (type == Document) + if (type == Document) { deleteTreeItem(item, false, false); + UBDocumentProxyTreeItem* proxyItem = dynamic_cast(item); + if (proxyItem && proxyItem->proxy() && (proxyItem->proxy() == mBoardController->selectedDocument())) + currentDocumentDeleted = true; + } + else if (type == Folder) // Delete folders later, to avoid deleting a document twice foldersToDelete << item; @@ -863,7 +899,7 @@ void UBDocumentController::deleteSelectedItem() deleteTreeItem(item, false, false); } - selectADocumentOnMultipleTrashing(); + selectATreeItemOnMultipleTrashing(currentDocumentDeleted); } else if (mSelectionType == Document || mSelectionType == Folder) { diff --git a/src/document/UBDocumentController.h b/src/document/UBDocumentController.h index 39e5f635..62160800 100644 --- a/src/document/UBDocumentController.h +++ b/src/document/UBDocumentController.h @@ -130,7 +130,8 @@ class UBDocumentController : public UBDocumentContainer QString mDefaultDocumentGroupName; void selectADocumentOnTrashingSelectedOne(UBDocumentGroupTreeItem* groupTi,UBDocumentProxyTreeItem *proxyTi); - void selectADocumentOnMultipleTrashing(); + void selectFirstDocumentInList(); + void selectATreeItemOnMultipleTrashing(bool selectNewCurrentDocument = false); void moveDocumentToTrash(UBDocumentGroupTreeItem* groupTi, UBDocumentProxyTreeItem *proxyTi, bool selectNewDocument); void moveFolderToTrash(UBDocumentGroupTreeItem* groupTi); void emptyTrash(bool showConfirmationDialog);