-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprintpdf.cpp
More file actions
345 lines (296 loc) · 10.8 KB
/
printpdf.cpp
File metadata and controls
345 lines (296 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/****************************************************************************
**
** Copyright (C) 2007-2009 Kevin Clague. All rights reserved.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include <QPainter>
#include <QPrinter>
#include <QFileInfo>
#include <QFileDialog>
#include <QDir>
#include <QString>
#include <QMessageBox>
#include "lpub.h"
struct paperSizes {
QPrinter::PaperSize paperSize;
float width;
float height;
} paperSizes[] = {
{ QPrinter::A0, 841, 1189 },
{ QPrinter::A1, 594, 841 },
{ QPrinter::A2, 420, 594 },
{ QPrinter::A3, 297, 420 },
{ QPrinter::A4, 210, 297 },
{ QPrinter::A5, 148, 210 },
{ QPrinter::A6, 105, 148 },
{ QPrinter::A7, 74, 105 },
{ QPrinter::A8, 52, 74 },
{ QPrinter::A9, 37, 52 },
{ QPrinter::B0, 1030, 1456 },
{ QPrinter::B1, 728, 1030 },
{ QPrinter::B10, 32, 45 },
{ QPrinter::B2, 515, 728 },
{ QPrinter::B3, 364, 515 },
{ QPrinter::B4, 257, 364 },
{ QPrinter::B5, 182, 257 },
{ QPrinter::B6, 128, 182 },
{ QPrinter::B7, 91, 128 },
{ QPrinter::B8, 64, 91 },
{ QPrinter::B9, 45, 64 },
{ QPrinter::B10, 31, 44 },
{ QPrinter::C5E, 163, 229 },
{ QPrinter::Comm10E, 105, 241 },
{ QPrinter::DLE, 110, 220 },
{ QPrinter::Executive, 190.5, 254 },
{ QPrinter::Folio, 210, 330 },
{ QPrinter::Ledger, 431.8, 279.4 },
{ QPrinter::Legal, 215.9, 355.6 },
{ QPrinter::Letter, 215.9, 279.4 },
{ QPrinter::Tabloid, 279.4, 431.8 },
};
int Gui::bestPaperSizeOrientation(
float widthMm,
float heightMm,
QPrinter::PaperSize &paperSize,
QPrinter::Orientation &orient)
{
int numPaperSizes = sizeof(paperSizes)/sizeof(paperSizes[0]);
float diffWidth = 1000;
float diffHeight = 1000;
int bestSize = 0;
for (int i = 0; i < numPaperSizes; i++) {
float widthDiff = paperSizes[i].width - widthMm;
float heightDiff = paperSizes[i].height - heightMm;
if (widthDiff >= 0 && heightDiff >= 0) {
if (widthDiff <= diffWidth && heightDiff <= diffHeight) {
bestSize = i;
paperSize = paperSizes[i].paperSize;
orient = QPrinter::Portrait;
diffWidth = widthDiff;
diffHeight = heightDiff;
}
}
widthDiff = paperSizes[i].height - widthMm;
heightDiff = paperSizes[i].width - heightMm;
if (widthDiff >= 0 && heightDiff >= 0) {
if (widthDiff <= diffWidth && heightDiff <= diffHeight) {
bestSize = i;
paperSize = paperSizes[i].paperSize;
orient = QPrinter::Landscape;
diffWidth = widthDiff;
diffHeight = heightDiff;
}
}
}
return bestSize;
}
void Gui::GetPixelDimensions(float &pixelWidth, float &pixelHeight)
{
float pageWidthIn, pageHeightIn;
// only concerned with inches because resolution() reports DPI
if (page.meta.LPub.resolution.type() == DPI) {
pageWidthIn = page.meta.LPub.page.size.value(0);
pageHeightIn = page.meta.LPub.page.size.value(1);
} else {
pageWidthIn = centimeters2inches(page.meta.LPub.page.size.value(0));
pageHeightIn = centimeters2inches(page.meta.LPub.page.size.value(1));
}
// pixel dimension are inches * pixels per inch
pixelWidth = int((pageWidthIn * resolution()) + 0.5);
pixelHeight = int((pageHeightIn * resolution()) + 0.5);
}
void Gui::GetPagePixelDimensions(float &pagePixelWidth, float &pagePixelHeight, QPrinter::PaperSize &paperSize, QPrinter::Orientation &orientation)
{
float pageWidthMm, pageHeightMm;
float pageWidthCm, pageHeightCm;
float pageWidthIn, pageHeightIn;
if (page.meta.LPub.resolution.type() == DPI) {
pageWidthIn = page.meta.LPub.page.size.value(0);
pageHeightIn = page.meta.LPub.page.size.value(1);
pageWidthCm = inches2centimeters(pageWidthIn);
pageHeightCm = inches2centimeters(pageHeightIn);
} else {
pageWidthCm = page.meta.LPub.page.size.value(0);
pageHeightCm = page.meta.LPub.page.size.value(1);
pageWidthIn = centimeters2inches(pageWidthCm);
pageHeightIn = centimeters2inches(pageHeightCm);
}
pageWidthMm = pageWidthCm * 10.0;
pageHeightMm = pageHeightCm * 10.0;
int sizeIndex = bestPaperSizeOrientation(pageWidthMm, pageHeightMm, paperSize, orientation);
if (orientation == QPrinter::Portrait) {
pageWidthCm = paperSizes[sizeIndex].width / 10.0;
pageHeightCm = paperSizes[sizeIndex].height / 10.0;
} else {
pageWidthCm = paperSizes[sizeIndex].height / 10.0;
pageHeightCm = paperSizes[sizeIndex].width / 10.0;
}
pageWidthIn = centimeters2inches(pageWidthCm);
pageHeightIn = centimeters2inches(pageHeightCm);
// important: note that pixel dimensions converted back from mm paper sizes may
// not match GetPixelDimensions. For example, Letter mm dimensions do not *exactly* equal 8.5 x 11
pagePixelWidth = int((pageWidthIn * resolution()) + 0.5);
pagePixelHeight = int((pageHeightIn * resolution()) + 0.5);
}
void Gui::printToFile()
{
// determine location for output file
QFileInfo fileInfo(curFile);
QString baseName = fileInfo.baseName();
QString fileName = QFileDialog::getSaveFileName(
this,
tr("Print File Name"),
QDir::currentPath() + "/" + baseName,
tr("PDF (*.pdf)\nPDF (*.PDF)"));
if (fileName == "") {
return;
}
// want info about output file now, not model file
fileInfo.setFile(fileName);
// append proper PDF file extension if needed
QString suffix = fileInfo.suffix();
if (suffix == "") {
fileName += ".pdf";
} else if (suffix != ".pdf" && suffix != ".PDF") {
fileName = fileInfo.path() + "/" + fileInfo.completeBaseName() + ".pdf";
}
// determine size of output pages, in pixels
QPrinter::PaperSize paperSize = QPrinter::PaperSize();
QPrinter::Orientation orientation = QPrinter::Orientation();
float pageWidthPx, pageHeightPx;
GetPagePixelDimensions(pageWidthPx, pageHeightPx, paperSize, orientation);
// create a PDF printer
QPrinter printer(QPrinter::ScreenResolution);
//printer.setResolution(resolution());
printer.setColorMode(QPrinter::Color);
printer.setOutputFileName(fileName);
printer.setOrientation(orientation);
printer.setPaperSize(paperSize);
printer.setPageMargins(0,0,0,0,QPrinter::Inch);
printer.setFullPage(true);
// paint to the printer the scene we view
QPainter painter;
painter.begin(&printer);
QGraphicsScene scene;
LGraphicsView view(&scene);
// set up the view
QRectF boundingRect(0.0, 0.0, pageWidthPx, pageHeightPx);
QRect bounding(0, 0, pageWidthPx, pageHeightPx);
view.scale(1.0,1.0);
view.setMinimumSize(pageWidthPx,pageHeightPx);
view.setMaximumSize(pageWidthPx,pageHeightPx);
view.setGeometry(bounding);
view.setSceneRect(boundingRect);
view.setRenderHints(
QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::SmoothPixmapTransform);
view.centerOn(boundingRect.center());
clearPage(&view,&scene);
int savePageNumber = displayPageNum;
for (displayPageNum = 1; displayPageNum <= maxPages; displayPageNum++) {
//qApp->processEvents();
// render this page
drawPage(&view,&scene,true);
scene.setSceneRect(0.0,0.0,pageWidthPx,pageHeightPx);
scene.render(&painter);
clearPage(&view,&scene);
// prepare to print another page
if(displayPageNum < maxPages) {
printer.newPage();
}
}
painter.end();
// return to whatever page we were viewing before output
displayPageNum = savePageNumber;
drawPage(KpageView,KpageScene,false);
}
void Gui::exportAsPng()
{
QString suffix(".png");
exportAs(suffix);
}
void Gui::exportAsJpg()
{
QString suffix(".jpg");
exportAs(suffix);
}
void Gui::exportAsBmp()
{
QString suffix(".bmp");
exportAs(suffix);
}
void Gui::exportAs(QString &suffix)
{
// determine location to output images
QFileInfo fileInfo(curFile);
//QDir initialDirectory = fileInfo.dir();
QString baseName = fileInfo.baseName();
QString directoryName = QFileDialog::getExistingDirectory(
this,
tr("Save images to folder"), // needs translation! also, include suffix in here
QDir::currentPath(),
QFileDialog::ShowDirsOnly);
if (directoryName == "") {
return;
}
// determine size of output image, in pixels
float pageWidthPx, pageHeightPx;
GetPixelDimensions(pageWidthPx, pageHeightPx);
// paint to the image the scene we view
QImage image(pageWidthPx, pageHeightPx, QImage::Format_ARGB32);
QPainter painter;
painter.begin(&image);
QGraphicsScene scene;
LGraphicsView view(&scene);
// set up the view
QRectF boundingRect(0.0,0.0,pageWidthPx,pageHeightPx);
QRect bounding(0,0,pageWidthPx,pageHeightPx);
view.scale(1.0,1.0);
view.setMinimumSize(pageWidthPx, pageHeightPx);
view.setMaximumSize(pageWidthPx, pageHeightPx);
view.setGeometry(bounding);
view.setSceneRect(boundingRect);
view.setRenderHints(
QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::SmoothPixmapTransform);
view.centerOn(boundingRect.center());
// view.fitInView(boundingRect);
clearPage(&view, &scene);
// Support transparency for formats that can handle it, but use white for those that can't.
QColor fill = (suffix.compare(".png", Qt::CaseInsensitive) == 0) ? Qt::transparent : Qt::white;
int savePageNumber = displayPageNum;
for (displayPageNum = 1; displayPageNum <= maxPages; displayPageNum++) {
//qApp->processEvents();
// clear the pixels of the image, just in case the background is
// transparent or uses a PNG image with transparency. This will
// prevent rendered pixels from each page layering on top of each
// other.
image.fill(fill.Rgb);
// render this page
// scene.render instead of view.render resolves "warm up" issue
drawPage(&view,&scene,false);
scene.setSceneRect(0.0,0.0,pageWidthPx,pageHeightPx);
scene.render(&painter);
clearPage(&view, &scene);
// save the image to the selected directory
// internationalization of "_page_"?
QString pn = QString("%1") .arg(displayPageNum);
image.save(directoryName + "/" + baseName + "_page_" + pn + suffix);
}
painter.end();
// return to whatever page we were viewing before output
displayPageNum = savePageNumber;
drawPage(KpageView,KpageScene,false);
}