Search the web with selected text.
authorChris H (Shade / Zren) <zrenfire@gmail.com>
Wed, 3 Apr 2013 00:52:02 +0000 (20:52 -0400)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 13 Jan 2014 21:51:47 +0000 (22:51 +0100)
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.

src/qtui/chatscene.cpp
src/qtui/chatscene.h
src/qtui/chatviewsettings.h
src/qtui/settingspages/chatviewsettingspage.ui

index 2c89519..2a065d7 100644 (file)
 
 #include <QApplication>
 #include <QClipboard>
+#include <QDesktopServices>
 #include <QDrag>
 #include <QGraphicsSceneMouseEvent>
 #include <QMenu>
 #include <QMenuBar>
 #include <QMimeData>
 #include <QPersistentModelIndex>
+#include <QUrl>
 
 #ifdef HAVE_KDE
 #  include <KMenuBar>
@@ -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()
index c128419..c640b6a 100644 (file)
@@ -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 {
index 16e2b30..c891f0f 100644 (file)
@@ -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); }
 };
 
 
index d1d47be..073ae2d 100644 (file)
      </property>
     </widget>
    </item>
+   <item>
+    <layout class="QHBoxLayout" name="horizontalLayout_4">
+     <item>
+      <widget class="QLabel" name="label_2">
+       <property name="text">
+        <string>Web Search Url:</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QLineEdit" name="webSearchUrlFormat">
+       <property name="toolTip">
+        <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;The URL to open with the selected text as the parameter. Place &lt;span style=&quot; font-weight:600;&quot;&gt;%s&lt;/span&gt; where selected text should go.&lt;/p&gt;&lt;p&gt;Eg:&lt;/p&gt;&lt;p&gt;https://www.google.com/search?q=&lt;span style=&quot; font-weight:600;&quot;&gt;%s&lt;br/&gt;&lt;/span&gt;https://duckduckgo.com/?q=&lt;span style=&quot; font-weight:600;&quot;&gt;%s&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
+       </property>
+       <property name="settingsKey" stdset="0">
+        <string>WebSearchUrlFormat</string>
+       </property>
+       <property name="defaultValue" stdset="0">
+        <string>https://www.google.com/search?q=%s</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
    <item>
     <widget class="QGroupBox" name="useCustomColors">
      <property name="title">