Warning, file /office/calligra/libs/main/KoPrintingDialog.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, 2009 Thomas Zander <zander@kde.org>
0003  * Copyright (C) 2011 Boudewijn Rempt <boud@kde.org>
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 #include "KoPrintingDialog.h"
0021 #include "KoPrintingDialog_p.h"
0022 #include "KoProgressUpdater.h"
0023 
0024 #include <KoZoomHandler.h>
0025 #include <KoShapeManager.h>
0026 #include <KoShape.h>
0027 #include <KoProgressBar.h>
0028 
0029 #include <QCoreApplication>
0030 #include <MainDebug.h>
0031 #include <klocalizedstring.h>
0032 #include <QPainter>
0033 #include <QPrinter>
0034 #include <QGridLayout>
0035 #include <QLabel>
0036 #include <QPushButton>
0037 #include <QTimer>
0038 #include <QDialog>
0039 #include <QThread>
0040 
0041 class PrintDialog : public QDialog {
0042 public:
0043     PrintDialog(KoPrintingDialogPrivate *d, QWidget *parent)
0044         : QDialog(parent)
0045     {
0046         setModal(true);
0047         QGridLayout *grid = new QGridLayout(this);
0048         setLayout(grid);
0049 
0050         d->pageNumber = new QLabel(this);
0051         d->pageNumber->setMinimumWidth(200);
0052         grid->addWidget(d->pageNumber, 0, 0, 1, 2);
0053         KoProgressBar *bar = new KoProgressBar(this);
0054         d->progress = new KoProgressUpdater(bar);
0055         grid->addWidget(bar, 1, 0, 1, 2);
0056         d->button = new QPushButton(i18n("Stop"), this);
0057         grid->addWidget(d->button, 2, 1);
0058         grid->setColumnStretch(0, 1);
0059     }
0060 };
0061 
0062 
0063 KoPrintingDialog::KoPrintingDialog(QWidget *parent)
0064     : KoPrintJob(parent),
0065       d(new KoPrintingDialogPrivate(this))
0066 {
0067     d->dialog = new PrintDialog(d, parent);
0068 
0069     connect(d->button, SIGNAL(released()), this, SLOT(stopPressed()));
0070 }
0071 
0072 KoPrintingDialog::~KoPrintingDialog()
0073 {
0074     d->stopPressed();
0075     delete d;
0076 }
0077 
0078 void KoPrintingDialog::setShapeManager(KoShapeManager *sm)
0079 {
0080     d->shapeManager = sm;
0081 }
0082 
0083 KoShapeManager *KoPrintingDialog::shapeManager() const
0084 {
0085     return d->shapeManager;
0086 }
0087 
0088 void KoPrintingDialog::setPageRange(const QList<int> &pages)
0089 {
0090     if (d->index == 0) // can't change after we started
0091         d->pageRange = pages;
0092 }
0093 
0094 QPainter & KoPrintingDialog::painter() const
0095 {
0096     if (d->painter == 0) {
0097         d->painter = new QPainter(d->printer);
0098         d->painter->save(); // state before page preparation (3)
0099     }
0100     return *d->painter;
0101 }
0102 
0103 bool KoPrintingDialog::isStopped() const
0104 {
0105     return d->stop;
0106 }
0107 
0108 void KoPrintingDialog::startPrinting(RemovePolicy removePolicy)
0109 {
0110     d->removePolicy = removePolicy;
0111     d->pages = d->pageRange;
0112     if (d->pages.isEmpty()) { // auto-fill from min/max
0113         switch (d->printer->printRange()) {
0114         case QPrinter::AllPages:
0115             for (int i=documentFirstPage(); i <= documentLastPage(); i++)
0116                 d->pages.append(i);
0117             break;
0118         case QPrinter::PageRange:
0119             for (int i=d->printer->fromPage(); i <= d->printer->toPage(); i++)
0120                 d->pages.append(i);
0121             break;
0122         case QPrinter::CurrentPage:
0123             d->pages.append(documentCurrentPage());
0124             break;
0125         default:
0126             return;
0127         }
0128     }
0129     if (d->pages.isEmpty()) {
0130         qWarning(/*30004*/) << "KoPrintingDialog::startPrinting: No pages to print, did you forget to call setPageRange()?";
0131         return;
0132     }
0133 
0134     const bool blocking = property("blocking").toBool();
0135     const bool noprogressdialog = property("noprogressdialog").toBool();
0136     if (d->index == 0 && d->pages.count() > 0 && d->printer) {
0137         if (!blocking && !noprogressdialog)
0138             d->dialog->show();
0139         d->stop = false;
0140         delete d->painter;
0141         d->painter = 0;
0142         d->zoomer.setZoom( 1.0 );
0143         d->zoomer.setDpi( d->printer->resolution(), d->printer->resolution() );
0144 
0145         d->progress->start(100, i18n("Printing"));
0146 
0147         if (d->printer->numCopies() > 1) {
0148             QList<int> oldPages = d->pages;
0149             if (d->printer->collateCopies()) { // means we print whole doc at once
0150                 for (int count = 1; count < d->printer->numCopies(); ++count)
0151                     d->pages.append(oldPages);
0152             } else {
0153                 d->pages.clear();
0154                 foreach (int page, oldPages) {
0155                     for (int count = 1; count < d->printer->numCopies(); ++count)
0156                         d->pages.append(page);
0157                 }
0158             }
0159         }
0160         if (d->printer->pageOrder() == QPrinter::LastPageFirst) {
0161             const QList<int> pages = d->pages;
0162             d->pages.clear();
0163             QList<int>::ConstIterator iter = pages.end();
0164             do {
0165                 --iter;
0166                 d->pages << *iter;
0167             } while (iter != pages.begin());
0168         }
0169 
0170 
0171         d->resetValues();
0172         foreach (int page, d->pages) {
0173             d->index++;
0174             d->updaters.append(d->progress->startSubtask()); // one per page
0175             d->preparePage(page);
0176             d->printPage(page);
0177             if (!blocking) {
0178                 qApp->processEvents();
0179             }
0180             
0181         }
0182         d->painter->end();
0183         if (blocking) {
0184             printingDone();
0185         }
0186         else {
0187             d->printingDone();
0188         }
0189         d->stop = true;
0190         d->resetValues();
0191     }
0192 }
0193 
0194 QPrinter &KoPrintingDialog::printer()
0195 {
0196     return *d->printer;
0197 }
0198 
0199 void KoPrintingDialog::printPage(int, QPainter &)
0200 {
0201 }
0202 
0203 QRectF KoPrintingDialog::preparePage(int)
0204 {
0205     return QRectF();
0206 }
0207 
0208 // have to include this because of Q_PRIVATE_SLOT
0209 #include <moc_KoPrintingDialog.cpp>