]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/commitdiff
Make dropping a bookmark under an open folder place it inside the folder. https...
authorSoren Stoutner <soren@stoutner.com>
Thu, 11 Jan 2024 16:36:58 +0000 (09:36 -0700)
committerSoren Stoutner <soren@stoutner.com>
Thu, 11 Jan 2024 16:36:58 +0000 (09:36 -0700)
src/widgets/DraggableTreeView.cpp
src/widgets/DraggableTreeView.h

index b6a28da6896e7ea4460469620b81add1847790cf..bbe086892c2dbc58ade9dd4038167c7610affcf5 100644 (file)
@@ -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<double> previousParentFolderIdList;
+    std::list<double> *previousParentFolderIdListPointer = new std::list<double>;
 
     // 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<BookmarkStruct> *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<BookmarkStruct> *itemsInFolderExceptSelectedListPointer = BookmarksDatabase::getBookmarksInFolderExcept(dropParentFolderId, selectedDatabaseIdsListPointer);
+                // Get a list of all the items in the target folder except those selected.
+                QList<BookmarkStruct> *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<int>* DraggableTreeView::getRootSelectedDatabaseIds(QList<int> *selectedDa
     // Return the root selected database IDs list.
     return rootSelectedDatabaseIdsListPointer;
 }
+
+void DraggableTreeView::moveToBeginningOfFolder(const QModelIndex &dropModelIndex, QList<int> *selectedDatabaseIdsListPointer, QList<int> *rootSelectedDatabaseIdsListPointer,
+                                                std::list<double> *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<BookmarkStruct> *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;
+            }
+}
index 983ff1c2924c5875dac86a72c0a50f996359efe3..961d2fa227f9e5c542b82ba048f7a87a301c0340 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2023 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2023-2024 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
  *
@@ -40,6 +40,9 @@ protected:
     void dropEvent(QDropEvent *event) override;
 
 private:
+    // The private functions
     QList<int>* getRootSelectedDatabaseIds(QList<int> *selectedDatabaseIdsPointer) const;
+    void moveToBeginningOfFolder(const QModelIndex &dropModelIndex, QList<int> *selectedDatabaseIdsListPointer, QList<int> *rootSelectedDatabaseIdsListPointer,
+                                 std::list<double> *previousParentFolderIdListPointer) const;
 };
 #endif