~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to src/libs/widgets/KoPagePreviewWidget.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 */
 
20
 
 
21
#include "KoPagePreviewWidget.h"
 
22
 
 
23
#include <KoDpi.h>
 
24
#include <KoUnit.h>
 
25
#include <KoPageLayout.h>
 
26
#include <KoColumns.h>
 
27
 
 
28
#include <QPainter>
 
29
#include <WidgetsDebug.h>
 
30
 
 
31
class Q_DECL_HIDDEN KoPagePreviewWidget::Private
 
32
{
 
33
public:
 
34
    KoPageLayout pageLayout;
 
35
    KoColumns columns;
 
36
};
 
37
 
 
38
 
 
39
KoPagePreviewWidget::KoPagePreviewWidget(QWidget *parent)
 
40
    : QWidget(parent)
 
41
    , d(new Private)
 
42
{
 
43
    setMinimumSize( 100, 100 );
 
44
}
 
45
 
 
46
KoPagePreviewWidget::~KoPagePreviewWidget()
 
47
{
 
48
    delete d;
 
49
}
 
50
 
 
51
void KoPagePreviewWidget::paintEvent(QPaintEvent *event)
 
52
{
 
53
    Q_UNUSED(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()) );
 
57
 
 
58
    qreal pageWidth = d->pageLayout.width * resolutionX;
 
59
    qreal pageHeight = d->pageLayout.height * resolutionY;
 
60
 
 
61
    const bool pageSpread = (d->pageLayout.bindingSide >= 0 && d->pageLayout.pageEdge >= 0);
 
62
    qreal sheetWidth = pageWidth / (pageSpread?2:1);
 
63
 
 
64
    qreal zoomH = (height() * 90 / 100) / pageHeight;
 
65
    qreal zoomW = (width() * 90 / 100) / pageWidth;
 
66
    qreal zoom = qMin( zoomW, zoomH );
 
67
 
 
68
    pageWidth *= zoom;
 
69
    sheetWidth *= zoom;
 
70
    pageHeight *= zoom;
 
71
    QPainter painter( this );
 
72
 
 
73
    QRect page = QRectF((width() - pageWidth) / 2.0,
 
74
            (height() - pageHeight) / 2.0, sheetWidth, pageHeight).toRect();
 
75
 
 
76
    painter.save();
 
77
    drawPage(painter, zoom, page, true);
 
78
    painter.restore();
 
79
    if(pageSpread) {
 
80
        page.moveLeft(page.left() + (int) (sheetWidth));
 
81
        painter.save();
 
82
        drawPage(painter, zoom, page, false);
 
83
        painter.restore();
 
84
    }
 
85
 
 
86
    painter.end();
 
87
 
 
88
    // paint scale
 
89
}
 
90
 
 
91
void KoPagePreviewWidget::drawPage(QPainter &painter, qreal zoom, const QRect &dimensions, bool left)
 
92
{
 
93
    painter.fillRect(dimensions, QBrush(palette().base()));
 
94
    painter.setPen(QPen(palette().color(QPalette::Dark), 0));
 
95
    painter.drawRect(dimensions);
 
96
 
 
97
    // draw text areas
 
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)) {
 
102
        // no margin
 
103
        return;
 
104
    }
 
105
    else {
 
106
        textArea.setTop(textArea.top() + qRound(zoom * d->pageLayout.topMargin));
 
107
        textArea.setBottom(textArea.bottom() - qRound(zoom * d->pageLayout.bottomMargin));
 
108
 
 
109
        qreal leftMargin, rightMargin;
 
110
        if(d->pageLayout.bindingSide < 0) { // normal margins.
 
111
            leftMargin = d->pageLayout.leftMargin;
 
112
            rightMargin = d->pageLayout.rightMargin;
 
113
        }
 
114
        else { // margins mirrored for left/right pages
 
115
            leftMargin = d->pageLayout.bindingSide;
 
116
            rightMargin = d->pageLayout.pageEdge;
 
117
            if(left)
 
118
                qSwap(leftMargin, rightMargin);
 
119
        }
 
120
        textArea.setLeft(textArea.left() + qRound(zoom * leftMargin));
 
121
        textArea.setRight(textArea.right() - qRound(zoom * rightMargin));
 
122
    }
 
123
    painter.setBrush( QBrush( palette().color(QPalette::ButtonText), Qt::HorPattern ) );
 
124
    painter.setPen(QPen(palette().color(QPalette::Dark), 0));
 
125
 
 
126
    // uniform columns?
 
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());
 
132
    } else {
 
133
        qreal totalRelativeWidth = 0.0;
 
134
        foreach(const KoColumns::ColumnDatum &cd, d->columns.columnData) {
 
135
            totalRelativeWidth += cd.relativeWidth;
 
136
        }
 
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;
 
142
 
 
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));
 
147
 
 
148
            relativeColumnXOffset += columnDatum.relativeWidth;
 
149
        }
 
150
    }
 
151
}
 
152
 
 
153
void KoPagePreviewWidget::setPageLayout(const KoPageLayout &layout)
 
154
{
 
155
    d->pageLayout = layout;
 
156
    update();
 
157
}
 
158
 
 
159
void KoPagePreviewWidget::setColumns(const KoColumns &columns)
 
160
{
 
161
    d->columns = columns;
 
162
    update();
 
163
}
 
164