]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/widgets/DraggableTreeView.cpp
Add dragging and dropping of bookmarks.
[PrivacyBrowserPC.git] / src / widgets / DraggableTreeView.cpp
1 /*
2  * Copyright 2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "DraggableTreeView.h"
22 #include "databases/BookmarksDatabase.h"
23 #include "dialogs/BookmarksDialog.h"
24
25 // Qt toolkit headers.
26 #include <QDebug>
27 #include <QDropEvent>
28 #include <QList>
29
30 // Construct the class.
31 DraggableTreeView::DraggableTreeView(QWidget *parentWidget) : QTreeView(parentWidget) {}
32
33 // Handle drop events.
34 void DraggableTreeView::dropEvent(QDropEvent *dropEvent)
35 {
36     // Get the list of currently selected items that are moving.
37     QList<QModelIndex> indexesMovingList = selectedIndexes();
38
39     // Create a list of database IDs that are moving.
40     QList<int> *databaseIdsMovingListPointer = new QList<int>;
41
42     // Populate the list of moving database IDs.
43     for (const QModelIndex &modelIndex : indexesMovingList)
44     {
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());
48     }
49
50     // Get the drop position.
51     int dropPosition = dropIndicatorPosition();
52
53     // Get the drop model index.
54     QModelIndex dropModelIndex = indexAt(dropEvent->pos());
55
56     // Get the drop display order.
57     int dropDisplayOrder = dropModelIndex.siblingAtColumn(BookmarksDialog::DISPLAY_ORDER).data().toInt();
58
59     // Process the move according to the drop position.
60     switch (dropPosition)
61     {
62         case QAbstractItemView::OnItem:
63             // TODO  Implement for moving into a folder.
64             break;
65
66         case QAbstractItemView::AboveItem:
67         {
68             // Get a list of all the bookmarks except those selected.
69             QList<BookmarkStruct> *bookmarksExceptSelectedListPointer = BookmarksDatabase::getBookmarksExcept(databaseIdsMovingListPointer);
70
71             // Initialize a new display order tracker.
72             int newDisplayOrder = 0;
73
74             // Move the bookmarks.
75             for (const BookmarkStruct &bookmarkStruct : *bookmarksExceptSelectedListPointer)
76             {
77                 // Check to see if this is the drop display order.
78                 if (bookmarkStruct.displayOrder == dropDisplayOrder)
79                 {
80                     // Add all of the bookmarks being moved to this point.
81                     for (const int databaseId : *databaseIdsMovingListPointer)
82                     {
83                         // Update the display order for each bookmark.
84                         BookmarksDatabase::updateDisplayOrder(databaseId, newDisplayOrder);
85
86                         // Increment the new display order.
87                         ++newDisplayOrder;
88                     }
89                 }
90
91                 // Set the bookmark's display order if it has changed.
92                 if (bookmarkStruct.displayOrder != newDisplayOrder)
93                     BookmarksDatabase::updateDisplayOrder(bookmarkStruct.id, newDisplayOrder);
94
95                 // Increment the new display order.
96                 ++newDisplayOrder;
97             }
98
99             break;
100         }
101
102         case QAbstractItemView::BelowItem:
103         {
104             // Get a list of all the bookmarks except those selected.
105             QList<BookmarkStruct> *bookmarksExceptSelectedListPointer = BookmarksDatabase::getBookmarksExcept(databaseIdsMovingListPointer);
106
107             // Initialize a new display order tracker.
108             int newDisplayOrder = 0;
109
110             // Move the bookmarks.
111             for (const BookmarkStruct &bookmarkStruct : *bookmarksExceptSelectedListPointer)
112             {
113                 // Set the bookmark's display order if it has changed.
114                 if (bookmarkStruct.displayOrder != newDisplayOrder)
115                     BookmarksDatabase::updateDisplayOrder(bookmarkStruct.id, newDisplayOrder);
116
117                 // Increment the new display order.
118                 ++newDisplayOrder;
119
120                 // Check to see if this is the drop display order.
121                 if (bookmarkStruct.displayOrder == dropDisplayOrder)
122                 {
123                     // Add all of the bookmarks being moved to this point.
124                     for (const int databaseId : *databaseIdsMovingListPointer)
125                     {
126                         // Update the display order for each bookmark.
127                         BookmarksDatabase::updateDisplayOrder(databaseId, newDisplayOrder);
128
129                         // Increment the new display order.
130                         ++newDisplayOrder;
131                     }
132                 }
133             }
134
135             break;
136         }
137
138         case QAbstractItemView::OnViewport:
139
140             // Get a list of all the bookmarks except those selected.
141             QList<BookmarkStruct> *bookmarksExceptSelectedListPointer = BookmarksDatabase::getBookmarksExcept(databaseIdsMovingListPointer);
142
143             // Initialize a new display order tracker.
144             int newDisplayOrder = 0;
145
146             // Move the bookmarks.
147             for (const BookmarkStruct &bookmarkStruct : *bookmarksExceptSelectedListPointer)
148             {
149                 // Set the bookmark's display order if it has changed.
150                 if (bookmarkStruct.displayOrder != newDisplayOrder)
151                     BookmarksDatabase::updateDisplayOrder(bookmarkStruct.id, newDisplayOrder);
152
153                 // Increment the new display order.
154                 ++newDisplayOrder;
155             }
156
157             // Add all of the bookmarks being moved to the end of the list.
158             for (const int databaseId : *databaseIdsMovingListPointer)
159             {
160                 // Update the display order for each bookmark.
161                 BookmarksDatabase::updateDisplayOrder(databaseId, newDisplayOrder);
162
163                 // Increment the new display order.
164                 ++newDisplayOrder;
165             }
166             break;
167     }
168
169     // Emit the bookmarks moved signal.
170     emit bookmarksMoved();
171 }