From 8f4d60073f982185522bdc20cfbaf0aea0f9de0c Mon Sep 17 00:00:00 2001 From: John Schember Date: Fri, 14 Jan 2011 19:24:31 -0500 Subject: [PATCH] Finish new GUI option widgets. --- src/calibre/ebooks/conversion/cli.py | 16 +++-- src/calibre/gui2/convert/bulk.py | 6 +- src/calibre/gui2/convert/heuristics.py | 68 ++++++++++--------- src/calibre/gui2/convert/heuristics.ui | 47 ++----------- .../gui2/convert/search_and_replace.py | 57 ++++++++++++++++ .../gui2/convert/search_and_replace.ui | 47 +++++++++++++ src/calibre/gui2/convert/single.py | 4 +- .../gui2/convert/structure_detection.ui | 6 +- src/calibre/gui2/preferences/conversion.py | 4 +- 9 files changed, 172 insertions(+), 83 deletions(-) create mode 100644 src/calibre/gui2/convert/search_and_replace.py create mode 100644 src/calibre/gui2/convert/search_and_replace.ui diff --git a/src/calibre/ebooks/conversion/cli.py b/src/calibre/ebooks/conversion/cli.py index f825776c9c..91f0f95348 100644 --- a/src/calibre/ebooks/conversion/cli.py +++ b/src/calibre/ebooks/conversion/cli.py @@ -131,18 +131,24 @@ def add_pipeline_options(parser, plumber): ), 'HEURISTICS' : ( - _('Modify the document text and strucutre using common patterns.'), + _('Modify the document text and structure using common patterns.'), [ 'enable_heuristics', 'markup_chapter_headings', 'italicize_common_cases', 'fix_indents', 'html_unwrap_factor', 'unwrap_lines', 'delete_blank_paragraphs', 'format_scene_breaks', 'dehyphenate', + ] + ), + + 'SEARCH AND REPLACE' : ( + _('Modify the document text and structure using user defined patterns.'), + [ 'sr1_search', 'sr1_replace', 'sr2_search', 'sr2_replace', 'sr3_search', 'sr3_replace', - ] - ), + ] + ), 'STRUCTURE DETECTION' : ( _('Control auto-detection of document structure.'), @@ -177,8 +183,8 @@ def add_pipeline_options(parser, plumber): } group_order = ['', 'LOOK AND FEEL', 'HEURISTICS', - 'STRUCTURE DETECTION', 'TABLE OF CONTENTS', - 'METADATA', 'DEBUG'] + 'SEARCH AND REPLACE' 'STRUCTURE DETECTION', + 'TABLE OF CONTENTS', 'METADATA', 'DEBUG'] for group in group_order: desc, options = groups[group] diff --git a/src/calibre/gui2/convert/bulk.py b/src/calibre/gui2/convert/bulk.py index 198f6144e4..b97ab1a2dc 100644 --- a/src/calibre/gui2/convert/bulk.py +++ b/src/calibre/gui2/convert/bulk.py @@ -11,6 +11,8 @@ from calibre.gui2.convert.single import Config, sort_formats_by_preference, \ from calibre.customize.ui import available_output_formats from calibre.gui2 import ResizableDialog from calibre.gui2.convert.look_and_feel import LookAndFeelWidget +from calibre.gui2.convert.heuristics import HeuristicsWidget +from calibre.gui2.convert.search_and_replace import SearchAndReplaceWidget from calibre.gui2.convert.page_setup import PageSetupWidget from calibre.gui2.convert.structure_detection import StructureDetectionWidget from calibre.gui2.convert.toc import TOCWidget @@ -69,6 +71,8 @@ class BulkConfig(Config): self.setWindowTitle(_('Bulk Convert')) lf = widget_factory(LookAndFeelWidget) + hw = widget_factory(HeuristicsWidget) + sr = widget_factory(SearchAndReplaceWidget) ps = widget_factory(PageSetupWidget) sd = widget_factory(StructureDetectionWidget) toc = widget_factory(TOCWidget) @@ -90,7 +94,7 @@ class BulkConfig(Config): if not c: break self.stack.removeWidget(c) - widgets = [lf, ps, sd, toc] + widgets = [lf, hw, sr, ps, sd, toc] if output_widget is not None: widgets.append(output_widget) for w in widgets: diff --git a/src/calibre/gui2/convert/heuristics.py b/src/calibre/gui2/convert/heuristics.py index 132652701a..2b9df50457 100644 --- a/src/calibre/gui2/convert/heuristics.py +++ b/src/calibre/gui2/convert/heuristics.py @@ -4,16 +4,15 @@ __license__ = 'GPL 3' __copyright__ = '2011, John Schember ' __docformat__ = 'restructuredtext en' -import re +from PyQt4.Qt import Qt from calibre.gui2.convert.heuristics_ui import Ui_Form from calibre.gui2.convert import Widget -from calibre.gui2 import error_dialog class HeuristicsWidget(Widget, Ui_Form): TITLE = _('Heuristics') - HELP = _('') + HELP = _('Modify the document text and structure using common patterns.') COMMIT_NAME = 'heuristics' def __init__(self, parent, get_option, get_help, db=None, book_id=None): @@ -22,41 +21,48 @@ class HeuristicsWidget(Widget, Ui_Form): 'italicize_common_cases', 'fix_indents', 'html_unwrap_factor', 'unwrap_lines', 'delete_blank_paragraphs', 'format_scene_breaks', - 'dehyphenate', - 'sr1_search', 'sr1_replace', - 'sr2_search', 'sr2_replace', - 'sr3_search', 'sr3_replace'] + 'dehyphenate'] ) self.db, self.book_id = db, book_id self.initialize_options(get_option, get_help, db, book_id) - self.opt_sr1_search.set_msg(_('Search regular expression 1:')) - self.opt_sr1_replace.set_msg(_('Replace regular expression 1:')) - self.opt_sr2_search.set_msg(_('Search regular expression 2:')) - self.opt_sr2_replace.set_msg(_('Replace regular expression 2:')) - self.opt_sr3_search.set_msg(_('Search regular expression 3:')) - self.opt_sr3_replace.set_msg(_('Replace regular expression 3:')) + + self.opt_enable_heuristics.stateChanged.connect(self.enable_heuristics) + self.opt_unwrap_lines.stateChanged.connect(self.enable_unwrap) + + self.enable_heuristics(self.opt_enable_heuristics.checkState()) def break_cycles(self): Widget.break_cycles(self) - self.opt_sr1_search.break_cycles() - self.opt_sr1_replace.break_cycles() - self.opt_sr2_search.break_cycles() - self.opt_sr2_replace.break_cycles() - self.opt_sr3_search.break_cycles() - self.opt_sr3_replace.break_cycles() - - def pre_commit_check(self): - for x in ('sr1-search', 'sr1-replace', 'sr2-search', 'sr2-replace', 'sr3-search', 'sr3-replace',): - x = getattr(self, 'opt_'+x) - try: - pat = unicode(x.regex) - re.compile(pat) - except Exception, err: - error_dialog(self, _('Invalid regular expression'), - _('Invalid regular expression: %s')%err).exec_() - return False - + + self.opt_enable_heuristics.stateChanged.disconnect() + self.opt_unwrap_lines.stateChanged.disconnect() + def set_value_handler(self, g, val): if val is None and g is self.opt_html_unwrap_factor: g.setValue(0.0) return True + + def enable_heuristics(self, state): + if state == Qt.Checked: + state = True + else: + state = False + self.opt_markup_chapter_headings.setEnabled(state) + self.opt_italicize_common_cases.setEnabled(state) + self.opt_fix_indents.setEnabled(state) + self.opt_delete_blank_paragraphs.setEnabled(state) + self.opt_format_scene_breaks.setEnabled(state) + self.opt_dehyphenate.setEnabled(state) + + self.opt_unwrap_lines.setEnabled(state) + if state and self.opt_unwrap_lines.checkState() == Qt.Checked: + self.opt_html_unwrap_factor.setEnabled(True) + else: + self.opt_html_unwrap_factor.setEnabled(False) + + def enable_unwrap(self, state): + if state == Qt.Checked: + state = True + else: + state = False + self.opt_html_unwrap_factor.setEnabled(state) diff --git a/src/calibre/gui2/convert/heuristics.ui b/src/calibre/gui2/convert/heuristics.ui index 2c103ff5b6..e64e79e1df 100644 --- a/src/calibre/gui2/convert/heuristics.ui +++ b/src/calibre/gui2/convert/heuristics.ui @@ -76,7 +76,7 @@ - markup_chapter_headings + Detect and markup unformatted chapter headings and sub headings @@ -90,28 +90,28 @@ - format_scene_breaks + Ensure scene breaks are consistently formatted - dehyphenate + Remove unnecessary hyphens - italicize_common_cases + Italicize common words and patterns - fix_indents + Replace entity indents with CSS indents @@ -123,7 +123,7 @@ 131 - 95 + 35 @@ -144,43 +144,8 @@ - - - - Search and Replace - - - - - - - - - - - - - - - - - - - - - - - - - - RegexEdit - QWidget -
regex_builder.h
- 1 -
-
diff --git a/src/calibre/gui2/convert/search_and_replace.py b/src/calibre/gui2/convert/search_and_replace.py new file mode 100644 index 0000000000..860cc11d4e --- /dev/null +++ b/src/calibre/gui2/convert/search_and_replace.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- + +__license__ = 'GPL 3' +__copyright__ = '2011, John Schember ' +__docformat__ = 'restructuredtext en' + +import re + +from PyQt4.Qt import Qt + +from calibre.gui2.convert.search_and_replace_ui import Ui_Form +from calibre.gui2.convert import Widget +from calibre.gui2 import error_dialog + +class SearchAndReplaceWidget(Widget, Ui_Form): + + TITLE = _('Search and Replace') + HELP = _('Modify the document text and structure using user defined patterns.') + COMMIT_NAME = 'search_and_replace' + + def __init__(self, parent, get_option, get_help, db=None, book_id=None): + Widget.__init__(self, parent, + ['sr1_search', 'sr1_replace', + 'sr2_search', 'sr2_replace', + 'sr3_search', 'sr3_replace'] + ) + self.db, self.book_id = db, book_id + self.initialize_options(get_option, get_help, db, book_id) + self.opt_sr1_search.set_msg(_('Search regular expression 1:')) + self.opt_sr1_replace.set_msg(_('Replace regular expression 1:')) + self.opt_sr2_search.set_msg(_('Search regular expression 2:')) + self.opt_sr2_replace.set_msg(_('Replace regular expression 2:')) + self.opt_sr3_search.set_msg(_('Search regular expression 3:')) + self.opt_sr3_replace.set_msg(_('Replace regular expression 3:')) + + def break_cycles(self): + Widget.break_cycles(self) + + self.opt_sr1_search.break_cycles() + self.opt_sr1_replace.break_cycles() + self.opt_sr2_search.break_cycles() + self.opt_sr2_replace.break_cycles() + self.opt_sr3_search.break_cycles() + self.opt_sr3_replace.break_cycles() + + def pre_commit_check(self): + for x in ('sr1-search', 'sr1-replace', 'sr2-search', 'sr2-replace', 'sr3-search', 'sr3-replace',): + x = getattr(self, 'opt_'+x) + try: + pat = unicode(x.regex) + re.compile(pat) + except Exception, err: + error_dialog(self, _('Invalid regular expression'), + _('Invalid regular expression: %s')%err).exec_() + return False + + diff --git a/src/calibre/gui2/convert/search_and_replace.ui b/src/calibre/gui2/convert/search_and_replace.ui new file mode 100644 index 0000000000..5913f2c098 --- /dev/null +++ b/src/calibre/gui2/convert/search_and_replace.ui @@ -0,0 +1,47 @@ + + + Form + + + + 0 + 0 + 657 + 479 + + + + Form + + + + + + + + + + + + + + + + + + + + + + + + + RegexEdit + QWidget +
regex_builder.h
+ 1 +
+
+ + +
diff --git a/src/calibre/gui2/convert/single.py b/src/calibre/gui2/convert/single.py index 0337b779a0..8826d398f5 100644 --- a/src/calibre/gui2/convert/single.py +++ b/src/calibre/gui2/convert/single.py @@ -17,6 +17,7 @@ from calibre.gui2.convert.single_ui import Ui_Dialog from calibre.gui2.convert.metadata import MetadataWidget from calibre.gui2.convert.look_and_feel import LookAndFeelWidget from calibre.gui2.convert.heuristics import HeuristicsWidget +from calibre.gui2.convert.search_and_replace import SearchAndReplaceWidget from calibre.gui2.convert.page_setup import PageSetupWidget from calibre.gui2.convert.structure_detection import StructureDetectionWidget from calibre.gui2.convert.toc import TOCWidget @@ -172,6 +173,7 @@ class Config(ResizableDialog, Ui_Dialog): self.setWindowTitle(_('Convert')+ ' ' + unicode(self.mw.title.text())) lf = widget_factory(LookAndFeelWidget) hw = widget_factory(HeuristicsWidget) + sr = widget_factory(SearchAndReplaceWidget) ps = widget_factory(PageSetupWidget) sd = widget_factory(StructureDetectionWidget) toc = widget_factory(TOCWidget) @@ -205,7 +207,7 @@ class Config(ResizableDialog, Ui_Dialog): if not c: break self.stack.removeWidget(c) - widgets = [self.mw, lf, hw, ps, sd, toc] + widgets = [self.mw, lf, hw, sr, ps, sd, toc] if input_widget is not None: widgets.append(input_widget) if output_widget is not None: diff --git a/src/calibre/gui2/convert/structure_detection.ui b/src/calibre/gui2/convert/structure_detection.ui index b690a68b0a..262894d42d 100644 --- a/src/calibre/gui2/convert/structure_detection.ui +++ b/src/calibre/gui2/convert/structure_detection.ui @@ -14,10 +14,10 @@ Form - + - + Chapter &mark: @@ -27,7 +27,7 @@ - + 20 diff --git a/src/calibre/gui2/preferences/conversion.py b/src/calibre/gui2/preferences/conversion.py index a20872cee0..0a8fc375ea 100644 --- a/src/calibre/gui2/preferences/conversion.py +++ b/src/calibre/gui2/preferences/conversion.py @@ -13,6 +13,7 @@ from calibre.utils.logging import Log from calibre.gui2.preferences.conversion_ui import Ui_Form from calibre.gui2.convert.look_and_feel import LookAndFeelWidget from calibre.gui2.convert.heuristics import HeuristicsWidget +from calibre.gui2.convert.search_and_replace import SearchAndReplaceWidget from calibre.gui2.convert.page_setup import PageSetupWidget from calibre.gui2.convert.structure_detection import StructureDetectionWidget from calibre.gui2.convert.toc import TOCWidget @@ -84,7 +85,8 @@ class CommonOptions(Base): def load_conversion_widgets(self): self.conversion_widgets = [LookAndFeelWidget, HeuristicsWidget, - PageSetupWidget, StructureDetectionWidget, TOCWidget] + SearchAndReplaceWidget, PageSetupWidget, + StructureDetectionWidget, TOCWidget] class InputOptions(Base):