2 * Copyright 2023 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "DraggableTreeView.h"
22 #include "databases/BookmarksDatabase.h"
23 #include "dialogs/BookmarksDialog.h"
25 // Qt toolkit headers.
30 // Construct the class.
31 DraggableTreeView::DraggableTreeView(QWidget *parentWidget) : QTreeView(parentWidget) {}
33 // Handle drop events.
34 void DraggableTreeView::dropEvent(QDropEvent *dropEvent)
36 // Get the list of currently selected items that are moving.
37 QList<QModelIndex> indexesMovingList = selectedIndexes();
39 // Create a list of database IDs that are moving.
40 QList<int> *databaseIdsMovingListPointer = new QList<int>;
42 // Populate the list of moving database IDs.
43 for (const QModelIndex &modelIndex : indexesMovingList)
45 // Only process model indexes from the bookmark name column (by default, there will be model indexes from all the visible columns in the list).
46 if (modelIndex.column() == BookmarksDialog::BOOKMARK_NAME_COLUMN)
47 databaseIdsMovingListPointer->append(modelIndex.siblingAtColumn(BookmarksDialog::DATABASE_ID_COLUMN).data().toInt());
50 // Get the drop position.
51 int dropPosition = dropIndicatorPosition();
53 // Get the drop model index.
54 QModelIndex dropModelIndex = indexAt(dropEvent->pos());
56 // Get the drop display order.
57 int dropDisplayOrder = dropModelIndex.siblingAtColumn(BookmarksDialog::DISPLAY_ORDER).data().toInt();
59 // Process the move according to the drop position.
62 case QAbstractItemView::OnItem:
63 // TODO Implement for moving into a folder.
66 case QAbstractItemView::AboveItem:
68 // Get a list of all the bookmarks except those selected.
69 QList<BookmarkStruct> *bookmarksExceptSelectedListPointer = BookmarksDatabase::getBookmarksExcept(databaseIdsMovingListPointer);
71 // Initialize a new display order tracker.
72 int newDisplayOrder = 0;
74 // Move the bookmarks.
75 for (const BookmarkStruct &bookmarkStruct : *bookmarksExceptSelectedListPointer)
77 // Check to see if this is the drop display order.
78 if (bookmarkStruct.displayOrder == dropDisplayOrder)
80 // Add all of the bookmarks being moved to this point.
81 for (const int databaseId : *databaseIdsMovingListPointer)
83 // Update the display order for each bookmark.
84 BookmarksDatabase::updateDisplayOrder(databaseId, newDisplayOrder);
86 // Increment the new display order.
91 // Set the bookmark's display order if it has changed.
92 if (bookmarkStruct.displayOrder != newDisplayOrder)
93 BookmarksDatabase::updateDisplayOrder(bookmarkStruct.id, newDisplayOrder);
95 // Increment the new display order.
102 case QAbstractItemView::BelowItem:
104 // Get a list of all the bookmarks except those selected.
105 QList<BookmarkStruct> *bookmarksExceptSelectedListPointer = BookmarksDatabase::getBookmarksExcept(databaseIdsMovingListPointer);
107 // Initialize a new display order tracker.
108 int newDisplayOrder = 0;
110 // Move the bookmarks.
111 for (const BookmarkStruct &bookmarkStruct : *bookmarksExceptSelectedListPointer)
113 // Set the bookmark's display order if it has changed.
114 if (bookmarkStruct.displayOrder != newDisplayOrder)
115 BookmarksDatabase::updateDisplayOrder(bookmarkStruct.id, newDisplayOrder);
117 // Increment the new display order.
120 // Check to see if this is the drop display order.
121 if (bookmarkStruct.displayOrder == dropDisplayOrder)
123 // Add all of the bookmarks being moved to this point.
124 for (const int databaseId : *databaseIdsMovingListPointer)
126 // Update the display order for each bookmark.
127 BookmarksDatabase::updateDisplayOrder(databaseId, newDisplayOrder);
129 // Increment the new display order.
138 case QAbstractItemView::OnViewport:
140 // Get a list of all the bookmarks except those selected.
141 QList<BookmarkStruct> *bookmarksExceptSelectedListPointer = BookmarksDatabase::getBookmarksExcept(databaseIdsMovingListPointer);
143 // Initialize a new display order tracker.
144 int newDisplayOrder = 0;
146 // Move the bookmarks.
147 for (const BookmarkStruct &bookmarkStruct : *bookmarksExceptSelectedListPointer)
149 // Set the bookmark's display order if it has changed.
150 if (bookmarkStruct.displayOrder != newDisplayOrder)
151 BookmarksDatabase::updateDisplayOrder(bookmarkStruct.id, newDisplayOrder);
153 // Increment the new display order.
157 // Add all of the bookmarks being moved to the end of the list.
158 for (const int databaseId : *databaseIdsMovingListPointer)
160 // Update the display order for each bookmark.
161 BookmarksDatabase::updateDisplayOrder(databaseId, newDisplayOrder);
163 // Increment the new display order.
169 // Emit the bookmarks moved signal.
170 emit bookmarksMoved();