| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt Linguist of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include "printout.h" | 
| 30 |  | 
| 31 | #include <QPrinter> | 
| 32 | #include <QFontMetrics> | 
| 33 |  | 
| 34 | QT_BEGIN_NAMESPACE | 
| 35 |  | 
| 36 | PrintOut::PrintOut(QPrinter *printer) | 
| 37 |     : pr(printer), nextRule(NoRule), page(0) | 
| 38 | { | 
| 39 |     p.begin(pr); | 
| 40 |     QFont f(QLatin1String("Arial" )); | 
| 41 |     f8 = f; | 
| 42 |     f8.setPointSize(8); | 
| 43 |     f10 = f; | 
| 44 |     f10.setPointSize(10); | 
| 45 |     p.setFont(f10); | 
| 46 |     fmetrics = new QFontMetrics(p.fontMetrics()); | 
| 47 |     hmargin = 5 * printer->width() / printer->widthMM(); // 5 mm | 
| 48 |     vmargin = 5 * printer->height() / printer->heightMM(); // 5 mm | 
| 49 |     hsize = printer->width() - 2 * hmargin; | 
| 50 |     vsize = printer->height() - vmargin; | 
| 51 |     dateTime = QDateTime::currentDateTime(); | 
| 52 |     breakPage(init: true); // init vsize and draw first header | 
| 53 |     cp = Paragraph(QPoint(hmargin, voffset)); | 
| 54 | } | 
| 55 |  | 
| 56 | PrintOut::~PrintOut() | 
| 57 | { | 
| 58 |     flushLine(); | 
| 59 |     delete fmetrics; | 
| 60 |     p.end(); | 
| 61 | } | 
| 62 |  | 
| 63 | void PrintOut::setRule(Rule rule) | 
| 64 | { | 
| 65 |     if (nextRule < rule) | 
| 66 |         nextRule = rule; | 
| 67 | } | 
| 68 |  | 
| 69 | void PrintOut::setGuide(const QString &guide) | 
| 70 | { | 
| 71 |     g = guide; | 
| 72 | } | 
| 73 |  | 
| 74 | void PrintOut::vskip() | 
| 75 | { | 
| 76 |     if (!firstParagraph) | 
| 77 |         voffset += 14; | 
| 78 | } | 
| 79 |  | 
| 80 | void PrintOut::flushLine(bool /* mayBreak */) | 
| 81 | { | 
| 82 |     if (voffset + cp.rect.height() > vsize) | 
| 83 |         breakPage(); | 
| 84 |     else if (!firstParagraph) | 
| 85 |         drawRule(rule: nextRule); | 
| 86 |  | 
| 87 |     for (int i = 0; i < cp.boxes.count(); ++i) { | 
| 88 |         Box b = cp.boxes[i]; | 
| 89 |         b.rect.translate(dx: 0, dy: voffset); | 
| 90 |         QRect r = b.rect; | 
| 91 |         p.setFont(b.font); | 
| 92 |         p.drawText(r, text: b.text, o: b.options); | 
| 93 |     } | 
| 94 |     voffset += cp.rect.height(); | 
| 95 |  | 
| 96 |     nextRule = NoRule; | 
| 97 |     cp = Paragraph(QPoint(hmargin, voffset)); | 
| 98 |     firstParagraph = false; | 
| 99 | } | 
| 100 |  | 
| 101 | void PrintOut::addBox(int percent, const QString &text, Style style, Qt::Alignment halign) | 
| 102 | { | 
| 103 |     QTextOption options; | 
| 104 |     options.setAlignment(halign | Qt::AlignTop); | 
| 105 |     options.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); | 
| 106 |     QFont f = f10; | 
| 107 |     if (style == Strong) | 
| 108 |         f.setBold(true); | 
| 109 |     else if (style == Emphasis) | 
| 110 |         f.setItalic(true); | 
| 111 |     int wd = hsize * percent / 100; | 
| 112 |     QRect r(cp.rect.x() + cp.rect.width(), 0, wd, vsize); | 
| 113 |     const int ht = static_cast<int>(p.boundingRect(rect: r, text, o: options).height()); | 
| 114 |  | 
| 115 |     Box b(r, text, f, options); | 
| 116 |     cp.boxes.append(t: b); | 
| 117 |     cp.rect.setSize(QSize(cp.rect.width() + wd, qMax(a: cp.rect.height(), b: ht))); | 
| 118 | } | 
| 119 |  | 
| 120 | // use init if initial vsize should be calculated (first breakPage call) | 
| 121 | void PrintOut::breakPage(bool init) | 
| 122 | { | 
| 123 |     static const int LeftAlign = Qt::AlignLeft | Qt::AlignTop; | 
| 124 |     static const int RightAlign = Qt::AlignRight | Qt::AlignTop; | 
| 125 |     QRect r1, r2; | 
| 126 |     int h1 = 0; | 
| 127 |     int h2 = 0; | 
| 128 |  | 
| 129 |     if (page > 0) | 
| 130 |         pr->newPage(); | 
| 131 |  | 
| 132 |     if (!init) | 
| 133 |         page++; | 
| 134 |  | 
| 135 |     voffset = 0; | 
| 136 |  | 
| 137 |     p.setFont(f10); | 
| 138 |     r1 = QRect(hmargin, voffset, 3 * hsize / 4, vsize); | 
| 139 |     r2 = QRect(r1.x() + r1.width(), voffset, hsize - r1.width(), vsize); | 
| 140 |     h1 = p.boundingRect(rect: r1, flags: LeftAlign, text: pr->docName()).height(); | 
| 141 |     if (!init) | 
| 142 |         p.drawText(r: r1, flags: LeftAlign, text: pr->docName()); | 
| 143 |     h2 = p.boundingRect(rect: r2, flags: RightAlign, text: QString::number(page)).height(); | 
| 144 |     if (!init) | 
| 145 |         p.drawText(r: r2, flags: RightAlign, text: QString::number(page)); | 
| 146 |     voffset += qMax(a: h1, b: h2 ); | 
| 147 |  | 
| 148 |     r1 = QRect(hmargin, voffset, hsize / 2, LeftAlign); | 
| 149 |     p.setFont(f8); | 
| 150 |     h1 = p.boundingRect(rect: r1, flags: LeftAlign, text: dateTime.toString()).height(); | 
| 151 |     if (!init) | 
| 152 |         p.drawText(r: r1, flags: LeftAlign, text: dateTime.toString()); | 
| 153 |     p.setFont(f10); | 
| 154 |     voffset += qMax(a: h1, b: h2); | 
| 155 |  | 
| 156 |     voffset += 4; | 
| 157 |     if (!init) | 
| 158 |         p.drawLine(p1: QPoint(hmargin, voffset), p2: QPoint(hmargin + hsize, voffset)); | 
| 159 |     voffset += 14; | 
| 160 |  | 
| 161 |     firstParagraph = true; | 
| 162 |  | 
| 163 |     if (init) { | 
| 164 |         vsize -= voffset; | 
| 165 |         breakPage(); // now draw it when the vsize is ok | 
| 166 |     } | 
| 167 |  | 
| 168 | } | 
| 169 |  | 
| 170 | void PrintOut::drawRule(Rule rule) | 
| 171 | { | 
| 172 |     QPen pen; | 
| 173 |  | 
| 174 |     switch (rule) { | 
| 175 |     case NoRule: | 
| 176 |         voffset += 5; | 
| 177 |         break; | 
| 178 |     case ThinRule: | 
| 179 |         pen.setColor(QColor(192, 192, 192)); | 
| 180 |         pen.setStyle(Qt::DotLine); | 
| 181 |         pen.setWidth(0); | 
| 182 |         p.setPen(pen); | 
| 183 |         voffset += 5; | 
| 184 |         p.drawLine(p1: QPoint(hmargin, voffset), | 
| 185 |                    p2: QPoint(hmargin + hsize, voffset)); | 
| 186 |         p.setPen(QPen()); | 
| 187 |         voffset += 2; | 
| 188 |         break; | 
| 189 |     case ThickRule: | 
| 190 |         voffset += 7; | 
| 191 |         p.drawLine(p1: QPoint(hmargin, voffset), | 
| 192 |                    p2: QPoint(hmargin + hsize, voffset)); | 
| 193 |         voffset += 4; | 
| 194 |     } | 
| 195 | } | 
| 196 |  | 
| 197 | QT_END_NAMESPACE | 
| 198 |  |