Debian Codegraph @

src/album/widgets/printhelper.cpp

raw
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "printhelper.h"
22 #include "utils/unionimage.h"
23
24 #include <DPrintPreviewDialog>
25 #include <DApplication>
26
27 #include <QPrintDialog>
28 #include <QPrintPreviewDialog>
29 #include <QPrintPreviewWidget>
30 #include <QPrinter>
31 #include <QPainter>
32 #include <QToolBar>
33 #include <QCoreApplication>
34 #include <QImageReader>
35 #include <QDebug>
36 #include <QVector>
37 #include <QFileInfo>
38
39 DWIDGET_USE_NAMESPACE
40
41 //绘制图片处理类
42 class RequestedSlot : public QObject
43 {
44 Q_OBJECT
45 public:
46 explicit RequestedSlot(QObject *parent = nullptr);
47 ~RequestedSlot();
48 public slots:
49 void paintRequestedAsyn(DPrinter *_printer, const QVector<int> &pageRange);
50 void paintRequestSync(DPrinter *_printer);
51
52 public:
53 QStringList m_paths;
54 QList<QImage> m_imgs;
55 };
56
57 PrintHelper *PrintHelper::m_Printer = nullptr;
58
59 PrintHelper *PrintHelper::getIntance()
60 {
61 if (!m_Printer) {
62 m_Printer = new PrintHelper();
63 }
64 return m_Printer;
65 }
66
67 PrintHelper::PrintHelper(QObject *parent)
68 : QObject(parent)
69 {
70 m_re = new RequestedSlot(this);
71 }
72
73 PrintHelper::~PrintHelper()
74 {
75 deconstruction();
76 }
77
78 void PrintHelper::showPrintDialog(const QStringList &paths, QWidget *parent)
79 {
80 Q_UNUSED(parent)
81 m_re->m_paths.clear();
82 m_re->m_imgs.clear();
83 QStringList tempExsitPaths;//保存存在的图片路径
84
85 m_re->m_paths = paths;
86 QImage imgTemp;
87 //判断图片文件是否存在并加载,耗时巨大
88 for (const QString &path : m_re->m_paths) {
89 QString errMsg;
90 UnionImage_NameSpace::loadStaticImageFromFile(path, imgTemp, errMsg);
91 if (!imgTemp.isNull()) {
92 m_re->m_imgs << imgTemp;
93 tempExsitPaths << path;
94 }
95 }
96 //适配打印接口2.0,dtk大于 5.4.10 版才合入最新的2.0打印控件接口
97 #if (DTK_VERSION_MAJOR > 5 \
98 || (DTK_VERSION_MAJOR >=5 && DTK_VERSION_MINOR > 4) \
99 || (DTK_VERSION_MAJOR >= 5 && DTK_VERSION_MINOR >= 4 && DTK_VERSION_PATCH >= 10))//5.4.4暂时没有合入
100 //增加运行时版本判断
101 DPrintPreviewDialog printDialog2(nullptr);
102 if (DApplication::runtimeDtkVersion() >= DTK_VERSION_CHECK(5, 4, 10, 0)) {
103 bool suc = printDialog2.setAsynPreview(m_re->m_imgs.size());//设置总页数,异步方式
104 //单张照片设置名称,可能多选照片,但能成功加载的可能只有一张,或从相册中选中的原图片不存在
105 if (tempExsitPaths.size() == 1) {
106 // 提供包含后缀的文件全名,由打印模块自己处理后缀
107 QString docName = QString(QFileInfo(tempExsitPaths.at(0)).completeBaseName());
108 docName += ".pdf";
109 printDialog2.setDocName(docName);
110 }//else 多张照片不设置名称,默认使用print模块的print.pdf
111 if (suc) {//异步
112 connect(&printDialog2, SIGNAL(paintRequested(DPrinter *, const QVector<int> &)),
113 m_re, SLOT(paintRequestedAsyn(DPrinter *, const QVector<int> &)));
114 } else {//同步
115 connect(&printDialog2, SIGNAL(paintRequested(DPrinter *)),
116 m_re, SLOT(paintRequestSync(DPrinter *)));
117 }
118 } else {
119 connect(&printDialog2, SIGNAL(paintRequested(DPrinter *)),
120 m_re, SLOT(paintRequestSync(DPrinter *)));
121 }
122 #else
123 DPrintPreviewDialog printDialog2(nullptr);
124 connect(&printDialog2, SIGNAL(paintRequested(DPrinter *)),
125 m_re, SLOT(paintRequestSync(DPrinter *)));
126 #endif
127 printDialog2.exec();
128 m_re->m_paths.clear();
129 m_re->m_imgs.clear();
130 }
131
132 void PrintHelper::deconstruction()
133 {
134 delete m_re;
135 m_re = nullptr;
136 }
137
138 RequestedSlot::RequestedSlot(QObject *parent)
139 {
140 Q_UNUSED(parent)
141 }
142
143 RequestedSlot::~RequestedSlot()
144 {
145 }
146
147 void RequestedSlot::paintRequestedAsyn(DPrinter *_printer, const QVector<int> &pageRange)
148 {
149 //更新逻辑,需要nepage和现实所有的,因为需要多版打印,需要显示多张图片
150 QPainter painter(_printer);
151 int index = 0;
152 for (int page : pageRange) {
153 if ((page < m_imgs.count() + 1) && page >= 1) {
154 QImage img = m_imgs.at(page - 1);
155 if (!img.isNull()) {
156 painter.setRenderHint(QPainter::Antialiasing);
157 painter.setRenderHint(QPainter::SmoothPixmapTransform);
158 QRect wRect = _printer->pageRect();
159
160 qreal ratio = wRect.width() * 1.0 / img.width();
161
162 painter.drawImage(QRectF(0, qreal(wRect.height() - img.height() * ratio) / 2,
163 wRect.width(), img.height() * ratio), img);
164 }
165 if (index < pageRange.size() - 1) {
166 _printer->newPage();
167 index++;
168 }
169 }
170 }
171 }
172
173 void RequestedSlot::paintRequestSync(DPrinter *_printer)
174 {
175 QPainter painter(_printer);
176 for (QImage img : m_imgs) {
177 if (!img.isNull()) {
178 painter.setRenderHint(QPainter::Antialiasing);
179 painter.setRenderHint(QPainter::SmoothPixmapTransform);
180 QRect wRect = _printer->pageRect();
181
182 qreal ratio = wRect.width() * 1.0 / img.width();
183
184 painter.drawImage(QRectF(0, qreal(wRect.height() - img.height() * ratio) / 2,
185 wRect.width(), img.height() * ratio), img);
186 }
187 if (img != m_imgs.last()) {
188 _printer->newPage();
189 }
190 }
191 painter.end();
192 }
193
194 #include "printhelper.moc"