]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/commitdiff
Add context menus to the bookmarks toolbar.
authorSoren Stoutner <soren@stoutner.com>
Thu, 7 Sep 2023 19:17:55 +0000 (12:17 -0700)
committerSoren Stoutner <soren@stoutner.com>
Thu, 7 Sep 2023 19:17:55 +0000 (12:17 -0700)
src/windows/BrowserWindow.cpp
src/windows/BrowserWindow.h

index dda65d9c466a4cb97a382481cc62188d9904e51a..0fdfe26375043dd813985d38200ebf64e36c3570 100644 (file)
@@ -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 <KXMLGUIFactory>
 
 // Qt toolkit headers.
+#include <QClipboard>
+#include <QContextMenuEvent>
 #include <QFileDialog>
 #include <QInputDialog>
 #include <QNetworkCookie>
@@ -47,7 +50,7 @@
 #include <QWebEngineFindTextResult>
 
 // 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()
index 92cdc8eca691bbf59c283e7c1b5cc104383cdcf3..73ac519565ed78c40720481a7cee956b7c6ae8d7 100644 (file)
@@ -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;