anki-word-query/addons21/fastwq/__init__.py

126 lines
3.6 KiB
Python
Raw Normal View History

2018-07-01 10:55:30 +08:00
#-*- coding:utf-8 -*-
#
2018-07-27 17:57:00 +08:00
# Copyright (C) 2018 sthoo <sth201807@gmail.com>
2018-07-01 10:55:30 +08:00
#
# Support: Report an issue at https://github.com/sth2018/FastWordQuery/issues
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version; http://www.gnu.org/copyleft/gpl.html.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from aqt import mw
2018-07-30 16:00:47 +08:00
from aqt.qt import *
2018-07-01 10:55:30 +08:00
from anki.hooks import addHook, wrap
from aqt.addcards import AddCards
from aqt.utils import showInfo, shortcut
2018-07-14 15:24:21 +08:00
from .gui import show_options, show_about_dialog
from .query import query_from_browser, query_from_editor_all_fields
2018-07-01 10:55:30 +08:00
from .context import config, APP_ICON
from .lang import _
2018-07-01 10:55:30 +08:00
2018-07-14 10:22:45 +08:00
__all__ = [
2018-07-14 15:24:21 +08:00
'add_query_button', 'browser_menu',
'customize_addcards', 'config_menu',
'window_shortcut'
2018-07-14 10:22:45 +08:00
]
2018-07-01 10:55:30 +08:00
have_setup = False
my_shortcut = ''
def wrap_method(func, *args, **kwargs):
'''
wrap a function with params when it's called
'''
2018-07-01 10:55:30 +08:00
def callback():
return func(*args, **kwargs)
2018-07-01 10:55:30 +08:00
return callback
2018-07-14 10:22:45 +08:00
2018-07-01 10:55:30 +08:00
def add_query_button(self):
2018-07-14 10:22:45 +08:00
'''
add a button in add card window
'''
2018-07-01 10:55:30 +08:00
bb = self.form.buttonBox
2018-07-30 16:00:47 +08:00
ar = QDialogButtonBox.ActionRole
2018-07-01 10:55:30 +08:00
self.queryButton = bb.addButton(_(u"Query"), ar)
self.queryButton.clicked.connect(wrap_method(
2018-07-01 10:55:30 +08:00
query_from_editor_all_fields, self.editor))
2018-07-30 16:00:47 +08:00
self.queryButton.setShortcut(QKeySequence(my_shortcut))
2018-07-01 10:55:30 +08:00
self.queryButton.setToolTip(
shortcut(_(u"Query (shortcut: %s)" % my_shortcut)))
def browser_menu():
"""
add add-on's menu to browser window
2018-07-01 10:55:30 +08:00
"""
def on_setup_menus(browser):
"""
on browser setupMenus was called
2018-07-01 10:55:30 +08:00
"""
# main menu
2018-07-30 16:00:47 +08:00
menu = QMenu("FastWQ", browser.form.menubar)
2018-07-01 10:55:30 +08:00
browser.form.menubar.addMenu(menu)
# Query Selected
2018-07-30 16:00:47 +08:00
action = QAction("Query Selected", browser)
action.triggered.connect(wrap_method(query_from_browser, browser))
2018-07-30 16:00:47 +08:00
action.setShortcut(QKeySequence(my_shortcut))
2018-07-01 10:55:30 +08:00
menu.addAction(action)
2018-07-14 15:24:21 +08:00
# Options
2018-07-30 16:00:47 +08:00
action = QAction("Options", browser)
2018-07-14 10:22:45 +08:00
def _show_options():
model_id = -1
for note_id in browser.selectedNotes():
note = browser.mw.col.getNote(note_id)
model_id = note.model()['id']
break
show_options(browser, model_id)
action.triggered.connect(_show_options)
2018-07-01 10:55:30 +08:00
menu.addAction(action)
2018-07-14 15:24:21 +08:00
# About
2018-07-30 16:00:47 +08:00
action = QAction("About", browser)
2018-07-14 15:24:21 +08:00
action.triggered.connect(wrap_method(show_about_dialog, browser))
menu.addAction(action)
2018-07-01 10:55:30 +08:00
2018-07-14 15:24:21 +08:00
addHook('browser.setupMenus', on_setup_menus)
2018-07-01 10:55:30 +08:00
def customize_addcards():
"""
add button to addcards window
2018-07-01 10:55:30 +08:00
"""
AddCards.setupButtons = wrap(
AddCards.setupButtons, add_query_button, "before")
def config_menu():
"""
add menu to anki window menebar
2018-07-01 10:55:30 +08:00
"""
2018-07-30 16:00:47 +08:00
action = QAction(APP_ICON, "FastWQ...", mw)
action.triggered.connect(wrap_method(show_options))
2018-07-01 10:55:30 +08:00
mw.form.menuTools.addAction(action)
global have_setup
have_setup = True
def window_shortcut(key_sequence):
"""
setup shortcut
2018-07-01 10:55:30 +08:00
"""
global my_shortcut
my_shortcut = key_sequence