]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/widgets/PrivacyWebEngineView.cpp
Add a background tab action to the context menu. https://redmine.stoutner.com/issues/949
[PrivacyBrowserPC.git] / src / widgets / PrivacyWebEngineView.cpp
index 10bff6d14554f45a2d922d4a1ae831e935705637..d7b3f8c1de62a7be5050dc274503d29a690781a2 100644 (file)
 #include "databases/CookiesDatabase.h"
 #include "windows/BrowserWindow.h"
 
+// Qt toolkit headers.
+#include <QContextMenuEvent>
+#include <QMenu>
+
 // Construct the class.
 PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) {}
 
@@ -40,21 +44,60 @@ void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const
     emit updateCookiesAction(cookieListPointer->size());
 }
 
-QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType webWindowType) {
-    qDebug().noquote().nospace() << "Web window type:  " << webWindowType;
+void PrivacyWebEngineView::contextMenuEvent(QContextMenuEvent *contextMenuEvent) {
+    // Get a handle for the
+    QWebEnginePage *webEnginePagePointer = page();
+
+    // Get a handle for the menu.
+    QMenu *contextMenu = webEnginePagePointer->createStandardContextMenu();
+
+    // Get the list of context menu actions.
+    const QList<QAction *> contextMenuActionsList = contextMenu->actions();
+
+    // Add the open link in new background tab action if the context menu already contains the open link in new window action.
+    if (contextMenuActionsList.contains(webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewWindow)))
+        contextMenu->insertAction(webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewWindow), webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewBackgroundTab));
+
+    // Display the menu using the location in the context menu event.
+    contextMenu->popup(contextMenuEvent->globalPos());
+}
 
+QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType webWindowType) {
     // Get a handle for the browser window.
     BrowserWindow *browserWindowPointer = qobject_cast<BrowserWindow*>(window());
 
     // Create the requsted window type.
-    switch (webWindowType) {
-        case QWebEnginePage::WebBrowserTab: {
-            // Create the new tab and return the privacy WebEngine view pointer.  It will then be populated with the link from the context menu.
-            return browserWindowPointer->tabWidgetPointer->addTab();
+    switch (webWindowType)
+    {
+        case QWebEnginePage::WebBrowserTab:
+        {
+            // Create the new tab and return the privacy WebEngine view pointer.  `true` removes the focus from the blank URL line edit.
+            // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
+            return browserWindowPointer->tabWidgetPointer->addTab(true);
+        }
+
+        case QWebEnginePage::WebBrowserWindow:
+        {
+            // Create a new browser window.
+            BrowserWindow *newBrowserWindowPointer = new BrowserWindow();
+
+            // Show the new browser window.
+            newBrowserWindowPointer->show();
+
+            // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
+            return newBrowserWindowPointer->tabWidgetPointer->loadBlankInitialWebsite();
+        }
+
+        case QWebEnginePage::WebBrowserBackgroundTab:
+        {
+            // Create the new tab and return the privacy WebEngine view pointer.  `false` does not clear the URL line edit.  `true` creates a background tab.
+            // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
+            return browserWindowPointer->tabWidgetPointer->addTab(false, true);
         }
 
-        default: {
-            // Return an null pointer.
+        default:
+        {
+            // Return an null pointer for opening a background tab and opening a web dialog.
             return nullptr;
         }
     }