From: Soren Stoutner Date: Thu, 7 Sep 2023 19:17:55 +0000 (-0700) Subject: Add context menus to the bookmarks toolbar. X-Git-Tag: v0.5~5 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=3331f3d1a5d8924a67bcac2a2c842e15a421fea2;p=PrivacyBrowserPC.git Add context menus to the bookmarks toolbar. --- diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index dda65d9..0fdfe26 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -28,6 +28,7 @@ #include "dialogs/BookmarksDialog.h" #include "dialogs/CookiesDialog.h" #include "dialogs/DomainSettingsDialog.h" +#include "dialogs/EditBookmarkDialog.h" #include "helpers/SearchEngineHelper.h" #include "helpers/UserAgentHelper.h" #include "structs/BookmarkStruct.h" @@ -38,6 +39,8 @@ #include // Qt toolkit headers. +#include +#include #include #include #include @@ -47,7 +50,7 @@ #include // Construct the class. -BrowserWindow::BrowserWindow(bool firstWindow) : KXmlGuiWindow() +BrowserWindow::BrowserWindow(bool firstWindow, QString *initialUrlStringPointer) : KXmlGuiWindow() { // Initialize the variables. javaScriptEnabled = false; @@ -526,9 +529,17 @@ BrowserWindow::BrowserWindow(bool firstWindow) : KXmlGuiWindow() updateUserAgentActions(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()), true); updateZoomActions(Settings::zoomFactor()); - // Load the initial website if this is the first window. - if (firstWindow) + // Populate the first tab. + if (firstWindow) // This is the first window. + { + // Load the initial website. tabWidgetPointer->loadInitialWebsite(); + } + else if (initialUrlStringPointer) // An initial URL was specified. + { + // Load the initial URL. + tabWidgetPointer->loadUrlFromLineEdit(*initialUrlStringPointer); + } } void BrowserWindow::addOrEditDomainSettings() const @@ -776,8 +787,11 @@ void BrowserWindow::loadUrlFromLineEdit(const QString &url) const void BrowserWindow::newWindow() const { - // Display a new instance of Privacy Browser. - (new BrowserWindow)->show(); + // Create a new browser window. + BrowserWindow *browserWindowPointer = new BrowserWindow(); + + // Show the new browser window. + browserWindowPointer->show(); } void BrowserWindow::populateBookmarks() @@ -831,6 +845,9 @@ void BrowserWindow::populateBookmarks() } ); + // Add the bookmark database ID to the toolbar action. + toolBarBookmarkActionPointer->setData(bookmarkStruct.id); + // Add the actions to the current bookmarks lists. bookmarksMenuCurrentActionList.append(menuBookmarkActionPointer); bookmarksToolBarCurrentActionList.append(toolBarBookmarkActionPointer); @@ -877,9 +894,124 @@ void BrowserWindow::showAddBookmarkDialog() const addBookmarkDialogPointer->show(); } -void BrowserWindow::showBookmarkContextMenu(const QPoint&) const +void BrowserWindow::showBookmarkContextMenu(const QPoint &point) { - qDebug() << "Show the bookmark context menu."; + // Get the bookmark action. + QAction *bookmarkActionPointer = bookmarksToolBarPointer->actionAt(point); + + // Check to see if an bookmark was clicked. + if (bookmarkActionPointer) // A bookmark was clicked. + { + // Create a bookmark context menu. + QMenu *bookmarkContextMenuPointer = new QMenu(); + + // Get the bookmark ID from the action. + int bookmarkId = bookmarkActionPointer->data().toInt(); + + // Add the open in new tab action to the menu. + bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("tab-new")), i18nc("Open bookmark in new tab context menu entry", "Open in New Tab"), [=] + { + // Get the bookmark. + BookmarkStruct *bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId); + + // Open the bookmark in a new tab. `true` removes the URL line edit focus, `false` does not load a background tab. + tabWidgetPointer->addTab(true, false, bookmarkStructPointer->bookmarkUrl); + } + ); + + // Add the open in background tab action to the menu. + bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("tab-new")), i18nc("Open bookmark in background tab context menu entry", "Open in Background Tab"), [=] + { + // Get the bookmark. + BookmarkStruct *bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId); + + // Open the bookmark in a new tab. `true` removes the URL line edit focus, `true` loads a background tab. + tabWidgetPointer->addTab(true, true, bookmarkStructPointer->bookmarkUrl); + } + ); + + // Add the open in new window action to the menu. + bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("window-new")), i18nc("Open bookmark in new window context menu entry", "Open in New Window"), [=] + { + // Get the bookmark. + BookmarkStruct *bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId); + + // Create a new browser window. + BrowserWindow *browserWindowPointer = new BrowserWindow(false, &bookmarkStructPointer->bookmarkUrl); + + // Show the new browser window. + browserWindowPointer->show(); + } + ); + + // Add a separator. + bookmarkContextMenuPointer->addSeparator(); + + // Add the edit action to the menu. + bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("edit-entry")), i18nc("Edit bookmark context menu entry", "Edit"), [=] + { + // Get the current tab favorite icon. + QIcon currentTabFavoriteIcon = tabWidgetPointer->getCurrentTabFavoritIcon(); + + // Instantiate an edit bookmark dialog. + QDialog *editBookmarkDialogPointer = new EditBookmarkDialog(bookmarkId, currentTabFavoriteIcon); + + // Show the dialog. + editBookmarkDialogPointer->show(); + + // Process bookmark events. + connect(editBookmarkDialogPointer, SIGNAL(bookmarkSaved()), this, SLOT(populateBookmarks())); + } + ); + + // Add the copy URL action to the menu. + bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("edit-copy")), i18nc("Copy bookmark url context menu entry", "Copy URL"), [=] + { + // Get the bookmark. + BookmarkStruct *bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId); + + // Get a handle for the clipboard. + QClipboard *clipboard = QGuiApplication::clipboard(); + + // Place the URL on the keyboard. + clipboard->setText(bookmarkStructPointer->bookmarkUrl); + } + ); + + // Add a separator. + bookmarkContextMenuPointer->addSeparator(); + + // Add the delete action to the menu. + bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("delete")), i18nc("Delete bookmark context menu entry", "Delete"), [=] + { + // Delete the bookmark. + BookmarksDatabase::deleteBookmark(bookmarkId); + + // Repopulate the bookmarks. + populateBookmarks(); + } + ); + + // Delete the menu from memory when it is closed. + bookmarkContextMenuPointer->setAttribute(Qt::WA_DeleteOnClose); + + // Display the context menu. + bookmarkContextMenuPointer->popup(bookmarksToolBarPointer->mapToGlobal(point)); + } + else // The toolbar background was clicked. + { + // Temporarily set the context menu policy to the default. + bookmarksToolBarPointer->setContextMenuPolicy(Qt::DefaultContextMenu); + + // Create a context menu event with the same position. + QContextMenuEvent *contextMenuEventPointer = new QContextMenuEvent(QContextMenuEvent::Mouse, point); + + // Send the context menu event to the toolbar. + QCoreApplication::sendEvent(bookmarksToolBarPointer, contextMenuEventPointer); + + // Reset the context menu policy. + bookmarksToolBarPointer->setContextMenuPolicy(Qt::CustomContextMenu); + } } void BrowserWindow::showCookiesDialog() diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index 92cdc8e..73ac519 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -41,7 +41,7 @@ class BrowserWindow : public KXmlGuiWindow public: // The default constructor. - BrowserWindow(bool firstWindow=true); + BrowserWindow(bool firstWindow=true, QString *initialUrlStringPointer = nullptr); // The public functions. QSize sizeHint() const override; @@ -71,7 +71,7 @@ private Q_SLOTS: void refresh() const; void reloadAndBypassCache() const; void showAddBookmarkDialog() const; - void showBookmarkContextMenu(const QPoint&) const; + void showBookmarkContextMenu(const QPoint &point); void showCookiesDialog(); void showDownloadLocationBrowseDialog() const; void showDomainSettingsDialog() const;