Add dictionary manager dialog
@ -90,6 +90,10 @@ class Config(object):
|
|||||||
def dirs(self):
|
def dirs(self):
|
||||||
return self.data.get('dirs', list())
|
return self.data.get('dirs', list())
|
||||||
|
|
||||||
|
@property
|
||||||
|
def dicts(self):
|
||||||
|
return self.data.get('dicts', dict())
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def use_filename(self):
|
def use_filename(self):
|
||||||
return self.data.get('use_filename', True)
|
return self.data.get('use_filename', True)
|
||||||
|
|||||||
@ -23,11 +23,12 @@ from aqt.qt import *
|
|||||||
from aqt.utils import showInfo
|
from aqt.utils import showInfo
|
||||||
from .options import OptionsDialog
|
from .options import OptionsDialog
|
||||||
from .foldermanager import FoldersManageDialog
|
from .foldermanager import FoldersManageDialog
|
||||||
|
from .dictmanager import DictManageDialog
|
||||||
from ..libs import ankihub
|
from ..libs import ankihub
|
||||||
from ..context import config
|
from ..context import config
|
||||||
from ..lang import _
|
from ..lang import _
|
||||||
from ..constants import Endpoint, Template
|
from ..constants import Endpoint, Template
|
||||||
from ..service import service_manager
|
from ..service import service_manager, service_pool
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['show_options', 'check_updates', 'show_fm_dialog', 'show_about_dialog']
|
__all__ = ['show_options', 'check_updates', 'show_fm_dialog', 'show_about_dialog']
|
||||||
@ -55,12 +56,27 @@ def show_fm_dialog(browser = None):
|
|||||||
fm_dialog.raise_()
|
fm_dialog.raise_()
|
||||||
if fm_dialog.exec_() == QDialog.Accepted:
|
if fm_dialog.exec_() == QDialog.Accepted:
|
||||||
# update local services
|
# update local services
|
||||||
|
service_pool.clean()
|
||||||
service_manager.update_services()
|
service_manager.update_services()
|
||||||
fm_dialog.destroy()
|
fm_dialog.destroy()
|
||||||
# reshow options window
|
# reshow options window
|
||||||
show_options(browser)
|
show_options(browser)
|
||||||
|
|
||||||
|
|
||||||
|
def show_dm_dialog(browser = None):
|
||||||
|
parent = mw if browser is None else browser
|
||||||
|
dm_dialog = DictManageDialog(parent, u'Dictionary Manager')
|
||||||
|
dm_dialog.activateWindow()
|
||||||
|
dm_dialog.raise_()
|
||||||
|
if dm_dialog.exec_() == QDialog.Accepted:
|
||||||
|
# update local services
|
||||||
|
service_pool.clean()
|
||||||
|
service_manager.update_services()
|
||||||
|
dm_dialog.destroy()
|
||||||
|
# reshow options window
|
||||||
|
show_options(browser)
|
||||||
|
|
||||||
|
|
||||||
def show_options(browser = None, model_id = -1, callback = None, *args, **kwargs):
|
def show_options(browser = None, model_id = -1, callback = None, *args, **kwargs):
|
||||||
'''open options window'''
|
'''open options window'''
|
||||||
parent = mw if browser is None else browser
|
parent = mw if browser is None else browser
|
||||||
@ -68,10 +84,15 @@ def show_options(browser = None, model_id = -1, callback = None, *args, **kwargs
|
|||||||
opt_dialog = OptionsDialog(parent, u'Options', model_id)
|
opt_dialog = OptionsDialog(parent, u'Options', model_id)
|
||||||
opt_dialog.activateWindow()
|
opt_dialog.activateWindow()
|
||||||
opt_dialog.raise_()
|
opt_dialog.raise_()
|
||||||
if opt_dialog.exec_() == QDialog.Accepted:
|
result = opt_dialog.exec_()
|
||||||
|
opt_dialog.destroy()
|
||||||
|
if result == QDialog.Accepted:
|
||||||
if isinstance(callback, types.FunctionType):
|
if isinstance(callback, types.FunctionType):
|
||||||
callback(*args, **kwargs)
|
callback(*args, **kwargs)
|
||||||
opt_dialog.destroy()
|
elif result == 1001:
|
||||||
|
show_fm_dialog(parent)
|
||||||
|
elif result == 1002:
|
||||||
|
show_dm_dialog(parent)
|
||||||
|
|
||||||
|
|
||||||
def show_about_dialog(parent):
|
def show_about_dialog(parent):
|
||||||
|
|||||||
173
addons/fastwq/gui/dictmanager.py
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
#-*- coding:utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright (C) 2018 sthoo <sth201807@gmail.com>
|
||||||
|
#
|
||||||
|
# Support: Report an issue at https://github.com/sth2018/FastWordQuery/issues
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# any later version; http://www.gnu.org/copyleft/gpl.html.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from aqt.qt import *
|
||||||
|
from aqt.forms.editaddon import Ui_Dialog
|
||||||
|
from .base import Dialog, WIDGET_SIZE
|
||||||
|
from ..service import service_manager, service_pool
|
||||||
|
from ..context import config
|
||||||
|
from ..lang import _, _sl
|
||||||
|
from ..utils import get_icon
|
||||||
|
|
||||||
|
# 2x3 compatible
|
||||||
|
if sys.hexversion >= 0x03000000:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['DictManageDialog']
|
||||||
|
|
||||||
|
|
||||||
|
class DictManageDialog(Dialog):
|
||||||
|
'''
|
||||||
|
Dictionary manager window. enabled or disabled dictionary, and setting params of dictionary.
|
||||||
|
'''
|
||||||
|
|
||||||
|
def __init__(self, parent, title=u'Dictionary Manager'):
|
||||||
|
super(DictManageDialog, self).__init__(parent, title)
|
||||||
|
self.main_layout = QVBoxLayout()
|
||||||
|
self.setLayout(self.main_layout)
|
||||||
|
self._options = list()
|
||||||
|
btnbox = QDialogButtonBox(QDialogButtonBox.Ok, Qt.Horizontal , self)
|
||||||
|
btnbox.accepted.connect(self.accept)
|
||||||
|
# add dicts mapping
|
||||||
|
self.dicts_layout = QGridLayout()
|
||||||
|
self.main_layout.addLayout(self.dicts_layout)
|
||||||
|
self.main_layout.addWidget(btnbox)
|
||||||
|
self.build()
|
||||||
|
|
||||||
|
def build(self):
|
||||||
|
''' '''
|
||||||
|
# labels
|
||||||
|
f = QFont()
|
||||||
|
f.setBold(True)
|
||||||
|
labels = ['', '']
|
||||||
|
for i, s in enumerate(labels):
|
||||||
|
if s:
|
||||||
|
label = QLabel(_(s))
|
||||||
|
label.setFont(f)
|
||||||
|
label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
|
||||||
|
self.dicts_layout.addWidget(label, 0, i)
|
||||||
|
# enabled all
|
||||||
|
self.enabled_all_check_btn = QCheckBox(_('DICTS_NAME'))
|
||||||
|
self.enabled_all_check_btn.setFont(f)
|
||||||
|
self.enabled_all_check_btn.setEnabled(True)
|
||||||
|
self.enabled_all_check_btn.setChecked(True)
|
||||||
|
# signal
|
||||||
|
self.enabled_all_check_btn.clicked.connect(self.enabled_all_changed)
|
||||||
|
# add widgets
|
||||||
|
self.dicts_layout.addWidget(self.enabled_all_check_btn, 0, 0)
|
||||||
|
# dict service list
|
||||||
|
confs = config.dicts
|
||||||
|
dicts = list()
|
||||||
|
for clazz in service_manager.web_services:
|
||||||
|
service = service_pool.get(clazz.__unique__)
|
||||||
|
if service and service.support:
|
||||||
|
dicts.append({
|
||||||
|
'title': service.title,
|
||||||
|
'unique': service.unique,
|
||||||
|
'path': clazz.__path__,
|
||||||
|
'enabled': confs.get(service.unique, dict()).get('enabled', True)
|
||||||
|
})
|
||||||
|
service_pool.put(service)
|
||||||
|
# add dict
|
||||||
|
for i, d in enumerate(dicts):
|
||||||
|
self.add_dict_layout(i, **d)
|
||||||
|
# update
|
||||||
|
self.enabled_all_update()
|
||||||
|
self.adjustSize()
|
||||||
|
|
||||||
|
def add_dict_layout(self, i, **kwargs):
|
||||||
|
# args
|
||||||
|
title, unique, enabled, path = (
|
||||||
|
kwargs.get('title', u''),
|
||||||
|
kwargs.get('unique', u''),
|
||||||
|
kwargs.get('enabled', False),
|
||||||
|
kwargs.get('path', u''),
|
||||||
|
)
|
||||||
|
# button
|
||||||
|
check_btn = QCheckBox(title)
|
||||||
|
check_btn.setMinimumSize(WIDGET_SIZE.map_dict_width*1.5, 0)
|
||||||
|
check_btn.setEnabled(True)
|
||||||
|
check_btn.setChecked(enabled)
|
||||||
|
edit_btn = QToolButton(self)
|
||||||
|
edit_btn.setText(_('EDIT'))
|
||||||
|
# signal
|
||||||
|
check_btn.stateChanged.connect(self.enabled_all_update)
|
||||||
|
edit_btn.clicked.connect(lambda: self.on_edit(path))
|
||||||
|
# add
|
||||||
|
self.dicts_layout.addWidget(check_btn, i + 1, 0)
|
||||||
|
self.dicts_layout.addWidget(edit_btn, i + 1, 1)
|
||||||
|
self._options.append({
|
||||||
|
'unique': unique,
|
||||||
|
'check_btn': check_btn,
|
||||||
|
'edit_btn': edit_btn,
|
||||||
|
})
|
||||||
|
|
||||||
|
def enabled_all_update(self):
|
||||||
|
b = True
|
||||||
|
for row in self._options:
|
||||||
|
if not row['check_btn'].isChecked():
|
||||||
|
b = False
|
||||||
|
break
|
||||||
|
self.enabled_all_check_btn.setChecked(b)
|
||||||
|
|
||||||
|
def enabled_all_changed(self):
|
||||||
|
b = self.enabled_all_check_btn.isChecked()
|
||||||
|
for row in self._options:
|
||||||
|
row['check_btn'].setChecked(b)
|
||||||
|
|
||||||
|
def on_edit(self, path):
|
||||||
|
'''edit dictionary file'''
|
||||||
|
d = QDialog(self)
|
||||||
|
frm = Ui_Dialog()
|
||||||
|
frm.setupUi(d)
|
||||||
|
d.setWindowTitle(os.path.basename(path))
|
||||||
|
# 2x3 compatible
|
||||||
|
if sys.hexversion >= 0x03000000:
|
||||||
|
frm.text.setPlainText(open(path, 'r', encoding="utf-8").read())
|
||||||
|
else:
|
||||||
|
frm.text.setPlainText(unicode(open(path).read(), "utf8"))
|
||||||
|
d.accepted.connect(lambda: self.on_accept_edit(path, frm))
|
||||||
|
d.exec_()
|
||||||
|
|
||||||
|
def on_accept_edit(self, path, frm):
|
||||||
|
'''save dictionary file'''
|
||||||
|
# 2x3 compatible
|
||||||
|
if sys.hexversion >= 0x03000000:
|
||||||
|
open(path, "w", encoding='utf-8').write(frm.text.toPlainText())
|
||||||
|
else:
|
||||||
|
open(path, "w").write(frm.text.toPlainText().encode("utf8"))
|
||||||
|
|
||||||
|
def accept(self):
|
||||||
|
'''ok button clicked'''
|
||||||
|
self.save()
|
||||||
|
super(DictManageDialog, self).accept()
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
'''save config to file'''
|
||||||
|
data = dict()
|
||||||
|
dicts = {}
|
||||||
|
for row in self._options:
|
||||||
|
dicts[row['unique']] = {
|
||||||
|
'enabled': row['check_btn'].isChecked(),
|
||||||
|
}
|
||||||
|
data['dicts'] = dicts
|
||||||
|
config.update(data)
|
||||||
@ -74,6 +74,7 @@ class OptionsDialog(Dialog):
|
|||||||
if s != 'before_build':
|
if s != 'before_build':
|
||||||
return
|
return
|
||||||
# dict service list
|
# dict service list
|
||||||
|
dicts = config.dicts
|
||||||
self.dict_services = {
|
self.dict_services = {
|
||||||
'local': [], #本地词典
|
'local': [], #本地词典
|
||||||
'web': [] #网络词典
|
'web': [] #网络词典
|
||||||
@ -87,13 +88,14 @@ class OptionsDialog(Dialog):
|
|||||||
})
|
})
|
||||||
service_pool.put(service)
|
service_pool.put(service)
|
||||||
for clazz in service_manager.web_services:
|
for clazz in service_manager.web_services:
|
||||||
service = service_pool.get(clazz.__unique__)
|
if dicts.get(clazz.__unique__, dict()).get('enabled', True):
|
||||||
if service and service.support:
|
service = service_pool.get(clazz.__unique__)
|
||||||
self.dict_services['web'].append({
|
if service and service.support:
|
||||||
'title': service.title,
|
self.dict_services['web'].append({
|
||||||
'unique': service.unique
|
'title': service.title,
|
||||||
})
|
'unique': service.unique
|
||||||
service_pool.put(service)
|
})
|
||||||
|
service_pool.put(service)
|
||||||
# emit finished
|
# emit finished
|
||||||
self._signal.emit('after_build')
|
self._signal.emit('after_build')
|
||||||
|
|
||||||
@ -117,6 +119,7 @@ class OptionsDialog(Dialog):
|
|||||||
self.tab_widget = QTabWidget()
|
self.tab_widget = QTabWidget()
|
||||||
self.tab_widget.setTabsClosable(True)
|
self.tab_widget.setTabsClosable(True)
|
||||||
self.tab_widget.setMovable(False)
|
self.tab_widget.setMovable(False)
|
||||||
|
self.tab_widget.tabBar().setExpanding(False)
|
||||||
self.tab_widget.setStyleSheet(
|
self.tab_widget.setStyleSheet(
|
||||||
"""
|
"""
|
||||||
QTabWidget::pane { /* The tab widget frame */
|
QTabWidget::pane { /* The tab widget frame */
|
||||||
@ -124,10 +127,21 @@ class OptionsDialog(Dialog):
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
tab_corner = QWidget()
|
||||||
|
tab_corner_layout = QHBoxLayout()
|
||||||
|
tab_corner_layout.setSpacing(1)
|
||||||
|
tab_corner_layout.setSizeConstraint(QLayout.SetMinAndMaxSize)
|
||||||
|
tab_corner_layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
tab_corner.setLayout(tab_corner_layout)
|
||||||
tab_add_button = QToolButton(self)
|
tab_add_button = QToolButton(self)
|
||||||
tab_add_button.setText(' + ')
|
tab_add_button.setIcon(get_icon('add.png'))
|
||||||
self.tab_widget.setCornerWidget(tab_add_button)
|
tab_set_button = QToolButton(self)
|
||||||
|
tab_set_button.setIcon(get_icon('setting.png'))
|
||||||
|
tab_corner_layout.addWidget(tab_set_button)
|
||||||
|
tab_corner_layout.addWidget(tab_add_button)
|
||||||
|
self.tab_widget.setCornerWidget(tab_corner)
|
||||||
# signals
|
# signals
|
||||||
|
tab_set_button.clicked.connect(self.show_dm_dialog)
|
||||||
tab_add_button.clicked.connect(self.addTab)
|
tab_add_button.clicked.connect(self.addTab)
|
||||||
self.tab_widget.tabCloseRequested.connect(self.removeTab)
|
self.tab_widget.tabCloseRequested.connect(self.removeTab)
|
||||||
# layout
|
# layout
|
||||||
@ -178,11 +192,13 @@ class OptionsDialog(Dialog):
|
|||||||
|
|
||||||
def show_fm_dialog(self):
|
def show_fm_dialog(self):
|
||||||
'''open folder manager dialog'''
|
'''open folder manager dialog'''
|
||||||
from .common import show_fm_dialog
|
self.accept()
|
||||||
self.save()
|
self.setResult(1001)
|
||||||
self.close()
|
|
||||||
self.destroy()
|
def show_dm_dialog(self):
|
||||||
show_fm_dialog(self._parent)
|
'''open dictionary manager dialog'''
|
||||||
|
self.accept()
|
||||||
|
self.setResult(1002)
|
||||||
|
|
||||||
def show_about(self):
|
def show_about(self):
|
||||||
'''open about dialog'''
|
'''open about dialog'''
|
||||||
@ -398,9 +414,9 @@ class TabContent(QWidget):
|
|||||||
dict_combo.setFocusPolicy(
|
dict_combo.setFocusPolicy(
|
||||||
Qt.TabFocus | Qt.ClickFocus | Qt.StrongFocus | Qt.WheelFocus
|
Qt.TabFocus | Qt.ClickFocus | Qt.StrongFocus | Qt.WheelFocus
|
||||||
)
|
)
|
||||||
dict_combo.setEnabled(not word_checked and not ignore)
|
ignore = not self.fill_dict_combo_options(dict_combo, dict_unique, self._services) or ignore
|
||||||
self.fill_dict_combo_options(dict_combo, dict_unique, self._services)
|
|
||||||
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)
|
||||||
# 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)
|
||||||
@ -500,15 +516,14 @@ class TabContent(QWidget):
|
|||||||
dict_combo.addItem(service['title'], userData=service['unique'])
|
dict_combo.addItem(service['title'], userData=service['unique'])
|
||||||
|
|
||||||
def set_dict_combo_index():
|
def set_dict_combo_index():
|
||||||
#dict_combo.setCurrentIndex(-1)
|
|
||||||
dict_combo.setCurrentIndex(0)
|
dict_combo.setCurrentIndex(0)
|
||||||
if current_unique:
|
if current_unique:
|
||||||
for i in range(dict_combo.count()):
|
for i in range(dict_combo.count()):
|
||||||
if dict_combo.itemData(i) == current_unique:
|
if dict_combo.itemData(i) == current_unique:
|
||||||
dict_combo.setCurrentIndex(i)
|
dict_combo.setCurrentIndex(i)
|
||||||
break
|
return True
|
||||||
|
return False
|
||||||
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'''
|
||||||
|
|||||||
@ -69,6 +69,8 @@ _arr = [
|
|||||||
['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'],
|
||||||
|
['EDIT', u'编辑', u'Edit'],
|
||||||
|
|
||||||
['BRE_PRON', u'英式发音', u'British Pronunciation'],
|
['BRE_PRON', u'英式发音', u'British Pronunciation'],
|
||||||
['AME_PRON', u'美式发音', u'American Pronunciation'],
|
['AME_PRON', u'美式发音', u'American Pronunciation'],
|
||||||
|
|||||||
BIN
addons/fastwq/res/add.png
Normal file
|
After Width: | Height: | Size: 378 B |
BIN
addons/fastwq/res/setting.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 577 B |
@ -89,6 +89,7 @@ class ServiceManager(object):
|
|||||||
continue
|
continue
|
||||||
service = service_wrap(clazz, *args)
|
service = service_wrap(clazz, *args)
|
||||||
service.__unique__ = name
|
service.__unique__ = name
|
||||||
|
service.__path__ = os.path.join(mypath, f)
|
||||||
if issubclass(clazz, WebService):
|
if issubclass(clazz, WebService):
|
||||||
web_services.append(service)
|
web_services.append(service)
|
||||||
# get the customized local services
|
# get the customized local services
|
||||||
|
|||||||
@ -91,6 +91,10 @@ class Config(object):
|
|||||||
def dirs(self):
|
def dirs(self):
|
||||||
return self.data.get('dirs', list())
|
return self.data.get('dirs', list())
|
||||||
|
|
||||||
|
@property
|
||||||
|
def dicts(self):
|
||||||
|
return self.data.get('dicts', dict())
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def use_filename(self):
|
def use_filename(self):
|
||||||
return self.data.get('use_filename', True)
|
return self.data.get('use_filename', True)
|
||||||
|
|||||||
@ -23,11 +23,12 @@ from aqt.qt import *
|
|||||||
from aqt.utils import showInfo
|
from aqt.utils import showInfo
|
||||||
from .options import OptionsDialog
|
from .options import OptionsDialog
|
||||||
from .foldermanager import FoldersManageDialog
|
from .foldermanager import FoldersManageDialog
|
||||||
|
from .dictmanager import DictManageDialog
|
||||||
from ..libs import ankihub
|
from ..libs import ankihub
|
||||||
from ..context import config
|
from ..context import config
|
||||||
from ..lang import _
|
from ..lang import _
|
||||||
from ..constants import Endpoint, Template
|
from ..constants import Endpoint, Template
|
||||||
from ..service import service_manager
|
from ..service import service_manager, service_pool
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['show_options', 'check_updates', 'show_fm_dialog', 'show_about_dialog']
|
__all__ = ['show_options', 'check_updates', 'show_fm_dialog', 'show_about_dialog']
|
||||||
@ -55,12 +56,27 @@ def show_fm_dialog(browser = None):
|
|||||||
fm_dialog.raise_()
|
fm_dialog.raise_()
|
||||||
if fm_dialog.exec_() == QDialog.Accepted:
|
if fm_dialog.exec_() == QDialog.Accepted:
|
||||||
# update local services
|
# update local services
|
||||||
|
service_pool.clean()
|
||||||
service_manager.update_services()
|
service_manager.update_services()
|
||||||
fm_dialog.destroy()
|
fm_dialog.destroy()
|
||||||
# reshow options window
|
# reshow options window
|
||||||
show_options(browser)
|
show_options(browser)
|
||||||
|
|
||||||
|
|
||||||
|
def show_dm_dialog(browser = None):
|
||||||
|
parent = mw if browser is None else browser
|
||||||
|
dm_dialog = DictManageDialog(parent, u'Dictionary Manager')
|
||||||
|
dm_dialog.activateWindow()
|
||||||
|
dm_dialog.raise_()
|
||||||
|
if dm_dialog.exec_() == QDialog.Accepted:
|
||||||
|
# update local services
|
||||||
|
service_pool.clean()
|
||||||
|
service_manager.update_services()
|
||||||
|
dm_dialog.destroy()
|
||||||
|
# reshow options window
|
||||||
|
show_options(browser)
|
||||||
|
|
||||||
|
|
||||||
def show_options(browser = None, model_id = -1, callback = None, *args, **kwargs):
|
def show_options(browser = None, model_id = -1, callback = None, *args, **kwargs):
|
||||||
'''open options window'''
|
'''open options window'''
|
||||||
parent = mw if browser is None else browser
|
parent = mw if browser is None else browser
|
||||||
@ -68,10 +84,15 @@ def show_options(browser = None, model_id = -1, callback = None, *args, **kwargs
|
|||||||
opt_dialog = OptionsDialog(parent, u'Options', model_id)
|
opt_dialog = OptionsDialog(parent, u'Options', model_id)
|
||||||
opt_dialog.activateWindow()
|
opt_dialog.activateWindow()
|
||||||
opt_dialog.raise_()
|
opt_dialog.raise_()
|
||||||
if opt_dialog.exec_() == QDialog.Accepted:
|
result = opt_dialog.exec_()
|
||||||
|
opt_dialog.destroy()
|
||||||
|
if result == QDialog.Accepted:
|
||||||
if isinstance(callback, types.FunctionType):
|
if isinstance(callback, types.FunctionType):
|
||||||
callback(*args, **kwargs)
|
callback(*args, **kwargs)
|
||||||
opt_dialog.destroy()
|
elif result == 1001:
|
||||||
|
show_fm_dialog(parent)
|
||||||
|
elif result == 1002:
|
||||||
|
show_dm_dialog(parent)
|
||||||
|
|
||||||
|
|
||||||
def show_about_dialog(parent):
|
def show_about_dialog(parent):
|
||||||
|
|||||||
173
addons21/fastwq/gui/dictmanager.py
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
#-*- coding:utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright (C) 2018 sthoo <sth201807@gmail.com>
|
||||||
|
#
|
||||||
|
# Support: Report an issue at https://github.com/sth2018/FastWordQuery/issues
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# any later version; http://www.gnu.org/copyleft/gpl.html.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from aqt.qt import *
|
||||||
|
from aqt.forms.editaddon import Ui_Dialog
|
||||||
|
from .base import Dialog, WIDGET_SIZE
|
||||||
|
from ..service import service_manager, service_pool
|
||||||
|
from ..context import config
|
||||||
|
from ..lang import _, _sl
|
||||||
|
from ..utils import get_icon
|
||||||
|
|
||||||
|
# 2x3 compatible
|
||||||
|
if sys.hexversion >= 0x03000000:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['DictManageDialog']
|
||||||
|
|
||||||
|
|
||||||
|
class DictManageDialog(Dialog):
|
||||||
|
'''
|
||||||
|
Dictionary manager window. enabled or disabled dictionary, and setting params of dictionary.
|
||||||
|
'''
|
||||||
|
|
||||||
|
def __init__(self, parent, title=u'Dictionary Manager'):
|
||||||
|
super(DictManageDialog, self).__init__(parent, title)
|
||||||
|
self.main_layout = QVBoxLayout()
|
||||||
|
self.setLayout(self.main_layout)
|
||||||
|
self._options = list()
|
||||||
|
btnbox = QDialogButtonBox(QDialogButtonBox.Ok, Qt.Horizontal , self)
|
||||||
|
btnbox.accepted.connect(self.accept)
|
||||||
|
# add dicts mapping
|
||||||
|
self.dicts_layout = QGridLayout()
|
||||||
|
self.main_layout.addLayout(self.dicts_layout)
|
||||||
|
self.main_layout.addWidget(btnbox)
|
||||||
|
self.build()
|
||||||
|
|
||||||
|
def build(self):
|
||||||
|
''' '''
|
||||||
|
# labels
|
||||||
|
f = QFont()
|
||||||
|
f.setBold(True)
|
||||||
|
labels = ['', '']
|
||||||
|
for i, s in enumerate(labels):
|
||||||
|
if s:
|
||||||
|
label = QLabel(_(s))
|
||||||
|
label.setFont(f)
|
||||||
|
label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
|
||||||
|
self.dicts_layout.addWidget(label, 0, i)
|
||||||
|
# enabled all
|
||||||
|
self.enabled_all_check_btn = QCheckBox(_('DICTS_NAME'))
|
||||||
|
self.enabled_all_check_btn.setFont(f)
|
||||||
|
self.enabled_all_check_btn.setEnabled(True)
|
||||||
|
self.enabled_all_check_btn.setChecked(True)
|
||||||
|
# signal
|
||||||
|
self.enabled_all_check_btn.clicked.connect(self.enabled_all_changed)
|
||||||
|
# add widgets
|
||||||
|
self.dicts_layout.addWidget(self.enabled_all_check_btn, 0, 0)
|
||||||
|
# dict service list
|
||||||
|
confs = config.dicts
|
||||||
|
dicts = list()
|
||||||
|
for clazz in service_manager.web_services:
|
||||||
|
service = service_pool.get(clazz.__unique__)
|
||||||
|
if service and service.support:
|
||||||
|
dicts.append({
|
||||||
|
'title': service.title,
|
||||||
|
'unique': service.unique,
|
||||||
|
'path': clazz.__path__,
|
||||||
|
'enabled': confs.get(service.unique, dict()).get('enabled', True)
|
||||||
|
})
|
||||||
|
service_pool.put(service)
|
||||||
|
# add dict
|
||||||
|
for i, d in enumerate(dicts):
|
||||||
|
self.add_dict_layout(i, **d)
|
||||||
|
# update
|
||||||
|
self.enabled_all_update()
|
||||||
|
self.adjustSize()
|
||||||
|
|
||||||
|
def add_dict_layout(self, i, **kwargs):
|
||||||
|
# args
|
||||||
|
title, unique, enabled, path = (
|
||||||
|
kwargs.get('title', u''),
|
||||||
|
kwargs.get('unique', u''),
|
||||||
|
kwargs.get('enabled', False),
|
||||||
|
kwargs.get('path', u''),
|
||||||
|
)
|
||||||
|
# button
|
||||||
|
check_btn = QCheckBox(title)
|
||||||
|
check_btn.setMinimumSize(WIDGET_SIZE.map_dict_width*1.5, 0)
|
||||||
|
check_btn.setEnabled(True)
|
||||||
|
check_btn.setChecked(enabled)
|
||||||
|
edit_btn = QToolButton(self)
|
||||||
|
edit_btn.setText(_('EDIT'))
|
||||||
|
# signal
|
||||||
|
check_btn.stateChanged.connect(self.enabled_all_update)
|
||||||
|
edit_btn.clicked.connect(lambda: self.on_edit(path))
|
||||||
|
# add
|
||||||
|
self.dicts_layout.addWidget(check_btn, i + 1, 0)
|
||||||
|
self.dicts_layout.addWidget(edit_btn, i + 1, 1)
|
||||||
|
self._options.append({
|
||||||
|
'unique': unique,
|
||||||
|
'check_btn': check_btn,
|
||||||
|
'edit_btn': edit_btn,
|
||||||
|
})
|
||||||
|
|
||||||
|
def enabled_all_update(self):
|
||||||
|
b = True
|
||||||
|
for row in self._options:
|
||||||
|
if not row['check_btn'].isChecked():
|
||||||
|
b = False
|
||||||
|
break
|
||||||
|
self.enabled_all_check_btn.setChecked(b)
|
||||||
|
|
||||||
|
def enabled_all_changed(self):
|
||||||
|
b = self.enabled_all_check_btn.isChecked()
|
||||||
|
for row in self._options:
|
||||||
|
row['check_btn'].setChecked(b)
|
||||||
|
|
||||||
|
def on_edit(self, path):
|
||||||
|
'''edit dictionary file'''
|
||||||
|
d = QDialog(self)
|
||||||
|
frm = Ui_Dialog()
|
||||||
|
frm.setupUi(d)
|
||||||
|
d.setWindowTitle(os.path.basename(path))
|
||||||
|
# 2x3 compatible
|
||||||
|
if sys.hexversion >= 0x03000000:
|
||||||
|
frm.text.setPlainText(open(path, 'r', encoding="utf-8").read())
|
||||||
|
else:
|
||||||
|
frm.text.setPlainText(unicode(open(path).read(), "utf8"))
|
||||||
|
d.accepted.connect(lambda: self.on_accept_edit(path, frm))
|
||||||
|
d.exec_()
|
||||||
|
|
||||||
|
def on_accept_edit(self, path, frm):
|
||||||
|
'''save dictionary file'''
|
||||||
|
# 2x3 compatible
|
||||||
|
if sys.hexversion >= 0x03000000:
|
||||||
|
open(path, "w", encoding='utf-8').write(frm.text.toPlainText())
|
||||||
|
else:
|
||||||
|
open(path, "w").write(frm.text.toPlainText().encode("utf8"))
|
||||||
|
|
||||||
|
def accept(self):
|
||||||
|
'''ok button clicked'''
|
||||||
|
self.save()
|
||||||
|
super(DictManageDialog, self).accept()
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
'''save config to file'''
|
||||||
|
data = dict()
|
||||||
|
dicts = {}
|
||||||
|
for row in self._options:
|
||||||
|
dicts[row['unique']] = {
|
||||||
|
'enabled': row['check_btn'].isChecked(),
|
||||||
|
}
|
||||||
|
data['dicts'] = dicts
|
||||||
|
config.update(data)
|
||||||
@ -74,6 +74,7 @@ class OptionsDialog(Dialog):
|
|||||||
if s != 'before_build':
|
if s != 'before_build':
|
||||||
return
|
return
|
||||||
# dict service list
|
# dict service list
|
||||||
|
dicts = config.dicts
|
||||||
self.dict_services = {
|
self.dict_services = {
|
||||||
'local': [], #本地词典
|
'local': [], #本地词典
|
||||||
'web': [] #网络词典
|
'web': [] #网络词典
|
||||||
@ -87,13 +88,14 @@ class OptionsDialog(Dialog):
|
|||||||
})
|
})
|
||||||
service_pool.put(service)
|
service_pool.put(service)
|
||||||
for clazz in service_manager.web_services:
|
for clazz in service_manager.web_services:
|
||||||
service = service_pool.get(clazz.__unique__)
|
if dicts.get(clazz.__unique__, dict()).get('enabled', True):
|
||||||
if service and service.support:
|
service = service_pool.get(clazz.__unique__)
|
||||||
self.dict_services['web'].append({
|
if service and service.support:
|
||||||
'title': service.title,
|
self.dict_services['web'].append({
|
||||||
'unique': service.unique
|
'title': service.title,
|
||||||
})
|
'unique': service.unique
|
||||||
service_pool.put(service)
|
})
|
||||||
|
service_pool.put(service)
|
||||||
# emit finished
|
# emit finished
|
||||||
self._signal.emit('after_build')
|
self._signal.emit('after_build')
|
||||||
|
|
||||||
@ -117,6 +119,7 @@ class OptionsDialog(Dialog):
|
|||||||
self.tab_widget = QTabWidget()
|
self.tab_widget = QTabWidget()
|
||||||
self.tab_widget.setTabsClosable(True)
|
self.tab_widget.setTabsClosable(True)
|
||||||
self.tab_widget.setMovable(False)
|
self.tab_widget.setMovable(False)
|
||||||
|
self.tab_widget.tabBar().setExpanding(False)
|
||||||
self.tab_widget.setStyleSheet(
|
self.tab_widget.setStyleSheet(
|
||||||
"""
|
"""
|
||||||
QTabWidget::pane { /* The tab widget frame */
|
QTabWidget::pane { /* The tab widget frame */
|
||||||
@ -124,10 +127,21 @@ class OptionsDialog(Dialog):
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
tab_corner = QWidget()
|
||||||
|
tab_corner_layout = QHBoxLayout()
|
||||||
|
tab_corner_layout.setSpacing(1)
|
||||||
|
tab_corner_layout.setSizeConstraint(QLayout.SetMinAndMaxSize)
|
||||||
|
tab_corner_layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
tab_corner.setLayout(tab_corner_layout)
|
||||||
tab_add_button = QToolButton(self)
|
tab_add_button = QToolButton(self)
|
||||||
tab_add_button.setText(' + ')
|
tab_add_button.setIcon(get_icon('add.png'))
|
||||||
self.tab_widget.setCornerWidget(tab_add_button)
|
tab_set_button = QToolButton(self)
|
||||||
|
tab_set_button.setIcon(get_icon('setting.png'))
|
||||||
|
tab_corner_layout.addWidget(tab_set_button)
|
||||||
|
tab_corner_layout.addWidget(tab_add_button)
|
||||||
|
self.tab_widget.setCornerWidget(tab_corner)
|
||||||
# signals
|
# signals
|
||||||
|
tab_set_button.clicked.connect(self.show_dm_dialog)
|
||||||
tab_add_button.clicked.connect(self.addTab)
|
tab_add_button.clicked.connect(self.addTab)
|
||||||
self.tab_widget.tabCloseRequested.connect(self.removeTab)
|
self.tab_widget.tabCloseRequested.connect(self.removeTab)
|
||||||
# layout
|
# layout
|
||||||
@ -178,11 +192,13 @@ class OptionsDialog(Dialog):
|
|||||||
|
|
||||||
def show_fm_dialog(self):
|
def show_fm_dialog(self):
|
||||||
'''open folder manager dialog'''
|
'''open folder manager dialog'''
|
||||||
from .common import show_fm_dialog
|
self.accept()
|
||||||
self.save()
|
self.setResult(1001)
|
||||||
self.close()
|
|
||||||
self.destroy()
|
def show_dm_dialog(self):
|
||||||
show_fm_dialog(self._parent)
|
'''open dictionary manager dialog'''
|
||||||
|
self.accept()
|
||||||
|
self.setResult(1002)
|
||||||
|
|
||||||
def show_about(self):
|
def show_about(self):
|
||||||
'''open about dialog'''
|
'''open about dialog'''
|
||||||
@ -398,9 +414,9 @@ class TabContent(QWidget):
|
|||||||
dict_combo.setFocusPolicy(
|
dict_combo.setFocusPolicy(
|
||||||
Qt.TabFocus | Qt.ClickFocus | Qt.StrongFocus | Qt.WheelFocus
|
Qt.TabFocus | Qt.ClickFocus | Qt.StrongFocus | Qt.WheelFocus
|
||||||
)
|
)
|
||||||
dict_combo.setEnabled(not word_checked and not ignore)
|
ignore = not self.fill_dict_combo_options(dict_combo, dict_unique, self._services) or ignore
|
||||||
self.fill_dict_combo_options(dict_combo, dict_unique, self._services)
|
|
||||||
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)
|
||||||
# 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)
|
||||||
@ -500,15 +516,14 @@ class TabContent(QWidget):
|
|||||||
dict_combo.addItem(service['title'], userData=service['unique'])
|
dict_combo.addItem(service['title'], userData=service['unique'])
|
||||||
|
|
||||||
def set_dict_combo_index():
|
def set_dict_combo_index():
|
||||||
#dict_combo.setCurrentIndex(-1)
|
|
||||||
dict_combo.setCurrentIndex(0)
|
dict_combo.setCurrentIndex(0)
|
||||||
if current_unique:
|
if current_unique:
|
||||||
for i in range(dict_combo.count()):
|
for i in range(dict_combo.count()):
|
||||||
if dict_combo.itemData(i) == current_unique:
|
if dict_combo.itemData(i) == current_unique:
|
||||||
dict_combo.setCurrentIndex(i)
|
dict_combo.setCurrentIndex(i)
|
||||||
break
|
return True
|
||||||
|
return False
|
||||||
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'''
|
||||||
|
|||||||
@ -69,6 +69,8 @@ _arr = [
|
|||||||
['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'],
|
||||||
|
['EDIT', u'编辑', u'Edit'],
|
||||||
|
|
||||||
['BRE_PRON', u'英式发音', u'British Pronunciation'],
|
['BRE_PRON', u'英式发音', u'British Pronunciation'],
|
||||||
['AME_PRON', u'美式发音', u'American Pronunciation'],
|
['AME_PRON', u'美式发音', u'American Pronunciation'],
|
||||||
|
|||||||
BIN
addons21/fastwq/res/add.png
Normal file
|
After Width: | Height: | Size: 378 B |
BIN
addons21/fastwq/res/setting.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 577 B |
@ -84,6 +84,7 @@ class ServiceManager(object):
|
|||||||
continue
|
continue
|
||||||
service = service_wrap(clazz, *args)
|
service = service_wrap(clazz, *args)
|
||||||
service.__unique__ = name
|
service.__unique__ = name
|
||||||
|
service.__path__ = os.path.join(mypath, f)
|
||||||
if issubclass(clazz, WebService):
|
if issubclass(clazz, WebService):
|
||||||
web_services.append(service)
|
web_services.append(service)
|
||||||
# get the customized local services
|
# get the customized local services
|
||||||
|
|||||||