From: Chris H (Shade / Zren) Date: Wed, 3 Apr 2013 00:52:02 +0000 (-0400) Subject: Search the web with selected text. X-Git-Tag: 0.10-beta1~55 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=d30eb5039971db578ba6e777d737344187e5b02a;ds=inline Search the web with selected text. Add context menu option when selecting text in order to search the web for the selection. The url used in web search configurable in the ChatView settings panel. Uses the zoom-in icon until it gets it's own icon. --- diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index 2c89519f..2a065d76 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -20,12 +20,14 @@ #include #include +#include #include #include #include #include #include #include +#include #ifdef HAVE_KDE # include @@ -813,6 +815,13 @@ void ChatScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) QAction *act = new Action(SmallIcon("edit-copy"), tr("Copy Selection"), &menu, this, SLOT(selectionToClipboard()), QKeySequence::Copy); menu.insertAction(sep, act); + + QString searchSelectionText = selection(); + if (searchSelectionText.length() > _webSearchSelectionTextMaxVisible) + searchSelectionText = searchSelectionText.left(_webSearchSelectionTextMaxVisible).append(QString::fromUtf8("\u2026")); + searchSelectionText = tr("Search '%1'").arg(searchSelectionText); + + menu.addAction(SmallIcon("zoom-in"), searchSelectionText, this, SLOT(webSearchOnSelection())); } if (QtUi::mainWindow()->menuBar()->isHidden()) @@ -1047,6 +1056,21 @@ void ChatScene::clearSelection() } +/******** *************************************************************************************/ + +void ChatScene::webSearchOnSelection() +{ + if (!hasSelection()) + return; + + ChatViewSettings settings; + QString webSearchBaseUrl = settings.webSearchUrlFormatString(); + QString webSearchUrl = webSearchBaseUrl.replace(QString("%s"), selection()); + QUrl url = QUrl::fromUserInput(webSearchUrl); + QDesktopServices::openUrl(url); +} + + /******** *************************************************************************************/ void ChatScene::requestBacklog() diff --git a/src/qtui/chatscene.h b/src/qtui/chatscene.h index c1284195..c640b6a5 100644 --- a/src/qtui/chatscene.h +++ b/src/qtui/chatscene.h @@ -143,6 +143,8 @@ public slots: void selectionToClipboard(QClipboard::Mode = QClipboard::Clipboard); void stringToClipboard(const QString &str, QClipboard::Mode = QClipboard::Clipboard); + void webSearchOnSelection(); + void requestBacklog(); #ifdef HAVE_WEBKIT @@ -221,6 +223,8 @@ private: bool _showWebPreview; + static const int _webSearchSelectionTextMaxVisible = 24; + #ifdef HAVE_WEBKIT struct WebPreview { enum PreviewState { diff --git a/src/qtui/chatviewsettings.h b/src/qtui/chatviewsettings.h index 16e2b30c..c891f0f3 100644 --- a/src/qtui/chatviewsettings.h +++ b/src/qtui/chatviewsettings.h @@ -47,6 +47,9 @@ public: inline QString timestampFormatString() { return localValue("TimestampFormat", "[hh:mm:ss]").toString(); } inline void setTimestampFormatString(const QString &format) { setLocalValue("TimestampFormat", format); } + + inline QString webSearchUrlFormatString() { return localValue("WebSearchUrlFormat", "https://www.google.com/search?q=%s").toString(); } + inline void setWebSearchUrlFormatString(const QString &format) { setLocalValue("WebSearchUrlFormat", format); } }; diff --git a/src/qtui/settingspages/chatviewsettingspage.ui b/src/qtui/settingspages/chatviewsettingspage.ui index d1d47bed..073ae2df 100644 --- a/src/qtui/settingspages/chatviewsettingspage.ui +++ b/src/qtui/settingspages/chatviewsettingspage.ui @@ -153,6 +153,30 @@ + + + + + + Web Search Url: + + + + + + + <html><head/><body><p>The URL to open with the selected text as the parameter. Place <span style=" font-weight:600;">%s</span> where selected text should go.</p><p>Eg:</p><p>https://www.google.com/search?q=<span style=" font-weight:600;">%s<br/></span>https://duckduckgo.com/?q=<span style=" font-weight:600;">%s</span></p></body></html> + + + WebSearchUrlFormat + + + https://www.google.com/search?q=%s + + + + +