From 436c5cc7e3fd04cabfa07d6e76d38907a0dfbd0d Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Thu, 11 Jan 2024 09:36:58 -0700 Subject: [PATCH] Make dropping a bookmark under an open folder place it inside the folder. https://redmine.stoutner.com/issues/1150 --- src/widgets/DraggableTreeView.cpp | 165 ++++++++++++++++-------------- src/widgets/DraggableTreeView.h | 5 +- 2 files changed, 95 insertions(+), 75 deletions(-) diff --git a/src/widgets/DraggableTreeView.cpp b/src/widgets/DraggableTreeView.cpp index b6a28da..bbe0868 100644 --- a/src/widgets/DraggableTreeView.cpp +++ b/src/widgets/DraggableTreeView.cpp @@ -57,49 +57,16 @@ void DraggableTreeView::dropEvent(QDropEvent *dropEvent) QModelIndex dropModelIndex = indexAt(dropEvent->pos()); // Create a previous parent folder ID standard C++ list (which can be sorted and from which duplicates can be removed). - std::list previousParentFolderIdList; + std::list *previousParentFolderIdListPointer = new std::list; // Process the move according to the drop position. switch (dropPosition) { case QAbstractItemView::OnItem: // This will only ever be called for folders as `BookmarksDialog::populateSubfolders` creates bookmarks without `setDropEnabled`. { - // Get the new parent folder ID. - double newParentFolderId = dropModelIndex.siblingAtColumn(BookmarksDialog::FOLDER_ID_COLUMN).data().toDouble(); + // Move everything to the beginning of the folder. + moveToBeginningOfFolder(dropModelIndex, selectedDatabaseIdsListPointer, rootSelectedDatabaseIdsListPointer, previousParentFolderIdListPointer); - // Get a list of all the items in the target folder except those selected. - QList *itemsInFolderExceptSelectedListPointer = BookmarksDatabase::getBookmarksInFolderExcept(newParentFolderId, selectedDatabaseIdsListPointer); - - // Initialize a new display order tracker. - int newDisplayOrder = 0; - - // Move all the items to the top of the target folder. - for (const int databaseId : *rootSelectedDatabaseIdsListPointer) - { - // Get the item's current parent folder ID. - double currentParentFolderId = BookmarksDatabase::getParentFolderId(databaseId); - - // Add the parent folder ID to the list of previous parent folders if the item is from a different folder. - if (currentParentFolderId != newParentFolderId) - previousParentFolderIdList.push_back(currentParentFolderId); - - // Update the parent folder and display order for each bookmark. - BookmarksDatabase::updateParentFolderAndDisplayOrder(databaseId, newParentFolderId, newDisplayOrder); - - // Increment the new display order. - ++newDisplayOrder; - } - - // Update the display order of the existing items in the folder. - for (const BookmarkStruct &bookmarkStruct : *itemsInFolderExceptSelectedListPointer) - { - // Set the bookmark's display order if it has changed. - if (bookmarkStruct.displayOrder != newDisplayOrder) - BookmarksDatabase::updateDisplayOrder(bookmarkStruct.databaseId, newDisplayOrder); - - // Increment the new display order. - ++newDisplayOrder; - } break; } @@ -131,7 +98,7 @@ void DraggableTreeView::dropEvent(QDropEvent *dropEvent) // Add the parent folder ID to the list of previous parent folders if the item is from a different folder. if (currentParentFolderId != dropParentFolderId) - previousParentFolderIdList.push_back(currentParentFolderId); + previousParentFolderIdListPointer->push_back(currentParentFolderId); // Update the parent folder and display order for each bookmark. BookmarksDatabase::updateParentFolderAndDisplayOrder(databaseId, dropParentFolderId, newDisplayOrder); @@ -153,46 +120,55 @@ void DraggableTreeView::dropEvent(QDropEvent *dropEvent) case QAbstractItemView::BelowItem: { - // Get the drop display order. - int dropDisplayOrder = dropModelIndex.siblingAtColumn(BookmarksDialog::DISPLAY_ORDER_COLUMN).data().toInt(); + // Check to see if the drop model index is an expanded folder. + if (dropModelIndex.siblingAtColumn(BookmarksDialog::IS_FOLDER_COLUMN).data().toBool() && isExpanded(dropModelIndex)) // The drop model index is an expanded folder. + { + // Move everything to the beginning of the folder. + moveToBeginningOfFolder(dropModelIndex, selectedDatabaseIdsListPointer, rootSelectedDatabaseIdsListPointer, previousParentFolderIdListPointer); + } + else // The drop model index is not an expanded folder. + { + // Get the drop display order. + int dropDisplayOrder = dropModelIndex.siblingAtColumn(BookmarksDialog::DISPLAY_ORDER_COLUMN).data().toInt(); - // Get the drop parent folder ID. - double dropParentFolderId = dropModelIndex.parent().siblingAtColumn(BookmarksDialog::FOLDER_ID_COLUMN).data().toDouble(); + // Get the drop parent folder ID. + double dropParentFolderId = dropModelIndex.parent().siblingAtColumn(BookmarksDialog::FOLDER_ID_COLUMN).data().toDouble(); - // Get a list of all the items in the target folder except those selected. - QList *itemsInFolderExceptSelectedListPointer = BookmarksDatabase::getBookmarksInFolderExcept(dropParentFolderId, selectedDatabaseIdsListPointer); + // Get a list of all the items in the target folder except those selected. + QList *itemsInFolderExceptSelectedListPointer = BookmarksDatabase::getBookmarksInFolderExcept(dropParentFolderId, selectedDatabaseIdsListPointer); - // Initialize a new display order tracker. - int newDisplayOrder = 0; + // Initialize a new display order tracker. + int newDisplayOrder = 0; - // Process all the items in the target folder, moving in the new ones. - for (const BookmarkStruct &bookmarkStruct : *itemsInFolderExceptSelectedListPointer) - { - // Set the bookmark's display order if it has changed. - if (bookmarkStruct.displayOrder != newDisplayOrder) - BookmarksDatabase::updateDisplayOrder(bookmarkStruct.databaseId, newDisplayOrder); - - // Increment the new display order. - ++newDisplayOrder; - - // Check to see if this is the drop display order. - if (bookmarkStruct.displayOrder == dropDisplayOrder) + // Process all the items in the target folder, moving in the new ones. + for (const BookmarkStruct &bookmarkStruct : *itemsInFolderExceptSelectedListPointer) { - // Add all of the bookmarks being moved to this point. - for (const int databaseId : *rootSelectedDatabaseIdsListPointer) - { - // Get the item's current parent folder ID. - double currentParentFolderId = BookmarksDatabase::getParentFolderId(databaseId); + // Set the bookmark's display order if it has changed. + if (bookmarkStruct.displayOrder != newDisplayOrder) + BookmarksDatabase::updateDisplayOrder(bookmarkStruct.databaseId, newDisplayOrder); - // Add the parent folder ID to the list of previous parent folders if the item is from a different folder. - if (currentParentFolderId != dropParentFolderId) - previousParentFolderIdList.push_back(currentParentFolderId); - - // Update the parent folder and display order for each bookmark. - BookmarksDatabase::updateParentFolderAndDisplayOrder(databaseId, dropParentFolderId, newDisplayOrder); + // Increment the new display order. + ++newDisplayOrder; - // Increment the new display order. - ++newDisplayOrder; + // Check to see if this is the drop display order. + if (bookmarkStruct.displayOrder == dropDisplayOrder) + { + // Add all of the bookmarks being moved to this point. + for (const int databaseId : *rootSelectedDatabaseIdsListPointer) + { + // Get the item's current parent folder ID. + double currentParentFolderId = BookmarksDatabase::getParentFolderId(databaseId); + + // Add the parent folder ID to the list of previous parent folders if the item is from a different folder. + if (currentParentFolderId != dropParentFolderId) + previousParentFolderIdListPointer->push_back(currentParentFolderId); + + // Update the parent folder and display order for each bookmark. + BookmarksDatabase::updateParentFolderAndDisplayOrder(databaseId, dropParentFolderId, newDisplayOrder); + + // Increment the new display order. + ++newDisplayOrder; + } } } } @@ -229,7 +205,7 @@ void DraggableTreeView::dropEvent(QDropEvent *dropEvent) // Add the parent folder ID to the list of previous parent folders if the item is from a different folder. if (currentParentFolderId != dropParentFolderId) - previousParentFolderIdList.push_back(currentParentFolderId); + previousParentFolderIdListPointer->push_back(currentParentFolderId); // Update the parent folder and display order for each bookmark. BookmarksDatabase::updateParentFolderAndDisplayOrder(databaseId, dropParentFolderId, newDisplayOrder); @@ -242,13 +218,13 @@ void DraggableTreeView::dropEvent(QDropEvent *dropEvent) } // Sort the previous parent folder ID list. - previousParentFolderIdList.sort(); + previousParentFolderIdListPointer->sort(); // Remove duplicates from the parent folder ID list. - previousParentFolderIdList.unique(); + previousParentFolderIdListPointer->unique(); // Update the folder contents display order for each previous parent folder. - for (const double parentFolderId : previousParentFolderIdList) + for (const double parentFolderId : *previousParentFolderIdListPointer) BookmarksDatabase::updateFolderContentsDisplayOrder(parentFolderId); // Emit the bookmarks moved signal. @@ -282,3 +258,44 @@ QList* DraggableTreeView::getRootSelectedDatabaseIds(QList *selectedDa // Return the root selected database IDs list. return rootSelectedDatabaseIdsListPointer; } + +void DraggableTreeView::moveToBeginningOfFolder(const QModelIndex &dropModelIndex, QList *selectedDatabaseIdsListPointer, QList *rootSelectedDatabaseIdsListPointer, + std::list *previousParentFolderIdListPointer) const +{ + // Get the new parent folder ID. + double newParentFolderId = dropModelIndex.siblingAtColumn(BookmarksDialog::FOLDER_ID_COLUMN).data().toDouble(); + + // Get a list of all the items in the target folder except those selected. + QList *itemsInFolderExceptSelectedListPointer = BookmarksDatabase::getBookmarksInFolderExcept(newParentFolderId, selectedDatabaseIdsListPointer); + + // Initialize a new display order tracker. + int newDisplayOrder = 0; + + // Move all the items to the top of the target folder. + for (const int databaseId : *rootSelectedDatabaseIdsListPointer) + { + // Get the item's current parent folder ID. + double currentParentFolderId = BookmarksDatabase::getParentFolderId(databaseId); + + // Add the parent folder ID to the list of previous parent folders if the item is from a different folder. + if (currentParentFolderId != newParentFolderId) + previousParentFolderIdListPointer->push_back(currentParentFolderId); + + // Update the parent folder and display order for each bookmark. + BookmarksDatabase::updateParentFolderAndDisplayOrder(databaseId, newParentFolderId, newDisplayOrder); + + // Increment the new display order. + ++newDisplayOrder; + } + + // Update the display order of the existing items in the folder. + for (const BookmarkStruct &bookmarkStruct : *itemsInFolderExceptSelectedListPointer) + { + // Set the bookmark's display order if it has changed. + if (bookmarkStruct.displayOrder != newDisplayOrder) + BookmarksDatabase::updateDisplayOrder(bookmarkStruct.databaseId, newDisplayOrder); + + // Increment the new display order. + ++newDisplayOrder; + } +} diff --git a/src/widgets/DraggableTreeView.h b/src/widgets/DraggableTreeView.h index 983ff1c..961d2fa 100644 --- a/src/widgets/DraggableTreeView.h +++ b/src/widgets/DraggableTreeView.h @@ -1,5 +1,5 @@ /* - * Copyright 2023 Soren Stoutner . + * Copyright 2023-2024 Soren Stoutner . * * This file is part of Privacy Browser PC . * @@ -40,6 +40,9 @@ protected: void dropEvent(QDropEvent *event) override; private: + // The private functions QList* getRootSelectedDatabaseIds(QList *selectedDatabaseIdsPointer) const; + void moveToBeginningOfFolder(const QModelIndex &dropModelIndex, QList *selectedDatabaseIdsListPointer, QList *rootSelectedDatabaseIdsListPointer, + std::list *previousParentFolderIdListPointer) const; }; #endif -- 2.45.2