From 60938d35f881b18355d6cc61cb9e6dca67ccdfa0 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Tue, 22 Nov 2011 09:31:31 +0100 Subject: Clean up WebView::contextMenuEvent function --- src/webview.cpp | 240 +++++++++++++++++++++++++++++--------------------------- src/webview.h | 9 +++ 2 files changed, 132 insertions(+), 117 deletions(-) (limited to 'src') diff --git a/src/webview.cpp b/src/webview.cpp index bd159bd6..72923158 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -137,7 +137,6 @@ WebPage *WebView::page() } -// TODO: refactor me! void WebView::contextMenuEvent(QContextMenuEvent *event) { QWebHitTestResult result = page()->mainFrame()->hitTestContent(event->pos()); @@ -149,10 +148,99 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) KAction *inspectAction = new KAction(KIcon("layer-visible-on"), i18n("Inspect Element"), this); connect(inspectAction, SIGNAL(triggered(bool)), this, SLOT(inspect())); - // is a link? - if (!result.linkUrl().isEmpty()) + // Choose right context + int resultHit = 0; + if (result.linkUrl().isEmpty()) + resultHit = WebView::EmptySelection; + else + resultHit = WebView::LinkSelection; + + if (!result.pixmap().isNull()) + resultHit |= WebView::ImageSelection; + + if (result.isContentSelected()) + resultHit = WebView::TextSelection; + + // -------------------------------------------------------------------------------------------- + // Ok, let's start filling up the menu... + + // is content editable? Add PASTE + if (result.isContentEditable()) + { + menu.addAction(pageAction(KWebPage::Paste)); + menu.addSeparator(); + } + + // EMPTY PAGE ACTIONS ------------------------------------------------------------------------- + if (resultHit == WebView::EmptySelection) + { + // navigation + QWebHistory *history = page()->history(); + if (history->canGoBack()) + { + menu.addAction(pageAction(KWebPage::Back)); + } + + if (history->canGoForward()) + { + menu.addAction(pageAction(KWebPage::Forward)); + } + + menu.addAction(mainwindow->actionByName("view_redisplay")); + + if (result.pixmap().isNull()) + { + menu.addSeparator(); + + if (!ReKonfig::alwaysShowTabBar() && mainwindow->mainView()->count() == 1) + menu.addAction(mainwindow->actionByName("new_tab")); + + menu.addAction(mainwindow->actionByName("new_window")); + + menu.addSeparator(); + + //Frame + KActionMenu *frameMenu = new KActionMenu(i18n("Current Frame"), this); + + frameMenu->addAction(pageAction(KWebPage::OpenFrameInNewWindow)); + + a = new KAction(KIcon("document-print-frame"), i18n("Print Frame"), this); + connect(a, SIGNAL(triggered()), this, SLOT(printFrame())); + frameMenu->addAction(a); + + menu.addAction(frameMenu); + + menu.addSeparator(); + + // Page Actions + menu.addAction(pageAction(KWebPage::SelectAll)); + + menu.addAction(mainwindow->actionByName(KStandardAction::name(KStandardAction::SaveAs))); + + if (!KStandardDirs::findExe("kget").isNull() && ReKonfig::kgetList()) + { + a = new KAction(KIcon("kget"), i18n("List All Links"), this); + connect(a, SIGNAL(triggered(bool)), page(), SLOT(downloadAllContentsWithKGet())); + menu.addAction(a); + } + + menu.addAction(mainwindow->actionByName("page_source")); + menu.addAction(inspectAction); + + a = rApp->bookmarkProvider()->actionByName("rekonq_add_bookmark"); + menu.addAction(a); + } + + if (mainwindow->isFullScreen()) + { + menu.addSeparator(); + menu.addAction(mainwindow->actionByName("fullscreen")); + } + } + + // LINK ACTIONS ------------------------------------------------------------------------------- + if (resultHit & WebView::LinkSelection) { - // link actions a = new KAction(KIcon("tab-new"), i18n("Open in New &Tab"), this); a->setData(result.linkUrl()); connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewTab())); @@ -174,23 +262,43 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) menu.addSeparator(); } - // is content editable && selected? Add CUT - if (result.isContentEditable() && result.isContentSelected()) + // IMAGE ACTIONS ------------------------------------------------------------------------------ + if (resultHit & WebView::ImageSelection) { - // actions for text selected in field - menu.addAction(pageAction(KWebPage::Cut)); + // TODO remove copy_this_image action + a = new KAction(KIcon("view-media-visualization"), i18n("&View Image"), this); + a->setData(result.imageUrl()); + connect(a, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(viewImage(Qt::MouseButtons, Qt::KeyboardModifiers))); + menu.addAction(a); + + menu.addAction(pageAction(KWebPage::DownloadImageToDisk)); + menu.addAction(pageAction(KWebPage::CopyImageToClipboard)); + + a = new KAction(KIcon("view-media-visualization"), i18n("&Copy Image Location"), this); + a->setData(result.imageUrl()); + connect(a, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(slotCopyImageLocation())); + menu.addAction(a); } - // is content selected) Add COPY - if (result.isContentSelected()) + // ACTIONS FOR TEXT SELECTION ----------------------------------------------------------------- + if (resultHit & WebView::TextSelection) { + if (result.isContentEditable()) + { + // actions for text selected in field + menu.addAction(pageAction(KWebPage::Cut)); + } + a = pageAction(KWebPage::Copy); if (!result.linkUrl().isEmpty()) a->setText(i18n("Copy Text")); //for link else a->setText(i18n("Copy")); menu.addAction(a); - if (selectedText().contains('.') && selectedText().indexOf('.') < selectedText().length() && !selectedText().trimmed().contains(" ")) + + if (selectedText().contains('.') && selectedText().indexOf('.') < selectedText().length() + && !selectedText().trimmed().contains(" ") + ) { QString text = selectedText(); text = text.trimmed(); @@ -218,17 +326,7 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) menu.addSeparator(); } } - } - // is content editable? Add PASTE - if (result.isContentEditable()) - { - menu.addAction(pageAction(KWebPage::Paste)); - } - - // is content selected? Add SEARCH actions - if (result.isContentSelected()) - { //Default SearchEngine KService::Ptr defaultEngine = SearchEngine::defaultEngine(); if (defaultEngine) // check if a default engine is set @@ -260,105 +358,13 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) { menu.addAction(searchMenu); } - - menu.addSeparator(); - - menu.addAction(inspectAction); - // TODO Add translate, show translation } - // is an image? - if (!result.pixmap().isNull()) - { - menu.addSeparator(); - - // TODO remove copy_this_image action - a = new KAction(KIcon("view-media-visualization"), i18n("&View Image"), this); - a->setData(result.imageUrl()); - connect(a, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(viewImage(Qt::MouseButtons, Qt::KeyboardModifiers))); - menu.addAction(a); - - menu.addAction(pageAction(KWebPage::DownloadImageToDisk)); - menu.addAction(pageAction(KWebPage::CopyImageToClipboard)); - - a = new KAction(KIcon("view-media-visualization"), i18n("&Copy Image Location"), this); - a->setData(result.imageUrl()); - connect(a, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(slotCopyImageLocation())); - menu.addAction(a); - - menu.addSeparator(); - - menu.addAction(inspectAction); - } - - // page actions - if (!result.isContentSelected() && result.linkUrl().isEmpty()) - { - - // navigation - QWebHistory *history = page()->history(); - if (history->canGoBack()) - { - menu.addAction(pageAction(KWebPage::Back)); - } - - if (history->canGoForward()) - { - menu.addAction(pageAction(KWebPage::Forward)); - } - - menu.addAction(mainwindow->actionByName("view_redisplay")); - - if (result.pixmap().isNull()) - { - menu.addSeparator(); - - if (!ReKonfig::alwaysShowTabBar() && mainwindow->mainView()->count() == 1) - menu.addAction(mainwindow->actionByName("new_tab")); - - menu.addAction(mainwindow->actionByName("new_window")); - - menu.addSeparator(); - - //Frame - KActionMenu *frameMenu = new KActionMenu(i18n("Current Frame"), this); - - frameMenu->addAction(pageAction(KWebPage::OpenFrameInNewWindow)); - - a = new KAction(KIcon("document-print-frame"), i18n("Print Frame"), this); - connect(a, SIGNAL(triggered()), this, SLOT(printFrame())); - frameMenu->addAction(a); - - menu.addAction(frameMenu); - - menu.addSeparator(); - - // Page Actions - menu.addAction(pageAction(KWebPage::SelectAll)); - - menu.addAction(mainwindow->actionByName(KStandardAction::name(KStandardAction::SaveAs))); - - if (!KStandardDirs::findExe("kget").isNull() && ReKonfig::kgetList()) - { - a = new KAction(KIcon("kget"), i18n("List All Links"), this); - connect(a, SIGNAL(triggered(bool)), page(), SLOT(downloadAllContentsWithKGet())); - menu.addAction(a); - } - - menu.addAction(mainwindow->actionByName("page_source")); - menu.addAction(inspectAction); - - a = rApp->bookmarkProvider()->actionByName("rekonq_add_bookmark"); - menu.addAction(a); - } - - if (mainwindow->isFullScreen()) - { - menu.addSeparator(); - menu.addAction(mainwindow->actionByName("fullscreen")); - } - } + // DEFAULT ACTIONs (on the bottom) --------------------------------------------------- + menu.addSeparator(); + menu.addAction(inspectAction); + // finally launch the menu... menu.exec(mapToGlobal(event->pos())); } diff --git a/src/webview.h b/src/webview.h index 790fa23f..042f95c6 100644 --- a/src/webview.h +++ b/src/webview.h @@ -47,6 +47,15 @@ class REKONQ_TESTS_EXPORT WebView : public KWebView Q_OBJECT public: + + enum ContextType + { + EmptySelection = 0x00000000, + LinkSelection = 0x00000001, + ImageSelection = 0x00000010, + TextSelection = 0x00000100 + }; + explicit WebView(QWidget *parent); ~WebView(); -- cgit v1.2.1