Skip to content

Commit

Permalink
First implementation of WebSearch ⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
ewerybody committed Aug 19, 2021
1 parent 0f144ea commit 0ff27ff
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
55 changes: 55 additions & 0 deletions WebSearch/a2_local_element_websearch_lister.py
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions WebSearch/a2module.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
37 changes: 37 additions & 0 deletions WebSearch/websearch.ahk
Original file line number Diff line number Diff line change
@@ -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%
}

0 comments on commit 0ff27ff

Please sign in to comment.