forked from Hopkins1/TradSimpChinese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogs.py
354 lines (305 loc) · 18.4 KB
/
dialogs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# -*- coding: utf-8 -*-
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
import os
try:
from PyQt5.Qt import (Qt, QVBoxLayout, QLabel, QComboBox, QApplication, QSizePolicy,
QGroupBox, QButtonGroup, QRadioButton, QDialogButtonBox, QHBoxLayout,
QProgressDialog, QSize, QDialog, QCheckBox, QSpinBox, QScrollArea, QWidget)
except ImportError:
from PyQt4.Qt import (Qt, QVBoxLayout, QLabel, QComboBox, QApplication, QSizePolicy,
QGroupBox, QButtonGroup, QRadioButton, QDialogButtonBox, QHBoxLayout,
QProgressDialog, QSize, QDialog, QCheckBox, QSpinBox, QScrollArea, QWidget)
from calibre.utils.config import config_dir
from calibre.gui2.tweak_book.widgets import Dialog
from calibre_plugins.chinese_text.__init__ import (PLUGIN_NAME, PLUGIN_SAFE_NAME)
'''
ConversionDialog
The conversion dialog asks/displays the following:
-Which direction of conversion is desired (i.e. Traditional->Simplified, Simplified->Traditional, or Traditional->Traditional)
-If converting from Traditional, what country style is the source of the text (Hong Kong, Mainland, or Taiwan)
-If converting to Traditional, what country style is desired (Hong Kong, Mainland, or Taiwan)
-What text should be converted (the currently selected file or the entire book)
The chosen settings are saved between program starts.
Note: This code is based on the Calibre plugin Diap's Editing Toolbag
'''
class ConversionDialog(Dialog):
def __init__(self, parent, force_entire_book=False):
self.prefs = self.prefsPrep()
self.parent = parent
self.force_entire_book = force_entire_book
self.criteria = None
Dialog.__init__(self, _('Chinese Conversion'), 'chinese_conversion_dialog', parent)
def setup_ui(self):
self.quote_for_trad_target = _("Update quotes: "",'' -> 「」,『』")
self.quote_for_simp_target = _("Update quotes: 「」,『』 -> "",''")
# Create layout for entire dialog
layout = QVBoxLayout(self)
self.setLayout(layout)
#Create a scroll area for the top part of the dialog
self.scrollArea = QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
# Create widget for all the contents of the dialog except the OK and Cancel buttons
self.scrollContentWidget = QWidget(self.scrollArea)
self.scrollArea.setWidget(self.scrollContentWidget)
widgetLayout = QVBoxLayout(self.scrollContentWidget)
# Add scrollArea to dialog
layout.addWidget(self.scrollArea)
self.operation_group_box = QGroupBox(_('Conversion Direction'))
widgetLayout.addWidget(self.operation_group_box)
operation_group_box_layout = QVBoxLayout()
self.operation_group_box.setLayout(operation_group_box_layout)
operation_group=QButtonGroup(self)
self.no_conversion_button = QRadioButton(_('No Conversion'))
operation_group.addButton(self.no_conversion_button)
self.trad_to_simp_button = QRadioButton(_('Traditional to Simplified'))
operation_group.addButton(self.trad_to_simp_button)
self.simp_to_trad_button = QRadioButton(_('Simplified to Traditional'))
operation_group.addButton(self.simp_to_trad_button)
self.trad_to_trad_button = QRadioButton(_('Traditional to Traditional'))
operation_group.addButton(self.trad_to_trad_button)
operation_group_box_layout.addWidget(self.no_conversion_button)
operation_group_box_layout.addWidget(self.trad_to_simp_button)
operation_group_box_layout.addWidget(self.simp_to_trad_button)
operation_group_box_layout.addWidget(self.trad_to_trad_button)
self.no_conversion_button.toggled.connect(self.update_gui)
self.trad_to_simp_button.toggled.connect(self.update_gui)
self.simp_to_trad_button.toggled.connect(self.update_gui)
self.trad_to_trad_button.toggled.connect(self.update_gui)
self.style_group_box = QGroupBox(_('Language Styles'))
widgetLayout.addWidget(self.style_group_box)
style_group_box_layout = QVBoxLayout()
self.style_group_box.setLayout(style_group_box_layout)
input_layout = QHBoxLayout()
style_group_box_layout.addLayout(input_layout)
self.input_region_label = QLabel(_('Input:'))
input_layout.addWidget(self.input_region_label)
self.input_combo = QComboBox()
input_layout.addWidget(self.input_combo)
self.input_combo.addItems([_('Mainland'), _('Hong Kong'), _('Taiwan')])
self.input_combo.setToolTip(_('Select the origin region of the input'))
self.input_combo.currentIndexChanged.connect(self.update_gui)
output_layout = QHBoxLayout()
style_group_box_layout.addLayout(output_layout)
self.output_region_label = QLabel(_('Output:'))
output_layout.addWidget(self.output_region_label)
self.output_combo = QComboBox()
output_layout.addWidget(self.output_combo)
self.output_combo.addItems([_('Mainland'), _('Hong Kong'), _('Taiwan')])
self.output_combo.setToolTip(_('Select the desired region of the output'))
self.output_combo.currentIndexChanged.connect(self.update_gui)
self.use_target_phrases = QCheckBox(_('Use output target phrases if possible'))
self.use_target_phrases.setToolTip(_('Check to allow region specific word replacements if available'))
style_group_box_layout.addWidget(self.use_target_phrases)
self.use_target_phrases.stateChanged.connect(self.update_gui)
self.quotation_group_box = QGroupBox(_('Quotation Marks'))
widgetLayout.addWidget(self.quotation_group_box)
quotation_group_box_layout = QVBoxLayout()
self.quotation_group_box.setLayout(quotation_group_box_layout)
quotation_group=QButtonGroup(self)
self.quotation_no_conversion_button = QRadioButton(_('No Conversion'))
quotation_group.addButton(self.quotation_no_conversion_button)
self.quotation_trad_to_simp_button = QRadioButton(self.quote_for_simp_target)
quotation_group.addButton(self.quotation_trad_to_simp_button)
self.quotation_simp_to_trad_button = QRadioButton(self.quote_for_trad_target)
quotation_group.addButton(self.quotation_simp_to_trad_button)
quotation_group_box_layout.addWidget(self.quotation_no_conversion_button)
quotation_group_box_layout.addWidget(self.quotation_simp_to_trad_button)
quotation_group_box_layout.addWidget(self.quotation_trad_to_simp_button)
self.quotation_no_conversion_button.toggled.connect(self.update_gui)
self.quotation_trad_to_simp_button.toggled.connect(self.update_gui)
self.quotation_simp_to_trad_button.toggled.connect(self.update_gui)
self.use_smart_quotes = QCheckBox("""Use curved 'Smart" quotes if applicable""")
self.use_smart_quotes.setToolTip(_('Use smart curved half-width quotes rather than straight full-width quotes'))
quotation_group_box_layout.addWidget(self.use_smart_quotes)
self.use_smart_quotes.stateChanged.connect(self.update_gui)
self.other_group_box = QGroupBox(_('Other Changes'))
widgetLayout.addWidget(self.other_group_box)
other_group_box_layout = QVBoxLayout()
self.other_group_box.setLayout(other_group_box_layout)
text_dir_layout = QHBoxLayout()
other_group_box_layout.addLayout(text_dir_layout)
direction_label = QLabel(_('Text Direction:'))
text_dir_layout.addWidget(direction_label)
self.text_dir_combo = QComboBox()
text_dir_layout.addWidget(self.text_dir_combo)
self.text_dir_combo.addItems([_('No Conversion'), _('Horizontal'), _('Vertical')])
self.text_dir_combo.setToolTip(_('Select the desired text orientation'))
self.text_dir_combo.currentIndexChanged.connect(self.update_gui)
self.optimization_group_box = QGroupBox(_('Reader Device Optimization'))
other_group_box_layout.addWidget(self.optimization_group_box)
optimization_group_box_layout = QVBoxLayout()
self.optimization_group_box.setLayout(optimization_group_box_layout)
punc_group=QButtonGroup(self)
self.text_dir_punc_none_button = QRadioButton("""No presentation optimization""")
optimization_group_box_layout.addWidget(self.text_dir_punc_none_button)
self.text_dir_punc_button = QRadioButton("""Optimize presentation for Readium reader""")
self.text_dir_punc_button.setToolTip(_('Use vert/horiz punctuation presentation forms for Chrome Readium Epub3 reader'))
optimization_group_box_layout.addWidget(self.text_dir_punc_button)
self.text_dir_punc_kindle_button = QRadioButton("""Optimize presentation for Kindle reader""")
self.text_dir_punc_kindle_button.setToolTip(_('Use vert/horiz puncuation presentation forms for Kindle reader'))
optimization_group_box_layout.addWidget(self.text_dir_punc_kindle_button)
self.text_dir_punc_none_button.toggled.connect(self.update_gui)
self.text_dir_punc_button.toggled.connect(self.update_gui)
self.text_dir_punc_kindle_button.toggled.connect(self.update_gui)
source_group=QButtonGroup(self)
self.file_source_button = QRadioButton(_('Selected File Only'))
self.book_source_button = QRadioButton(_('Entire eBook'))
source_group.addButton(self.file_source_button)
source_group.addButton(self.book_source_button)
self.source_group_box = QGroupBox(_('Source'))
if not self.force_entire_book:
widgetLayout.addWidget(self.source_group_box)
source_group_box_layout = QVBoxLayout()
self.source_group_box.setLayout(source_group_box_layout)
source_group_box_layout.addWidget(self.file_source_button)
source_group_box_layout.addWidget(self.book_source_button)
layout.addSpacing(10)
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.button_box.accepted.connect(self._ok_clicked)
self.button_box.rejected.connect(self.reject)
layout.addWidget(self.button_box)
self.input_combo.setCurrentIndex(self.prefs['input_format'])
self.output_combo.setCurrentIndex(self.prefs['output_format'])
self.no_conversion_button.setChecked(self.prefs['no_conversion'])
self.trad_to_simp_button.setChecked(self.prefs['trad_to_simp'])
self.simp_to_trad_button.setChecked(self.prefs['simp_to_trad'])
self.trad_to_trad_button.setChecked(self.prefs['trad_to_trad'])
if not self.force_entire_book:
self.file_source_button.setChecked(self.prefs['use_html_file'])
self.book_source_button.setChecked(self.prefs['use_entire_book'])
else:
self.file_source_button.setChecked(False)
self.book_source_button.setChecked(True)
self.quotation_no_conversion_button.setChecked(self.prefs['quote_no_conversion'])
self.quotation_trad_to_simp_button.setChecked(self.prefs['quote_trad_to_simp'])
self.quotation_simp_to_trad_button.setChecked(self.prefs['quote_simp_to_trad'])
self.use_smart_quotes.setChecked(self.prefs['use_smart_quotes'])
self.text_dir_combo.setCurrentIndex(self.prefs['orientation'])
self.text_dir_punc_none_button.setChecked(self.prefs['no_optimization'])
self.text_dir_punc_button.setChecked(self.prefs['readium_optimization'])
self.text_dir_punc_kindle_button.setChecked(self.prefs['kindle_optimization'])
self.update_gui()
def update_gui(self):
if (self.quotation_trad_to_simp_button.isChecked()):
self.use_smart_quotes.setEnabled(True)
else:
self.use_smart_quotes.setEnabled(False)
if self.text_dir_combo.currentIndex() == 0:
self.optimization_group_box.setEnabled(False)
self.text_dir_punc_none_button.setEnabled(False)
self.text_dir_punc_button.setEnabled(False)
self.text_dir_punc_kindle_button.setEnabled(False)
else:
self.optimization_group_box.setEnabled(True)
self.text_dir_punc_none_button.setEnabled(True)
self.text_dir_punc_button.setEnabled(True)
self.text_dir_punc_kindle_button.setEnabled(True)
if self.no_conversion_button.isChecked():
self.input_combo.setEnabled(False)
self.output_combo.setEnabled(False)
self.use_target_phrases.setEnabled(False)
self.output_region_label.setEnabled(False)
self.input_region_label.setEnabled(False)
self.style_group_box.setEnabled(False)
elif self.trad_to_simp_button.isChecked():
self.input_combo.setEnabled(True)
#only mainland output locale for simplified output
self.output_combo.setCurrentIndex(0)
self.output_combo.setEnabled(False)
self.use_target_phrases.setEnabled(True)
self.output_region_label.setEnabled(False)
self.input_region_label.setEnabled(True)
self.style_group_box.setEnabled(True)
elif self.simp_to_trad_button.isChecked():
#only mainland input locale for simplified input
self.input_combo.setCurrentIndex(0)
self.input_combo.setEnabled(False)
self.output_combo.setEnabled(True)
self.use_target_phrases.setEnabled(True)
self.output_region_label.setEnabled(True)
self.input_region_label.setEnabled(False)
self.style_group_box.setEnabled(True)
elif self.trad_to_trad_button.isChecked():
#Trad->Trad
#currently only mainland input locale for Trad->Trad
self.input_combo.setCurrentIndex(0)
self.input_combo.setEnabled(False)
self.output_combo.setEnabled(True)
self.use_target_phrases.setEnabled(True)
self.output_region_label.setEnabled(True)
self.input_region_label.setEnabled(False)
self.style_group_box.setEnabled(True)
else:
self.input_combo.setEnabled(True)
self.output_combo.setEnabled(True)
self.use_target_phrases.setEnabled(True)
self.style_group_box.setEnabled(True)
self.output_region_label.setEnabled(True)
self.input_region_label.setEnabled(True)
def _ok_clicked(self):
output_mode = 0
if self.trad_to_simp_button.isChecked():
output_mode = 1 #trad -> simp
if self.simp_to_trad_button.isChecked():
output_mode = 2 #simp -> trad
elif self.trad_to_trad_button.isChecked():
output_mode = 3 #trad -> trad
quote_mode = 0
if self.quotation_trad_to_simp_button.isChecked():
quote_mode = 1 #trad -> simp
if self.quotation_simp_to_trad_button.isChecked():
quote_mode = 2 #simp -> trad
optimization_mode = 0
if self.text_dir_punc_button.isChecked():
optimization_mode = 1 #Readium
if self.text_dir_punc_kindle_button.isChecked():
optimization_mode = 2 #Kindle
self.criteria = (
self.file_source_button.isChecked(), output_mode, self.input_combo.currentIndex(),
self.output_combo.currentIndex(), self.use_target_phrases.isChecked(), quote_mode,
self.use_smart_quotes.isChecked(), self.text_dir_combo.currentIndex(), optimization_mode)
self.savePrefs()
self.accept()
def getCriteria(self):
return self.criteria
def prefsPrep(self):
from calibre.utils.config import JSONConfig
plugin_prefs = JSONConfig('plugins/{0}_ChineseConversion_settings'.format(PLUGIN_SAFE_NAME))
plugin_prefs.defaults['input_format'] = 0
plugin_prefs.defaults['output_format'] = 0
plugin_prefs.defaults['no_conversion'] = True
plugin_prefs.defaults['trad_to_simp'] = False
plugin_prefs.defaults['use_html_file'] = True
plugin_prefs.defaults['simp_to_trad'] = False
plugin_prefs.defaults['trad_to_trad'] = False
plugin_prefs.defaults['use_entire_book'] = True
plugin_prefs.defaults['use_target_phrases'] = True
plugin_prefs.defaults['quote_no_conversion'] = True
plugin_prefs.defaults['quote_trad_to_simp'] = False
plugin_prefs.defaults['quote_simp_to_trad'] = False
plugin_prefs.defaults['use_smart_quotes'] = False
plugin_prefs.defaults['orientation'] = 0
plugin_prefs.defaults['no_optimization'] = True
plugin_prefs.defaults['readium_optimization'] = False
plugin_prefs.defaults['kindle_optimization'] = False
return plugin_prefs
def savePrefs(self):
self.prefs['input_format'] = self.input_combo.currentIndex()
self.prefs['output_format'] = self.output_combo.currentIndex()
self.prefs['no_conversion'] = self.no_conversion_button.isChecked()
self.prefs['trad_to_simp'] = self.trad_to_simp_button.isChecked()
self.prefs['use_html_file'] = self.file_source_button.isChecked()
self.prefs['simp_to_trad'] = self.simp_to_trad_button.isChecked()
self.prefs['trad_to_trad'] = self.trad_to_trad_button.isChecked()
self.prefs['use_entire_book'] = self.book_source_button.isChecked()
self.prefs['use_target_phrases'] = self.use_target_phrases.isChecked()
self.prefs['quote_no_conversion'] = self.quotation_no_conversion_button.isChecked()
self.prefs['quote_trad_to_simp'] = self.quotation_trad_to_simp_button.isChecked()
self.prefs['quote_simp_to_trad'] = self.quotation_simp_to_trad_button.isChecked()
self.prefs['use_smart_quotes'] = self.use_smart_quotes.isChecked()
self.prefs['orientation'] = self.text_dir_combo.currentIndex()
self.prefs['no_optimization'] = self.text_dir_punc_none_button.isChecked()
self.prefs['readium_optimization'] = self.text_dir_punc_button.isChecked()
self.prefs['kindle_optimization'] = self.text_dir_punc_kindle_button.isChecked()