| 1 |
/* This file is part of the KDE project |
| 2 |
* SPDX-FileCopyrightText: 2007 Thomas Zander <[email protected]> |
| 3 |
* SPDX-FileCopyrightText: 2006 Gary Cramblitt <[email protected]> |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
// clazy:excludeall=qstring-arg |
| 9 |
#include "KoPagePreviewWidget.h" |
| 10 |
|
| 11 |
#include <KoDpi.h> |
| 12 |
#include <KoUnit.h> |
| 13 |
#include <KoPageLayout.h> |
| 14 |
#include <KoColumns.h> |
| 15 |
|
| 16 |
#include <QPainter> |
| 17 |
#include <WidgetsDebug.h> |
| 18 |
|
| 19 |
class Q_DECL_HIDDEN KoPagePreviewWidget::Private |
| 20 |
{ |
| 21 |
public: |
| 22 |
KoPageLayout pageLayout; |
| 23 |
KoColumns columns; |
| 24 |
}; |
| 25 |
|
| 26 |
|
| 27 |
KoPagePreviewWidget::KoPagePreviewWidget(QWidget *parent) |
| 28 |
: QWidget(parent) |
| 29 |
, d(new Private) |
| 30 |
{ |
| 31 |
setMinimumSize(100, 100); |
| 32 |
} |
| 33 |
|
| 34 |
KoPagePreviewWidget::~KoPagePreviewWidget() |
| 35 |
{ |
| 36 |
delete d; |
| 37 |
} |
| 38 |
|
| 39 |
void KoPagePreviewWidget::paintEvent(QPaintEvent *event) |
| 40 |
{ |
| 41 |
Q_UNUSED(event); |
| 42 |
// resolution[XY] is in pixel per pt |
| 43 |
qreal resolutionX = POINT_TO_INCH(static_cast<qreal>(KoDpi::dpiX())); |
| 44 |
qreal resolutionY = POINT_TO_INCH(static_cast<qreal>(KoDpi::dpiY())); |
| 45 |
|
| 46 |
qreal pageWidth = d->pageLayout.width * resolutionX; |
| 47 |
qreal pageHeight = d->pageLayout.height * resolutionY; |
| 48 |
|
| 49 |
const bool pageSpread = (d->pageLayout.bindingSide >= 0 && d->pageLayout.pageEdge >= 0); |
| 50 |
qreal sheetWidth = pageWidth / (pageSpread?2:1); |
| 51 |
|
| 52 |
qreal zoomH = (height() * 90 / 100) / pageHeight; |
| 53 |
qreal zoomW = (width() * 90 / 100) / pageWidth; |
| 54 |
qreal zoom = qMin(zoomW, zoomH); |
| 55 |
|
| 56 |
pageWidth *= zoom; |
| 57 |
sheetWidth *= zoom; |
| 58 |
pageHeight *= zoom; |
| 59 |
QPainter painter(this); |
| 60 |
|
| 61 |
QRect page = QRectF((width() - pageWidth) / 2.0, |
| 62 |
(height() - pageHeight) / 2.0, sheetWidth, pageHeight).toRect(); |
| 63 |
|
| 64 |
painter.save(); |
| 65 |
drawPage(painter, zoom, page, true); |
| 66 |
painter.restore(); |
| 67 |
if(pageSpread) { |
| 68 |
page.moveLeft(page.left() + (int) (sheetWidth)); |
| 69 |
painter.save(); |
| 70 |
drawPage(painter, zoom, page, false); |
| 71 |
painter.restore(); |
| 72 |
} |
| 73 |
|
| 74 |
painter.end(); |
| 75 |
|
| 76 |
// paint scale |
| 77 |
} |
| 78 |
|
| 79 |
void KoPagePreviewWidget::drawPage(QPainter &painter, qreal zoom, const QRect &dimensions, bool left) |
| 80 |
{ |
| 81 |
painter.fillRect(dimensions, QBrush(palette().base())); |
| 82 |
painter.setPen(QPen(palette().color(QPalette::Dark), 0)); |
| 83 |
painter.drawRect(dimensions); |
| 84 |
|
| 85 |
// draw text areas |
| 86 |
QRect textArea = dimensions; |
| 87 |
if ((d->pageLayout.topMargin == 0 && d->pageLayout.bottomMargin == 0 && |
| 88 |
d->pageLayout.leftMargin == 0 && d->pageLayout.rightMargin == 0) || |
| 89 |
(d->pageLayout.pageEdge == 0 && d->pageLayout.bindingSide == 0)) { |
| 90 |
// no margin |
| 91 |
return; |
| 92 |
} |
| 93 |
else { |
| 94 |
textArea.setTop(textArea.top() + qRound(zoom * d->pageLayout.topMargin)); |
| 95 |
textArea.setBottom(textArea.bottom() - qRound(zoom * d->pageLayout.bottomMargin)); |
| 96 |
|
| 97 |
qreal leftMargin, rightMargin; |
| 98 |
if(d->pageLayout.bindingSide < 0) { // normal margins. |
| 99 |
leftMargin = d->pageLayout.leftMargin; |
| 100 |
rightMargin = d->pageLayout.rightMargin; |
| 101 |
} |
| 102 |
else { // margins mirrored for left/right pages |
| 103 |
leftMargin = d->pageLayout.bindingSide; |
| 104 |
rightMargin = d->pageLayout.pageEdge; |
| 105 |
if(left) |
| 106 |
qSwap(leftMargin, rightMargin); |
| 107 |
} |
| 108 |
textArea.setLeft(textArea.left() + qRound(zoom * leftMargin)); |
| 109 |
textArea.setRight(textArea.right() - qRound(zoom * rightMargin)); |
| 110 |
} |
| 111 |
painter.setBrush(QBrush(palette().color(QPalette::ButtonText), Qt::HorPattern)); |
| 112 |
painter.setPen(QPen(palette().color(QPalette::Dark), 0)); |
| 113 |
|
| 114 |
// uniform columns? |
| 115 |
if (d->columns.columnData.isEmpty()) { |
| 116 |
qreal columnWidth = (textArea.width() + (d->columns.gapWidth * zoom)) / d->columns.count; |
| 117 |
int width = qRound(columnWidth - d->columns.gapWidth * zoom); |
| 118 |
for (int i = 0; i < d->columns.count; ++i) |
| 119 |
painter.drawRect(qRound(textArea.x() + i * columnWidth), textArea.y(), width, textArea.height()); |
| 120 |
} else { |
| 121 |
qreal totalRelativeWidth = 0.0; |
| 122 |
for (const KoColumns::ColumnDatum &cd : std::as_const(d->columns.columnData)) { |
| 123 |
totalRelativeWidth += cd.relativeWidth; |
| 124 |
} |
| 125 |
int relativeColumnXOffset = 0; |
| 126 |
for (int i = 0; i < d->columns.count; i++) { |
| 127 |
const KoColumns::ColumnDatum &columnDatum = d->columns.columnData.at(i); |
| 128 |
const qreal columnWidth = textArea.width() * columnDatum.relativeWidth / totalRelativeWidth; |
| 129 |
const qreal columnXOffset = textArea.width() * relativeColumnXOffset / totalRelativeWidth; |
| 130 |
|
| 131 |
painter.drawRect(qRound(textArea.x() + columnXOffset + columnDatum.leftMargin * zoom), |
| 132 |
qRound(textArea.y() + columnDatum.topMargin * zoom), |
| 133 |
qRound(columnWidth - (columnDatum.leftMargin + columnDatum.rightMargin) * zoom), |
| 134 |
qRound(textArea.height() - (columnDatum.topMargin + columnDatum.bottomMargin) * zoom)); |
| 135 |
|
| 136 |
relativeColumnXOffset += columnDatum.relativeWidth; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void KoPagePreviewWidget::setPageLayout(const KoPageLayout &layout) |
| 142 |
{ |
| 143 |
d->pageLayout = layout; |
| 144 |
update(); |
| 145 |
} |
| 146 |
|
| 147 |
void KoPagePreviewWidget::setColumns(const KoColumns &columns) |
| 148 |
{ |
| 149 |
d->columns = columns; |
| 150 |
update(); |
| 151 |
} |
| 152 |
|