Skip to content

Commit fd2d1b3

Browse files
committed
Adapt to anki 2.1
1 parent 0e0cc52 commit fd2d1b3

File tree

5 files changed

+144
-147
lines changed

5 files changed

+144
-147
lines changed

addons21/FastWQ.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

addons21/fastwq/__init__.py

Lines changed: 16 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -17,109 +17,24 @@
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919

20-
from aqt import mw
21-
from aqt.qt import *
22-
from anki.hooks import addHook, wrap
23-
from aqt.addcards import AddCards
24-
from aqt.utils import showInfo, shortcut
25-
from .gui import show_options, show_about_dialog
26-
from .query import query_from_browser, query_from_editor_all_fields
27-
from .context import config, APP_ICON
28-
from .lang import _
20+
from anki.hooks import addHook
21+
from . import common as fastwq
22+
from .context import config
2923

3024

31-
__all__ = [
32-
'add_query_button', 'browser_menu',
33-
'customize_addcards', 'config_menu',
34-
'window_shortcut'
35-
]
25+
############## other config here ##################
26+
shortcut = 'Ctrl+Q'
27+
LDOCE6_PATH = u'D:\\mdx_server\\mdx\\LDOCE6.mdx'
28+
###################################################
3629

3730

38-
have_setup = False
39-
my_shortcut = ''
31+
def start_here():
32+
config.read()
33+
config.LDOCE6_PATH = LDOCE6_PATH
34+
if not fastwq.have_setup:
35+
fastwq.config_menu()
36+
fastwq.browser_menu()
37+
fastwq.customize_addcards()
38+
fastwq.window_shortcut(shortcut)
4039

41-
42-
def wrap_method(func, *args, **kwargs):
43-
'''
44-
wrap a function with params when it's called
45-
'''
46-
def callback():
47-
return func(*args, **kwargs)
48-
return callback
49-
50-
51-
def add_query_button(self):
52-
'''
53-
add a button in add card window
54-
'''
55-
bb = self.form.buttonBox
56-
ar = QDialogButtonBox.ActionRole
57-
self.queryButton = bb.addButton(_(u"Query"), ar)
58-
self.queryButton.clicked.connect(wrap_method(
59-
query_from_editor_all_fields, self.editor))
60-
self.queryButton.setShortcut(QKeySequence(my_shortcut))
61-
self.queryButton.setToolTip(
62-
shortcut(_(u"Query (shortcut: %s)" % my_shortcut)))
63-
64-
65-
def browser_menu():
66-
"""
67-
add add-on's menu to browser window
68-
"""
69-
def on_setup_menus(browser):
70-
"""
71-
on browser setupMenus was called
72-
"""
73-
# main menu
74-
menu = QMenu("FastWQ", browser.form.menubar)
75-
browser.form.menubar.addMenu(menu)
76-
# Query Selected
77-
action = QAction("Query Selected", browser)
78-
action.triggered.connect(wrap_method(query_from_browser, browser))
79-
action.setShortcut(QKeySequence(my_shortcut))
80-
menu.addAction(action)
81-
# Options
82-
action = QAction("Options", browser)
83-
def _show_options():
84-
model_id = -1
85-
for note_id in browser.selectedNotes():
86-
note = browser.mw.col.getNote(note_id)
87-
model_id = note.model()['id']
88-
break
89-
show_options(browser, model_id)
90-
action.triggered.connect(_show_options)
91-
menu.addAction(action)
92-
# About
93-
action = QAction("About", browser)
94-
action.triggered.connect(wrap_method(show_about_dialog, browser))
95-
menu.addAction(action)
96-
97-
addHook('browser.setupMenus', on_setup_menus)
98-
99-
100-
def customize_addcards():
101-
"""
102-
add button to addcards window
103-
"""
104-
AddCards.setupButtons = wrap(
105-
AddCards.setupButtons, add_query_button, "before")
106-
107-
108-
def config_menu():
109-
"""
110-
add menu to anki window menebar
111-
"""
112-
action = QAction(APP_ICON, "FastWQ...", mw)
113-
action.triggered.connect(wrap_method(show_options))
114-
mw.form.menuTools.addAction(action)
115-
global have_setup
116-
have_setup = True
117-
118-
119-
def window_shortcut(key_sequence):
120-
"""
121-
setup shortcut
122-
"""
123-
global my_shortcut
124-
my_shortcut = key_sequence
125-
40+
addHook("profileLoaded", start_here)

