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 = nullptr); |
37 | |
38 | bool print(QPrinter *printer); |
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) |
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 | QWidget *parentWidget = m_doc->widget(); |
75 | |
76 | if (!parentWidget) { |
77 | parentWidget = QApplication::activeWindow(); |
78 | } |
79 | |
80 | readSettings(printer); |
81 | |
82 | QPointer<QPrintDialog> printDialog(new QPrintDialog(printer, parentWidget)); |
83 | printDialog->setOptionTabs(tabs); |
84 | |
85 | if (m_view && m_view->selection()) { |
86 | printer->setPrintRange(QPrinter::Selection); |
87 | printDialog->setOption(option: QAbstractPrintDialog::PrintSelection, on: true); |
88 | } |
89 | |
90 | printDialog->setOption(option: QAbstractPrintDialog::PrintPageRange, on: true); |
91 | |
92 | const int dlgCode = printDialog->exec(); |
93 | if (dlgCode != QDialog::Accepted || !printDialog) { |
94 | delete printDialog; |
95 | return false; |
96 | } |
97 | |
98 | writeSettings(printer); |
99 | |
100 | // configure the painter |
101 | m_painter.setPrintGuide(kpts->printGuide()); |
102 | m_painter.setPrintLineNumbers(kpts->printLineNumbers()); |
103 | m_painter.setDontPrintFoldedCode(kpts->dontPrintFoldedCode()); |
104 | |
105 | m_painter.setColorScheme(kpl->colorScheme()); |
106 | m_painter.setTextFont(kpl->textFont()); |
107 | m_painter.setUseBackground(kpl->useBackground()); |
108 | m_painter.setUseBox(kpl->useBox()); |
109 | m_painter.setBoxMargin(kpl->boxMargin()); |
110 | m_painter.setBoxWidth(kpl->boxWidth()); |
111 | m_painter.setBoxColor(kpl->boxColor()); |
112 | |
113 | m_painter.setHeadersFont(kphf->font()); |
114 | |
115 | m_painter.setUseHeader(kphf->useHeader()); |
116 | m_painter.setHeaderBackground(kphf->headerBackground()); |
117 | m_painter.setHeaderForeground(kphf->headerForeground()); |
118 | m_painter.setUseHeaderBackground(kphf->useHeaderBackground()); |
119 | m_painter.setHeaderFormat(kphf->headerFormat()); |
120 | |
121 | m_painter.setUseFooter(kphf->useFooter()); |
122 | m_painter.setFooterBackground(kphf->footerBackground()); |
123 | m_painter.setFooterForeground(kphf->footerForeground()); |
124 | m_painter.setUseFooterBackground(kphf->useFooterBackground()); |
125 | m_painter.setFooterFormat(kphf->footerFormat()); |
126 | |
127 | delete printDialog; |
128 | m_painter.paint(printer); |
129 | |
130 | return true; |
131 | } |
132 | |
133 | void KatePrinterPrivate::paint(QPrinter *printer) |
134 | { |
135 | m_painter.paint(printer); |
136 | } |
137 | |
138 | void KatePrinterPrivate::setColorScheme(const QString &scheme) |
139 | { |
140 | m_painter.setColorScheme(scheme); |
141 | } |
142 | |
143 | void KatePrinterPrivate::writeSettings(QPrinter *printer) |
144 | { |
145 | KSharedConfigPtr config = KTextEditor::EditorPrivate::config(); |
146 | KConfigGroup group(config, QStringLiteral("Kate Print Settings" )); |
147 | KConfigGroup margins(&group, QStringLiteral("Margins" )); |
148 | |
149 | QMarginsF m = printer->pageLayout().margins(units: QPageLayout::Millimeter); |
150 | margins.writeEntry(key: "left" , value: m.left()); |
151 | margins.writeEntry(key: "top" , value: m.top()); |
152 | margins.writeEntry(key: "right" , value: m.right()); |
153 | margins.writeEntry(key: "bottom" , value: m.bottom()); |
154 | } |
155 | |
156 | void KatePrinterPrivate::readSettings(QPrinter *printer) |
157 | { |
158 | KSharedConfigPtr config = KTextEditor::EditorPrivate::config(); |
159 | KConfigGroup group(config, QStringLiteral("Kate Print Settings" )); |
160 | KConfigGroup margins(&group, QStringLiteral("Margins" )); |
161 | |
162 | qreal left{}; |
163 | qreal right{}; |
164 | qreal top{}; |
165 | qreal bottom{}; |
166 | left = margins.readEntry(key: "left" , defaultValue: left); |
167 | top = margins.readEntry(key: "top" , defaultValue: top); |
168 | right = margins.readEntry(key: "right" , defaultValue: right); |
169 | bottom = margins.readEntry(key: "bottom" , defaultValue: bottom); |
170 | QMarginsF m = QMarginsF(left, top, right, bottom); |
171 | printer->setPageMargins(margins: m, units: QPageLayout::Millimeter); |
172 | } |
173 | |
174 | // END KatePrinterPrivate |
175 | |
176 | // BEGIN KatePrinter |
177 | |
178 | bool KatePrinter::print(KTextEditor::ViewPrivate *view) |
179 | { |
180 | QPrinter printer; |
181 | KatePrinterPrivate p(view->doc(), view); |
182 | return p.print(printer: &printer); |
183 | } |
184 | |
185 | bool KatePrinter::printPreview(KTextEditor::ViewPrivate *view) |
186 | { |
187 | QPrinter printer; |
188 | KatePrinterPrivate p(view->doc(), view); |
189 | p.setColorScheme(QStringLiteral("Printing" )); |
190 | QPrintPreviewDialog preview(&printer, view); |
191 | QObject::connect(sender: &preview, signal: &QPrintPreviewDialog::paintRequested, context: &p, slot: &KatePrinterPrivate::paint); |
192 | return preview.exec(); |
193 | } |
194 | |
195 | bool KatePrinter::print(KTextEditor::DocumentPrivate *doc) |
196 | { |
197 | QPrinter printer; |
198 | KatePrinterPrivate p(doc); |
199 | return p.print(printer: &printer); |
200 | } |
201 | |
202 | bool KatePrinter::printPreview(KTextEditor::DocumentPrivate *doc) |
203 | { |
204 | QPrinter printer; |
205 | KatePrinterPrivate p(doc); |
206 | p.setColorScheme(QStringLiteral("Printing" )); |
207 | QPrintPreviewDialog preview(&printer); |
208 | QObject::connect(sender: &preview, signal: &QPrintPreviewDialog::paintRequested, context: &p, slot: &KatePrinterPrivate::paint); |
209 | return preview.exec(); |
210 | } |
211 | |
212 | // END KatePrinter |
213 | |