X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fdialogs%2FBookmarksDialog.cpp;fp=src%2Fdialogs%2FBookmarksDialog.cpp;h=4dc66ee873a04b892564b5e35b0f3a901c5551b9;hp=509fade9f7796b3e6f240c8ac30a7b958fa1cf4a;hb=f18185adbdce9891be0cbd2197838441aaa5ed3e;hpb=7c6edb3608791950c6146ac242e2b6f493ca8e8c diff --git a/src/dialogs/BookmarksDialog.cpp b/src/dialogs/BookmarksDialog.cpp index 509fade..4dc66ee 100644 --- a/src/dialogs/BookmarksDialog.cpp +++ b/src/dialogs/BookmarksDialog.cpp @@ -21,6 +21,8 @@ #include "BookmarksDialog.h" #include "ui_BookmarksDialog.h" #include "databases/BookmarksDatabase.h" +#include "dialogs/AddBookmarkDialog.h" +#include "dialogs/EditBookmarkDialog.h" // KDE Frameworks headers. #include @@ -28,10 +30,9 @@ // Qt toolkit headers. #include #include -#include // Construct the class. -BookmarksDialog::BookmarksDialog() : QDialog(nullptr) +BookmarksDialog::BookmarksDialog(QIcon currentWebsiteFavorieIcon) : QDialog(nullptr), websiteFavoriteIcon(currentWebsiteFavorieIcon) { // Set the dialog window title. setWindowTitle(i18nc("The bookmarks dialog window title", "Bookmarks")); @@ -45,62 +46,169 @@ BookmarksDialog::BookmarksDialog() : QDialog(nullptr) // Setup the UI. bookmarksDialogUi.setupUi(this); - // Get the list of bookmarks. - std::list *bookmarksListPointer = BookmarksDatabase::getBookmarks(); - - // Get a handle for the tree view. - QTreeView *treeViewPointer = bookmarksDialogUi.treeView; + // Get a handle for the draggable tree view. + draggableTreeViewPointer = bookmarksDialogUi.draggableTreeView; // Initialize the tree model. - QStandardItemModel *treeModelPointer = new QStandardItemModel(); + treeModelPointer = new QStandardItemModel(); + + // Auto resize the headers. + draggableTreeViewPointer->header()->setSectionResizeMode(QHeaderView::ResizeToContents); + + // Indicate that all the rows are the same height, which improves performance. + draggableTreeViewPointer->setUniformRowHeights(true); + + // Set the selection mode to allow multiple rows to be selected at once. + draggableTreeViewPointer->setSelectionMode(QAbstractItemView::ExtendedSelection); + + // Allow dragging of bookmarks to reorder. + draggableTreeViewPointer->setDragDropMode(QAbstractItemView::InternalMove); + + // Set the tree model. + draggableTreeViewPointer->setModel(treeModelPointer); + + // Get a handle for the tree selection model. + treeSelectionModelPointer = draggableTreeViewPointer->selectionModel(); + + // Listen for selection changes. + connect(treeSelectionModelPointer, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(updateUi())); + + // Repopulate the bookmarks when they are moved. + connect(draggableTreeViewPointer, SIGNAL(bookmarksMoved()), this, SLOT(refreshBookmarks())); + + // Get handles for the buttons. + QPushButton *addBookmarkButtonPointer = bookmarksDialogUi.addBookmarkButton; + editButtonPointer = bookmarksDialogUi.editButton; + deleteItemsButtonPointer = bookmarksDialogUi.deleteItemsButton; + QDialogButtonBox *dialogButtonBoxPointer = bookmarksDialogUi.dialogButtonBox; + QPushButton *closeButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Close); + + // Connect the buttons. + connect(addBookmarkButtonPointer, SIGNAL(clicked()), this, SLOT(showAddBookmarkDialog())); + connect(editButtonPointer, SIGNAL(clicked()), this, SLOT(showEditDialog())); + connect(deleteItemsButtonPointer, SIGNAL(clicked()), this, SLOT(deleteItems())); + connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject())); + + // Set the close button to be the default. + closeButtonPointer->setDefault(true); + + // Monitor editing of data in the tree model. + connect(treeModelPointer, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateBookmarkFromTree(QStandardItem*))); + + // Populate the bookmarks. + populateBookmarks(); +} + +void BookmarksDialog::deleteItems() const +{ + // Get the list of selected model indexes. + QList selectedModelIndexList = treeSelectionModelPointer->selectedRows(DATABASE_ID_COLUMN); + + // Delete each of the bookmarks. + for (const QModelIndex &modelIndex : selectedModelIndexList) + { + // Delete the bookmark. + BookmarksDatabase::deleteBookmark(modelIndex.data().toInt()); + } + + // Repopulate the bookmarks in this dialog + populateBookmarks(); + + // Emit the bookmark updated signal to redraw the bookmarks in the menu and toolbar. + emit bookmarkUpdated(); +} + +void BookmarksDialog::populateBookmarks() const +{ + // Clear the current contents of the tree model. + treeModelPointer->clear(); // Set the column count. - treeModelPointer->setColumnCount(3); + treeModelPointer->setColumnCount(4); - // Set the tree header data. The first column is the database ID, which is not displayed. - treeModelPointer->setHeaderData(1, Qt::Horizontal, i18nc("The bookmark Name header.", "Name")); - treeModelPointer->setHeaderData(2, Qt::Horizontal, i18nc("The bookmark URL header.", "URL")); + // Set the tree header data. The last column is the database ID, which is not displayed. + treeModelPointer->setHeaderData(BOOKMARK_NAME_COLUMN, Qt::Horizontal, i18nc("The bookmark Name header.", "Name")); + treeModelPointer->setHeaderData(BOOKMARK_URL_COLUMN, Qt::Horizontal, i18nc("The bookmark URL header.", "URL")); + + // Hide the backend columns. + draggableTreeViewPointer->setColumnHidden(DATABASE_ID_COLUMN, true); + draggableTreeViewPointer->setColumnHidden(DISPLAY_ORDER, true); + + // Get the list of bookmarks. + std::list *bookmarksListPointer = BookmarksDatabase::getBookmarks(); // Populate the bookmarks tree view. - for (BookmarkStruct bookmarkStruct : *bookmarksListPointer) + for (const BookmarkStruct &bookmarkStruct : *bookmarksListPointer) { // Create a list for the bookmark items. QList bookmarkItemList; // Create the bookmark items. - QStandardItem *idItemPointer = new QStandardItem(QString::number(bookmarkStruct.id)); QStandardItem *nameItemPointer = new QStandardItem(bookmarkStruct.favoriteIcon, bookmarkStruct.bookmarkName); QStandardItem *urlItemPointer = new QStandardItem(bookmarkStruct.bookmarkUrl); + QStandardItem *idItemPointer = new QStandardItem(QString::number(bookmarkStruct.id)); + QStandardItem *displayOrderPointer = new QStandardItem(QString::number(bookmarkStruct.displayOrder)); - // Populate the cookie standard item list. - bookmarkItemList.append(idItemPointer); + nameItemPointer->setDragEnabled(true); + nameItemPointer->setDropEnabled(true); + + // Disable dragging the URL. + urlItemPointer->setDragEnabled(false); + + // Disable dropping on the URL. For some reason this doesn't work. + urlItemPointer->setDropEnabled(false); + + // Disable selecting the URL. + urlItemPointer->setSelectable(false); + + // Populate the bookmark item list. bookmarkItemList.append(nameItemPointer); bookmarkItemList.append(urlItemPointer); + bookmarkItemList.append(idItemPointer); + bookmarkItemList.append(displayOrderPointer); - // Add the cookie to the tree. + // Add the bookmark to the tree. treeModelPointer->appendRow(bookmarkItemList); } - // Auto resize the headers. - treeViewPointer->header()->setSectionResizeMode(QHeaderView::ResizeToContents); + // Update the UI. + updateUi(); +} - // Indicate that all the rows are the same height, which improves performance. - treeViewPointer->setUniformRowHeights(true); +void BookmarksDialog::refreshBookmarks() const +{ + // Repopulate the bookmarks in this dialog + populateBookmarks(); - // Set the tree model. - treeViewPointer->setModel(treeModelPointer); + // Emit the bookmark updated signal to redraw the bookmarks in the menu and toolbar. + emit bookmarkUpdated(); +} - // Hide the database ID column. - treeViewPointer->setColumnHidden(0, true); +void BookmarksDialog::showAddBookmarkDialog() const +{ + // Instantiate an add bookmark dialog. + AddBookmarkDialog *addBookmarkDialogPointer = new AddBookmarkDialog(QLatin1String(""), QLatin1String(""), websiteFavoriteIcon); - // Get handles for the buttons. - QDialogButtonBox *dialogButtonBoxPointer = bookmarksDialogUi.dialogButtonBox; + // Update the displayed bookmarks when a new one is added. + connect(addBookmarkDialogPointer, SIGNAL(bookmarkAdded()), this, SLOT(refreshBookmarks())); - // Connect the buttons. - connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject())); + // Show the dialog. + addBookmarkDialogPointer->show(); +} - // Monitor editing of data in the tree model. - connect(treeModelPointer, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateBookmarkFromTree(QStandardItem*))); +void BookmarksDialog::showEditDialog() +{ + // Get the current model index. + QModelIndex currentIndex = treeSelectionModelPointer->currentIndex(); + + // Instantiate an edit bookmark dialog. + QDialog *editBookmarkDialogPointer = new EditBookmarkDialog(currentIndex.siblingAtColumn(DATABASE_ID_COLUMN).data().toInt(), websiteFavoriteIcon); + + // Show the dialog. + editBookmarkDialogPointer->show(); + + // Process bookmark events. + connect(editBookmarkDialogPointer, SIGNAL(bookmarkSaved()), this, SLOT(refreshBookmarks())); } void BookmarksDialog::updateBookmarkFromTree(QStandardItem *modifiedStandardItem) @@ -109,13 +217,13 @@ void BookmarksDialog::updateBookmarkFromTree(QStandardItem *modifiedStandardItem QModelIndex modifiedItemModelIndex = modifiedStandardItem->index(); // Get the model index of the database ID. - QModelIndex databaseIdModelIndex = modifiedItemModelIndex.siblingAtColumn(0); + QModelIndex databaseIdModelIndex = modifiedItemModelIndex.siblingAtColumn(DATABASE_ID_COLUMN); // Get the database ID. int databaseId = databaseIdModelIndex.data().toInt(); // Check to see if the bookmark name or the URL was edited. - if (modifiedStandardItem->column() == 1) // The bookmark name was edited. + if (modifiedStandardItem->column() == BOOKMARK_NAME_COLUMN) // The bookmark name was edited. { // Update the bookmark name. BookmarksDatabase::updateBookmarkName(databaseId, modifiedStandardItem->text()); @@ -129,3 +237,26 @@ void BookmarksDialog::updateBookmarkFromTree(QStandardItem *modifiedStandardItem // Emit the bookmark updated signal. emit bookmarkUpdated(); } + +void BookmarksDialog::updateUi() const +{ + // Set the status of the buttons. + if (treeSelectionModelPointer->hasSelection()) // A bookmark or folder is selected. + { + // Enabled the buttons. + editButtonPointer->setEnabled(true); + deleteItemsButtonPointer->setEnabled(true); + + // Update the delete items button text. + deleteItemsButtonPointer->setText(i18ncp("Delete items button populated text.", "Delete %1 item", "Delete %1 items", treeSelectionModelPointer->selectedRows().count())); + } + else // Nothing is selected. + { + // Disable the buttons. + editButtonPointer->setEnabled(false); + deleteItemsButtonPointer->setEnabled(false); + + // Update the delete items button text. + deleteItemsButtonPointer->setText(i18nc("Delete items button default text", "Delete items")); + } +}