From 98f4712f0ddec076f612a5d8abde11ba422f64ec Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 13 Apr 2017 08:12:41 +0530 Subject: [PATCH] DOCX Output: Add separate, docx specific, page margin settings that override the common settings --- .../ebooks/conversion/plugins/docx_output.py | 31 +++++++-- src/calibre/ebooks/docx/writer/container.py | 6 +- src/calibre/gui2/convert/docx_output.py | 25 ++++++- src/calibre/gui2/convert/docx_output.ui | 68 ------------------- 4 files changed, 52 insertions(+), 78 deletions(-) delete mode 100644 src/calibre/gui2/convert/docx_output.ui diff --git a/src/calibre/ebooks/conversion/plugins/docx_output.py b/src/calibre/ebooks/conversion/plugins/docx_output.py index e140b902ff..d596496183 100644 --- a/src/calibre/ebooks/conversion/plugins/docx_output.py +++ b/src/calibre/ebooks/conversion/plugins/docx_output.py @@ -40,13 +40,31 @@ class DOCXOutput(OutputFormatPlugin): help=_('Extract the contents of the generated %s file to the ' 'specified directory. The contents of the directory are first ' 'deleted, so be careful.') % 'DOCX'), - } - recommendations = { - ('margin_left', 72.0, OptionRecommendation.MED), - ('margin_right', 72.0, OptionRecommendation.MED), - ('margin_top', 72.0, OptionRecommendation.MED), - ('margin_bottom', 72.0, OptionRecommendation.MED), + OptionRecommendation(name='docx_page_margin_left', recommended_value=72.0, + level=OptionRecommendation.LOW, + help=_('The size of the left page margin, in pts. Default is 72pt.' + ' Overrides the main left page margin setting.') + ), + + OptionRecommendation(name='docx_page_margin_top', recommended_value=72.0, + level=OptionRecommendation.LOW, + help=_('The size of the top page margin, in pts. Default is 72pt.' + ' Overrides the main top page margin setting, unless set to zero.') + ), + + OptionRecommendation(name='docx_page_margin_right', recommended_value=72.0, + level=OptionRecommendation.LOW, + help=_('The size of the right page margin, in pts. Default is 72pt.' + ' Overrides the main right page margin setting, unless set to zero.') + ), + + OptionRecommendation(name='docx_page_margin_bottom', recommended_value=72.0, + level=OptionRecommendation.LOW, + help=_('The size of the bottom page margin, in pts. Default is 72pt.' + ' Overrides the main bottom page margin setting, unless set to zero.') + ), + } def convert_metadata(self, oeb): @@ -68,4 +86,3 @@ class DOCXOutput(OutputFormatPlugin): if opts.extract_to: from calibre.ebooks.docx.dump import do_dump do_dump(output_path, opts.extract_to) - diff --git a/src/calibre/ebooks/docx/writer/container.py b/src/calibre/ebooks/docx/writer/container.py index 841a67a7b6..787736face 100644 --- a/src/calibre/ebooks/docx/writer/container.py +++ b/src/calibre/ebooks/docx/writer/container.py @@ -50,7 +50,10 @@ def create_skeleton(opts, namespaces=None): width, height = int(20 * width), int(20 * height) def margin(which): - return w(which), str(int(getattr(opts, 'margin_'+which) * 20)) + val = getattr(opts, 'docx_page_margin_' + which) + if val == 0.0: + val = getattr(opts, 'margin_' + which) + return w(which), str(int(val * 20)) body.append(E.sectPr( E.pgSz(**{w('w'):str(width), w('h'):str(height)}), E.pgMar(**dict(map(margin, 'left top right bottom'.split()))), @@ -260,6 +263,7 @@ class DOCX(object): for fname, data in self.fonts.iteritems(): zf.writestr(fname, data) + if __name__ == '__main__': d = DOCX(None, None) print (d.websettings) diff --git a/src/calibre/gui2/convert/docx_output.py b/src/calibre/gui2/convert/docx_output.py index dbba690f2f..c3d0c55406 100644 --- a/src/calibre/gui2/convert/docx_output.py +++ b/src/calibre/gui2/convert/docx_output.py @@ -4,14 +4,15 @@ __license__ = 'GPL 3' __copyright__ = '2009, John Schember ' __docformat__ = 'restructuredtext en' -from calibre.gui2.convert.docx_output_ui import Ui_Form +from PyQt5.Qt import QFormLayout, QComboBox, QCheckBox, QLineEdit, QDoubleSpinBox, QSizePolicy + from calibre.gui2.convert import Widget paper_size_model = None orientation_model = None -class PluginWidget(Widget, Ui_Form): +class PluginWidget(Widget): TITLE = _('DOCX Output') HELP = _('Options specific to')+' DOCX '+_('output') @@ -21,9 +22,29 @@ class PluginWidget(Widget, Ui_Form): def __init__(self, parent, get_option, get_help, db=None, book_id=None): Widget.__init__(self, parent, [ 'docx_page_size', 'docx_custom_page_size', 'docx_no_cover', 'docx_no_toc', + 'docx_page_margin_left', 'docx_page_margin_top', 'docx_page_margin_right', + 'docx_page_margin_bottom', ]) for x in get_option('docx_page_size').option.choices: self.opt_docx_page_size.addItem(x) self.initialize_options(get_option, get_help, db, book_id) self.layout().setFieldGrowthPolicy(self.layout().ExpandingFieldsGrow) + + def setupUi(self, *a): + self.l = l = QFormLayout(self) + self.opt_docx_page_size = QComboBox(self) + l.addRow(_('Paper si&ze:'), self.opt_docx_page_size) + self.opt_docx_custom_page_size = w = QLineEdit(self) + w.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) + l.addRow(_('&Custom size:'), w) + for i, text in enumerate((_('Page &left margin'), _('Page &top margin'), _('Page &right margin'), _('Page &bottom margin'))): + m = 'left top right bottom'.split()[i] + w = QDoubleSpinBox(self) + w.setRange(-100, 500), w.setSuffix(' pt'), w.setDecimals(1) + setattr(self, 'opt_docx_page_margin_' + m, w) + l.addRow(text + ':', w) + self.opt_docx_no_toc = QCheckBox(_('Do not insert the &Table of Contents as a page at the start of the document')) + l.addRow(self.opt_docx_no_toc) + self.opt_docx_no_cover = QCheckBox(_('Do not insert &cover as image at start of document')) + l.addRow(self.opt_docx_no_cover) diff --git a/src/calibre/gui2/convert/docx_output.ui b/src/calibre/gui2/convert/docx_output.ui deleted file mode 100644 index aae6012bda..0000000000 --- a/src/calibre/gui2/convert/docx_output.ui +++ /dev/null @@ -1,68 +0,0 @@ - - - Form - - - - 0 - 0 - 638 - 588 - - - - Form - - - - - - Paper Si&ze: - - - opt_docx_page_size - - - - - - - - - - &Custom size: - - - opt_docx_custom_page_size - - - - - - - - 0 - 0 - - - - - - - - Do not insert &cover as image at start of document - - - - - - - Do not insert the &Table of Contents as a page at the start of the document - - - - - - - -