Skip to content

Commit

Permalink
Add SearchOnWeb plugin
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <[email protected]>
  • Loading branch information
QQxiaoming committed Dec 26, 2023
1 parent b870406 commit 6b3ab9e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 34 deletions.
20 changes: 0 additions & 20 deletions helloworld.cpp

This file was deleted.

100 changes: 100 additions & 0 deletions searchonweb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

#include "searchonweb.h"

#include <QMap>
#include <QActionGroup>
#include <QDesktopServices>

int SearchOnWeb::init(QMap<QString, QString> params, QWidget *parent)
{
m_mainMenu = new QMenu("Search On Web", parent);

actionGroup = new QActionGroup(m_mainMenu);
actionGroup->setExclusive(true);

QAction *googleAction = new QAction("Google", actionGroup);
googleAction->setCheckable(true);
m_mainMenu->addAction(googleAction);

QAction *baiduAction = new QAction("Baidu", actionGroup);
baiduAction->setCheckable(true);
m_mainMenu->addAction(baiduAction);

QAction *bingAction = new QAction("Bing", actionGroup);
bingAction->setCheckable(true);
m_mainMenu->addAction(bingAction);

QAction *githubAction = new QAction("Github", actionGroup);
githubAction->setCheckable(true);
m_mainMenu->addAction(githubAction);

QAction *stackoverflowAction = new QAction("Stackoverflow", actionGroup);
stackoverflowAction->setCheckable(true);
m_mainMenu->addAction(stackoverflowAction);

googleAction->setChecked(true);

return 0;
}

QList<QAction *> SearchOnWeb::terminalContextAction(QString selectedText, QString workingDirectory, QMenu *parentMenu)
{
if(selectedText.isEmpty())
{
return QList<QAction *>();
}

QAction *checkedAction = actionGroup->checkedAction();
if (checkedAction == nullptr)
{
return QList<QAction *>();
}

if(checkedAction->text() == "Google")
{
QAction *googleAction = new QAction("Google", parentMenu);
googleAction->setData("https://www.google.com/search?q=" + selectedText);
connect(googleAction, &QAction::triggered, [=](){
QDesktopServices::openUrl(QUrl(googleAction->data().toString()));
});
return QList<QAction *>() << googleAction;
}
else if(checkedAction->text() == "Baidu")
{
QAction *baiduAction = new QAction("Baidu", parentMenu);
baiduAction->setData("https://www.baidu.com/s?wd=" + selectedText);
connect(baiduAction, &QAction::triggered, [=](){
QDesktopServices::openUrl(QUrl(baiduAction->data().toString()));
});
return QList<QAction *>() << baiduAction;
}
else if(checkedAction->text() == "Bing")
{
QAction *bingAction = new QAction("Bing", parentMenu);
bingAction->setData("https://bing.com/search?q=" + selectedText);
connect(bingAction, &QAction::triggered, [=](){
QDesktopServices::openUrl(QUrl(bingAction->data().toString()));
});
return QList<QAction *>() << bingAction;
}
else if(checkedAction->text() == "Github")
{
QAction *githubAction = new QAction("Github", parentMenu);
githubAction->setData("https://github.com/search?q=" + selectedText);
connect(githubAction, &QAction::triggered, [=](){
QDesktopServices::openUrl(QUrl(githubAction->data().toString()));
});
return QList<QAction *>() << githubAction;
}
else if(checkedAction->text() == "Stackoverflow")
{
QAction *stackoverflowAction = new QAction("Stackoverflow", parentMenu);
stackoverflowAction->setData("https://stackoverflow.com/search?q=" + selectedText);
connect(stackoverflowAction, &QAction::triggered, [=](){
QDesktopServices::openUrl(QUrl(stackoverflowAction->data().toString()));
});
return QList<QAction *>() << stackoverflowAction;
}

return QList<QAction *>();
}
23 changes: 12 additions & 11 deletions helloworld.h → searchonweb.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#ifndef HELLOWORLD_H_
#define HELLOWORLD_H_
#ifndef SERCHONWEB_H_
#define SERCHONWEB_H_

#include "plugininterface.h"

#define PLUGIN_NAME "Hello World"
#define PLUGIN_NAME "Search On Web"
#define PLUGIN_VERSION "0.0.1"

class HelloWorld : public PluginInterface
class SearchOnWeb : public PluginInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.quardCRT.PluginInterface" FILE "./plugininterface/plugininterface.json")
Q_INTERFACES(PluginInterface)

public:
HelloWorld() : m_action(nullptr) {}
virtual ~HelloWorld() {}
SearchOnWeb() : m_mainMenu(nullptr) {}
virtual ~SearchOnWeb() {}

int init(QMap<QString, QString> params, QWidget *parent);

Expand All @@ -23,14 +23,15 @@ class HelloWorld : public PluginInterface
QString name() { return PLUGIN_NAME; }
QString version() { return PLUGIN_VERSION; }

QMenu *mainMenu() { return nullptr; }
QAction *mainAction() { return m_action; }
QMenu *mainMenu() { return m_mainMenu; }
QAction *mainAction() { return nullptr; }

QMenu *terminalContextMenu(QString selectedText, QString workingDirectory, QMenu *parentMenu) {Q_UNUSED(selectedText);Q_UNUSED(workingDirectory);Q_UNUSED(parentMenu); return nullptr;}
QList<QAction *> terminalContextAction(QString selectedText, QString workingDirectory, QMenu *parentMenu) {Q_UNUSED(selectedText);Q_UNUSED(workingDirectory);Q_UNUSED(parentMenu); return QList<QAction *>();}
QList<QAction *> terminalContextAction(QString selectedText, QString workingDirectory, QMenu *parentMenu);

private:
QAction *m_action;
QMenu *m_mainMenu;
QActionGroup *actionGroup;
};

#endif /* HELLOWORLD_H_ */
#endif /* SERCHONWEB_H_ */
6 changes: 3 additions & 3 deletions helloworld.pro → searchonweb.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ QT += core widgets
INCLUDEPATH += $$PWD/plugininterface \
$$PWD
HEADERS = plugininterface/plugininterface.h \
helloworld.h
searchonweb.h
SOURCES = plugininterface/plugininterface.cpp \
helloworld.cpp
searchonweb.cpp

TARGET = $$qtLibraryTarget(helloworld)
TARGET = $$qtLibraryTarget(searchonweb)

unix:{
QMAKE_RPATHDIR=$ORIGIN
Expand Down

0 comments on commit 6b3ab9e

Please sign in to comment.