code format

This commit is contained in:
St.Huang 2019-02-10 11:44:25 +08:00
parent a3f3d8eb95
commit ab9593074b
13 changed files with 330 additions and 283 deletions

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -19,6 +19,7 @@
import ssl import ssl
import sys import sys
from anki.hooks import addHook from anki.hooks import addHook
from anki.utils import isMac from anki.utils import isMac
@ -28,6 +29,7 @@ if isMac:
############## other config here ################## ############## other config here ##################
shortcut = ('Ctrl+Alt' if isMac else 'Ctrl') + '+Q' shortcut = ('Ctrl+Alt' if isMac else 'Ctrl') + '+Q'
################################################### ###################################################
@ -42,7 +44,6 @@ def start_here():
fastwq.browser_menu() fastwq.browser_menu()
fastwq.context_menu() fastwq.context_menu()
fastwq.customize_addcards() fastwq.customize_addcards()
# if fastwq.config.auto_update:
# fastwq.check_updates(True)
addHook("profileLoaded", start_here)
addHook("profileLoaded", start_here)

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -18,24 +18,23 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from operator import itemgetter from operator import itemgetter
from anki.hooks import addHook, remHook, wrap
from aqt import mw from aqt import mw
from aqt.qt import *
from anki.hooks import addHook, wrap, remHook
from aqt.addcards import AddCards from aqt.addcards import AddCards
from aqt.utils import showInfo, shortcut, downArrow from aqt.qt import *
from .gui import show_options, show_about_dialog #, check_updates from aqt.utils import downArrow, shortcut, showInfo
from .query import query_from_browser, query_from_editor_fields
from .context import config, APP_ICON, Config from .context import APP_ICON, Config, config
from .gui import show_about_dialog, show_options # , check_updates
from .lang import _ from .lang import _
from .query import query_from_browser, query_from_editor_fields
from .service import service_pool from .service import service_pool
from .utils import get_icon from .utils import get_icon
__all__ = [ __all__ = [
'browser_menu', 'customize_addcards', 'browser_menu', 'customize_addcards', 'config_menu', 'context_menu'
'config_menu', 'context_menu' ] # 'check_updates',
] # 'check_updates',
have_setup = False have_setup = False
my_shortcut = '' my_shortcut = ''
@ -52,18 +51,23 @@ def set_options_def(mid, i):
data = dict() data = dict()
data[mid] = conf data[mid] = conf
config.update(data) config.update(data)
# end set_options_def # end set_options_def
def browser_menu(): def browser_menu():
""" """
add add-on's menu to browser window add add-on's menu to browser window
""" """
def on_setup_menus(browser): def on_setup_menus(browser):
""" """
on browser setupMenus was called on browser setupMenus was called
""" """
# main menu # main menu
menu = browser.form.menubar.addMenu("FastWQ") menu = browser.form.menubar.addMenu("FastWQ")
# menu gen # menu gen
def init_fastwq_menu(): def init_fastwq_menu():
try: try:
@ -78,6 +82,7 @@ def browser_menu():
menu.addAction(action) menu.addAction(action)
# Options # Options
action = QAction(_('OPTIONS'), browser) action = QAction(_('OPTIONS'), browser)
def _show_options(): def _show_options():
model_id = -1 model_id = -1
for note_id in browser.selectedNotes(): for note_id in browser.selectedNotes():
@ -85,26 +90,31 @@ def browser_menu():
model_id = note.model()['id'] model_id = note.model()['id']
break break
show_options(browser, model_id) show_options(browser, model_id)
action.triggered.connect(_show_options) action.triggered.connect(_show_options)
menu.addAction(action) menu.addAction(action)
# Default configs # Default configs
menu.addSeparator() menu.addSeparator()
b = False b = False
for m in sorted(browser.mw.col.models.all(), key=itemgetter("name")): for m in sorted(
browser.mw.col.models.all(), key=itemgetter("name")):
conf = config.get_maps(m['id']) conf = config.get_maps(m['id'])
conf = {'list': [conf], 'def': 0} if isinstance(conf, list) else conf conf = {
'list': [conf],
'def': 0
} if isinstance(conf, list) else conf
maps_list = conf['list'] maps_list = conf['list']
if len(maps_list) > 1: if len(maps_list) > 1:
submenu = menu.addMenu(m['name']) submenu = menu.addMenu(m['name'])
for i, maps in enumerate(maps_list): for i, maps in enumerate(maps_list):
submenu.addAction( submenu.addAction(
_OK_ICON if i==conf['def'] else _NULL_ICON, _OK_ICON if i == conf['def'] else _NULL_ICON,
_('CONFIG_INDEX') % (i+1) if isinstance(maps, list) else maps['name'], _('CONFIG_INDEX') % (i + 1) if isinstance(
lambda mid=m['id'], i=i: set_options_def(mid, i) maps, list) else maps['name'],
) lambda mid=m['id'], i=i: set_options_def(mid, i))
b = True b = True
if b: if b:
menu.addSeparator() menu.addSeparator()
# # check update # # check update
@ -120,6 +130,7 @@ def browser_menu():
# end init_fastwq_menu # end init_fastwq_menu
init_fastwq_menu() init_fastwq_menu()
addHook('config.update', init_fastwq_menu) addHook('config.update', init_fastwq_menu)
addHook('browser.setupMenus', on_setup_menus) addHook('browser.setupMenus', on_setup_menus)
@ -127,6 +138,7 @@ def customize_addcards():
""" """
add button to addcards window add button to addcards window
""" """
def add_query_button(self): def add_query_button(self):
''' '''
add a button in add card window add a button in add card window
@ -136,44 +148,47 @@ def customize_addcards():
# button # button
fastwqBtn = QPushButton(_("QUERY") + u" " + downArrow()) fastwqBtn = QPushButton(_("QUERY") + u" " + downArrow())
fastwqBtn.setShortcut(QKeySequence(my_shortcut)) fastwqBtn.setShortcut(QKeySequence(my_shortcut))
fastwqBtn.setToolTip( fastwqBtn.setToolTip(_(u"Shortcut: %s") % shortcut(my_shortcut))
_(u"Shortcut: %s") % shortcut(my_shortcut)
)
bb.addButton(fastwqBtn, ar) bb.addButton(fastwqBtn, ar)
# signal # signal
def onQuery(e): def onQuery(e):
if isinstance(e, QMouseEvent): if isinstance(e, QMouseEvent):
if e.buttons() & Qt.LeftButton: if e.buttons() & Qt.LeftButton:
menu = QMenu(self) menu = QMenu(self)
menu.addAction(_("ALL_FIELDS"), lambda: query_from_editor_fields(self.editor), QKeySequence(my_shortcut)) menu.addAction(
_("ALL_FIELDS"),
lambda: query_from_editor_fields(self.editor),
QKeySequence(my_shortcut))
# default options # default options
mid = self.editor.note.model()['id'] mid = self.editor.note.model()['id']
conf = config.get_maps(mid) conf = config.get_maps(mid)
conf = {'list': [conf], 'def': 0} if isinstance(conf, list) else conf conf = {
'list': [conf],
'def': 0
} if isinstance(conf, list) else conf
maps_list = conf['list'] maps_list = conf['list']
if len(maps_list) > 1: if len(maps_list) > 1:
menu.addSeparator() menu.addSeparator()
for i, maps in enumerate(maps_list): for i, maps in enumerate(maps_list):
menu.addAction( menu.addAction(
_OK_ICON if i==conf['def'] else _NULL_ICON, _OK_ICON if i == conf['def'] else _NULL_ICON,
_('CONFIG_INDEX') % (i+1) if isinstance(maps, list) else maps['name'], _('CONFIG_INDEX') % (i + 1) if isinstance(
lambda mid=mid, i=i: set_options_def(mid, i) maps, list) else maps['name'],
) lambda mid=mid, i=i: set_options_def(mid, i))
menu.addSeparator() menu.addSeparator()
# end default options # end default options
menu.addAction(_("OPTIONS"), lambda: show_options(self, self.editor.note.model()['id'])) menu.addAction(_("OPTIONS"), lambda: show_options(self, self.editor.note.model()['id']))
menu.exec_(fastwqBtn.mapToGlobal(QPoint(0, fastwqBtn.height()))) menu.exec_(
fastwqBtn.mapToGlobal(QPoint(0, fastwqBtn.height())))
else: else:
query_from_editor_fields(self.editor) query_from_editor_fields(self.editor)
fastwqBtn.mousePressEvent = onQuery fastwqBtn.mousePressEvent = onQuery
fastwqBtn.clicked.connect(onQuery) fastwqBtn.clicked.connect(onQuery)
AddCards.setupButtons = wrap( AddCards.setupButtons = wrap(AddCards.setupButtons, add_query_button,
AddCards.setupButtons, "after")
add_query_button,
"after"
)
def config_menu(): def config_menu():
@ -187,6 +202,7 @@ def config_menu():
def context_menu(): def context_menu():
'''mouse right click menu''' '''mouse right click menu'''
def on_setup_menus(web_view, menu): def on_setup_menus(web_view, menu):
""" """
add context menu to webview add context menu to webview
@ -217,32 +233,30 @@ def context_menu():
name = s.title + ' :-> ' + s.fields[dict_fld_ord] name = s.title + ' :-> ' + s.fields[dict_fld_ord]
if name not in names: if name not in names:
names.append(name) names.append(name)
curr_flds.append({ curr_flds.append({'name': name, 'def': i})
'name': name,
'def': i
})
service_pool.put(s) service_pool.put(s)
submenu = menu.addMenu(_('QUERY')) submenu = menu.addMenu(_('QUERY'))
submenu.addAction(_('ALL_FIELDS'), lambda: query_from_editor_fields(web_view.editor), QKeySequence(my_shortcut)) submenu.addAction(
_('ALL_FIELDS'), lambda: query_from_editor_fields(web_view.editor),
QKeySequence(my_shortcut))
if len(curr_flds) > 0: if len(curr_flds) > 0:
# quer hook method # quer hook method
def query_from_editor_hook(i): def query_from_editor_hook(i):
conf = config.get_maps(current_model_id) conf = config.get_maps(current_model_id)
maps_old_def = 0 if isinstance(conf, list) else conf.get('def', 0) maps_old_def = 0 if isinstance(conf, list) else conf.get(
'def', 0)
set_options_def(current_model_id, i) set_options_def(current_model_id, i)
query_from_editor_fields( query_from_editor_fields(
web_view.editor, web_view.editor, fields=[web_view.editor.currentField])
fields=[web_view.editor.currentField]
)
set_options_def(current_model_id, maps_old_def) set_options_def(current_model_id, maps_old_def)
# sub menu # sub menu
#flds_menu = submenu.addMenu(_('CURRENT_FIELDS')) # flds_menu = submenu.addMenu(_('CURRENT_FIELDS'))
submenu.addSeparator() submenu.addSeparator()
for c in curr_flds: for c in curr_flds:
submenu.addAction(c['name'], submenu.addAction(
lambda i=c['def']: query_from_editor_hook(i) c['name'], lambda i=c['def']: query_from_editor_hook(i))
)
submenu.addSeparator() submenu.addSeparator()
submenu.addAction(_("OPTIONS"), lambda: show_options(web_view, web_view.editor.note.model()['id'])) submenu.addAction(_("OPTIONS"), lambda: show_options(web_view, web_view.editor.note.model()['id']))

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -17,13 +17,12 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from .lang import _ from .lang import _
__all__ = ['VERSION', 'Endpoint', 'Template'] __all__ = ['VERSION', 'Endpoint', 'Template']
VERSION = 'v1.3.5' VERSION = 'v2.0.0b'
class Endpoint: class Endpoint:
repository = u'https://github.com/sth2018/FastWordQuery' repository = u'https://github.com/sth2018/FastWordQuery'
@ -36,10 +35,13 @@ class Endpoint:
class Template: class Template:
tmpl_about = u'''<b>{t0}</b><br/>{version}<br/><b>{t1}</b><br/> tmpl_about = u'''<b>{t0}</b><br/>{version}<br/><b>{t1}</b><br/>
<a href="{url}">{url}</a><br/><b>{t2}</b><br/> <a href="{url}">{url}</a><br/><b>{t2}</b><br/>
<a href="{feedback0}">{feedback0}</a><br/> <a href="{feedback0}">{feedback0}</a><br/>
<a href="mailto:{feedback1}">{feedback1}</a>'''.format( <a href="mailto:{feedback1}">{feedback1}</a>'''.format(
t0=_('VERSION'), version=VERSION, t0=_('VERSION'),
t1=_('REPOSITORY'), url=Endpoint.repository, version=VERSION,
t2=_('FEEDBACK'), feedback0=Endpoint.feedback_issue, feedback1=Endpoint.feedback_mail) t1=_('REPOSITORY'),
miss_css = u'MDX dictonary <b>{dict}</b> misses css file <b>{css}</b>. <br/>Please choose the file.' url=Endpoint.repository,
t2=_('FEEDBACK'),
feedback0=Endpoint.feedback_issue,
feedback1=Endpoint.feedback_mail)

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -28,17 +28,15 @@ from .utils import get_icon
__all__ = ['APP_ICON', 'config'] __all__ = ['APP_ICON', 'config']
APP_ICON = get_icon('wqicon.png') # Addon Icon
APP_ICON = get_icon('wqicon.png') # Addon Icon
class Config(object): class Config(object):
""" """
Addon Config Addon Config
""" """
_CONFIG_FILENAME = 'fastwqcfg.json' # Config File Path _CONFIG_FILENAME = 'fastwqcfg.json' # Config File Path
def __init__(self, window): def __init__(self, window):
self.path = u'_' + self._CONFIG_FILENAME self.path = u'_' + self._CONFIG_FILENAME
@ -56,10 +54,12 @@ class Config(object):
Update && Save Update && Save
""" """
data['version'] = VERSION data['version'] = VERSION
data['%s_last' % self.pmname] = data.get('last_model', self.last_model_id) data['%s_last' % self.pmname] = data.get('last_model',
self.last_model_id)
self.data.update(data) self.data.update(data)
with open(self.path, 'w', encoding='utf-8') as f: with open(self.path, 'w', encoding='utf-8') as f:
json.dump(self.data, f, indent=4, sort_keys=True, ensure_ascii=False) json.dump(
self.data, f, indent=4, sort_keys=True, ensure_ascii=False)
f.close() f.close()
runHook('config.update') runHook('config.update')
@ -70,13 +70,15 @@ class Config(object):
if self.data: if self.data:
return self.data return self.data
try: try:
path = self.path if os.path.exists(self.path) else u'.' + self._CONFIG_FILENAME path = self.path if os.path.exists(
self.path) else u'.' + self._CONFIG_FILENAME
with open(path, 'r', encoding="utf-8") as f: with open(path, 'r', encoding="utf-8") as f:
self.data = json.load(f) self.data = json.load(f)
f.close() f.close()
if not os.path.exists(self.path): if not os.path.exists(self.path):
self.update(self.data) self.update(self.data)
except: except Exception as e:
print('can not find config file', e)
self.data = dict() self.data = dict()
def get_maps(self, model_id): def get_maps(self, model_id):
@ -108,7 +110,7 @@ class Config(object):
@property @property
def force_update(self): def force_update(self):
return self.data.get('force_update', False) return self.data.get('force_update', False)
@property @property
def ignore_mdx_wordcase(self): def ignore_mdx_wordcase(self):
return self.data.get('ignore_mdx_wordcase', False) return self.data.get('ignore_mdx_wordcase', False)
@ -140,4 +142,5 @@ class Config(object):
tmpstr = '{{c1::%s}}' tmpstr = '{{c1::%s}}'
return tmpstr return tmpstr
config = Config(mw) config = Config(mw)

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
from .common import * from .common import *
from .progress import * from .progress import *

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -18,10 +18,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys import sys
from anki.utils import isMac from anki.utils import isMac
from aqt.qt import * from aqt.qt import *
from ..context import APP_ICON
from ..context import APP_ICON
__all__ = ['Dialog', 'WIDGET_SIZE'] __all__ = ['Dialog', 'WIDGET_SIZE']
@ -37,20 +38,15 @@ class Dialog(QDialog):
return value of the _ui() method, and sets a default title. return value of the _ui() method, and sets a default title.
''' '''
self._title = title self._title = title if "FastWQ" in title else "FastWQ - " + title
self._parent = parent self._parent = parent
super(Dialog, self).__init__(parent) super(Dialog, self).__init__(parent)
self.setModal(True) self.setModal(True)
self.setWindowFlags( self.setWindowFlags(
self.windowFlags() & self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
~Qt.WindowContextHelpButtonHint
)
self.setWindowIcon(APP_ICON) self.setWindowIcon(APP_ICON)
self.setWindowTitle( self.setWindowTitle(self._title)
title if "FastWQ" in title
else "FastWQ - " + title
)
# 2 & 3 & mac compatible # 2 & 3 & mac compatible
if isMac and sys.hexversion >= 0x03000000: if isMac and sys.hexversion >= 0x03000000:
QApplication.setStyle('Fusion') QApplication.setStyle('Fusion')

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -19,19 +19,20 @@
import os import os
import sys import sys
from aqt.qt import *
from aqt.forms.editaddon import Ui_Dialog from aqt.forms.editaddon import Ui_Dialog
from .base import Dialog, WIDGET_SIZE from aqt.qt import *
from ..service import service_manager, service_pool
from ..context import config from ..context import config
from ..lang import _, _sl from ..lang import _, _sl
from ..service import service_manager, service_pool
from ..utils import get_icon from ..utils import get_icon
from .base import WIDGET_SIZE, Dialog
# 2x3 compatible # 2x3 compatible
if sys.hexversion >= 0x03000000: if sys.hexversion >= 0x03000000:
unicode = str unicode = str
__all__ = ['DictManageDialog'] __all__ = ['DictManageDialog']
@ -45,14 +46,14 @@ class DictManageDialog(Dialog):
self.main_layout = QVBoxLayout() self.main_layout = QVBoxLayout()
self.setLayout(self.main_layout) self.setLayout(self.main_layout)
self._options = list() self._options = list()
btnbox = QDialogButtonBox(QDialogButtonBox.Ok, Qt.Horizontal , self) btnbox = QDialogButtonBox(QDialogButtonBox.Ok, Qt.Horizontal, self)
btnbox.accepted.connect(self.accept) btnbox.accepted.connect(self.accept)
# add dicts mapping # add dicts mapping
self.dicts_layout = QGridLayout() self.dicts_layout = QGridLayout()
self.main_layout.addLayout(self.dicts_layout) self.main_layout.addLayout(self.dicts_layout)
self.main_layout.addWidget(btnbox) self.main_layout.addWidget(btnbox)
self.build() self.build()
def build(self): def build(self):
''' ''' ''' '''
# labels # labels
@ -80,10 +81,14 @@ class DictManageDialog(Dialog):
services = service_manager.local_custom_services + service_manager.web_services services = service_manager.local_custom_services + service_manager.web_services
for clazz in services: for clazz in services:
dicts.append({ dicts.append({
'title': clazz.__title__, 'title':
'unique': clazz.__unique__, clazz.__title__,
'path': clazz.__path__, 'unique':
'enabled': confs.get(clazz.__unique__, dict()).get('enabled', True) clazz.__unique__,
'path':
clazz.__path__,
'enabled':
confs.get(clazz.__unique__, dict()).get('enabled', True)
}) })
# add dict # add dict
for i, d in enumerate(dicts): for i, d in enumerate(dicts):
@ -102,7 +107,7 @@ class DictManageDialog(Dialog):
) )
# button # button
check_btn = QCheckBox(title) check_btn = QCheckBox(title)
check_btn.setMinimumSize(WIDGET_SIZE.map_dict_width*1.5, 0) check_btn.setMinimumSize(WIDGET_SIZE.map_dict_width * 1.5, 0)
check_btn.setEnabled(True) check_btn.setEnabled(True)
check_btn.setChecked(enabled) check_btn.setChecked(enabled)
edit_btn = QToolButton(self) edit_btn = QToolButton(self)
@ -114,9 +119,9 @@ class DictManageDialog(Dialog):
self.dicts_layout.addWidget(check_btn, i + 1, 0) self.dicts_layout.addWidget(check_btn, i + 1, 0)
self.dicts_layout.addWidget(edit_btn, i + 1, 1) self.dicts_layout.addWidget(edit_btn, i + 1, 1)
self._options.append({ self._options.append({
'unique': unique, 'unique': unique,
'check_btn': check_btn, 'check_btn': check_btn,
'edit_btn': edit_btn, 'edit_btn': edit_btn,
}) })
def enabled_all_update(self): def enabled_all_update(self):
@ -126,7 +131,7 @@ class DictManageDialog(Dialog):
b = False b = False
break break
self.enabled_all_check_btn.setChecked(b) self.enabled_all_check_btn.setChecked(b)
def enabled_all_changed(self): def enabled_all_changed(self):
b = self.enabled_all_check_btn.isChecked() b = self.enabled_all_check_btn.isChecked()
for row in self._options: for row in self._options:

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -18,10 +18,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from aqt.qt import * from aqt.qt import *
from .base import Dialog, WIDGET_SIZE
from ..context import config from ..context import config
from ..lang import _, _sl from ..lang import _, _sl
from .base import WIDGET_SIZE, Dialog
__all__ = ['FoldersManageDialog'] __all__ = ['FoldersManageDialog']
@ -33,7 +33,7 @@ class FoldersManageDialog(Dialog):
def __init__(self, parent, title=u'Dictionary Folder Manager'): def __init__(self, parent, title=u'Dictionary Folder Manager'):
super(FoldersManageDialog, self).__init__(parent, title) super(FoldersManageDialog, self).__init__(parent, title)
#self._dict_paths = [] # self._dict_paths = []
self.build() self.build()
def build(self): def build(self):
@ -65,10 +65,9 @@ class FoldersManageDialog(Dialog):
def add_folder(self): def add_folder(self):
dir_ = QFileDialog.getExistingDirectory( dir_ = QFileDialog.getExistingDirectory(
self, self,
caption=u"Select Folder", caption=u"Select Folder",
directory=config.last_folder, directory=config.last_folder,
options=QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks options=QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
)
if dir_: if dir_:
self.folders_lst.addItem(dir_) self.folders_lst.addItem(dir_)
config.update({'last_folder': dir_}) config.update({'last_folder': dir_})
@ -80,8 +79,10 @@ class FoldersManageDialog(Dialog):
@property @property
def dirs(self): def dirs(self):
'''dictionary folders list''' '''dictionary folders list'''
return [self.folders_lst.item(i).text() return [
for i in range(self.folders_lst.count())] self.folders_lst.item(i).text()
for i in range(self.folders_lst.count())
]
def accept(self): def accept(self):
'''ok button clicked''' '''ok button clicked'''

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -18,22 +18,23 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys import sys
import anki import anki
import aqt import aqt
import aqt.models import aqt.models
import sip import sip
from anki.utils import isMac
from aqt import mw from aqt import mw
from aqt.qt import * from aqt.qt import *
from aqt.studydeck import StudyDeck from aqt.studydeck import StudyDeck
from anki.utils import isMac
from .base import Dialog, WIDGET_SIZE from ..constants import Endpoint
from .setting import SettingDialog
from ..context import config from ..context import config
from ..lang import _, _sl from ..lang import _, _sl
from ..service import service_manager, service_pool from ..service import service_manager, service_pool
from ..utils import get_model_byId, get_icon from ..utils import get_icon, get_model_byId
from ..constants import Endpoint from .base import WIDGET_SIZE, Dialog
from .setting import SettingDialog
__all__ = ['OptionsDialog'] __all__ = ['OptionsDialog']
@ -44,16 +45,13 @@ class OptionsDialog(Dialog):
setting query dictionary and fileds setting query dictionary and fileds
''' '''
__slot__ = [ __slot__ = ['begore_build', 'after_build']
'begore_build',
'after_build'
]
_signal = pyqtSignal(str) _signal = pyqtSignal(str)
_NULL_ICON = get_icon('null.png') _NULL_ICON = get_icon('null.png')
_OK_ICON = get_icon('ok.png') _OK_ICON = get_icon('ok.png')
def __init__(self, parent, title=u'Options', model_id = -1): def __init__(self, parent, title=u'Options', model_id=-1):
super(OptionsDialog, self).__init__(parent, title) super(OptionsDialog, self).__init__(parent, title)
self._signal.connect(self._before_build) self._signal.connect(self._before_build)
self._signal.connect(self._after_build) self._signal.connect(self._after_build)
@ -61,32 +59,34 @@ class OptionsDialog(Dialog):
self.main_layout = QVBoxLayout() self.main_layout = QVBoxLayout()
self.loading_label = QLabel(_('INITLIZING_DICT')) self.loading_label = QLabel(_('INITLIZING_DICT'))
self.main_layout.addWidget(self.loading_label, 0, Qt.AlignCenter) self.main_layout.addWidget(self.loading_label, 0, Qt.AlignCenter)
#self.loading_layout.addLayout(models_layout) # self.loading_layout.addLayout(models_layout)
self.setLayout(self.main_layout) self.setLayout(self.main_layout)
#initlize properties # initlize properties
self.model_id = model_id if model_id != -1 else config.last_model_id self.model_id = model_id if model_id != -1 else config.last_model_id
self.current_model = None self.current_model = None
self.tabs = [] self.tabs = []
self.dict_services = None self.dict_services = None
# size and signal # size and signal
self.resize(WIDGET_SIZE.dialog_width, 4 * WIDGET_SIZE.map_max_height + WIDGET_SIZE.dialog_height_margin) self.resize(
WIDGET_SIZE.dialog_width,
4 * WIDGET_SIZE.map_max_height + WIDGET_SIZE.dialog_height_margin)
self._signal.emit('before_build') self._signal.emit('before_build')
def _before_build(self, s): def _before_build(self, s):
if s != 'before_build': if s != 'before_build':
return return
# dict service list # dict service list
dicts = config.dicts dicts = config.dicts
self.dict_services = { self.dict_services = {
'local': [], #本地词典 'local': [], # 本地词典
'web': [] #网络词典 'web': [] # 网络词典
} }
for clazz in service_manager.local_services: for clazz in service_manager.local_services:
if dicts.get(clazz.__unique__, dict()).get('enabled', True): if dicts.get(clazz.__unique__, dict()).get('enabled', True):
service = service_pool.get(clazz.__unique__) service = service_pool.get(clazz.__unique__)
if service and service.support: if service and service.support:
self.dict_services['local'].append({ self.dict_services['local'].append({
'title': service.title, 'title': service.title,
'unique': service.unique 'unique': service.unique
}) })
service_pool.put(service) service_pool.put(service)
@ -95,7 +95,7 @@ class OptionsDialog(Dialog):
service = service_pool.get(clazz.__unique__) service = service_pool.get(clazz.__unique__)
if service and service.support: if service and service.support:
self.dict_services['web'].append({ self.dict_services['web'].append({
'title': service.title, 'title': service.title,
'unique': service.unique 'unique': service.unique
}) })
service_pool.put(service) service_pool.put(service)
@ -121,13 +121,11 @@ class OptionsDialog(Dialog):
# tabs # tabs
self.tab_widget = QTabWidget() self.tab_widget = QTabWidget()
self.tab_widget.setTabBar(CTabBar()) self.tab_widget.setTabBar(CTabBar())
self.tab_widget.setStyleSheet( self.tab_widget.setStyleSheet("""
"""
QTabWidget::pane { /* The tab widget frame */ QTabWidget::pane { /* The tab widget frame */
border: 1px solid #c3c3c3; border: 1px solid #c3c3c3;
} }
""" """)
)
tab_corner = QWidget() tab_corner = QWidget()
tab_corner_layout = QHBoxLayout() tab_corner_layout = QHBoxLayout()
tab_corner_layout.setSpacing(1) tab_corner_layout.setSpacing(1)
@ -160,8 +158,7 @@ class OptionsDialog(Dialog):
# chk_update_btn = QPushButton(_('UPDATE')) # chk_update_btn = QPushButton(_('UPDATE'))
# chk_update_btn.clicked.connect(self.check_updates) # chk_update_btn.clicked.connect(self.check_updates)
home_label = QLabel( home_label = QLabel(
'<a href="{url}">User Guide</a>'.format(url=Endpoint.user_guide) '<a href="{url}">User Guide</a>'.format(url=Endpoint.user_guide))
)
home_label.setOpenExternalLinks(True) home_label.setOpenExternalLinks(True)
# buttons # buttons
btnbox = QDialogButtonBox(QDialogButtonBox.Ok, Qt.Horizontal, self) btnbox = QDialogButtonBox(QDialogButtonBox.Ok, Qt.Horizontal, self)
@ -171,7 +168,7 @@ class OptionsDialog(Dialog):
bottom_layout.addWidget(about_btn) bottom_layout.addWidget(about_btn)
bottom_layout.addWidget(home_label) bottom_layout.addWidget(home_label)
bottom_layout.addWidget(btnbox) bottom_layout.addWidget(btnbox)
#self.setLayout(self.main_layout) # self.setLayout(self.main_layout)
self.main_layout.addLayout(bottom_layout) self.main_layout.addLayout(bottom_layout)
# init from saved data # init from saved data
self.current_model = None self.current_model = None
@ -179,7 +176,8 @@ class OptionsDialog(Dialog):
self.current_model = get_model_byId(mw.col.models, self.model_id) self.current_model = get_model_byId(mw.col.models, self.model_id)
if self.current_model: if self.current_model:
self.models_button.setText( self.models_button.setText(
u'%s [%s]' % (_('CHOOSE_NOTE_TYPES'), self.current_model['name'])) u'%s [%s]' % (_('CHOOSE_NOTE_TYPES'),
self.current_model['name']))
# build fields -- dicts layout # build fields -- dicts layout
self.build_tabs_layout() self.build_tabs_layout()
@ -198,7 +196,7 @@ class OptionsDialog(Dialog):
'''open folder manager dialog''' '''open folder manager dialog'''
self.accept() self.accept()
self.setResult(1001) self.setResult(1001)
def show_dm_dialog(self): def show_dm_dialog(self):
'''open dictionary manager dialog''' '''open dictionary manager dialog'''
self.accept() self.accept()
@ -225,13 +223,18 @@ class OptionsDialog(Dialog):
''' '''
build dictionaryfields etc build dictionaryfields etc
''' '''
try: self.tab_widget.currentChanged.disconnect() try:
except Exception: pass self.tab_widget.currentChanged.disconnect()
except Exception:
pass
while len(self.tabs) > 0: while len(self.tabs) > 0:
self.removeTab(0, True) self.removeTab(0, True)
# tabs # tabs
conf = config.get_maps(self.current_model['id']) conf = config.get_maps(self.current_model['id'])
maps_list = {'list': [conf], 'def': 0} if isinstance(conf, list) else conf maps_list = {
'list': [conf],
'def': 0
} if isinstance(conf, list) else conf
for maps in maps_list['list']: for maps in maps_list['list']:
self.addTab(maps, False) self.addTab(maps, False)
self.tab_widget.currentChanged.connect(self.changedTab) self.tab_widget.currentChanged.connect(self.changedTab)
@ -240,28 +243,21 @@ class OptionsDialog(Dialog):
self.tab_widget.setCurrentIndex(maps_list['def']) self.tab_widget.setCurrentIndex(maps_list['def'])
# size # size
self.resize( self.resize(
WIDGET_SIZE.dialog_width, WIDGET_SIZE.dialog_width,
min(max(3, len(self.current_model['flds'])+1), 14) * WIDGET_SIZE.map_max_height + WIDGET_SIZE.dialog_height_margin min(max(3,
) len(self.current_model['flds']) + 1), 14) *
WIDGET_SIZE.map_max_height + WIDGET_SIZE.dialog_height_margin)
self.save() self.save()
def addTab(self, maps=None, forcus=True): def addTab(self, maps=None, forcus=True):
i = len(self.tabs) i = len(self.tabs)
if isinstance(maps, list): if isinstance(maps, list):
maps = { maps = {'fields': maps, 'name': _('CONFIG_INDEX') % (i + 1)}
'fields': maps, tab = TabContent(self.current_model, maps['fields'] if maps else None,
'name': _('CONFIG_INDEX') % (i+1) self.dict_services)
}
tab = TabContent(
self.current_model,
maps['fields'] if maps else None,
self.dict_services
)
self.tabs.append(tab) self.tabs.append(tab)
self.tab_widget.addTab( self.tab_widget.addTab(
tab, tab, maps['name'] if maps else _('CONFIG_INDEX') % (i + 1))
maps['name'] if maps else _('CONFIG_INDEX') % (i+1)
)
if forcus: if forcus:
self.tab_widget.setCurrentIndex(i) self.tab_widget.setCurrentIndex(i)
@ -273,7 +269,7 @@ class OptionsDialog(Dialog):
del self.tabs[i] del self.tabs[i]
self.tab_widget.removeTab(i) self.tab_widget.removeTab(i)
tab.destroy() tab.destroy()
#for k in range(0, len(self.tabs)): # for k in range(0, len(self.tabs)):
# self.tab_widget.setTabText(k, _('CONFIG_INDEX') % (k+1)) # self.tab_widget.setTabText(k, _('CONFIG_INDEX') % (k+1))
def changedTab(self, i): def changedTab(self, i):
@ -289,12 +285,18 @@ class OptionsDialog(Dialog):
''' '''
show choose note type window show choose note type window
''' '''
edit = QPushButton(anki.lang._("Manage"), edit = QPushButton(
clicked=lambda: aqt.models.Models(mw, self)) anki.lang._("Manage"), clicked=lambda: aqt.models.Models(mw, self))
ret = StudyDeck(mw, names=lambda: sorted(mw.col.models.allNames()), ret = StudyDeck(
accept=anki.lang._("Choose"), title=anki.lang._("Choose Note Type"), mw,
help="_notes", parent=self, buttons=[edit], names=lambda: sorted(mw.col.models.allNames()),
cancel=True, geomKey="selectModel") accept=anki.lang._("Choose"),
title=anki.lang._("Choose Note Type"),
help="_notes",
parent=self,
buttons=[edit],
cancel=True,
geomKey="selectModel")
if ret.name: if ret.name:
model = mw.col.models.byName(ret.name) model = mw.col.models.byName(ret.name)
self.models_button.setText( self.models_button.setText(
@ -306,18 +308,17 @@ class OptionsDialog(Dialog):
if not self.current_model: if not self.current_model:
return return
data = dict() data = dict()
maps_list = { maps_list = {'list': [], 'def': self.tab_widget.currentIndex()}
'list': [],
'def': self.tab_widget.currentIndex()
}
for i, tab in enumerate(self.tabs): for i, tab in enumerate(self.tabs):
maps_list['list'].append({ maps_list['list'].append({
'fields': tab.data, 'fields':
'name': self.tab_widget.tabBar().tabText(i) tab.data,
'name':
self.tab_widget.tabBar().tabText(i)
}) })
current_model_id = str(self.current_model['id']) current_model_id = str(self.current_model['id'])
data[current_model_id] = maps_list data[current_model_id] = maps_list
data['last_model'] = self.current_model['id'] data['last_model'] = self.current_model['id']
config.update(data) config.update(data)
@ -339,7 +340,7 @@ class TabContent(QScrollArea):
self.setWidgetResizable(True) self.setWidgetResizable(True)
self.setWidget(dicts) self.setWidget(dicts)
self.dicts_layout = dicts.layout() self.dicts_layout = dicts.layout()
#self.dicts_layout.setSizeConstraint(QLayout.SetFixedSize) # self.dicts_layout.setSizeConstraint(QLayout.SetFixedSize)
def build_layout(self): def build_layout(self):
''' '''
@ -352,7 +353,7 @@ class TabContent(QScrollArea):
self._last_checkeds = None self._last_checkeds = None
self._was_built = True self._was_built = True
model = self._model model = self._model
maps = self._conf maps = self._conf
# labels # labels
@ -372,7 +373,8 @@ class TabContent(QScrollArea):
self.ignore_all_check_btn.setEnabled(True) self.ignore_all_check_btn.setEnabled(True)
self.ignore_all_check_btn.setChecked(True) self.ignore_all_check_btn.setChecked(True)
self.dicts_layout.addWidget(self.ignore_all_check_btn, 0, 1) self.dicts_layout.addWidget(self.ignore_all_check_btn, 0, 1)
self.ignore_all_check_btn.clicked.connect(self.ignore_all_check_changed) self.ignore_all_check_btn.clicked.connect(
self.ignore_all_check_changed)
# Skip valued all # Skip valued all
self.skip_all_check_btn = QCheckBox(_('SELECT_ALL')) self.skip_all_check_btn = QCheckBox(_('SELECT_ALL'))
@ -381,7 +383,7 @@ class TabContent(QScrollArea):
self.skip_all_check_btn.setChecked(True) self.skip_all_check_btn.setChecked(True)
self.dicts_layout.addWidget(self.skip_all_check_btn, 0, 4) self.dicts_layout.addWidget(self.skip_all_check_btn, 0, 4)
self.skip_all_check_btn.clicked.connect(self.skip_all_check_changed) self.skip_all_check_btn.clicked.connect(self.skip_all_check_changed)
# dict & fields # dict & fields
self.radio_group = QButtonGroup() self.radio_group = QButtonGroup()
for i, fld in enumerate(model['flds']): for i, fld in enumerate(model['flds']):
@ -389,16 +391,19 @@ class TabContent(QScrollArea):
name = fld['name'] name = fld['name']
if maps: if maps:
for j, each in enumerate(maps): for j, each in enumerate(maps):
if each.get('fld_ord', -1) == ord or each.get('fld_name', '') == name: if each.get('fld_ord', -1) == ord or each.get(
'fld_name', '') == name:
each['fld_name'] = name each['fld_name'] = name
each['fld_ord'] = ord each['fld_ord'] = ord
self.add_dict_layout(j, **each) self.add_dict_layout(j, **each)
break break
else: else:
self.add_dict_layout(i, fld_name=name, fld_ord=ord, word_checked=i==0) self.add_dict_layout(
i, fld_name=name, fld_ord=ord, word_checked=i == 0)
else: else:
self.add_dict_layout(i, fld_name=name, fld_ord=ord, word_checked=i==0) self.add_dict_layout(
i, fld_name=name, fld_ord=ord, word_checked=i == 0)
# update # update
self.ignore_all_update() self.ignore_all_update()
self.skip_all_update() self.skip_all_update()
@ -410,21 +415,21 @@ class TabContent(QScrollArea):
word_checked = kwargs.get('word_checked', False) word_checked = kwargs.get('word_checked', False)
fld_name, fld_ord = ( fld_name, fld_ord = (
kwargs.get('fld_name', ''), #笔记类型的字段名 kwargs.get('fld_name', ''), # 笔记类型的字段名
kwargs.get('fld_ord', ''), #笔记类型的字段编号 kwargs.get('fld_ord', ''), # 笔记类型的字段编号
) )
dict_name, dict_unique, dict_fld_name, dict_fld_ord = ( dict_name, dict_unique, dict_fld_name, dict_fld_ord = (
kwargs.get('dict_name', ''), #字典名 kwargs.get('dict_name', ''), # 字典名
kwargs.get('dict_unique', ''), #字典ID kwargs.get('dict_unique', ''), # 字典ID
kwargs.get('dict_fld_name', ''), #对应字典的字段名 kwargs.get('dict_fld_name', ''), # 对应字典的字段名
kwargs.get('dcit_fld_ord', 0) #对应字典的字段编号 kwargs.get('dcit_fld_ord', 0) # 对应字典的字段编号
) )
ignore, skip, cloze = ( ignore, skip, cloze = (
kwargs.get('ignore', True), #忽略标志 kwargs.get('ignore', True), # 忽略标志
kwargs.get('skip_valued', True), #略过有值项标志 kwargs.get('skip_valued', True), # 略过有值项标志
kwargs.get('cloze_word', False), #单词填空 kwargs.get('cloze_word', False), # 单词填空
) )
# check # check
@ -436,26 +441,23 @@ class TabContent(QScrollArea):
# dict combox # dict combox
dict_combo = QComboBox() dict_combo = QComboBox()
dict_combo.setMinimumSize(WIDGET_SIZE.map_dict_width, 0) dict_combo.setMinimumSize(WIDGET_SIZE.map_dict_width, 0)
dict_combo.setMaximumSize( dict_combo.setMaximumSize(WIDGET_SIZE.map_dict_width,
WIDGET_SIZE.map_dict_width, WIDGET_SIZE.map_max_height)
WIDGET_SIZE.map_max_height dict_combo.setFocusPolicy(Qt.TabFocus | Qt.ClickFocus | Qt.StrongFocus
) | Qt.WheelFocus)
dict_combo.setFocusPolicy( ignore = not self.fill_dict_combo_options(dict_combo, dict_unique,
Qt.TabFocus | Qt.ClickFocus | Qt.StrongFocus | Qt.WheelFocus self._services) or ignore
)
ignore = not self.fill_dict_combo_options(dict_combo, dict_unique, self._services) or ignore
dict_unique = dict_combo.itemData(dict_combo.currentIndex()) dict_unique = dict_combo.itemData(dict_combo.currentIndex())
dict_combo.setEnabled(not word_checked and not ignore) dict_combo.setEnabled(not word_checked and not ignore)
# field combox # field combox
field_combo = QComboBox() field_combo = QComboBox()
field_combo.setMinimumSize(WIDGET_SIZE.map_field_width, 0) field_combo.setMinimumSize(WIDGET_SIZE.map_field_width, 0)
field_combo.setMaximumSize( field_combo.setMaximumSize(WIDGET_SIZE.map_field_width,
WIDGET_SIZE.map_field_width, WIDGET_SIZE.map_max_height)
WIDGET_SIZE.map_max_height
)
field_combo.setEnabled(not word_checked and not ignore) field_combo.setEnabled(not word_checked and not ignore)
self.fill_field_combo_options(field_combo, dict_name, dict_unique, dict_fld_name, dict_fld_ord) self.fill_field_combo_options(field_combo, dict_name, dict_unique,
dict_fld_name, dict_fld_ord)
# ignore # ignore
ignore_check_btn = QCheckBox(_("NOT_DICT_FIELD")) ignore_check_btn = QCheckBox(_("NOT_DICT_FIELD"))
ignore_check_btn.setEnabled(not word_checked) ignore_check_btn.setEnabled(not word_checked)
@ -489,13 +491,14 @@ class TabContent(QScrollArea):
cloze_check_btn.setEnabled(not word_checked and not ignore) cloze_check_btn.setEnabled(not word_checked and not ignore)
if word_checked: if word_checked:
self._last_checkeds = [ self._last_checkeds = [
ignore_check_btn, dict_combo, ignore_check_btn, dict_combo, field_combo, skip_check_btn
field_combo, skip_check_btn
] ]
word_check_btn.clicked.connect(radio_btn_checked) word_check_btn.clicked.connect(radio_btn_checked)
if word_checked: if word_checked:
self._last_checkeds = None self._last_checkeds = None
radio_btn_checked() radio_btn_checked()
# ignor # ignor
def ignore_check_changed(): def ignore_check_changed():
word_checked = word_check_btn.isChecked() word_checked = word_check_btn.isChecked()
@ -504,35 +507,38 @@ class TabContent(QScrollArea):
field_combo.setEnabled(not word_checked and not ignore) field_combo.setEnabled(not word_checked and not ignore)
skip_check_btn.setEnabled(not word_checked and not ignore) skip_check_btn.setEnabled(not word_checked and not ignore)
cloze_check_btn.setEnabled(not word_checked and not ignore) cloze_check_btn.setEnabled(not word_checked and not ignore)
ignore_check_btn.stateChanged.connect(ignore_check_changed) ignore_check_btn.stateChanged.connect(ignore_check_changed)
ignore_check_btn.clicked.connect(self.ignore_all_update) ignore_check_btn.clicked.connect(self.ignore_all_update)
# skip # skip
skip_check_btn.clicked.connect(self.skip_all_update) skip_check_btn.clicked.connect(self.skip_all_update)
# dict # dict
def dict_combo_changed(index): def dict_combo_changed(index):
'''dict combo box index changed''' '''dict combo box index changed'''
self.fill_field_combo_options( self.fill_field_combo_options(
field_combo, field_combo, dict_combo.currentText(),
dict_combo.currentText(), dict_combo.itemData(index), field_combo.currentText(),
dict_combo.itemData(index), field_combo.itemData(field_combo.currentIndex()))
field_combo.currentText(),
field_combo.itemData(field_combo.currentIndex())
)
dict_combo.currentIndexChanged.connect(dict_combo_changed) dict_combo.currentIndexChanged.connect(dict_combo_changed)
self.dicts_layout.addWidget(word_check_btn, i + 1, 0) self.dicts_layout.addWidget(word_check_btn, i + 1, 0)
self.dicts_layout.addWidget(ignore_check_btn, i + 1, 1) self.dicts_layout.addWidget(ignore_check_btn, i + 1, 1)
self.dicts_layout.addWidget(dict_combo, i + 1, 2) self.dicts_layout.addWidget(dict_combo, i + 1, 2)
self.dicts_layout.addWidget(field_combo, i + 1, 3) self.dicts_layout.addWidget(field_combo, i + 1, 3)
self.dicts_layout.addWidget(skip_check_btn, i + 1, 4) self.dicts_layout.addWidget(skip_check_btn, i + 1, 4)
self.dicts_layout.addWidget(cloze_check_btn, i + 1, 5) self.dicts_layout.addWidget(cloze_check_btn, i + 1, 5)
self._options.append({ self._options.append({
'model': {'fld_name': fld_name, 'fld_ord': fld_ord}, 'model': {
'word_check_btn': word_check_btn, 'fld_name': fld_name,
'dict_combo': dict_combo, 'fld_ord': fld_ord
'field_combo': field_combo, },
'ignore_check_btn': ignore_check_btn, 'word_check_btn': word_check_btn,
'dict_combo': dict_combo,
'field_combo': field_combo,
'ignore_check_btn': ignore_check_btn,
'skip_check_btn': skip_check_btn, 'skip_check_btn': skip_check_btn,
'cloze_check_btn': cloze_check_btn 'cloze_check_btn': cloze_check_btn
}) })
@ -548,7 +554,7 @@ class TabContent(QScrollArea):
# hr # hr
if len(services['local']) > 0: if len(services['local']) > 0:
dict_combo.insertSeparator(dict_combo.count()) dict_combo.insertSeparator(dict_combo.count())
# web dict service # web dict service
for service in services['web']: for service in services['web']:
dict_combo.addItem(service['title'], userData=service['unique']) dict_combo.addItem(service['title'], userData=service['unique'])
@ -561,15 +567,18 @@ class TabContent(QScrollArea):
dict_combo.setCurrentIndex(i) dict_combo.setCurrentIndex(i)
return True return True
return False return False
return set_dict_combo_index() return set_dict_combo_index()
def fill_field_combo_options(self, field_combo, dict_combo_text, dict_combo_itemdata, dict_fld_name, dict_fld_ord): def fill_field_combo_options(self, field_combo, dict_combo_text,
dict_combo_itemdata, dict_fld_name,
dict_fld_ord):
'''setup field combobox''' '''setup field combobox'''
field_combo.clear() field_combo.clear()
field_combo.setEditable(False) field_combo.setEditable(False)
#if dict_combo_text in _sl('NOT_DICT_FIELD'): # if dict_combo_text in _sl('NOT_DICT_FIELD'):
# field_combo.setEnabled(False) # field_combo.setEnabled(False)
#el # el
if dict_combo_text in _sl('MDX_SERVER'): if dict_combo_text in _sl('MDX_SERVER'):
text = dict_fld_name if dict_fld_name else 'http://' text = dict_fld_name if dict_fld_name else 'http://'
field_combo.setEditable(True) field_combo.setEditable(True)
@ -594,16 +603,26 @@ class TabContent(QScrollArea):
maps = [] maps = []
for row in self._options: for row in self._options:
maps.append({ maps.append({
'fld_name': row['model']['fld_name'], 'fld_name':
'fld_ord': row['model']['fld_ord'], row['model']['fld_name'],
'word_checked': row['word_check_btn'].isChecked(), 'fld_ord':
'dict_name': row['dict_combo'].currentText().strip(), row['model']['fld_ord'],
'dict_unique': row['dict_combo'].itemData(row['dict_combo'].currentIndex()), 'word_checked':
'dict_fld_name': row['field_combo'].currentText().strip(), row['word_check_btn'].isChecked(),
'dict_fld_ord': row['field_combo'].itemData(row['field_combo'].currentIndex()), 'dict_name':
'ignore': row['ignore_check_btn'].isChecked(), row['dict_combo'].currentText().strip(),
'skip_valued': row['skip_check_btn'].isChecked(), 'dict_unique':
'cloze_word': row['cloze_check_btn'].isChecked() row['dict_combo'].itemData(row['dict_combo'].currentIndex()),
'dict_fld_name':
row['field_combo'].currentText().strip(),
'dict_fld_ord':
row['field_combo'].itemData(row['field_combo'].currentIndex()),
'ignore':
row['ignore_check_btn'].isChecked(),
'skip_valued':
row['skip_check_btn'].isChecked(),
'cloze_word':
row['cloze_check_btn'].isChecked()
}) })
return maps return maps
@ -624,7 +643,7 @@ class TabContent(QScrollArea):
b = False b = False
break break
self.ignore_all_check_btn.setChecked(b) self.ignore_all_check_btn.setChecked(b)
def skip_all_update(self): def skip_all_update(self):
b = True b = True
for row in self._options: for row in self._options:
@ -635,8 +654,7 @@ class TabContent(QScrollArea):
class CTabBar(QTabBar): class CTabBar(QTabBar):
def __init__(self, parent=None):
def __init__(self, parent = None):
super(CTabBar, self).__init__(parent) super(CTabBar, self).__init__(parent)
# style # style
self.setTabsClosable(True) self.setTabsClosable(True)
@ -651,14 +669,17 @@ class CTabBar(QTabBar):
self._editor.installEventFilter(self) self._editor.installEventFilter(self)
def eventFilter(self, widget, event): def eventFilter(self, widget, event):
if ((event.type() == QEvent.MouseButtonPress and \ bhide = False
not self._editor.geometry().contains(event.globalPos()) \ if event.type() == QEvent.MouseButtonPress:
) or \ if not self._editor.geometry().contains(event.globalPos()):
(event.type() == QEvent.KeyPress and \ bhide = True
event.key() == Qt.Key_Escape) if not bhide:
): if event.type() == QEvent.KeyPress:
self.hideEditor() if event.key() == Qt.Key_Escape:
return True bhide = True
if bhide:
self.hideEditor()
return True
return QTabBar.eventFilter(self, widget, event) return QTabBar.eventFilter(self, widget, event)
def mouseDoubleClickEvent(self, event): def mouseDoubleClickEvent(self, event):
@ -676,7 +697,7 @@ class CTabBar(QTabBar):
self._editor.selectAll() self._editor.selectAll()
self._editor.setEnabled(True) self._editor.setEnabled(True)
self._editor.setFocus() self._editor.setFocus()
def hideEditor(self): def hideEditor(self):
if self._editor.isVisible(): if self._editor.isVisible():
self._editor.setEnabled(False) self._editor.setEnabled(False)

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -17,24 +17,22 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import time import time
from collections import defaultdict from collections import defaultdict
from aqt.qt import * from aqt.qt import *
from ..lang import _
from ..context import APP_ICON
from ..context import APP_ICON
from ..lang import _
__all__ = ['ProgressWindow'] __all__ = ['ProgressWindow']
_INFO_TEMPLATE = u''.join([ _INFO_TEMPLATE = u''.join([
u'<strong>' + _('QUERIED') + u'</strong>', u'<strong>' + _('QUERIED') + u'</strong>',
u'<p>' + 45 * u'-' + u'</p>', u'<p>' + 45 * u'-' + u'</p>',
u'<p>' + _('SUCCESS') + u' <b>{}</b> ' + _('WORDS') + u'</p>', u'<p>' + _('SUCCESS') + u' <b>{}</b> ' + _('WORDS') + u'</p>',
u'<p>' + _('SKIPED') + u' <b>{}</b> ' + _('WORDS') + u'</p>', u'<p>' + _('SKIPED') + u' <b>{}</b> ' + _('WORDS') + u'</p>',
u'<p>' + _('UPDATE') + u' <b>{}</b> ' + _('FIELDS') + u'</p>', u'<p>' + _('UPDATE') + u' <b>{}</b> ' + _('FIELDS') + u'</p>',
u'<p>' + _('FAILURE') + u' <b>{}</b> ' + _('WORDS') + u'</p>', u'<p>' + _('FAILURE') + u' <b>{}</b> ' + _('WORDS') + u'</p>',
]) ])
@ -66,15 +64,12 @@ class ProgressWindow(object):
self._msg_count.get('words_number', 0), self._msg_count.get('words_number', 0),
self._msg_count.get('fields_number', 0), self._msg_count.get('fields_number', 0),
self._msg_count.get('fails_number', 0), self._msg_count.get('fails_number', 0),
self._msg_count.get('skips_number', 0) self._msg_count.get('skips_number', 0))
) number_info = _INFO_TEMPLATE.format(words_number, skips_number,
number_info = _INFO_TEMPLATE.format( fields_number, fails_number)
words_number, self._update(
skips_number, label=number_info,
fields_number, value=words_number + skips_number + fails_number)
fails_number
)
self._update(label=number_info, value=words_number+skips_number+fails_number)
self._win.adjustSize() self._win.adjustSize()
self.app.processEvents() self.app.processEvents()
@ -95,9 +90,7 @@ class ProgressWindow(object):
self._win.setWindowTitle("FastWQ - Querying...") self._win.setWindowTitle("FastWQ - Querying...")
self._win.setModal(True) self._win.setModal(True)
self._win.setWindowFlags( self._win.setWindowFlags(
self._win.windowFlags() & self._win.windowFlags() & ~Qt.WindowContextHelpButtonHint)
~Qt.WindowContextHelpButtonHint
)
self._win.setWindowIcon(APP_ICON) self._win.setWindowIcon(APP_ICON)
self._win.setAutoReset(True) self._win.setAutoReset(True)
self._win.setAutoClose(True) self._win.setAutoClose(True)

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -21,7 +21,7 @@ from aqt.qt import *
from ..context import config from ..context import config
from ..lang import _ from ..lang import _
from .base import WIDGET_SIZE, Dialog from .base import Dialog
__all__ = ['SettingDialog'] __all__ = ['SettingDialog']
@ -89,23 +89,23 @@ class SettingDialog(Dialog):
hbox.addSpacing(300) hbox.addSpacing(300)
hbox.addWidget(resetbtn) hbox.addWidget(resetbtn)
hbox.addWidget(okbtn) hbox.addWidget(okbtn)
layout.addSpacing(48) layout.addSpacing(48)
layout.addLayout(hbox) layout.addLayout(hbox)
self.check_force_update = check_force_update self.check_force_update = check_force_update
self.check_ignore_accents = check_ignore_accents self.check_ignore_accents = check_ignore_accents
self.check_ighore_mdx_wordcase = check_ighore_mdx_wordcase self.check_ighore_mdx_wordcase = check_ighore_mdx_wordcase
self.input_thread_number = input_thread_number self.input_thread_number = input_thread_number
self.input_cloze_str = input_cloze_str self.input_cloze_str = input_cloze_str
layout.setAlignment(Qt.AlignTop|Qt.AlignLeft) layout.setAlignment(Qt.AlignTop | Qt.AlignLeft)
self.setLayout(layout) self.setLayout(layout)
def accept(self): def accept(self):
self.save() self.save()
super(SettingDialog, self).accept() super(SettingDialog, self).accept()
def reset(self): def reset(self):
data = { data = {
'force_update': False, 'force_update': False,
@ -120,7 +120,7 @@ class SettingDialog(Dialog):
self.check_ighore_mdx_wordcase.setChecked(config.ignore_mdx_wordcase) self.check_ighore_mdx_wordcase.setChecked(config.ignore_mdx_wordcase)
self.input_thread_number.setValue(config.thread_number) self.input_thread_number.setValue(config.thread_number)
self.input_cloze_str.setText(config.cloze_str) self.input_cloze_str.setText(config.cloze_str)
def save(self): def save(self):
data = { data = {
'force_update': self.check_force_update.isChecked(), 'force_update': self.check_force_update.isChecked(),

View File

@ -1,4 +1,4 @@
#-*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# #
# Copyright (C) 2018 sthoo <sth201807@gmail.com> # Copyright (C) 2018 sthoo <sth201807@gmail.com>
# #
@ -18,15 +18,15 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from anki.lang import currentLang from anki.lang import currentLang
try: try:
basestring basestring
except NameError: except NameError:
basestring = str basestring = str
__all__ = ['_', '_cl', '_sl'] __all__ = ['_', '_cl', '_sl']
# Language Define, [Key, zh_CN, en]
#Language Define, [Key, zh_CN, en]
_arr = [ _arr = [
['CHECK_FILENAME_LABEL', u'使用文件名作为标签', u'Use the Filename as Label'], ['CHECK_FILENAME_LABEL', u'使用文件名作为标签', u'Use the Filename as Label'],
['EXPORT_MEDIA', u'导出媒体文件', u'Export Media Files'], ['EXPORT_MEDIA', u'导出媒体文件', u'Export Media Files'],
@ -42,13 +42,19 @@ _arr = [
['QUERIED', u'查询', u'Queried'], ['QUERIED', u'查询', u'Queried'],
['FIELDS', u'字段', u'Fields'], ['FIELDS', u'字段', u'Fields'],
['WORDS', u'单词', u'Words'], ['WORDS', u'单词', u'Words'],
['NOT_DICT_FIELD', u'忽略', u'Ignore'], #不是字典字段 ['NOT_DICT_FIELD', u'忽略', u'Ignore'], # 不是字典字段
['NOTE_TYPE_FIELDS', u'<b>笔记字段</b>', u'<b>Note Fields</b>'], ['NOTE_TYPE_FIELDS', u'<b>笔记字段</b>', u'<b>Note Fields</b>'],
['DICTS', u'<b>字典</b>', u'<b>Dictionary</b>'], ['DICTS', u'<b>字典</b>', u'<b>Dictionary</b>'],
['DICT_FIELDS', u'<b>字典字段</b>', u'<b>Fields</b>'], ['DICT_FIELDS', u'<b>字典字段</b>', u'<b>Fields</b>'],
['RADIOS_DESC', u'<b>单选框选中为待查询单词字段</b>', u'<b> Select the field to be queried with single selection.</b>'], [
'RADIOS_DESC', u'<b>单选框选中为待查询单词字段</b>',
u'<b> Select the field to be queried with single selection.</b>'
],
['NO_QUERY_WORD', u'查询字段无单词', u'The query field is empty'], ['NO_QUERY_WORD', u'查询字段无单词', u'The query field is empty'],
['CSS_NOT_FOUND', u'没有找到CSS文件请手动选择', u'No CSS file found, please select one manually.'], [
'CSS_NOT_FOUND', u'没有找到CSS文件请手动选择',
u'No CSS file found, please select one manually.'
],
['ABOUT', u'关于', u'About'], ['ABOUT', u'关于', u'About'],
['REPOSITORY', u'项目地址', u'Project Repo'], ['REPOSITORY', u'项目地址', u'Project Repo'],
['FEEDBACK', u'反馈', u'Feedback'], ['FEEDBACK', u'反馈', u'Feedback'],
@ -60,7 +66,10 @@ _arr = [
['UPDATE', u'更新', u'Update'], ['UPDATE', u'更新', u'Update'],
['AUTO_UPDATE', u'自动检测新版本', u'Auto check new version'], ['AUTO_UPDATE', u'自动检测新版本', u'Auto check new version'],
['CHECK_UPDATE', u'检测更新', u'Check Update'], ['CHECK_UPDATE', u'检测更新', u'Check Update'],
['IGNORE_MDX_WORDCASE', u'忽略本地词典单词大小写', u'Ignore MDX dictionary word case'], [
'IGNORE_MDX_WORDCASE', u'忽略本地词典单词大小写',
u'Ignore MDX dictionary word case'
],
['FORCE_UPDATE', u'强制更新字段', u'Forced Updates of all fields'], ['FORCE_UPDATE', u'强制更新字段', u'Forced Updates of all fields'],
['IGNORE_ACCENTS', u'忽略声调', u'Ignore Accents'], ['IGNORE_ACCENTS', u'忽略声调', u'Ignore Accents'],
['SKIP_VALUED', u'跳过有值项', u'Skip non-empty'], ['SKIP_VALUED', u'跳过有值项', u'Skip non-empty'],
@ -68,7 +77,10 @@ _arr = [
['SETTINGS', u'参数', u'Settings'], ['SETTINGS', u'参数', u'Settings'],
['THREAD_NUMBER', u'线程数', u'Number of Threads'], ['THREAD_NUMBER', u'线程数', u'Number of Threads'],
['INITLIZING_DICT', u'初始化词典...', u'Initlizing Dictionary...'], ['INITLIZING_DICT', u'初始化词典...', u'Initlizing Dictionary...'],
['PLS_SET_DICTIONARY_FIELDS', u'请设置字典和字段', u'Please set the dictionary and fields.'], [
'PLS_SET_DICTIONARY_FIELDS', u'请设置字典和字段',
u'Please set the dictionary and fields.'
],
['CONFIG_INDEX', u'配置 %s', u'Config %s'], ['CONFIG_INDEX', u'配置 %s', u'Config %s'],
['SELECT_ALL', u'全选', u'All'], ['SELECT_ALL', u'全选', u'All'],
['DICTS_NAME', u'字典名称', u'Dictionary Name'], ['DICTS_NAME', u'字典名称', u'Dictionary Name'],
@ -80,7 +92,6 @@ _arr = [
['OPTIONS', u'选项', u'Options'], ['OPTIONS', u'选项', u'Options'],
['CLOZE_WORD', u'单词填空', u'Cloze word'], ['CLOZE_WORD', u'单词填空', u'Cloze word'],
['CLOZE_WORD_FORMAT', '单词填空格式', 'Cloze word formater'], ['CLOZE_WORD_FORMAT', '单词填空格式', 'Cloze word formater'],
['BRE_PRON', u'英式发音', u'British Pronunciation'], ['BRE_PRON', u'英式发音', u'British Pronunciation'],
['AME_PRON', u'美式发音', u'American Pronunciation'], ['AME_PRON', u'美式发音', u'American Pronunciation'],
['PRON', u'发音', u'Audio Pronunciation'], ['PRON', u'发音', u'Audio Pronunciation'],
@ -103,7 +114,7 @@ def _(key, lang=currentLang):
def disp(s): def disp(s):
return s.lower().capitalize() return s.lower().capitalize()
if key not in _trans or lang not in _trans[key]: if key not in _trans or lang not in _trans[key]:
return disp(key) return disp(key)
return _trans[key][lang] return _trans[key][lang]