1 | /* |
2 | SPDX-FileCopyrightText: 2002-2010 Anders Lund <anders@alweb.dk> |
3 | |
4 | Rewritten based on code of: |
5 | SPDX-FileCopyrightText: 2002 Michael Goffioul <kdeprint@swing.be> |
6 | |
7 | SPDX-License-Identifier: LGPL-2.0-or-later |
8 | */ |
9 | |
10 | #include "kateprinter.h" |
11 | |
12 | #include "kateconfig.h" |
13 | #include "katedocument.h" |
14 | #include "kateglobal.h" |
15 | #include "kateview.h" |
16 | |
17 | #include <KConfigGroup> |
18 | #include <KSharedConfig> |
19 | |
20 | #include <QApplication> |
21 | #include <QMarginsF> |
22 | #include <QPageLayout> |
23 | #include <QPrintDialog> |
24 | #include <QPrintPreviewDialog> |
25 | #include <QPrinter> |
26 | |
27 | #include "printconfigwidgets.h" |
28 | #include "printpainter.h" |
29 | |
30 | using namespace KatePrinter; |
31 | |
32 | // BEGIN KatePrinterPrivate |
33 | class KatePrinterPrivate : public QObject |
34 | { |
35 | public: |
36 | KatePrinterPrivate(KTextEditor::DocumentPrivate *doc, KTextEditor::ViewPrivate *view); |
37 | |
38 | bool print(QPrinter *printer, QWidget *parentWidget); |
39 | void setColorScheme(const QString &scheme); |
40 | |
41 | public: |
42 | void paint(QPrinter *printer); |
43 | |
44 | private: |
45 | KTextEditor::ViewPrivate *m_view; |
46 | KTextEditor::DocumentPrivate *m_doc; |
47 | PrintPainter m_painter; |
48 | static void readSettings(QPrinter *printer); |
49 | static void writeSettings(QPrinter *printer); |
50 | }; |
51 | |
52 | KatePrinterPrivate::KatePrinterPrivate(KTextEditor::DocumentPrivate *doc, KTextEditor::ViewPrivate *view) |
53 | : QObject() |
54 | , m_view(view) |
55 | , m_doc(doc) |
56 | , m_painter(m_doc, m_view) |
57 | { |
58 | } |
59 | |
60 | bool KatePrinterPrivate::print(QPrinter *printer, QWidget *parentWidget) |
61 | { |
62 | // docname is now always there, including the right Untitled name |
63 | printer->setDocName(m_doc->documentName()); |
64 | |
65 | KatePrintTextSettings *kpts = new KatePrintTextSettings; |
66 | KatePrintHeaderFooter *kphf = new KatePrintHeaderFooter; |
67 | KatePrintLayout *kpl = new KatePrintLayout; |
68 | |
69 | QList<QWidget *> tabs; |
70 | tabs << kpts; |
71 | tabs << kphf; |
72 | tabs << kpl; |
73 | |
74 | readSettings(printer); |
75 | |
76 | QPointer<QPrintDialog> printDialog(new QPrintDialog(printer, parentWidget)); |
77 | printDialog->setOptionTabs(tabs); |
78 | |
79 | if (m_view && m_view->selection()) { |
80 | printer->setPrintRange(QPrinter::Selection); |
81 | printDialog->setOption(option: QAbstractPrintDialog::PrintSelection, on: true); |
82 | } |
83 | |
84 | printDialog->setOption(option: QAbstractPrintDialog::PrintPageRange, on: true); |
85 | |
86 | const int dlgCode = printDialog->exec(); |
87 | if (dlgCode != QDialog::Accepted || !printDialog) { |
88 | delete printDialog; |
89 | return false; |
90 | } |
91 | |
92 | writeSettings(printer); |
93 | |
94 | // configure the painter |
95 | m_painter.setPrintGuide(kpts->printGuide()); |
96 | m_painter.setPrintLineNumbers(kpts->printLineNumbers()); |
97 | m_painter.setDontPrintFoldedCode(kpts->dontPrintFoldedCode()); |
98 | |
99 | m_painter.setColorScheme(kpl->colorScheme()); |
100 | m_painter.setTextFont(kpl->textFont()); |
101 | m_painter.setUseBackground(kpl->useBackground()); |
102 | m_painter.setUseBox(kpl->useBox()); |
103 | m_painter.setBoxMargin(kpl->boxMargin()); |
104 | m_painter.setBoxWidth(kpl->boxWidth()); |
105 | m_painter.setBoxColor(kpl->boxColor()); |
106 | |
107 | m_painter.setHeadersFont(kphf->font()); |
108 | |
109 | m_painter.setUseHeader(kphf->useHeader()); |
110 | m_painter.setHeaderBackground(kphf->headerBackground()); |
111 | m_painter.setHeaderForeground(kphf->headerForeground()); |
112 | m_painter.setUseHeaderBackground(kphf->useHeaderBackground()); |
113 | m_painter.setHeaderFormat(kphf->headerFormat()); |
114 | |
115 | m_painter.setUseFooter(kphf->useFooter()); |
116 | m_painter.setFooterBackground(kphf->footerBackground()); |
117 | m_painter.setFooterForeground(kphf->footerForeground()); |
118 | m_painter.setUseFooterBackground(kphf->useFooterBackground()); |
119 | m_painter.setFooterFormat(kphf->footerFormat()); |
120 | |
121 | delete printDialog; |
122 | m_painter.paint(printer); |
123 | |
124 | return true; |
125 | } |
126 | |
127 | void KatePrinterPrivate::paint(QPrinter *printer) |
128 | { |
129 | m_painter.paint(printer); |
130 | } |
131 | |
132 | void KatePrinterPrivate::setColorScheme(const QString &scheme) |
133 | { |
134 | m_painter.setColorScheme(scheme); |
135 | } |
136 | |
137 | void KatePrinterPrivate::writeSettings(QPrinter *printer) |
138 | { |
139 | KSharedConfigPtr config = KTextEditor::EditorPrivate::config(); |
140 | KConfigGroup group(config, QStringLiteral("Kate Print Settings" )); |
141 | KConfigGroup margins(&group, QStringLiteral("Margins" )); |
142 | |
143 | QMarginsF m = printer->pageLayout().margins(units: QPageLayout::Millimeter); |
144 | margins.writeEntry(key: "left" , value: m.left()); |
145 | margins.writeEntry(key: "top" , value: m.top()); |
146 | margins.writeEntry(key: "right" , value: m.right()); |
147 | margins.writeEntry(key: "bottom" , value: m.bottom()); |
148 | } |
149 | |
150 | void KatePrinterPrivate::readSettings(QPrinter *printer) |
151 | { |
152 | KSharedConfigPtr config = KTextEditor::EditorPrivate::config(); |
153 | KConfigGroup group(config, QStringLiteral("Kate Print Settings" )); |
154 | KConfigGroup margins(&group, QStringLiteral("Margins" )); |
155 | |
156 | qreal left{}; |
157 | qreal right{}; |
158 | qreal top{}; |
159 | qreal bottom{}; |
160 | left = margins.readEntry(key: "left" , defaultValue: left); |
161 | top = margins.readEntry(key: "top" , defaultValue: top); |
162 | right = margins.readEntry(key: "right" , defaultValue: right); |
163 | bottom = margins.readEntry(key: "bottom" , defaultValue: bottom); |
164 | QMarginsF m = QMarginsF(left, top, right, bottom); |
165 | printer->setPageMargins(margins: m, units: QPageLayout::Millimeter); |
166 | } |
167 | |
168 | // END KatePrinterPrivate |
169 | |
170 | // BEGIN KatePrinter |
171 | |
172 | bool KatePrinter::print(KTextEditor::DocumentPrivate *doc, KTextEditor::ViewPrivate *view) |
173 | { |
174 | QPrinter printer; |
175 | KatePrinterPrivate p(doc, view); |
176 | return p.print(printer: &printer, parentWidget: view ? view : QApplication::activeWindow()); |
177 | } |
178 | |
179 | bool KatePrinter::printPreview(KTextEditor::DocumentPrivate *doc, KTextEditor::ViewPrivate *view) |
180 | { |
181 | QPrinter printer; |
182 | KatePrinterPrivate p(doc, view); |
183 | p.setColorScheme(QStringLiteral("Printing" )); |
184 | |
185 | // ensure proper parent & sizing |
186 | auto dialogParent = view ? view : QApplication::activeWindow(); |
187 | QPrintPreviewDialog preview(&printer, dialogParent); |
188 | if (dialogParent && dialogParent->window()) { |
189 | preview.resize(dialogParent->window()->size() * 0.75); |
190 | } |
191 | |
192 | QObject::connect(sender: &preview, signal: &QPrintPreviewDialog::paintRequested, context: &p, slot: &KatePrinterPrivate::paint); |
193 | return preview.exec(); |
194 | } |
195 | |
196 | // END KatePrinter |
197 | |