From 4124b30954c55b0ff54d04a6faa47616514bbae5 Mon Sep 17 00:00:00 2001 From: "St.Huang" Date: Sat, 14 Jul 2018 15:24:21 +0800 Subject: [PATCH] refactoring. --- src/fastwq/__init__.py | 35 ++-- src/fastwq/constants.py | 3 +- src/fastwq/context.py | 8 +- src/fastwq/gui/__init__.py | 4 + src/fastwq/gui/base.py | 67 +++++++ src/fastwq/gui/common.py | 72 ++++++++ src/fastwq/gui/foldermanager.py | 98 ++++++++++ src/fastwq/{ui.py => gui/options.py} | 265 +++------------------------ src/fastwq/{ => gui}/progress.py | 10 +- src/fastwq/gui/setting.py | 90 +++++++++ src/fastwq/lang.py | 13 +- src/fastwq/query.py | 11 +- 12 files changed, 401 insertions(+), 275 deletions(-) create mode 100644 src/fastwq/gui/__init__.py create mode 100644 src/fastwq/gui/base.py create mode 100644 src/fastwq/gui/common.py create mode 100644 src/fastwq/gui/foldermanager.py rename src/fastwq/{ui.py => gui/options.py} (68%) rename src/fastwq/{ => gui}/progress.py (95%) create mode 100644 src/fastwq/gui/setting.py diff --git a/src/fastwq/__init__.py b/src/fastwq/__init__.py index 510560d..a84930a 100644 --- a/src/fastwq/__init__.py +++ b/src/fastwq/__init__.py @@ -17,22 +17,21 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import anki -import aqt +from PyQt4 import QtCore, QtGui from aqt import mw -from aqt.qt import * from anki.hooks import addHook, wrap from aqt.addcards import AddCards from aqt.utils import showInfo, shortcut -from .ui import show_options -from .query import * +from .gui import show_options, show_about_dialog +from .query import query_from_browser, query_from_editor_all_fields from .context import config, APP_ICON from .lang import _ __all__ = [ - 'wrap_method', 'add_query_button', 'browser_menu', - 'customize_addcards', 'config_menu', 'window_shortcut' + 'add_query_button', 'browser_menu', + 'customize_addcards', 'config_menu', + 'window_shortcut' ] @@ -54,11 +53,11 @@ def add_query_button(self): add a button in add card window ''' bb = self.form.buttonBox - ar = QDialogButtonBox.ActionRole + ar = QtGui.QDialogButtonBox.ActionRole self.queryButton = bb.addButton(_(u"Query"), ar) self.queryButton.clicked.connect(wrap_method( query_from_editor_all_fields, self.editor)) - self.queryButton.setShortcut(QKeySequence(my_shortcut)) + self.queryButton.setShortcut(QtGui.QKeySequence(my_shortcut)) self.queryButton.setToolTip( shortcut(_(u"Query (shortcut: %s)" % my_shortcut))) @@ -72,15 +71,15 @@ def browser_menu(): on browser setupMenus was called """ # main menu - menu = QMenu("FastWQ", browser.form.menubar) + menu = QtGui.QMenu("FastWQ", browser.form.menubar) browser.form.menubar.addMenu(menu) # Query Selected - action = QAction("Query Selected", browser) + action = QtGui.QAction("Query Selected", browser) action.triggered.connect(wrap_method(query_from_browser, browser)) - action.setShortcut(QKeySequence(my_shortcut)) + action.setShortcut(QtGui.QKeySequence(my_shortcut)) menu.addAction(action) - #Options - action = QAction("Options", browser) + # Options + action = QtGui.QAction("Options", browser) def _show_options(): model_id = -1 for note_id in browser.selectedNotes(): @@ -90,8 +89,12 @@ def browser_menu(): show_options(browser, model_id) action.triggered.connect(_show_options) menu.addAction(action) + # About + action = QtGui.QAction("About", browser) + action.triggered.connect(wrap_method(show_about_dialog, browser)) + menu.addAction(action) - anki.hooks.addHook('browser.setupMenus', on_setup_menus) + addHook('browser.setupMenus', on_setup_menus) def customize_addcards(): @@ -106,7 +109,7 @@ def config_menu(): """ add menu to anki window menebar """ - action = QAction(APP_ICON, "FastWQ...", mw) + action = QtGui.QAction(APP_ICON, "FastWQ...", mw) action.triggered.connect(wrap_method(show_options)) mw.form.menuTools.addAction(action) global have_setup diff --git a/src/fastwq/constants.py b/src/fastwq/constants.py index 6a643c8..74766b3 100644 --- a/src/fastwq/constants.py +++ b/src/fastwq/constants.py @@ -21,8 +21,9 @@ from .lang import _ -VERSION = 'v1.1.4' +__all__ = ['VERSION', 'Endpoint', 'Template'] +VERSION = 'v1.1.4' class Endpoint: repository = u'https://github.com/sth2018/FastWordQuery' diff --git a/src/fastwq/context.py b/src/fastwq/context.py index 4c57fea..436939f 100644 --- a/src/fastwq/context.py +++ b/src/fastwq/context.py @@ -23,7 +23,9 @@ from .constants import VERSION from .utils import get_icon -CONFIG_FILENAME = '.fastwqcfg.json' #Config File Path +__all__ = ['APP_ICON', 'config'] + + APP_ICON = get_icon('wqicon.png') #Addon Icon @@ -33,8 +35,10 @@ class Config(object): Addon Config """ + _CONFIG_FILENAME = '.fastwqcfg.json' #Config File Path + def __init__(self, window): - self.path = CONFIG_FILENAME + self.path = self._CONFIG_FILENAME self.window = window self.version = '0' self.read() diff --git a/src/fastwq/gui/__init__.py b/src/fastwq/gui/__init__.py new file mode 100644 index 0000000..6febba1 --- /dev/null +++ b/src/fastwq/gui/__init__.py @@ -0,0 +1,4 @@ +#-*- coding:utf-8 -*- + +from .common import * +from .progress import * diff --git a/src/fastwq/gui/base.py b/src/fastwq/gui/base.py new file mode 100644 index 0000000..b8f0e2d --- /dev/null +++ b/src/fastwq/gui/base.py @@ -0,0 +1,67 @@ +#-*- coding:utf-8 -*- +# +# Copyright © 2016–2017 sthoo +# +# 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 . + +from PyQt4 import QtCore, QtGui +from ..context import APP_ICON + + +__all__ = ['Dialog', 'WIDGET_SIZE'] + + +class Dialog(QtGui.QDialog): + ''' + Base used for all dialog windows. + ''' + + def __init__(self, parent, title): + ''' + Set the modal status for the dialog, sets its layout to the + return value of the _ui() method, and sets a default title. + ''' + + self._title = title + self._parent = parent + super(Dialog, self).__init__(parent) + + self.setModal(True) + self.setWindowFlags( + self.windowFlags() & + ~QtCore.Qt.WindowContextHelpButtonHint + ) + self.setWindowIcon(APP_ICON) + self.setWindowTitle( + title if "FastWQ" in title + else "FastWQ - " + title + ) + + +class WidgetSize(object): + ''' + constant values + ''' + dialog_width = 700 + dialog_height_margin = 120 + map_min_height = 0 + map_max_height = 31 + map_fld_width = 100 + map_dictname_width = 150 + map_dictfield_width = 160 + + +WIDGET_SIZE = WidgetSize() diff --git a/src/fastwq/gui/common.py b/src/fastwq/gui/common.py new file mode 100644 index 0000000..f806dfc --- /dev/null +++ b/src/fastwq/gui/common.py @@ -0,0 +1,72 @@ +#-*- coding:utf-8 -*- +# +# Copyright © 2016–2017 sthoo +# +# 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 . + +import types +from PyQt4 import QtGui +from aqt import mw +from aqt.utils import showInfo +from .options import OptionsDialog +from .foldermanager import FoldersManageDialog +from ..libs import ankihub +from ..context import config +from ..lang import _ +from ..constants import Endpoint, Template +from ..service import service_manager + + +__all__ = ['show_options', 'check_updates', 'show_fm_dialog', 'show_about_dialog'] + + +def check_updates(): + '''check add-on last version''' + try: + if not ankihub.update([Endpoint.check_version], False, Endpoint.version): + showInfo(_('LATEST_VERSION')) + except: + showInfo(_('CHECK_FAILURE')) + + +def show_fm_dialog(browser = None): + '''open dictionary folder manager window''' + parent = mw if browser is None else browser + fm_dialog = FoldersManageDialog(parent, u'Dictionary Folder Manager') + fm_dialog.activateWindow() + fm_dialog.raise_() + if fm_dialog.exec_() == QtGui.QDialog.Accepted: + # update local services + service_manager.update_services() + # reshow options window + show_options(browser) + + +def show_options(browser = None, model_id = -1, callback = None, *args, **kwargs): + '''open options window''' + parent = mw if browser is None else browser + config.read() + opt_dialog = OptionsDialog(parent, u'Options', model_id) + opt_dialog.activateWindow() + opt_dialog.raise_() + if opt_dialog.exec_() == QtGui.QDialog.Accepted: + if isinstance(callback, types.FunctionType): + callback(*args, **kwargs) + + +def show_about_dialog(parent): + '''open about dialog''' + QtGui.QMessageBox.about(parent, _('ABOUT'), Template.tmpl_about) diff --git a/src/fastwq/gui/foldermanager.py b/src/fastwq/gui/foldermanager.py new file mode 100644 index 0000000..9bff6a3 --- /dev/null +++ b/src/fastwq/gui/foldermanager.py @@ -0,0 +1,98 @@ +#-*- coding:utf-8 -*- +# +# Copyright © 2016–2017 sthoo +# +# 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 . + +from PyQt4 import QtCore, QtGui +from .base import Dialog, WIDGET_SIZE +from ..context import config +from ..lang import _, _sl + + +__all__ = ['FoldersManageDialog'] + + +class FoldersManageDialog(Dialog): + ''' + Dictionary folder manager window. add or remove dictionary folders. + ''' + + def __init__(self, parent, title=u'Dictionary Folder Manager'): + super(FoldersManageDialog, self).__init__(parent, title) + #self._dict_paths = [] + self.build() + + def build(self): + layout = QtGui.QVBoxLayout() + btn_layout = QtGui.QHBoxLayout() + add_btn = QtGui.QPushButton("+") + remove_btn = QtGui.QPushButton("-") + btn_layout.addWidget(add_btn) + btn_layout.addWidget(remove_btn) + add_btn.clicked.connect(self.add_folder) + remove_btn.clicked.connect(self.remove_folder) + self.folders_lst = QtGui.QListWidget() + self.folders_lst.addItems(config.dirs) + self.chk_use_filename = QtGui.QCheckBox(_('CHECK_FILENAME_LABEL')) + self.chk_export_media = QtGui.QCheckBox(_('EXPORT_MEDIA')) + self.chk_use_filename.setChecked(config.use_filename) + self.chk_export_media.setChecked(config.export_media) + chk_layout = QtGui.QHBoxLayout() + chk_layout.addWidget(self.chk_use_filename) + chk_layout.addWidget(self.chk_export_media) + btnbox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok, QtCore.Qt.Horizontal, self) + btnbox.accepted.connect(self.accept) + layout.addLayout(btn_layout) + layout.addWidget(self.folders_lst) + layout.addLayout(chk_layout) + layout.addWidget(btnbox) + self.setLayout(layout) + + def add_folder(self): + dir_ = QtGui.QFileDialog.getExistingDirectory( + self, + caption=u"Select Folder", + directory=config.last_folder, + options=QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks + ) + if dir_: + self.folders_lst.addItem(dir_) + config.update({'last_folder': dir_}) + + def remove_folder(self): + item = self.folders_lst.takeItem(self.folders_lst.currentRow()) + del item + + @property + def dirs(self): + '''dictionary folders list''' + return [self.folders_lst.item(i).text() + for i in range(self.folders_lst.count())] + + def accept(self): + '''ok button clicked''' + self.save() + super(FoldersManageDialog, self).accept() + + def save(self): + '''save config to file''' + data = { + 'dirs': self.dirs, + 'use_filename': self.chk_use_filename.isChecked(), + 'export_media': self.chk_export_media.isChecked() + } + config.update(data) diff --git a/src/fastwq/ui.py b/src/fastwq/gui/options.py similarity index 68% rename from src/fastwq/ui.py rename to src/fastwq/gui/options.py index b470367..6815465 100644 --- a/src/fastwq/ui.py +++ b/src/fastwq/gui/options.py @@ -17,212 +17,22 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import os -import sys -import json -import types -from collections import namedtuple - +from PyQt4 import QtCore, QtGui import anki import aqt import aqt.models from aqt import mw -from PyQt4 import QtCore, QtGui from aqt.studydeck import StudyDeck -from aqt.utils import shortcut, showInfo -from .constants import VERSION, Endpoint, Template -from .context import APP_ICON, config -from .lang import _, _sl -from .service import service_manager, service_pool -from .utils import MapDict, get_icon, get_model_byId, get_ord_from_fldname +from .base import Dialog, WIDGET_SIZE +from .setting import SettingDialog +from ..context import config +from ..lang import _, _sl +from ..service import service_manager, service_pool +from ..utils import get_model_byId +from ..constants import Endpoint -__all__ = ['WIDGET_SIZE', 'Dialog', 'ParasDialog', - 'FoldersManageDialog', 'OptionsDialog', 'check_updates', - 'show_options', 'show_fm_dialog' -] - - -class WIDGET_SIZE: - dialog_width = 700 - dialog_height_margin = 120 - map_min_height = 0 - map_max_height = 31 - map_fld_width = 100 - map_dictname_width = 150 - map_dictfield_width = 160 - - -class Dialog(QtGui.QDialog): - ''' - Base used for all dialog windows. - ''' - - def __init__(self, parent, title): - ''' - Set the modal status for the dialog, sets its layout to the - return value of the _ui() method, and sets a default title. - ''' - - self._title = title - self._parent = parent - super(Dialog, self).__init__(parent) - - self.setModal(True) - self.setWindowFlags( - self.windowFlags() & - ~QtCore.Qt.WindowContextHelpButtonHint - ) - self.setWindowIcon(APP_ICON) - self.setWindowTitle( - title if "FastWQ" in title - else "FastWQ - " + title - ) - - -class ParasDialog(Dialog): - ''' - Setting window, some golbal params for query function. - ''' - - def __init__(self, parent, title=u'Setting'): - super(ParasDialog, self).__init__(parent, title) - self.setFixedWidth(400) - self.check_force_update = None - self.input_thread_number = None - self.build() - - def build(self): - layout = QtGui.QVBoxLayout() - - check_force_update = QtGui.QCheckBox(_("FORCE_UPDATE")) - check_force_update.setChecked(config.force_update) - layout.addWidget(check_force_update) - layout.addSpacing(10) - - check_ignore_accents = QtGui.QCheckBox(_("IGNORE_ACCENTS")) - check_ignore_accents.setChecked(config.ignore_accents) - layout.addWidget(check_ignore_accents) - layout.addSpacing(10) - - hbox = QtGui.QHBoxLayout() - input_thread_number = QtGui.QSpinBox(parent=self) - input_thread_number.setRange(1, 120) - input_thread_number.setValue(config.thread_number) - input_label = QtGui.QLabel(_("THREAD_NUMBER") + ":", parent=self) - hbox.addWidget(input_label) - hbox.setStretchFactor(input_label, 1) - hbox.addWidget(input_thread_number) - hbox.setStretchFactor(input_thread_number, 2) - layout.addLayout(hbox) - - buttonBox = QtGui.QDialogButtonBox(parent=self) - buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok) - buttonBox.accepted.connect(self.accept) # 确定 - - layout.addSpacing(48) - layout.addWidget(buttonBox) - - self.check_force_update = check_force_update - self.check_ignore_accents = check_ignore_accents - self.input_thread_number = input_thread_number - - layout.setAlignment(QtCore.Qt.AlignTop|QtCore.Qt.AlignLeft) - self.setLayout(layout) - - def accept(self): - self.save() - super(ParasDialog, self).accept() - - def save(self): - data = { - 'force_update': self.check_force_update.isChecked(), - 'ignore_accents': self.check_ignore_accents.isChecked(), - 'thread_number': self.input_thread_number.value() - } - config.update(data) - - -class FoldersManageDialog(Dialog): - ''' - Dictionary folder manager window. add or remove dictionary folders. - ''' - - def __init__(self, parent, title=u'Dictionary Folder Manager'): - super(FoldersManageDialog, self).__init__(parent, title) - #self._dict_paths = [] - self.build() - - def build(self): - layout = QtGui.QVBoxLayout() - btn_layout = QtGui.QHBoxLayout() - add_btn = QtGui.QPushButton("+") - remove_btn = QtGui.QPushButton("-") - btn_layout.addWidget(add_btn) - btn_layout.addWidget(remove_btn) - add_btn.clicked.connect(self.add_folder) - remove_btn.clicked.connect(self.remove_folder) - self.folders_lst = QtGui.QListWidget() - self.folders_lst.addItems(config.dirs) - self.chk_use_filename = QtGui.QCheckBox(_('CHECK_FILENAME_LABEL')) - self.chk_export_media = QtGui.QCheckBox(_('EXPORT_MEDIA')) - self.chk_use_filename.setChecked(config.use_filename) - self.chk_export_media.setChecked(config.export_media) - chk_layout = QtGui.QHBoxLayout() - chk_layout.addWidget(self.chk_use_filename) - chk_layout.addWidget(self.chk_export_media) - btnbox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok, QtCore.Qt.Horizontal, self) - btnbox.accepted.connect(self.accept) - layout.addLayout(btn_layout) - layout.addWidget(self.folders_lst) - layout.addLayout(chk_layout) - layout.addWidget(btnbox) - self.setLayout(layout) - - def add_folder(self): - dir_ = QtGui.QFileDialog.getExistingDirectory( - self, - caption=u"Select Folder", - directory=config.last_folder, - options=QtGui.QFileDialog.ShowDirsOnly | QtGui.QFileDialog.DontResolveSymlinks - ) - if dir_: - self.folders_lst.addItem(dir_) - config.update({'last_folder': dir_}) - - def remove_folder(self): - item = self.folders_lst.takeItem(self.folders_lst.currentRow()) - del item - - ''' - def find_mdxes(self): - for each in self.dirs: - for dirpath, dirnames, filenames in os.walk(each): - self._dict_paths.extend([os.path.join(dirpath, filename) - for filename in filenames if filename.lower().endswith(u'.mdx')]) - return list(set(self._dict_paths)) - - @property - def dict_paths(self): - return self.find_mdxes() - ''' - - @property - def dirs(self): - return [self.folders_lst.item(i).text() - for i in range(self.folders_lst.count())] - - def accept(self): - self.save() - super(FoldersManageDialog, self).accept() - - def save(self): - data = { - 'dirs': self.dirs, - 'use_filename': self.chk_use_filename.isChecked(), - 'export_media': self.chk_export_media.isChecked() - } - config.update(data) +__all__ = ['OptionsDialog'] class OptionsDialog(Dialog): @@ -250,6 +60,7 @@ class OptionsDialog(Dialog): self.___last_checkeds___ = None self.___options___ = list() self.model_id = model_id if model_id != -1 else config.last_model_id + self.current_model = None # size and signal self.resize(WIDGET_SIZE.dialog_width, 4 * WIDGET_SIZE.map_max_height + WIDGET_SIZE.dialog_height_margin) self.emit(QtCore.SIGNAL('before_build')) @@ -257,6 +68,8 @@ class OptionsDialog(Dialog): def _before_build(self): for cls in service_manager.services: service = service_pool.get(cls.__unique__) + if service: + service_pool.put(service) self.emit(QtCore.SIGNAL('after_build')) def _after_build(self): @@ -289,7 +102,7 @@ class OptionsDialog(Dialog): about_btn.clicked.connect(self.show_about) # about_btn.clicked.connect(self.show_paras) chk_update_btn = QtGui.QPushButton(_('UPDATE')) - chk_update_btn.clicked.connect(check_updates) + chk_update_btn.clicked.connect(self.check_updates) home_label = QtGui.QLabel( 'User Guide'.format(url=Endpoint.user_guide)) home_label.setOpenExternalLinks(True) @@ -317,22 +130,34 @@ class OptionsDialog(Dialog): self.build_mappings_layout(self.current_model) def show_paras(self): - dialog = ParasDialog(self, u'Setting') + '''open setting dialog''' + dialog = SettingDialog(self, u'Setting') dialog.exec_() + def check_updates(self): + '''check addon version''' + from .common import check_updates + check_updates() + def show_fm_dialog(self): + '''open folder manager dialog''' + from .common import show_fm_dialog self.save() self.close() show_fm_dialog(self._parent) def show_about(self): - QtGui.QMessageBox.about(self, _('ABOUT'), Template.tmpl_about) + '''open about dialog''' + from .common import show_about_dialog + show_about_dialog(self) def accept(self): + '''on button was clicked''' self.save() super(OptionsDialog, self).accept() def btn_models_pressed(self): + '''on choose model button was clicker''' self.save() self.current_model = self.show_models() if self.current_model: @@ -602,39 +427,3 @@ class OptionsDialog(Dialog): data[current_model_id] = maps data['last_model'] = self.current_model['id'] config.update(data) - - -def check_updates(): - '''check add-on last version''' - try: - import libs.ankihub - if not libs.ankihub.update([Endpoint.check_version], False, Endpoint.version): - showInfo(_('LATEST_VERSION')) - except: - showInfo(_('CHECK_FAILURE')) - pass - - -def show_options(browser = None, model_id = -1, callback = None, *args, **kwargs): - '''open options window''' - parent = mw if browser is None else browser - config.read() - opt_dialog = OptionsDialog(parent, u'Options', model_id) - opt_dialog.activateWindow() - opt_dialog.raise_() - if opt_dialog.exec_() == QtGui.QDialog.Accepted: - if isinstance(callback, types.FunctionType): - callback(*args, **kwargs) - - -def show_fm_dialog(browser = None): - '''open dictionary folder manager window''' - parent = mw if browser is None else browser - fm_dialog = FoldersManageDialog(parent, u'Dictionary Folder Manager') - fm_dialog.activateWindow() - fm_dialog.raise_() - if fm_dialog.exec_() == QtGui.QDialog.Accepted: - # update local services - service_manager.update_services() - # reshow options window - show_options(browser) diff --git a/src/fastwq/progress.py b/src/fastwq/gui/progress.py similarity index 95% rename from src/fastwq/progress.py rename to src/fastwq/gui/progress.py index a564619..de5b55c 100644 --- a/src/fastwq/progress.py +++ b/src/fastwq/gui/progress.py @@ -22,18 +22,18 @@ import time from collections import defaultdict from PyQt4 import QtCore, QtGui -from .lang import _ +from ..lang import _ -__all__ = ['INFO_TEMPLATE', 'ProgressWindow'] +__all__ = ['ProgressWindow'] -INFO_TEMPLATE = u''.join([ +_INFO_TEMPLATE = u''.join([ _('QUERIED') + u'
' + 45 * u'-' + u'
', _('SUCCESS') + u' {} ' + _('WORDS') + u'
', _('SKIPED') + u' {} ' + _('WORDS') + u'
', _('UPDATE') + u' {} ' + _('FIELDS') + u'
', - _('FAILURE') + u' {} ' + _('WORDS') + u'
' + _('FAILURE') + u' {} ' + _('WORDS') + u'' ]) @@ -63,7 +63,7 @@ class ProgressWindow(object): self._msg_count.get('fails_number', 0), self._msg_count.get('skips_number', 0) ) - number_info = INFO_TEMPLATE.format( + number_info = _INFO_TEMPLATE.format( words_number, skips_number, fields_number, diff --git a/src/fastwq/gui/setting.py b/src/fastwq/gui/setting.py new file mode 100644 index 0000000..bb34154 --- /dev/null +++ b/src/fastwq/gui/setting.py @@ -0,0 +1,90 @@ +#-*- coding:utf-8 -*- +# +# Copyright © 2016–2017 sthoo +# +# 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 . + +from PyQt4 import QtCore, QtGui +from .base import Dialog, WIDGET_SIZE +from ..context import config +from ..lang import _ + + +__all__ = ['SettingDialog'] + + +class SettingDialog(Dialog): + ''' + Setting window, some golbal params for query function. + ''' + + def __init__(self, parent, title=u'Setting'): + super(SettingDialog, self).__init__(parent, title) + self.setFixedWidth(400) + self.check_force_update = None + self.input_thread_number = None + self.build() + + def build(self): + layout = QtGui.QVBoxLayout() + + check_force_update = QtGui.QCheckBox(_("FORCE_UPDATE")) + check_force_update.setChecked(config.force_update) + layout.addWidget(check_force_update) + layout.addSpacing(10) + + check_ignore_accents = QtGui.QCheckBox(_("IGNORE_ACCENTS")) + check_ignore_accents.setChecked(config.ignore_accents) + layout.addWidget(check_ignore_accents) + layout.addSpacing(10) + + hbox = QtGui.QHBoxLayout() + input_thread_number = QtGui.QSpinBox(parent=self) + input_thread_number.setRange(1, 120) + input_thread_number.setValue(config.thread_number) + input_label = QtGui.QLabel(_("THREAD_NUMBER") + ":", parent=self) + hbox.addWidget(input_label) + hbox.setStretchFactor(input_label, 1) + hbox.addWidget(input_thread_number) + hbox.setStretchFactor(input_thread_number, 2) + layout.addLayout(hbox) + + buttonBox = QtGui.QDialogButtonBox(parent=self) + buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok) + buttonBox.accepted.connect(self.accept) # 确定 + + layout.addSpacing(48) + layout.addWidget(buttonBox) + + self.check_force_update = check_force_update + self.check_ignore_accents = check_ignore_accents + self.input_thread_number = input_thread_number + + layout.setAlignment(QtCore.Qt.AlignTop|QtCore.Qt.AlignLeft) + self.setLayout(layout) + + def accept(self): + self.save() + super(SettingDialog, self).accept() + + def save(self): + data = { + 'force_update': self.check_force_update.isChecked(), + 'ignore_accents': self.check_ignore_accents.isChecked(), + 'thread_number': self.input_thread_number.value() + } + config.update(data) + \ No newline at end of file diff --git a/src/fastwq/lang.py b/src/fastwq/lang.py index f45103d..424e00f 100644 --- a/src/fastwq/lang.py +++ b/src/fastwq/lang.py @@ -24,7 +24,7 @@ __all__ = ['_', '_cl', '_sl'] #Language Define, [Key, zh_CN, en] -arr = [ +_arr = [ ['CHECK_FILENAME_LABEL', u'使用文件名作为标签', u'Use Filename As Label'], ['EXPORT_MEDIA', u'导出媒体文件', u'Export Media Files'], ['DICTS_FOLDERS', u'字典文件夹', u'Dictionary Folder'], @@ -76,21 +76,24 @@ arr = [ ['IMAGE', u'图片', u'Image'], ] -trans = {item[0]: {'zh_CN': item[1], 'en': item[2]} for item in arr} +_trans = {item[0]: {'zh_CN': item[1], 'en': item[2]} for item in _arr} + def _(key, lang=currentLang): + '''get local language string''' if lang != 'zh_CN' and lang != 'en': lang = 'en' def disp(s): 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 trans[key][lang] + return _trans[key][lang] def _cl(labels, lang=currentLang): + '''get local language string from labels''' if isinstance(labels, basestring): return _(labels) if lang != 'zh_CN' and lang != 'en': @@ -99,4 +102,4 @@ def _cl(labels, lang=currentLang): def _sl(key): - return trans[key].values() + return _trans[key].values() diff --git a/src/fastwq/query.py b/src/fastwq/query.py index 7a407f6..622b613 100644 --- a/src/fastwq/query.py +++ b/src/fastwq/query.py @@ -28,19 +28,14 @@ from aqt.utils import showInfo, showText, tooltip from .constants import Endpoint, Template from .context import config -from .lang import _, _sl -from .progress import ProgressWindow +from .lang import _ +from .gui import ProgressWindow from .service import service_manager, service_pool, QueryResult, copy_static_file from .service.base import LocalService from .utils import Empty, MapDict, Queue, wrap_css -__all__ = ['QueryThread', 'QueryWorkerManager', 'InvalidWordException', - 'query_from_browser', 'query_from_editor_all_fields', - 'query_all', 'update_note_fields', 'update_note_field', - 'promot_choose_css', 'add_to_tmpl', 'strip_combining', - 'query_all_flds', 'inspect_note' -] +__all__ = ['query_from_browser', 'query_from_editor_all_fields'] def inspect_note(note):