Messagelib

printmessage.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "printmessage.h"
8
9#include <QPainter>
10#include <QPrintDialog>
11#include <QPrintPreviewDialog>
12#include <QWebEngineView>
13
14#include <KLocalizedString>
15
16using namespace MessageViewer;
17PrintMessage::PrintMessage(QObject *parent)
18 : QObject{parent}
19{
20}
21
22PrintMessage::~PrintMessage() = default;
23
24void PrintMessage::setView(QWebEngineView *view)
25{
26 Q_ASSERT(!mView);
27 mView = view;
28 connect(mView, &QWebEngineView::printRequested, this, &PrintMessage::printPreview);
29 connect(mView, &QWebEngineView::printFinished, this, &PrintMessage::printFinished);
30}
31
32void PrintMessage::setDocumentName(const QString &name)
33{
34 mDocumentName = name;
35}
36
37void PrintMessage::print()
38{
39 QPrintDialog dialog(&mPrinter, mView);
40 if (dialog.exec() != QDialog::Accepted) {
42 return;
43 }
44 printDocument(&mPrinter, dialog.printer()->pageLayout());
45}
46
47void PrintMessage::printPreviewDocument(QPrinter *printer)
48{
49 mView->print(printer);
50 mWaitForResult.exec();
51}
52
53void PrintMessage::printDocument(QPrinter *printer, const QPageLayout &layout)
54{
55 printer->setPageLayout(layout);
56 mView->print(printer);
57 mWaitForResult.exec();
58}
59
60void PrintMessage::printFinished(bool success)
61{
62 if (!success) {
63 QPainter painter;
64 if (painter.begin(&mPrinter)) {
65 QFont font = painter.font();
66 font.setPixelSize(20);
67 painter.setFont(font);
68 painter.drawText(QPointF(10, 25), i18n("Could not generate print preview."));
69 painter.end();
70 }
71 }
72 mWaitForResult.quit();
73 Q_EMIT printingFinished();
74}
75
76QWidget *PrintMessage::parentWidget() const
77{
78 return mParentWidget;
79}
80
81void PrintMessage::setParentWidget(QWidget *newParentWidget)
82{
83 mParentWidget = newParentWidget;
84}
85
86QString PrintMessage::documentName() const
87{
88 return mDocumentName;
89}
90
91void PrintMessage::printPreview()
92{
93 if (!mView) {
95 return;
96 }
97 if (mInPrintPreview) {
99 return;
100 }
101 mInPrintPreview = true;
102
103 QPrintPreviewDialog preview(&mPrinter, mParentWidget);
104 preview.setWindowTitle(i18nc("@title:window", "Print Document"));
105 preview.resize(800, 750);
106 connect(&preview, &QPrintPreviewDialog::paintRequested, this, &PrintMessage::printPreviewDocument);
107 preview.exec();
108 mInPrintPreview = false;
109 deleteLater();
110}
111
112#include "moc_printmessage.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString name(GameStandardAction id)
int exec(ProcessEventsFlags flags)
void quit()
void setPixelSize(int pixelSize)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
virtual bool setPageLayout(const QPageLayout &newPageLayout)
bool begin(QPaintDevice *device)
void drawText(const QPoint &position, const QString &text)
bool end()
const QFont & font() const const
void setFont(const QFont &font)
void paintRequested(QPrinter *printer)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.