addons21/fastwq/common.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#-*- coding:utf-8 -*-
2+
#
3+
# Copyright (C) 2018 sthoo <[email protected]>
4+
#
5+
# Support: Report an issue at https://github.com/sth2018/FastWordQuery/issues
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# any later version; http://www.gnu.org/copyleft/gpl.html.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
20+
from aqt import mw
21+
from aqt.qt import *
22+
from anki.hooks import addHook, wrap
23+
from aqt.addcards import AddCards
24+
from aqt.utils import showInfo, shortcut
25+
from .gui import show_options, show_about_dialog
26+
from .query import query_from_browser, query_from_editor_all_fields
27+
from .context import config, APP_ICON
28+
from .lang import _
29+
30+
31+
__all__ = [
32+
'add_query_button', 'browser_menu',
33+
'customize_addcards', 'config_menu',
34+
'window_shortcut'
35+
]
36+
37+
38+
have_setup = False
39+
my_shortcut = ''
40+
41+
42+
def wrap_method(func, *args, **kwargs):
43+
'''
44+
wrap a function with params when it's called
45+
'''
46+
def callback():
47+
return func(*args, **kwargs)
48+
return callback
49+
50+
51+
def add_query_button(self):
52+
'''
53+
add a button in add card window
54+
'''
55+
bb = self.form.buttonBox
56+
ar = QDialogButtonBox.ActionRole
57+
self.queryButton = bb.addButton(_(u"Query"), ar)
58+
self.queryButton.clicked.connect(wrap_method(
59+
query_from_editor_all_fields, self.editor))
60+
self.queryButton.setShortcut(QKeySequence(my_shortcut))
61+
self.queryButton.setToolTip(
62+
shortcut(_(u"Query (shortcut: %s)" % my_shortcut)))
63+
64+
65+
def browser_menu():
66+
"""
67+
add add-on's menu to browser window
68+
"""
69+
def on_setup_menus(browser):
70+
"""
71+
on browser setupMenus was called
72+
"""
73+
# main menu
74+
menu = QMenu("FastWQ", browser.form.menubar)
75+
browser.form.menubar.addMenu(menu)
76+
# Query Selected
77+
action = QAction("Query Selected", browser)
78+
action.triggered.connect(wrap_method(query_from_browser, browser))
79+
action.setShortcut(QKeySequence(my_shortcut))
80+
menu.addAction(action)
81+
# Options
82+
action = QAction("Options", browser)
83+
def _show_options():
84+
model_id = -1
85+
for note_id in browser.selectedNotes():
86+
note = browser.mw.col.getNote(note_id)
87+
model_id = note.model()['id']
88+
break
89+
show_options(browser, model_id)
90+
action.triggered.connect(_show_options)
91+
menu.addAction(action)
92+
# About
93+
action = QAction("About", browser)
94+
action.triggered.connect(wrap_method(show_about_dialog, browser))
95+
menu.addAction(action)
96+
97+
addHook('browser.setupMenus', on_setup_menus)
98+
99+
100+
def customize_addcards():
101+
"""
102+
add button to addcards window
103+
"""
104+
AddCards.setupButtons = wrap(
105+
AddCards.setupButtons, add_query_button, "before")
106+
107+
108+
def config_menu():
109+
"""
110+
add menu to anki window menebar
111+
"""
112+
action = QAction(APP_ICON, "FastWQ...", mw)
113+
action.triggered.connect(wrap_method(show_options))
114+
mw.form.menuTools.addAction(action)
115+
global have_setup
116+
have_setup = True
117+
118+
119+
def window_shortcut(key_sequence):
120+
"""
121+
setup shortcut
122+
"""
123+
global my_shortcut
124+
my_shortcut = key_sequence
125+

addons21/fastwq/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Config(object):
3636
"""
3737

3838
_CONFIG_FILENAME = '.fastwqcfg.json' #Config File Path
39+
LDOCE6_PATH = ''
3940

4041
def __init__(self, window):
4142
self.path = self._CONFIG_FILENAME

addons21/fastwq/service/dict/LDOCE6.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#-*- coding:utf-8 -*-
22
import re
3-
import FastWQ
43
from ..base import *
4+
from ...context import config
55

6-
PATH = FastWQ.LDOCE6_PATH
76

87
VOICE_PATTERN = r'<a href="sound://([\w/]+\w*\.mp3)"><img src="img/spkr_%s.png"></a>'
98
MAPPINGS = [
@@ -17,7 +16,7 @@
1716
class Ldoce6(MdxService):
1817

1918
def __init__(self):
20-
super(Ldoce6, self).__init__(PATH)
19+
super(Ldoce6, self).__init__(config.LDOCE6_PATH)
2120

2221
@property
2322
def title(self):

0 commit comments

Comments
 (0)