mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Edit Book: Fix Table of Contents editor not remembering its saved state across calibre sessions
This commit is contained in:
parent
4e4d50eff6
commit
57fdd09610
@ -17,7 +17,7 @@ from PyQt5.QtWebKitWidgets import QWebView, QWebPage
|
||||
from PyQt5.QtWebKit import QWebElement
|
||||
|
||||
from calibre.ebooks.oeb.display.webview import load_html
|
||||
from calibre.gui2 import error_dialog, question_dialog, gprefs
|
||||
from calibre.gui2 import error_dialog, question_dialog
|
||||
from calibre.utils.logging import default_log
|
||||
|
||||
class Page(QWebPage): # {{{
|
||||
@ -97,8 +97,9 @@ class WebView(QWebView): # {{{
|
||||
|
||||
class ItemEdit(QWidget):
|
||||
|
||||
def __init__(self, parent):
|
||||
def __init__(self, parent, prefs):
|
||||
QWidget.__init__(self, parent)
|
||||
self.prefs = prefs
|
||||
self.setLayout(QVBoxLayout())
|
||||
|
||||
self.la = la = QLabel('<b>'+_(
|
||||
@ -168,7 +169,7 @@ class ItemEdit(QWidget):
|
||||
|
||||
l.addStretch()
|
||||
|
||||
state = gprefs.get('toc_edit_splitter_state', None)
|
||||
state = self.prefs.get('toc_edit_splitter_state', None)
|
||||
if state is not None:
|
||||
sp.restoreState(state)
|
||||
|
||||
|
@ -29,8 +29,9 @@ ICON_SIZE = 24
|
||||
|
||||
class XPathDialog(QDialog): # {{{
|
||||
|
||||
def __init__(self, parent):
|
||||
def __init__(self, parent, prefs):
|
||||
QDialog.__init__(self, parent)
|
||||
self.prefs = prefs
|
||||
self.setWindowTitle(_('Create ToC from XPath'))
|
||||
self.l = l = QVBoxLayout()
|
||||
self.setLayout(l)
|
||||
@ -73,13 +74,13 @@ class XPathDialog(QDialog): # {{{
|
||||
if ok:
|
||||
name = unicode(name).strip()
|
||||
if name:
|
||||
saved = gprefs.get('xpath_toc_settings', {})
|
||||
saved = self.prefs.get('xpath_toc_settings', {})
|
||||
saved[name] = {i:x for i, x in enumerate(xpaths)}
|
||||
gprefs.set('xpath_toc_settings', saved)
|
||||
self.prefs.set('xpath_toc_settings', saved)
|
||||
self.setup_load_button()
|
||||
|
||||
def setup_load_button(self):
|
||||
saved = gprefs.get('xpath_toc_settings', {})
|
||||
saved = self.prefs.get('xpath_toc_settings', {})
|
||||
m = self.load_menu
|
||||
m.clear()
|
||||
self.__actions = []
|
||||
@ -91,11 +92,11 @@ class XPathDialog(QDialog): # {{{
|
||||
self.load_button.setEnabled(bool(saved))
|
||||
|
||||
def clear_settings(self):
|
||||
gprefs.set('xpath_toc_settings', {})
|
||||
self.prefs.set('xpath_toc_settings', {})
|
||||
self.setup_load_button()
|
||||
|
||||
def load_settings(self, name):
|
||||
saved = gprefs.get('xpath_toc_settings', {}).get(name, {})
|
||||
saved = self.prefs.get('xpath_toc_settings', {}).get(name, {})
|
||||
for i, w in enumerate(self.widgets):
|
||||
txt = saved.get(i, '')
|
||||
w.edit.setText(txt)
|
||||
@ -129,8 +130,9 @@ class ItemView(QFrame): # {{{
|
||||
create_from_files = pyqtSignal()
|
||||
flatten_toc = pyqtSignal()
|
||||
|
||||
def __init__(self, parent):
|
||||
def __init__(self, parent, prefs):
|
||||
QFrame.__init__(self, parent)
|
||||
self.prefs = prefs
|
||||
self.setFrameShape(QFrame.StyledPanel)
|
||||
self.setMinimumWidth(250)
|
||||
self.stack = s = QStackedWidget(self)
|
||||
@ -303,7 +305,7 @@ class ItemView(QFrame): # {{{
|
||||
self.create_from_xpath.emit(['//h:h%d'%i for i in xrange(1, 7)])
|
||||
|
||||
def create_from_user_xpath(self):
|
||||
d = XPathDialog(self)
|
||||
d = XPathDialog(self, self.prefs)
|
||||
if d.exec_() == d.Accepted and d.xpaths:
|
||||
self.create_from_xpath.emit(d.xpaths)
|
||||
|
||||
@ -564,8 +566,9 @@ class TOCView(QWidget): # {{{
|
||||
|
||||
add_new_item = pyqtSignal(object, object)
|
||||
|
||||
def __init__(self, parent):
|
||||
def __init__(self, parent, prefs):
|
||||
QWidget.__init__(self, parent)
|
||||
self.prefs = prefs
|
||||
l = self.l = QGridLayout()
|
||||
self.setLayout(l)
|
||||
self.tocw = t = TreeWidget(self)
|
||||
@ -615,7 +618,7 @@ class TOCView(QWidget): # {{{
|
||||
self.default_msg = _('Double click on an entry to change the text')
|
||||
self.hl = hl = QLabel(self.default_msg)
|
||||
l.addWidget(hl, col, 2, 1, -1)
|
||||
self.item_view = i = ItemView(self)
|
||||
self.item_view = i = ItemView(self, self.prefs)
|
||||
self.item_view.delete_item.connect(self.delete_current_item)
|
||||
i.add_new_item.connect(self.add_new_item)
|
||||
i.create_from_xpath.connect(self.create_from_xpath)
|
||||
@ -840,8 +843,9 @@ class TOCEditor(QDialog): # {{{
|
||||
explode_done = pyqtSignal(object)
|
||||
writing_done = pyqtSignal(object)
|
||||
|
||||
def __init__(self, pathtobook, title=None, parent=None):
|
||||
def __init__(self, pathtobook, title=None, parent=None, prefs=None):
|
||||
QDialog.__init__(self, parent)
|
||||
self.prefs = prefs or gprefs
|
||||
self.pathtobook = pathtobook
|
||||
self.working = True
|
||||
|
||||
@ -867,7 +871,7 @@ class TOCEditor(QDialog): # {{{
|
||||
la.setWordWrap(True)
|
||||
la.setStyleSheet('QLabel { font-size: 20pt }')
|
||||
ll.addWidget(la, alignment=Qt.AlignHCenter|Qt.AlignTop)
|
||||
self.toc_view = TOCView(self)
|
||||
self.toc_view = TOCView(self, self.prefs)
|
||||
self.toc_view.add_new_item.connect(self.add_new_item)
|
||||
s.addWidget(self.toc_view)
|
||||
self.item_edit = ItemEdit(self)
|
||||
@ -882,7 +886,7 @@ class TOCEditor(QDialog): # {{{
|
||||
self.writing_done.connect(self.really_accept, type=Qt.QueuedConnection)
|
||||
|
||||
self.resize(950, 630)
|
||||
geom = gprefs.get('toc_editor_window_geom', None)
|
||||
geom = self.prefs.get('toc_editor_window_geom', None)
|
||||
if geom is not None:
|
||||
self.restoreGeometry(bytes(geom))
|
||||
|
||||
@ -893,7 +897,7 @@ class TOCEditor(QDialog): # {{{
|
||||
def accept(self):
|
||||
if self.stacks.currentIndex() == 2:
|
||||
self.toc_view.update_item(*self.item_edit.result)
|
||||
gprefs['toc_edit_splitter_state'] = bytearray(self.item_edit.splitter.saveState())
|
||||
self.prefs['toc_edit_splitter_state'] = bytearray(self.item_edit.splitter.saveState())
|
||||
self.stacks.setCurrentIndex(1)
|
||||
elif self.stacks.currentIndex() == 1:
|
||||
self.working = False
|
||||
@ -905,12 +909,12 @@ class TOCEditor(QDialog): # {{{
|
||||
self.bb.setEnabled(False)
|
||||
|
||||
def really_accept(self, tb):
|
||||
gprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
self.prefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
if tb:
|
||||
error_dialog(self, _('Failed to write book'),
|
||||
_('Could not write %s. Click "Show details" for'
|
||||
' more information.')%self.book_title, det_msg=tb, show=True)
|
||||
gprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
self.prefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
super(TOCEditor, self).reject()
|
||||
return
|
||||
|
||||
@ -920,11 +924,11 @@ class TOCEditor(QDialog): # {{{
|
||||
if not self.bb.isEnabled():
|
||||
return
|
||||
if self.stacks.currentIndex() == 2:
|
||||
gprefs['toc_edit_splitter_state'] = bytearray(self.item_edit.splitter.saveState())
|
||||
self.prefs['toc_edit_splitter_state'] = bytearray(self.item_edit.splitter.saveState())
|
||||
self.stacks.setCurrentIndex(1)
|
||||
else:
|
||||
self.working = False
|
||||
gprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
self.prefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
super(TOCEditor, self).reject()
|
||||
|
||||
def start(self):
|
||||
|
@ -13,9 +13,9 @@ from PyQt5.Qt import (
|
||||
|
||||
from calibre.constants import plugins
|
||||
from calibre.ebooks.oeb.polish.toc import commit_toc, get_toc
|
||||
from calibre.gui2 import gprefs, error_dialog
|
||||
from calibre.gui2 import error_dialog
|
||||
from calibre.gui2.toc.main import TOCView, ItemEdit
|
||||
from calibre.gui2.tweak_book import current_container, TOP, actions
|
||||
from calibre.gui2.tweak_book import current_container, TOP, actions, tprefs
|
||||
|
||||
class TOCEditor(QDialog):
|
||||
|
||||
@ -35,10 +35,10 @@ class TOCEditor(QDialog):
|
||||
|
||||
self.stacks = s = QStackedWidget(self)
|
||||
l.addWidget(s)
|
||||
self.toc_view = TOCView(self)
|
||||
self.toc_view = TOCView(self, tprefs)
|
||||
self.toc_view.add_new_item.connect(self.add_new_item)
|
||||
s.addWidget(self.toc_view)
|
||||
self.item_edit = ItemEdit(self)
|
||||
self.item_edit = ItemEdit(self, tprefs)
|
||||
s.addWidget(self.item_edit)
|
||||
|
||||
bb = self.bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
|
||||
@ -49,7 +49,7 @@ class TOCEditor(QDialog):
|
||||
self.read_toc()
|
||||
|
||||
self.resize(950, 630)
|
||||
geom = gprefs.get('toc_editor_window_geom', None)
|
||||
geom = tprefs.get('toc_editor_window_geom', None)
|
||||
if geom is not None:
|
||||
self.restoreGeometry(bytes(geom))
|
||||
|
||||
@ -60,19 +60,19 @@ class TOCEditor(QDialog):
|
||||
def accept(self):
|
||||
if self.stacks.currentIndex() == 1:
|
||||
self.toc_view.update_item(*self.item_edit.result)
|
||||
gprefs['toc_edit_splitter_state'] = bytearray(self.item_edit.splitter.saveState())
|
||||
tprefs['toc_edit_splitter_state'] = bytearray(self.item_edit.splitter.saveState())
|
||||
self.stacks.setCurrentIndex(0)
|
||||
elif self.stacks.currentIndex() == 0:
|
||||
self.write_toc()
|
||||
super(TOCEditor, self).accept()
|
||||
|
||||
def really_accept(self, tb):
|
||||
gprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
tprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
if tb:
|
||||
error_dialog(self, _('Failed to write book'),
|
||||
_('Could not write %s. Click "Show details" for'
|
||||
' more information.')%self.book_title, det_msg=tb, show=True)
|
||||
gprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
tprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
super(TOCEditor, self).reject()
|
||||
return
|
||||
|
||||
@ -82,10 +82,10 @@ class TOCEditor(QDialog):
|
||||
if not self.bb.isEnabled():
|
||||
return
|
||||
if self.stacks.currentIndex() == 1:
|
||||
gprefs['toc_edit_splitter_state'] = bytearray(self.item_edit.splitter.saveState())
|
||||
tprefs['toc_edit_splitter_state'] = bytearray(self.item_edit.splitter.saveState())
|
||||
self.stacks.setCurrentIndex(0)
|
||||
else:
|
||||
gprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
tprefs['toc_editor_window_geom'] = bytearray(self.saveGeometry())
|
||||
super(TOCEditor, self).reject()
|
||||
|
||||
def read_toc(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user