From e2b1c6d508ee4eae136ddce567106d3a03d9cf51 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 9 Sep 2009 19:52:01 -0600 Subject: [PATCH] IGN:Word wrap tooltips in the conversion preferences --- src/calibre/ebooks/txt/output.py | 11 +++++++++ src/calibre/ebooks/txt/txtml.py | 34 ++++++++++++++++++++++++++ src/calibre/gui2/convert/__init__.py | 9 +++++-- src/calibre/gui2/convert/txt_output.py | 5 ++-- src/calibre/gui2/convert/txt_output.ui | 28 ++++++++++++++++++--- 5 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/calibre/ebooks/txt/output.py b/src/calibre/ebooks/txt/output.py index b3bda7fa9d..32bde90fe8 100644 --- a/src/calibre/ebooks/txt/output.py +++ b/src/calibre/ebooks/txt/output.py @@ -36,6 +36,17 @@ class TXTOutput(OutputFormatPlugin): OptionRecommendation(name='flush_paras', recommended_value=False, level=OptionRecommendation.LOW, help=_('Do not add a blank line between paragraphs.')), + OptionRecommendation(name='max_line_length', + recommended_value=0, level=OptionRecommendation.LOW, + help=_('The maximum number of characters per line. This splits on ' + 'the first space before the specified value. If no space is found ' + 'the line will be broken at the space after and will exceed the ' + 'specified value. Also, there is a minimum of 25 characters. ' + 'Use 0 to disable line splitting.')), + OptionRecommendation(name='force_max_line_length', + recommended_value=False, level=OptionRecommendation.LOW, + help=_('Force splitting on the max-line-length value when no space ' + 'is present. Also allows max-line-length to be below the minimum')), OptionRecommendation(name='indent_paras', recommended_value=False, level=OptionRecommendation.LOW, help=_('Add a tab at the beginning of each paragraph.')), diff --git a/src/calibre/ebooks/txt/txtml.py b/src/calibre/ebooks/txt/txtml.py index c705bcf221..63a5cdc8af 100644 --- a/src/calibre/ebooks/txt/txtml.py +++ b/src/calibre/ebooks/txt/txtml.py @@ -105,6 +105,40 @@ class TXTMLizer(object): if self.opts.indent_paras: text = re.sub('(?imu)^(?=.)', '\t', text) + if self.opts.max_line_length: + max_length = self.opts.max_line_length + if self.opts.max_line_length < 25 and not self.opts.force_max_line_length: + max_length = 25 + short_lines = [] + lines = text.splitlines() + for line in lines: + while len(line) > max_length: + space = line.rfind(' ', 0, max_length) + if space != -1: + # Space was found. + short_lines.append(line[:space]) + line = line[space + 1:] + else: + # Space was not found. + if self.opts.force_max_line_length: + # Force breaking at max_lenght. + short_lines.append(line[:max_length]) + line = line[max_length:] + else: + # Look for the first space after max_length. + space = line.find(' ', max_length, len(line)) + if space != -1: + # Space was found. + short_lines.append(line[:space]) + line = line[space + 1:] + else: + # No space was found cannot break line. + short_lines.append(line) + line = '' + # Add the text that was less than max_lengh to the list + short_lines.append(line) + text = '\n'.join(short_lines) + return text def dump_text(self, elem, stylizer, end=''): diff --git a/src/calibre/gui2/convert/__init__.py b/src/calibre/gui2/convert/__init__.py index 90fd1b56ad..5022a2ae76 100644 --- a/src/calibre/gui2/convert/__init__.py +++ b/src/calibre/gui2/convert/__init__.py @@ -6,6 +6,8 @@ __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' +import textwrap + from PyQt4.Qt import QWidget, QSpinBox, QDoubleSpinBox, QLineEdit, QTextEdit, \ QCheckBox, QComboBox, Qt, QIcon, SIGNAL @@ -135,6 +137,7 @@ class Widget(QWidget): pass def setup_help(self, help_provider): + w = textwrap.TextWrapper(80) for name in self._options: g = getattr(self, 'opt_'+name, None) if g is None: @@ -142,8 +145,10 @@ class Widget(QWidget): help = help_provider(name) if not help: continue g._help = help - g.setToolTip(help.replace('<', '<').replace('>', '>')) - g.setWhatsThis(help.replace('<', '<').replace('>', '>')) + g.setToolTip('\n'.join(w.wrap(help.replace('<', '<').replace('>', + '>')))) + g.setWhatsThis('\n'.join(w.wrap(help.replace('<', '<').replace('>', + '>')))) g.__class__.enterEvent = lambda obj, event: self.set_help(getattr(obj, '_help', obj.toolTip())) diff --git a/src/calibre/gui2/convert/txt_output.py b/src/calibre/gui2/convert/txt_output.py index eca85f1292..2fc7f19908 100644 --- a/src/calibre/gui2/convert/txt_output.py +++ b/src/calibre/gui2/convert/txt_output.py @@ -17,8 +17,9 @@ class PluginWidget(Widget, Ui_Form): HELP = _('Options specific to')+' TXT '+_('output') def __init__(self, parent, get_option, get_help, db=None, book_id=None): - Widget.__init__(self, parent, 'txt_output', ['newline', 'inline_toc', - 'flush_paras', 'indent_paras']) + Widget.__init__(self, parent, 'txt_output', + ['newline', 'max_line_length', 'force_max_line_length', + 'inline_toc', 'flush_paras', 'indent_paras']) self.db, self.book_id = db, book_id self.initialize_options(get_option, get_help, db, book_id) diff --git a/src/calibre/gui2/convert/txt_output.ui b/src/calibre/gui2/convert/txt_output.ui index 900198aca9..8e5429b0ce 100644 --- a/src/calibre/gui2/convert/txt_output.ui +++ b/src/calibre/gui2/convert/txt_output.ui @@ -27,7 +27,7 @@ - + Qt::Vertical @@ -40,27 +40,47 @@ - + &Inline TOC - + Do not add a blank line between paragraphs. - + Add a tab at the beginning of each paragraph + + + + + + + &Maximum line length: + + + opt_max_line_length + + + + + + + Force maximum line lenght + + +