Warning, file /office/calligra/libs/widgets/KoPagePreviewWidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
0003  * Copyright (C) 2006 Gary Cramblitt <garycramblitt@comcast.net>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KoPagePreviewWidget.h"
0022 
0023 #include <KoDpi.h>
0024 #include <KoUnit.h>
0025 #include <KoPageLayout.h>
0026 #include <KoColumns.h>
0027 
0028 #include <QPainter>
0029 #include <WidgetsDebug.h>
0030 
0031 class Q_DECL_HIDDEN KoPagePreviewWidget::Private
0032 {
0033 public:
0034     KoPageLayout pageLayout;
0035     KoColumns columns;
0036 };
0037 
0038 
0039 KoPagePreviewWidget::KoPagePreviewWidget(QWidget *parent)
0040     : QWidget(parent)
0041     , d(new Private)
0042 {
0043     setMinimumSize( 100, 100 );
0044 }
0045 
0046 KoPagePreviewWidget::~KoPagePreviewWidget()
0047 {
0048     delete d;
0049 }
0050 
0051 void KoPagePreviewWidget::paintEvent(QPaintEvent *event)
0052 {
0053     Q_UNUSED(event);
0054     // resolution[XY] is in pixel per pt
0055     qreal resolutionX = POINT_TO_INCH( static_cast<qreal>(KoDpi::dpiX()) );
0056     qreal resolutionY = POINT_TO_INCH( static_cast<qreal>(KoDpi::dpiY()) );
0057 
0058     qreal pageWidth = d->pageLayout.width * resolutionX;
0059     qreal pageHeight = d->pageLayout.height * resolutionY;
0060 
0061     const bool pageSpread = (d->pageLayout.bindingSide >= 0 && d->pageLayout.pageEdge >= 0);
0062     qreal sheetWidth = pageWidth / (pageSpread?2:1);
0063 
0064     qreal zoomH = (height() * 90 / 100) / pageHeight;
0065     qreal zoomW = (width() * 90 / 100) / pageWidth;
0066     qreal zoom = qMin( zoomW, zoomH );
0067 
0068     pageWidth *= zoom;
0069     sheetWidth *= zoom;
0070     pageHeight *= zoom;
0071     QPainter painter( this );
0072 
0073     QRect page = QRectF((width() - pageWidth) / 2.0,
0074             (height() - pageHeight) / 2.0, sheetWidth, pageHeight).toRect();
0075 
0076     painter.save();
0077     drawPage(painter, zoom, page, true);
0078     painter.restore();
0079     if(pageSpread) {
0080         page.moveLeft(page.left() + (int) (sheetWidth));
0081         painter.save();
0082         drawPage(painter, zoom, page, false);
0083         painter.restore();
0084     }
0085 
0086     painter.end();
0087 
0088     // paint scale
0089 }
0090 
0091 void KoPagePreviewWidget::drawPage(QPainter &painter, qreal zoom, const QRect &dimensions, bool left)
0092 {
0093     painter.fillRect(dimensions, QBrush(palette().base()));
0094     painter.setPen(QPen(palette().color(QPalette::Dark), 0));
0095     painter.drawRect(dimensions);
0096 
0097     // draw text areas
0098     QRect textArea = dimensions;
0099     if ((d->pageLayout.topMargin == 0 && d->pageLayout.bottomMargin == 0 &&
0100             d->pageLayout.leftMargin == 0 && d->pageLayout.rightMargin == 0) ||
0101             ( d->pageLayout.pageEdge == 0 && d->pageLayout.bindingSide == 0)) {
0102         // no margin
0103         return;
0104     }
0105     else {
0106         textArea.setTop(textArea.top() + qRound(zoom * d->pageLayout.topMargin));
0107         textArea.setBottom(textArea.bottom() - qRound(zoom * d->pageLayout.bottomMargin));
0108 
0109         qreal leftMargin, rightMargin;
0110         if(d->pageLayout.bindingSide < 0) { // normal margins.
0111             leftMargin = d->pageLayout.leftMargin;
0112             rightMargin = d->pageLayout.rightMargin;
0113         }
0114         else { // margins mirrored for left/right pages
0115             leftMargin = d->pageLayout.bindingSide;
0116             rightMargin = d->pageLayout.pageEdge;
0117             if(left)
0118                 qSwap(leftMargin, rightMargin);
0119         }
0120         textArea.setLeft(textArea.left() + qRound(zoom * leftMargin));
0121         textArea.setRight(textArea.right() - qRound(zoom * rightMargin));
0122     }
0123     painter.setBrush( QBrush( palette().color(QPalette::ButtonText), Qt::HorPattern ) );
0124     painter.setPen(QPen(palette().color(QPalette::Dark), 0));
0125 
0126     // uniform columns?
0127     if (d->columns.columnData.isEmpty()) {
0128         qreal columnWidth = (textArea.width() + (d->columns.gapWidth * zoom)) / d->columns.count;
0129         int width = qRound(columnWidth - d->columns.gapWidth * zoom);
0130         for ( int i = 0; i < d->columns.count; ++i )
0131             painter.drawRect( qRound(textArea.x() + i * columnWidth), textArea.y(), width, textArea.height());
0132     } else {
0133         qreal totalRelativeWidth = 0.0;
0134         foreach(const KoColumns::ColumnDatum &cd, d->columns.columnData) {
0135             totalRelativeWidth += cd.relativeWidth;
0136         }
0137         int relativeColumnXOffset = 0;
0138         for (int i = 0; i < d->columns.count; i++) {
0139             const KoColumns::ColumnDatum &columnDatum = d->columns.columnData.at(i);
0140             const qreal columnWidth = textArea.width() * columnDatum.relativeWidth / totalRelativeWidth;
0141             const qreal columnXOffset = textArea.width() * relativeColumnXOffset / totalRelativeWidth;
0142 
0143             painter.drawRect( qRound(textArea.x() + columnXOffset + columnDatum.leftMargin * zoom),
0144                               qRound(textArea.y()  + columnDatum.topMargin * zoom),
0145                               qRound(columnWidth - (columnDatum.leftMargin + columnDatum.rightMargin) * zoom),
0146                               qRound(textArea.height() - (columnDatum.topMargin + columnDatum.bottomMargin) * zoom));
0147 
0148             relativeColumnXOffset += columnDatum.relativeWidth;
0149         }
0150     }
0151 }
0152 
0153 void KoPagePreviewWidget::setPageLayout(const KoPageLayout &layout)
0154 {
0155     d->pageLayout = layout;
0156     update();
0157 }
0158 
0159 void KoPagePreviewWidget::setColumns(const KoColumns &columns)
0160 {
0161     d->columns = columns;
0162     update();
0163 }
0164