forked from JimmXinu/EpubSplit
/
dialogs.py
361 lines (293 loc) · 13.2 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
355
356
357
358
359
360
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import (unicode_literals, division,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2015, Jim Miller'
__docformat__ = 'restructuredtext en'
import traceback
from functools import partial
import logging
logger = logging.getLogger(__name__)
from datetime import datetime
try:
from PyQt5 import QtWidgets as QtGui
from PyQt5.Qt import (QTableWidget, QVBoxLayout, QHBoxLayout, QProgressDialog, QTimer,
QDialogButtonBox, Qt, QAbstractItemView, QTableWidgetItem, QTextBrowser)
except ImportError as e:
from PyQt4 import QtGui
from PyQt4.Qt import (QTableWidget, QVBoxLayout, QHBoxLayout, QProgressDialog, QTimer,
QDialogButtonBox, Qt, QAbstractItemView, QTableWidgetItem, QTextBrowser)
try:
from calibre.gui2 import QVariant
del QVariant
except ImportError:
is_qt4 = False
convert_qvariant = lambda x: x
else:
is_qt4 = True
def convert_qvariant(x):
vt = x.type()
if vt == x.String:
return unicode(x.toString())
if vt == x.List:
return [convert_qvariant(i) for i in x.toList()]
return x.toPyObject()
from calibre.gui2 import error_dialog, warning_dialog, question_dialog, info_dialog
from calibre.gui2.dialogs.confirm_delete import confirm
from calibre.ebooks.metadata import fmt_sidx
from calibre import confirm_config_name
from calibre.gui2 import dynamic
# pulls in translation files for _() strings
try:
load_translations()
except NameError:
pass # load_translations() added in calibre 1.9
from calibre_plugins.epubsplit.common_utils \
import (ReadOnlyTableWidgetItem, SizePersistedDialog,
ImageTitleLayout, get_icon)
SAMPLE_NOTE=_("<p><b><i>Double click to copy from sample.</i></b></p>")
class SelectLinesDialog(SizePersistedDialog):
def __init__(self, gui, header, prefs, icon, lines,
do_split_fn,
do_splits_fn,
get_split_size_fn,
save_size_name='epubsplit:update list dialog'):
SizePersistedDialog.__init__(self, gui, save_size_name)
self.gui = gui
self.do_split_fn = do_split_fn
self.do_splits_fn = do_splits_fn
self.get_split_size_fn = get_split_size_fn
self.setWindowTitle(header)
self.setWindowIcon(icon)
layout = QVBoxLayout(self)
self.setLayout(layout)
title_layout = ImageTitleLayout(self, 'images/icon.png',
header)
layout.addLayout(title_layout)
lines_layout = QHBoxLayout()
layout.addLayout(lines_layout)
self.lines_table = LinesTableWidget(self)
lines_layout.addWidget(self.lines_table)
options_layout = QHBoxLayout()
button_box = QDialogButtonBox(self)
new_book = button_box.addButton(_("New Book"), button_box.ActionRole)
new_book.setToolTip(_("Make <i>one</i> new book containing the sections selected above and then edit its Metadata."))
new_book.clicked.connect(self.new_book)
new_books = button_box.addButton(_("New Book per Section"), button_box.ActionRole)
new_books.setToolTip(_("Make a new book for <i>each</i> of the sections selected above. Title for each will be the Table of Contents, which you can edit here first."))
new_books.clicked.connect(self.new_books)
get_split_size = button_box.addButton(_("Get Size"), button_box.ActionRole)
get_split_size.setToolTip("<i></i>" + _("Calculate the size of the new book from the currently selected sections."))
get_split_size.clicked.connect(self.get_split_size)
button_box.addButton(_("Done"), button_box.RejectRole)
button_box.rejected.connect(self.reject)
options_layout.addWidget(button_box)
layout.addLayout(options_layout)
# Cause our dialog size to be restored from prefs or created on first usage
self.resize_dialog()
self.lines_table.populate_table(lines)
def get_split_size(self):
self.get_split_size_fn(self.get_selected_linenums_tocs())
def new_book(self):
self.do_split_fn(self.get_selected_linenums_tocs())
def new_books(self):
if not self.lines_table.selected_all_have_toc():
error_dialog(self.gui,
_('Missing Title(s)'),
_("Books not split.\n\nSome selected sections don't have a Table of Contents text. You must enter one before splitting sections to individual books. Double click to edit the Table of Contents entry for a section."),
show_copy_button=False).exec_()
else:
self.do_splits_fn(self.get_selected_linenums_tocs())
def get_selected_linenums_tocs(self):
return self.lines_table.get_selected_linenums_tocs()
class LinesTableWidget(QTableWidget):
def __init__(self, parent):
QTableWidget.__init__(self, parent)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
def populate_table(self, lines):
self.clear()
self.setAlternatingRowColors(True)
self.setRowCount(len(lines))
header_labels = [_('HREF'), _('Guide'), _('Table of Contents')] #, 'extra'
self.setColumnCount(len(header_labels))
self.setHorizontalHeaderLabels(header_labels)
self.horizontalHeader().setStretchLastSection(True)
self.verticalHeader().hide()
self.lines={}
for row, line in enumerate(lines):
self.populate_table_row(row, line)
self.lines[row] = line
# can't connect to individual cells, it seems.
self.doubleClicked.connect(self.show_tooltip)
self.resizeColumnsToContents()
self.setMinimumColumnWidth(1, 100)
self.setMinimumColumnWidth(2, 10)
self.setMinimumColumnWidth(3, 100)
self.setMinimumSize(300, 0)
def setMinimumColumnWidth(self, col, minimum):
if self.columnWidth(col) < minimum:
self.setColumnWidth(col, minimum)
def populate_table_row(self, row, line):
href = line['href']
if line['anchor']:
href = "%s#%s"%(href,line['anchor'])
href_cell = ReadOnlyTableWidgetItem(href)
href_cell.setToolTip(line['sample']+SAMPLE_NOTE)
href_cell.setData(Qt.UserRole, line['num'])
self.setItem(row, 0, href_cell)
if 'guide' in line:
guide = "(%s):%s"%line['guide']
else:
guide = ""
guide_cell = ReadOnlyTableWidgetItem(guide)
guide_cell.setToolTip(_("Indicates 'special' pages: copyright, titlepage, etc."))
self.setItem(row, 1, guide_cell)
toc_str = "|".join(line['toc'])
toc_cell = QTableWidgetItem(toc_str)
toc_cell.setData(Qt.UserRole, toc_str)
toc_cell.setToolTip(_('''Click and copy hotkey to copy text.
Double-click to edit ToC entry.
Pipes(|) divide different ToC entries to the same place.'''))
self.setItem(row, 2, toc_cell)
def selected_all_have_toc(self):
"Return false if any of the sections would not have a title when doing split all."
for row in self.selectionModel().selectedRows():
linenum = convert_qvariant(row.data(Qt.UserRole))
pre = convert_qvariant(self.item(row.row(),2).data(Qt.UserRole)).strip()
changed = self.item(row.row(),2).text().strip()
if not changed or ( (pre == changed) and not pre ):
return False
return True
def get_selected_linenums_tocs(self):
# lines = []
# for row in range(self.rowCount()):
# rnum = self.item(row, 0).data(Qt.UserRole).toPyObject()
# line = self.lines[rnum]
# lines.append(line)
# return lines
linenums = []
changedtocs = {}
for row in self.selectionModel().selectedRows():
linenum = convert_qvariant(row.data(Qt.UserRole))
linenums.append(linenum)
# changed tocs only.
if convert_qvariant(self.item(row.row(),2).data(Qt.UserRole)) != self.item(row.row(),2).text():
changedtocs[linenum] = unicode(self.item(row.row(),2).text()).strip().split('|')
linenums.sort()
return linenums, changedtocs
def show_tooltip(self,modidx):
"Show section sample from tooltip in an info for copying when double clicked."
if modidx.column() == 0: # first column only.
ViewSample(_("Section Sample"),
self.item(modidx.row(),modidx.column()).toolTip().replace(SAMPLE_NOTE,''),
parent=self.parent()).exec_()
def LoopProgressDialog(gui,
split_list,
foreach_function,
finish_function,
init_label=_("Splitting Sections..."),
win_title=_("Splitting Sections..."),
status_prefix=_("Splitting Sections...")):
ld = _LoopProgressDialog(gui,
split_list,
foreach_function,
init_label,
win_title,
status_prefix)
# Mac OS X gets upset if the finish_function is called from inside
# the real _LoopProgressDialog class.
# reflect old behavior.
if not ld.wasCanceled():
finish_function(split_list)
class _LoopProgressDialog(QProgressDialog):
'''
ProgressDialog displayed while splitting each section.
'''
def __init__(self, gui,
split_list,
foreach_function,
init_label=_("Splitting Sections..."),
win_title=_("Splitting Sections..."),
status_prefix=_("Splitting Sections...")):
QProgressDialog.__init__(self,
init_label,
_('Cancel'), 0, len(split_list), gui)
self.setWindowTitle(win_title)
self.setMinimumWidth(500)
self.split_list = split_list
self.foreach_function = foreach_function
self.status_prefix = status_prefix
self.i = 0
self.start_time = datetime.now()
## self.do_loop does QTimer.singleShot on self.do_loop also.
## A weird way to do a loop, but that was the example I had.
QTimer.singleShot(0, self.do_loop)
self.exec_()
def updateStatus(self):
remaining_time_string = ''
if self.i > -1:
time_spent = (datetime.now() - self.start_time).total_seconds()
estimated_remaining = (time_spent/(self.i+1)) * len(self.split_list) - time_spent
remaining_time_string = _(' - %s estimated until done') % ( time_duration_format(estimated_remaining))
self.setLabelText('%s %d / %d%s' % (self.status_prefix, self.i+1, len(self.split_list), remaining_time_string))
self.setValue(self.i+1)
#print(self.labelText())
def do_loop(self):
if self.i == 0:
self.setValue(0)
split = self.split_list[self.i]
self.foreach_function(split)
self.updateStatus()
self.i += 1
if self.i >= len(self.split_list) or self.wasCanceled():
return self.do_when_finished()
else:
QTimer.singleShot(0, self.do_loop)
def do_when_finished(self):
self.hide()
class ViewSample(SizePersistedDialog):
def __init__(self, title, html, parent=None):
SizePersistedDialog.__init__(self, parent, 'epubsplit:view sample')
self.l = l = QVBoxLayout()
self.setLayout(l)
self.tb = QTextBrowser(self)
self.tb.setHtml(html)
l.addWidget(self.tb)
self.bb = QDialogButtonBox(QDialogButtonBox.Ok)
self.bb.accepted.connect(self.accept)
self.bb.rejected.connect(self.reject)
l.addWidget(self.bb)
self.setModal(False)
self.setWindowTitle(title)
self.setWindowIcon(get_icon('format-justify-fill.png'))
# Cause our dialog size to be restored from prefs or created on first usage
self.resize_dialog()
self.show()
def time_duration_format(seconds):
"""
Convert seconds into a string describing the duration in larger time units (seconds, minutes, hours, days)
Only returns the two largest time divisions (eg, will drop seconds if there's hours remaining)
:param seconds: number of seconds
:return: string description of the duration
"""
periods = [
(_('%d day'),_('%d days'), 60*60*24),
(_('%d hour'),_('%d hours'), 60*60),
(_('%d minute'),_('%d minutes'), 60),
(_('%d second'),_('%d seconds'), 1)
]
strings = []
for period_label, period_plural_label, period_seconds in periods:
if seconds > period_seconds:
period_value, seconds = divmod(seconds,period_seconds)
if period_value == 1:
strings.append( period_label % period_value)
else:
strings.append(period_plural_label % period_value)
if len(strings) == 2:
break
if len(strings) == 0:
return _('less than 1 second')
else:
return ', '.join(strings)