mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
GUI, Search and Replace: Cache the conversion document used in the wizard so we don't have to run the it multiple times as the user sets sr 1, 2 and 3.
This commit is contained in:
parent
156dc57e99
commit
66fdb25b28
@ -6,7 +6,7 @@ __docformat__ = 'restructuredtext en'
|
||||
|
||||
import re
|
||||
|
||||
from PyQt4.QtCore import SIGNAL, Qt
|
||||
from PyQt4.QtCore import SIGNAL, Qt, pyqtSignal
|
||||
from PyQt4.QtGui import QDialog, QWidget, QDialogButtonBox, \
|
||||
QBrush, QTextCursor, QTextEdit
|
||||
|
||||
@ -19,8 +19,8 @@ from calibre.gui2.dialogs.choose_format import ChooseFormatDialog
|
||||
|
||||
class RegexBuilder(QDialog, Ui_RegexBuilder):
|
||||
|
||||
def __init__(self, db, book_id, regex, *args):
|
||||
QDialog.__init__(self, *args)
|
||||
def __init__(self, db, book_id, regex, doc=None, parent=None):
|
||||
QDialog.__init__(self, parent)
|
||||
self.setupUi(self)
|
||||
|
||||
self.regex.setText(regex)
|
||||
@ -28,9 +28,13 @@ class RegexBuilder(QDialog, Ui_RegexBuilder):
|
||||
|
||||
if not db or not book_id:
|
||||
self.button_box.addButton(QDialogButtonBox.Open)
|
||||
elif not self.select_format(db, book_id):
|
||||
elif not doc and not self.select_format(db, book_id):
|
||||
self.cancelled = True
|
||||
return
|
||||
|
||||
if doc:
|
||||
self.preview.setPlainText(doc)
|
||||
|
||||
self.cancelled = False
|
||||
self.connect(self.button_box, SIGNAL('clicked(QAbstractButton*)'), self.button_clicked)
|
||||
self.connect(self.regex, SIGNAL('textChanged(QString)'), self.regex_valid)
|
||||
@ -152,25 +156,37 @@ class RegexBuilder(QDialog, Ui_RegexBuilder):
|
||||
self.open_book(files[0])
|
||||
if button == self.button_box.button(QDialogButtonBox.Ok):
|
||||
self.accept()
|
||||
|
||||
def doc(self):
|
||||
return unicode(self.preview.toPlainText())
|
||||
|
||||
class RegexEdit(QWidget, Ui_Edit):
|
||||
|
||||
doc_update = pyqtSignal(unicode)
|
||||
|
||||
def __init__(self, parent=None):
|
||||
QWidget.__init__(self, parent)
|
||||
self.setupUi(self)
|
||||
|
||||
self.book_id = None
|
||||
self.db = None
|
||||
self.doc_cache = None
|
||||
|
||||
self.connect(self.button, SIGNAL('clicked()'), self.builder)
|
||||
|
||||
def builder(self):
|
||||
bld = RegexBuilder(self.db, self.book_id, self.edit.text(), self)
|
||||
bld = RegexBuilder(self.db, self.book_id, self.edit.text(), self.doc_cache, self)
|
||||
if bld.cancelled:
|
||||
return
|
||||
if not self.doc_cache:
|
||||
self.doc_cache = bld.doc()
|
||||
self.doc_update.emit(self.doc_cache)
|
||||
if bld.exec_() == bld.Accepted:
|
||||
self.edit.setText(bld.regex.text())
|
||||
|
||||
def doc(self):
|
||||
return self.doc_cache
|
||||
|
||||
def setObjectName(self, *args):
|
||||
QWidget.setObjectName(self, *args)
|
||||
if hasattr(self, 'edit'):
|
||||
@ -184,6 +200,9 @@ class RegexEdit(QWidget, Ui_Edit):
|
||||
|
||||
def set_db(self, db):
|
||||
self.db = db
|
||||
|
||||
def set_doc(self, doc):
|
||||
self.doc_cache = doc
|
||||
|
||||
def break_cycles(self):
|
||||
self.db = None
|
||||
|
@ -34,14 +34,27 @@ class SearchAndReplaceWidget(Widget, Ui_Form):
|
||||
self.opt_sr3_search.set_msg(_('&Search Regular Expression'))
|
||||
self.opt_sr3_search.set_book_id(book_id)
|
||||
self.opt_sr3_search.set_db(db)
|
||||
|
||||
self.opt_sr1_search.doc_update.connect(self.update_doc)
|
||||
self.opt_sr2_search.doc_update.connect(self.update_doc)
|
||||
self.opt_sr3_search.doc_update.connect(self.update_doc)
|
||||
|
||||
def break_cycles(self):
|
||||
Widget.break_cycles(self)
|
||||
|
||||
self.opt_sr1_search.doc_update.disconnect()
|
||||
self.opt_sr2_search.doc_update.disconnect()
|
||||
self.opt_sr3_search.doc_update.disconnect()
|
||||
|
||||
self.opt_sr1_search.break_cycles()
|
||||
self.opt_sr2_search.break_cycles()
|
||||
self.opt_sr3_search.break_cycles()
|
||||
|
||||
def update_doc(self, doc):
|
||||
self.opt_sr1_search.set_doc(doc)
|
||||
self.opt_sr2_search.set_doc(doc)
|
||||
self.opt_sr3_search.set_doc(doc)
|
||||
|
||||
def pre_commit_check(self):
|
||||
for x in ('sr1_search', 'sr2_search', 'sr3_search'):
|
||||
x = getattr(self, 'opt_'+x)
|
||||
|
Loading…
x
Reference in New Issue
Block a user