File indexing completed on 2024-05-19 15:27:52
0001 /* This file is part of KGraphViewer. 0002 Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr> 0003 0004 KGraphViewer is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU General Public 0006 License as published by the Free Software Foundation, version 2. 0007 0008 This program is distributed in the hope that it will be useful, 0009 but WITHOUT ANY WARRANTY; without even the implied warranty of 0010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0011 General Public License for more details. 0012 0013 You should have received a copy of the GNU General Public License 0014 along with this program; if not, write to the Free Software 0015 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0016 02110-1301, USA 0017 */ 0018 0019 /* This file was part of the KDE project 0020 Copyright (C) 2005 Jarosław Staniek <staniek@kde.org> 0021 0022 This program is free software; you can redistribute it and/or 0023 modify it under the terms of the GNU Library General Public 0024 License as published by the Free Software Foundation; either 0025 version 2 of the License, or (at your option) any later version. 0026 */ 0027 0028 #include "simpleprintingcommand.h" 0029 #include "kgraphviewerlib_debug.h" 0030 #include "simpleprintingpagesetup.h" 0031 #include "simpleprintingsettings.h" 0032 #include "simpleprintpreviewwindow.h" 0033 0034 // #include <core/keximainwindow.h> 0035 // #include <kexiutils/utils.h> 0036 // #include <kexi_version.h> 0037 0038 #include <QApplication> 0039 #include <QFontDialog> 0040 #include <QIcon> 0041 #include <QInputDialog> 0042 #include <QLineEdit> 0043 #include <QMessageBox> 0044 #include <QPrintDialog> 0045 #include <QPushButton> 0046 #include <QStandardPaths> 0047 #include <kurllabel.h> 0048 0049 #include <QPrintDialog> 0050 #include <QPrinter> 0051 #include <qcheckbox.h> 0052 #include <qlabel.h> 0053 #include <qlayout.h> 0054 #include <qpainter.h> 0055 #include <qslider.h> 0056 #include <qtimer.h> 0057 #include <qtooltip.h> 0058 // Added by qt3to4: 0059 #include <QPageSize> 0060 #include <QVBoxLayout> 0061 #include <iostream> 0062 #include <klocalizedstring.h> 0063 0064 namespace KGraphViewer 0065 { 0066 KGVSimplePrintingCommand::KGVSimplePrintingCommand(DotGraphView *mainWin, int objectId, QObject *parent) 0067 : QObject(parent) 0068 , m_previewEngine(nullptr) 0069 , m_graphView(mainWin) 0070 , m_objectId(objectId) 0071 , m_settings(new KGVSimplePrintingSettings(KGVSimplePrintingSettings::load())) 0072 , m_previewWindow(nullptr) 0073 , m_printPreviewNeedsReloading(true) 0074 , m_pageSetupDialog(nullptr) 0075 { 0076 setObjectName("KGVSimplePrintCommand"); 0077 connect(this, &KGVSimplePrintingCommand::showPageSetupRequested, this, &KGVSimplePrintingCommand::slotShowPageSetupRequested); 0078 } 0079 0080 KGVSimplePrintingCommand::~KGVSimplePrintingCommand() 0081 { 0082 delete m_previewWindow; 0083 delete m_previewEngine; 0084 delete m_settings; 0085 } 0086 0087 bool KGVSimplePrintingCommand::init(const QString &aTitleText) 0088 { 0089 if (!m_previewEngine) 0090 m_previewEngine = new KGVSimplePrintingEngine(m_settings, this); 0091 0092 QString titleText(aTitleText.trimmed()); 0093 if (!m_previewWindow) { 0094 QString errorMessage; 0095 if (!m_previewEngine->init(*m_graphView, titleText, errorMessage)) { 0096 if (!errorMessage.isEmpty()) 0097 QMessageBox::warning(m_graphView, i18n("Print Preview"), errorMessage); 0098 return false; 0099 } 0100 m_previewWindow = new KGVSimplePrintPreviewWindow(*m_previewEngine, QString(), nullptr); 0101 connect(m_previewWindow, &KGVSimplePrintPreviewWindow::printRequested, this, [&]() { print(); }); 0102 connect(m_previewWindow, &KGVSimplePrintPreviewWindow::pageSetupRequested, this, &KGVSimplePrintingCommand::slotShowPageSetupRequested); 0103 // KDialog::centerOnScreen(m_previewWindow); 0104 m_printPreviewNeedsReloading = false; 0105 } 0106 return true; 0107 } 0108 0109 bool KGVSimplePrintingCommand::print(const QString &aTitleText) 0110 { 0111 init(aTitleText); 0112 m_previewEngine->clear(); 0113 0114 // setup printing 0115 QPrinter printer; 0116 printer.setOrientation(m_settings->pageLayout.orientation == PG_PORTRAIT ? QPrinter::Portrait : QPrinter::Landscape); 0117 printer.setPageSize(QPageSize((QPageSize::PageSizeId)KgvPageFormat::printerPageSize(m_settings->pageLayout.format))); 0118 // #endif 0119 0120 printer.setFullPage(true); 0121 QString docName(aTitleText); 0122 printer.setDocName(docName); 0123 printer.setCreator("kgraphviewer"); 0124 QPointer<QPrintDialog> dlg = new QPrintDialog(&printer, m_graphView); 0125 if (dlg->exec() != QDialog::Accepted) { 0126 return true; 0127 } 0128 0129 // now we have final settings 0130 0131 //! @todo get printer.pageOrder() (for reversed order requires improved engine) 0132 QPainter painter; 0133 0134 if (!painter.begin(&printer)) { 0135 //! @todo msg 0136 return false; 0137 } 0138 m_previewEngine->calculatePagesCount(painter); 0139 0140 uint loops, loopsPerPage; 0141 QList<int> pagesToPrint; 0142 int fromPage = 0; 0143 // on !win32 print QPrinter::numCopies() times (the OS does not perform buffering) 0144 // pagesToPrint = printer.pageList(); 0145 // qCDebug(KGRAPHVIEWERLIB_LOG) << pagesToPrint; 0146 if (pagesToPrint.isEmpty()) { 0147 fromPage = 0; 0148 for (int i = 0; i < (int)m_previewEngine->pagesCount(); i++) { 0149 // std::cerr << "Page " << i << " has to be printed" << std::endl; 0150 pagesToPrint.append(i); 0151 } 0152 } else 0153 fromPage = pagesToPrint.first(); 0154 if (printer.collateCopies()) { 0155 // collation: p1, p2,..pn; p1, p2,..pn; ......; p1, p2,..pn 0156 loops = printer.numCopies(); 0157 loopsPerPage = 1; 0158 } else { 0159 // no collation: p1, p1, ..., p1; p2, p2, ..., p2; ......; pn, pn,..pn 0160 loops = 1; 0161 loopsPerPage = printer.numCopies(); 0162 } 0163 //! @todo also look at printer.pageSet() option : all/odd/even pages 0164 // #endif 0165 // now, total number of printed pages is printer.numCopies()*printer.pageList().count() 0166 0167 // qCDebug(KGRAPHVIEWERLIB_LOG) << "printing..."; 0168 bool firstPage = true; 0169 for (uint copy = 0; copy < loops; copy++) { 0170 // qCDebug(KGRAPHVIEWERLIB_LOG) << "copy " << (copy+1) << " of " << loops; 0171 // std::cerr << "fromPage = " << fromPage << " ; eof = " << m_previewEngine->eof() << std::endl; 0172 uint pageNumber = fromPage; 0173 QList<int>::ConstIterator pagesIt = pagesToPrint.constBegin(); 0174 for (; (int)pageNumber == fromPage || !m_previewEngine->eof(); ++pageNumber) { 0175 // std::cerr << "printing..." << std::endl; 0176 if (pagesIt == pagesToPrint.constEnd()) // no more pages to print 0177 break; 0178 if ((int)pageNumber < *pagesIt) { // skip pages without printing (needed for computation) 0179 m_previewEngine->paintPage(pageNumber, painter, false); 0180 continue; 0181 } 0182 if (*pagesIt < (int)pageNumber) { // sanity 0183 ++pagesIt; 0184 continue; 0185 } 0186 for (uint onePageCounter = 0; onePageCounter < loopsPerPage; onePageCounter++) { 0187 if (!firstPage) 0188 printer.newPage(); 0189 else 0190 firstPage = false; 0191 // std::cerr << "page #" << pageNumber << std::endl; 0192 m_previewEngine->paintPage(pageNumber, painter); 0193 } 0194 ++pagesIt; 0195 } 0196 } 0197 // qCDebug(KGRAPHVIEWERLIB_LOG) << "end of printing."; 0198 0199 // stop painting, this will automatically send the print data to the printer 0200 if (!painter.end()) 0201 return false; 0202 0203 // if (!m_previewEngine->done()) 0204 // return false; 0205 0206 return true; 0207 } 0208 0209 bool KGVSimplePrintingCommand::showPrintPreview(const QString &aTitleText, bool reload) 0210 { 0211 init(aTitleText); 0212 0213 if (reload) 0214 m_printPreviewNeedsReloading = true; 0215 0216 if (m_printPreviewNeedsReloading) { // dirty 0217 m_previewEngine->clear(); 0218 //! @todo progress bar... 0219 m_previewEngine->setTitleText(aTitleText); 0220 m_previewWindow->setFullWidth(); 0221 m_previewWindow->updatePagesCount(); 0222 m_printPreviewNeedsReloading = false; 0223 m_previewWindow->goToPage(0); 0224 } 0225 m_previewWindow->show(); 0226 m_previewWindow->raise(); 0227 return true; 0228 } 0229 0230 void KGVSimplePrintingCommand::hidePageSetup() 0231 { 0232 if (m_pageSetupDialog) { 0233 m_pageSetupDialog->hide(); 0234 } 0235 } 0236 0237 void KGVSimplePrintingCommand::hidePrintPreview() 0238 { 0239 if (m_previewWindow) { 0240 m_previewWindow->hide(); 0241 } 0242 } 0243 0244 void KGVSimplePrintingCommand::slotShowPageSetupRequested() 0245 { 0246 if (m_pageSetupDialog == nullptr) { 0247 m_pageSetupDialog = new QDialog(nullptr); 0248 QMap<QString, QString> map; 0249 map["action"] = "pageSetup"; 0250 map["title"] = m_graphView->dotFileName(); 0251 QVBoxLayout *lyr = new QVBoxLayout(m_pageSetupDialog); 0252 KGVSimplePrintingPageSetup *sppsb = new KGVSimplePrintingPageSetup(this, m_graphView, m_pageSetupDialog, &map); 0253 if (m_previewWindow) { 0254 connect(sppsb, &KGVSimplePrintingPageSetup::needsRedraw, m_previewWindow, &KGVSimplePrintPreviewWindow::slotRedraw); 0255 } 0256 lyr->addWidget(sppsb); 0257 } 0258 m_pageSetupDialog->show(); 0259 m_pageSetupDialog->raise(); 0260 } 0261 0262 void KGVSimplePrintingCommand::showPageSetup(const QString &aTitleText) 0263 { 0264 init(aTitleText); 0265 emit showPageSetupRequested(); 0266 } 0267 0268 } 0269 0270 #include "moc_simpleprintingcommand.cpp"