1# -*- coding: utf-8 -*-
2
3
4__license__ = 'GPL 3'
5__copyright__ = '2009, John Schember <john@nachtimwald.com>'
6__docformat__ = 'restructuredtext en'
7
8
9from qt.core import QHBoxLayout, QFormLayout, QDoubleSpinBox, QCheckBox, QVBoxLayout
10
11from calibre.gui2.convert.pdf_output_ui import Ui_Form
12from calibre.gui2.convert import Widget
13from calibre.utils.localization import localize_user_manual_link
14from calibre.ebooks.conversion.config import OPTIONS
15
16paper_size_model = None
17orientation_model = None
18
19
20class PluginWidget(Widget, Ui_Form):
21
22    TITLE = _('PDF output')
23    HELP = _('Options specific to')+' PDF '+_('output')
24    COMMIT_NAME = 'pdf_output'
25    ICON = I('mimetypes/pdf.png')
26
27    def __init__(self, parent, get_option, get_help, db=None, book_id=None):
28        Widget.__init__(self, parent, OPTIONS['output']['pdf'])
29        self.db, self.book_id = db, book_id
30        try:
31            self.hf_label.setText(self.hf_label.text() % localize_user_manual_link(
32                'https://manual.calibre-ebook.com/conversion.html#converting-to-pdf'))
33        except TypeError:
34            pass  # link already localized
35
36        self.opt_paper_size.initialize(get_option('paper_size').option.choices)
37        for x in get_option('unit').option.choices:
38            self.opt_unit.addItem(x)
39        for x in get_option('pdf_standard_font').option.choices:
40            self.opt_pdf_standard_font.addItem(x)
41
42        self.initialize_options(get_option, get_help, db, book_id)
43        self.layout().setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.ExpandingFieldsGrow)
44        self.template_box.layout().setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)
45        self.profile_size_toggled()
46
47    def profile_size_toggled(self):
48        enabled = not self.opt_use_profile_size.isChecked()
49        self.opt_paper_size.setEnabled(enabled)
50        self.opt_custom_size.setEnabled(enabled)
51        self.opt_unit.setEnabled(enabled)
52
53    def setupUi(self, *a):
54        Ui_Form.setupUi(self, *a)
55        v = self.page_margins_box.v = QVBoxLayout(self.page_margins_box)
56        self.opt_pdf_use_document_margins = c = QCheckBox(_('Use page margins from the &document being converted'))
57        v.addWidget(c)
58        h = self.page_margins_box.h = QHBoxLayout()
59        l = self.page_margins_box.l = QFormLayout()
60        r = self.page_margins_box.r = QFormLayout()
61        h.addLayout(l), h.addLayout(r)
62        v.addLayout(h)
63
64        def margin(which):
65            w = QDoubleSpinBox(self)
66            w.setRange(-100, 500), w.setSuffix(' pt'), w.setDecimals(1)
67            setattr(self, 'opt_pdf_page_margin_' + which, w)
68            return w
69
70        l.addRow(_('&Left:'), margin('left'))
71        l.addRow(_('&Right:'), margin('right'))
72        r.addRow(_('&Top:'), margin('top'))
73        r.addRow(_('&Bottom:'), margin('bottom'))
74        self.opt_use_profile_size.toggled.connect(self.profile_size_toggled)
75