KTextEditor

kateprinter.cpp
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
30using namespace KatePrinter;
31
32// BEGIN KatePrinterPrivate
33class KatePrinterPrivate : public QObject
34{
35public:
36 KatePrinterPrivate(KTextEditor::DocumentPrivate *doc, KTextEditor::ViewPrivate *view = nullptr);
37
38 bool print(QPrinter *printer);
39 void setColorScheme(const QString &scheme);
40
41public:
42 void paint(QPrinter *printer);
43
44private:
45 KTextEditor::ViewPrivate *m_view;
47 PrintPainter m_painter;
48 static void readSettings(QPrinter *printer);
49 static void writeSettings(QPrinter *printer);
50};
51
52KatePrinterPrivate::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
60bool 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
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()) {
87 printDialog->setOption(QAbstractPrintDialog::PrintSelection, true);
88 }
89
90 printDialog->setOption(QAbstractPrintDialog::PrintPageRange, 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
133void KatePrinterPrivate::paint(QPrinter *printer)
134{
135 m_painter.paint(printer);
136}
137
138void KatePrinterPrivate::setColorScheme(const QString &scheme)
139{
140 m_painter.setColorScheme(scheme);
141}
142
143void 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
150 margins.writeEntry("left", m.left());
151 margins.writeEntry("top", m.top());
152 margins.writeEntry("right", m.right());
153 margins.writeEntry("bottom", m.bottom());
154}
155
156void 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("left", left);
167 top = margins.readEntry("top", top);
168 right = margins.readEntry("right", right);
169 bottom = margins.readEntry("bottom", bottom);
170 QMarginsF m = QMarginsF(left, top, right, bottom);
172}
173
174// END KatePrinterPrivate
175
176// BEGIN KatePrinter
177
178bool KatePrinter::print(KTextEditor::ViewPrivate *view)
179{
180 QPrinter printer;
181 KatePrinterPrivate p(view->doc(), view);
182 return p.print(&printer);
183}
184
185bool 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(&preview, &QPrintPreviewDialog::paintRequested, &p, &KatePrinterPrivate::paint);
192 return preview.exec();
193}
194
195bool KatePrinter::print(KTextEditor::DocumentPrivate *doc)
196{
197 QPrinter printer;
198 KatePrinterPrivate p(doc);
199 return p.print(&printer);
200}
201
202bool 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(&preview, &QPrintPreviewDialog::paintRequested, &p, &KatePrinterPrivate::paint);
209 return preview.exec();
210}
211
212// END KatePrinter
Backend of KTextEditor::Document related public KTextEditor interfaces.
QString documentName() const override
Get this document's name.
QWidget * widget() override
static KSharedConfigPtr config()
The global configuration of katepart, e.g.
QWidget * activeWindow()
qreal bottom() const const
qreal left() const const
qreal right() const const
qreal top() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QPageLayout pageLayout() const const
virtual bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units)
QMarginsF margins() const const
void setDocName(const QString &name)
void setPrintRange(PrintRange range)
void paintRequested(QPrinter *printer)
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.