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 
16 using namespace MessageViewer;
17 PrintMessage::PrintMessage(QObject *parent)
18  : QObject{parent}
19 {
20 }
21 
22 PrintMessage::~PrintMessage() = default;
23 
24 void 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 
32 void PrintMessage::setDocumentName(const QString &name)
33 {
34  mDocumentName = name;
35 }
36 
37 void PrintMessage::print()
38 {
39  QPrintDialog dialog(&mPrinter, mView);
40  if (dialog.exec() != QDialog::Accepted) {
41  deleteLater();
42  return;
43  }
44  printDocument(&mPrinter);
45 }
46 
47 void PrintMessage::printDocument(QPrinter *printer)
48 {
49  mView->print(printer);
50  mWaitForResult.exec();
51 }
52 
53 void PrintMessage::printFinished(bool success)
54 {
55  if (!success) {
56  QPainter painter;
57  if (painter.begin(&mPrinter)) {
58  QFont font = painter.font();
59  font.setPixelSize(20);
60  painter.setFont(font);
61  painter.drawText(QPointF(10, 25), i18n("Could not generate print preview."));
62  painter.end();
63  }
64  }
65  mWaitForResult.quit();
66  Q_EMIT printingFinished();
67 }
68 
69 QWidget *PrintMessage::parentWidget() const
70 {
71  return mParentWidget;
72 }
73 
74 void PrintMessage::setParentWidget(QWidget *newParentWidget)
75 {
76  mParentWidget = newParentWidget;
77 }
78 
79 QString PrintMessage::documentName() const
80 {
81  return mDocumentName;
82 }
83 
84 void PrintMessage::printPreview()
85 {
86  if (!mView) {
87  deleteLater();
88  return;
89  }
90  if (mInPrintPreview) {
91  deleteLater();
92  return;
93  }
94  mInPrintPreview = true;
95 
96  QPrintPreviewDialog preview(&mPrinter, mParentWidget);
97  preview.setWindowTitle(i18nc("@title:window", "Print Document"));
98  preview.resize(800, 750);
99  connect(&preview, &QPrintPreviewDialog::paintRequested, this, &PrintMessage::printDocument);
100  preview.exec();
101  mInPrintPreview = false;
102  deleteLater();
103 }
104 
105 #include "moc_printmessage.cpp"
void drawText(const QPointF &position, const QString &text)
bool begin(QPaintDevice *device)
bool end()
QString i18n(const char *text, const TYPE &arg...)
void setPixelSize(int pixelSize)
QString name(StandardAction id)
void paintRequested(QPrinter *printer)
const QFont & font() const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setFont(const QFont &font)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.