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);