1
/* This file is part of the KDE project
2
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
3
* Copyright (C) 2006 Gary Cramblitt <garycramblitt@comcast.net>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "KoPagePreviewWidget.h"
25
#include <KoPageLayout.h>
26
#include <KoColumns.h>
29
#include <WidgetsDebug.h>
31
class Q_DECL_HIDDEN KoPagePreviewWidget::Private
34
KoPageLayout pageLayout;
39
KoPagePreviewWidget::KoPagePreviewWidget(QWidget *parent)
43
setMinimumSize( 100, 100 );
46
KoPagePreviewWidget::~KoPagePreviewWidget()
51
void KoPagePreviewWidget::paintEvent(QPaintEvent *event)
54
// resolution[XY] is in pixel per pt
55
qreal resolutionX = POINT_TO_INCH( static_cast<qreal>(KoDpi::dpiX()) );
56
qreal resolutionY = POINT_TO_INCH( static_cast<qreal>(KoDpi::dpiY()) );
58
qreal pageWidth = d->pageLayout.width * resolutionX;
59
qreal pageHeight = d->pageLayout.height * resolutionY;
61
const bool pageSpread = (d->pageLayout.bindingSide >= 0 && d->pageLayout.pageEdge >= 0);
62
qreal sheetWidth = pageWidth / (pageSpread?2:1);
64
qreal zoomH = (height() * 90 / 100) / pageHeight;
65
qreal zoomW = (width() * 90 / 100) / pageWidth;
66
qreal zoom = qMin( zoomW, zoomH );
71
QPainter painter( this );
73
QRect page = QRectF((width() - pageWidth) / 2.0,
74
(height() - pageHeight) / 2.0, sheetWidth, pageHeight).toRect();
77
drawPage(painter, zoom, page, true);
80
page.moveLeft(page.left() + (int) (sheetWidth));
82
drawPage(painter, zoom, page, false);
91
void KoPagePreviewWidget::drawPage(QPainter &painter, qreal zoom, const QRect &dimensions, bool left)
93
painter.fillRect(dimensions, QBrush(palette().base()));
94
painter.setPen(QPen(palette().color(QPalette::Dark), 0));
95
painter.drawRect(dimensions);
98
QRect textArea = dimensions;
99
if ((d->pageLayout.topMargin == 0 && d->pageLayout.bottomMargin == 0 &&
100
d->pageLayout.leftMargin == 0 && d->pageLayout.rightMargin == 0) ||
101
( d->pageLayout.pageEdge == 0 && d->pageLayout.bindingSide == 0)) {
106
textArea.setTop(textArea.top() + qRound(zoom * d->pageLayout.topMargin));
107
textArea.setBottom(textArea.bottom() - qRound(zoom * d->pageLayout.bottomMargin));
109
qreal leftMargin, rightMargin;
110
if(d->pageLayout.bindingSide < 0) { // normal margins.
111
leftMargin = d->pageLayout.leftMargin;
112
rightMargin = d->pageLayout.rightMargin;
114
else { // margins mirrored for left/right pages
115
leftMargin = d->pageLayout.bindingSide;
116
rightMargin = d->pageLayout.pageEdge;
118
qSwap(leftMargin, rightMargin);
120
textArea.setLeft(textArea.left() + qRound(zoom * leftMargin));
121
textArea.setRight(textArea.right() - qRound(zoom * rightMargin));
123
painter.setBrush( QBrush( palette().color(QPalette::ButtonText), Qt::HorPattern ) );
124
painter.setPen(QPen(palette().color(QPalette::Dark), 0));
127
if (d->columns.columnData.isEmpty()) {
128
qreal columnWidth = (textArea.width() + (d->columns.gapWidth * zoom)) / d->columns.count;
129
int width = qRound(columnWidth - d->columns.gapWidth * zoom);
130
for ( int i = 0; i < d->columns.count; ++i )
131
painter.drawRect( qRound(textArea.x() + i * columnWidth), textArea.y(), width, textArea.height());
133
qreal totalRelativeWidth = 0.0;
134
foreach(const KoColumns::ColumnDatum &cd, d->columns.columnData) {
135
totalRelativeWidth += cd.relativeWidth;
137
int relativeColumnXOffset = 0;
138
for (int i = 0; i < d->columns.count; i++) {
139
const KoColumns::ColumnDatum &columnDatum = d->columns.columnData.at(i);
140
const qreal columnWidth = textArea.width() * columnDatum.relativeWidth / totalRelativeWidth;
141
const qreal columnXOffset = textArea.width() * relativeColumnXOffset / totalRelativeWidth;
143
painter.drawRect( qRound(textArea.x() + columnXOffset + columnDatum.leftMargin * zoom),
144
qRound(textArea.y() + columnDatum.topMargin * zoom),
145
qRound(columnWidth - (columnDatum.leftMargin + columnDatum.rightMargin) * zoom),
146
qRound(textArea.height() - (columnDatum.topMargin + columnDatum.bottomMargin) * zoom));
148
relativeColumnXOffset += columnDatum.relativeWidth;
153
void KoPagePreviewWidget::setPageLayout(const KoPageLayout &layout)
155
d->pageLayout = layout;
159
void KoPagePreviewWidget::setColumns(const KoColumns &columns)
161
d->columns = columns;