1 /* This file is part of the KDE project
2  * Copyright (C) 2007, 2009 Thomas Zander <zander@kde.org>
3  * Copyright (C) 2010 Boudewijn Rempt <boud@kogmbh.com>
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 "KWPrintingDialog.h"
22 
23 #include "KWDocument.h"
24 #include "KWPageManager.h"
25 #include "KWPage.h"
26 #include "KWView.h"
27 #include "frames/KWTextFrameSet.h"
28 #include "frames/KWFrame.h"
29 
30 #include <KoInsets.h>
31 #include <KoShapeManager.h>
32 #include "KoCanvasBase.h"
33 #include <KoUnit.h>
34 
35 #include <QApplication>
36 #include <QTextBlock>
37 #include <QTextLayout>
38 #include <QTextDocument>
39 
KWPrintingDialog(KWDocument * document,KoShapeManager * shapeManager,KWView * view)40 KWPrintingDialog::KWPrintingDialog(KWDocument *document, KoShapeManager *shapeManager, KWView *view)
41     : KoPrintingDialog(view)
42     , m_document(document)
43     , m_view(view)
44 {
45     setShapeManager(shapeManager);
46 
47     while (! m_document->layoutFinishedAtleastOnce()) {
48         QCoreApplication::processEvents();
49         if (! QCoreApplication::hasPendingEvents())
50             break;
51     }
52     printer().setFromTo(documentFirstPage(), documentLastPage());
53 }
54 
~KWPrintingDialog()55 KWPrintingDialog::~KWPrintingDialog()
56 {
57 }
58 
preparePage(int pageNumber)59 QRectF KWPrintingDialog::preparePage(int pageNumber)
60 {
61     const int resolution = printer().resolution();
62     KWPage page = m_document->pageManager()->page(pageNumber);
63     if (! page.isValid())
64         return QRectF();
65     printer().setPaperSize(page.rect().size(), QPrinter::Point);
66 
67     KoInsets bleed = m_document->pageManager()->padding();
68     const int bleedOffsetX = qRound(POINT_TO_INCH(bleed.left * resolution));
69     const int bleedOffsetY = qRound(POINT_TO_INCH(bleed.top * resolution));
70     const int bleedWidth = qRound(POINT_TO_INCH((bleed.left + bleed.right) * resolution));
71     const int bleedHeight = qRound(POINT_TO_INCH((bleed.top + bleed.bottom) * resolution));
72 
73     const int pageOffset = qRound(POINT_TO_INCH(resolution * page.offsetInDocument()));
74     painter().translate(0, -pageOffset);
75 
76     const int clipHeight = (int) POINT_TO_INCH(resolution * page.height());
77     int clipWidth = (int) POINT_TO_INCH(resolution * page.width());
78     int offsetX = -bleedOffsetX;
79 
80     return QRectF(offsetX, pageOffset - bleedOffsetY, clipWidth + bleedWidth, clipHeight + bleedHeight);
81 }
82 
shapesOnPage(int pageNumber)83 QList<KoShape*> KWPrintingDialog::shapesOnPage(int pageNumber)
84 {
85     Q_ASSERT(pageNumber > 0);
86     KWPage page = m_document->pageManager()->page(pageNumber);
87     Q_ASSERT(page.isValid());
88     return shapeManager()->shapesAt(page.rect());
89 }
90 
printingDone()91 void KWPrintingDialog::printingDone()
92 {
93 }
94 
createOptionWidgets() const95 QList<QWidget*> KWPrintingDialog::createOptionWidgets() const
96 {
97     return QList<QWidget*>();
98 }
99 
documentFirstPage() const100 int KWPrintingDialog::documentFirstPage() const
101 {
102     return m_document->pageManager()->begin().pageNumber();
103 }
104 
documentLastPage() const105 int KWPrintingDialog::documentLastPage() const
106 {
107     KWPage lastPage = m_document->pageManager()->last();
108     return lastPage.pageNumber();
109 }
110 
documentCurrentPage() const111 int KWPrintingDialog::documentCurrentPage() const
112 {
113     return m_view->currentPage().pageNumber();
114 }
115 
printDialogOptions() const116 QAbstractPrintDialog::PrintDialogOptions KWPrintingDialog::printDialogOptions() const
117 {
118     return QAbstractPrintDialog::PrintToFile |
119            QAbstractPrintDialog::PrintPageRange |
120            QAbstractPrintDialog::PrintCurrentPage |
121            QAbstractPrintDialog::PrintCollateCopies |
122            QAbstractPrintDialog::DontUseSheet |
123            QAbstractPrintDialog::PrintShowPageSize;
124 }
125 
126 // options;
127 //   DPI
128 //   fontEmbeddingEnabled
129 
130