From 0ff27ffdf7645f5c97fad5b38269c74e5146d86c Mon Sep 17 00:00:00 2001 From: Eric Werner Date: Thu, 19 Aug 2021 19:43:04 +0200 Subject: [PATCH] =?UTF-8?q?First=20implementation=20of=20WebSearch=20?= =?UTF-8?q?=E2=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../a2_local_element_websearch_lister.py | 55 +++++++++++++++++++ WebSearch/a2module.json | 37 +++++++++++++ WebSearch/websearch.ahk | 37 +++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 WebSearch/a2_local_element_websearch_lister.py create mode 100644 WebSearch/a2module.json create mode 100644 WebSearch/websearch.ahk diff --git a/WebSearch/a2_local_element_websearch_lister.py b/WebSearch/a2_local_element_websearch_lister.py new file mode 100644 index 0000000..b62ab4d --- /dev/null +++ b/WebSearch/a2_local_element_websearch_lister.py @@ -0,0 +1,55 @@ +import a2ctrl +from a2qt import QtWidgets +from a2element import DrawCtrl, EditCtrl +from a2widget import a2item_editor, a2text_field + + +class Draw(DrawCtrl): + """ + The frontend widget visible to the user with options + to change the default behavior of the element. + """ + + def __init__(self, *args): + super(Draw, self).__init__(*args) + self.main_layout = QtWidgets.QVBoxLayout(self) + self.main_layout.setContentsMargins(0, 0, 0, 0) + + self.editor = a2item_editor.A2ItemEditor(self) + # self.editor.ignore_default_values = False + self.editor.set_data(self.user_cfg) + self.editor.data_changed.connect(self.delayed_check) + self.main_layout.addWidget(self.editor) + self.is_expandable_widget = True + + url = a2text_field.A2CodeField(self) + self.editor.add_data_label_widget('url', url, url.setText, url.editing_finished, '', 'URL') + + def check(self): + self.user_cfg.update(self.editor.data) + self.set_user_value(self.user_cfg) + self.change() + + +class Edit(EditCtrl): + """ + The background widget that sets up how the user can edit the element, + visible when editing the module. + """ + + def __init__(self, cfg, main, parent_cfg): + super(Edit, self).__init__(cfg, main, parent_cfg) + + @staticmethod + def element_name(): + """The elements display name shown in UI""" + return 'Websearch_Lister' + + @staticmethod + def element_icon(): + return a2ctrl.Icons.inst().check + + +def get_settings(module_key, cfg, db_dict, user_cfg): + if user_cfg: + db_dict['variables']['websearch_data'] = user_cfg diff --git a/WebSearch/a2module.json b/WebSearch/a2module.json new file mode 100644 index 0000000..27c0ee5 --- /dev/null +++ b/WebSearch/a2module.json @@ -0,0 +1,37 @@ +[ + { + "author": "eric", + "date": "2021 8 19", + "description": "Look up selected text on any search engine via Hotkey or menu.", + "display_name": "", + "tags": [ + "lookup" + ], + "typ": "nfo", + "url": "", + "version": "0.1" + }, + { + "disablable": true, + "enabled": true, + "functionCode": "websearch()", + "functionMode": 0, + "key": [ + "Win+W" + ], + "keyChange": true, + "label": "Call the WebSeach menu", + "multiple": true, + "name": "WebSearch_Hotkey", + "scopeChange": true, + "typ": "hotkey" + }, + { + "file": "websearch.ahk", + "typ": "include" + }, + { + "name": "websearch_lister", + "typ": "a2_local_element" + } +] \ No newline at end of file diff --git a/WebSearch/websearch.ahk b/WebSearch/websearch.ahk new file mode 100644 index 0000000..2f3bc58 --- /dev/null +++ b/WebSearch/websearch.ahk @@ -0,0 +1,37 @@ +; WebSearch - websearch.ahk +; author: eric +; created: 2021 8 19 + +websearch() { + if !websearch_data + { + MsgBox, Nothing set up?!, Please open the user interface of "WebSearch" and add at least one item. + Return + } + + global _webseach_selection := clipboard_get() + + for name, data in websearch_data + Menu, WebSearchMenu, Add, %name%, websearch_handler + + Menu, WebSearchMenu, Show + Menu, WebSearchMenu, DeleteAll +} + +websearch_handler(menu_name) { + global _webseach_selection + phrase := _webseach_selection + _webseach_selection := "" + + if (!phrase) { + msg := "Nothing selected! What do you want to look up on " menu_name "?" + InputBox, phrase, WebSearch "%menu_name%", %msg%,,450, 130,,,,, + if ErrorLevel + Return + if !phrase + Return + } + + url := StringReplace(websearch_data[menu_name]["url"], "###", phrase) + Run, %url% +} \ No newline at end of file