File indexing completed on 2024-05-19 15:27:53

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 "simpleprintpreviewwindow.h"
0029 #include "kgraphviewerlib_debug.h"
0030 #include "simpleprintingengine.h"
0031 #include "simpleprintpreviewwindow_p.h"
0032 // #include <kexi_version.h>
0033 
0034 #include <qlabel.h>
0035 #include <qlayout.h>
0036 #include <qtimer.h>
0037 // Added by qt3to4:
0038 #include <QDesktopWidget>
0039 #include <QEvent>
0040 #include <QKeyEvent>
0041 #include <QPixmap>
0042 #include <QVBoxLayout>
0043 
0044 #include <QAction>
0045 #include <QApplication>
0046 #include <QDebug>
0047 #include <QDialog>
0048 #include <QIcon>
0049 #include <QPushButton>
0050 #include <QToolButton>
0051 #include <kparts/mainwindow.h>
0052 #include <kstandardaction.h>
0053 
0054 #include <iostream>
0055 #include <klocalizedstring.h>
0056 
0057 namespace KGraphViewer
0058 {
0059 KGVSimplePrintPreviewWindow::KGVSimplePrintPreviewWindow(KGVSimplePrintingEngine &engine, const QString &previewName, QWidget *parent)
0060     : QWidget(parent)
0061     , m_engine(engine)
0062     , m_settings(m_engine.settings())
0063     , m_pageNumber(-1)
0064     , m_actions(this)
0065 {
0066     setObjectName("KGVSimplePrintPreviewWindow");
0067     //  m_pagesCount = INT_MAX;
0068 
0069     setWindowTitle(i18n("%1 - Print Preview - %2", previewName, QString("")));
0070     setWindowIcon(QIcon::fromTheme(QLatin1String("document-print-preview")));
0071     QVBoxLayout *lyr = new QVBoxLayout();
0072 
0073     m_toolbar = new KToolBar(this);
0074     m_toolbar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0075     lyr->addWidget(m_toolbar);
0076 
0077     QAction *printAction = KStandardAction::print(this, SLOT(slotPrintClicked()), &m_actions);
0078     m_toolbar->addAction((QAction *)printAction);
0079     /// @todo handle the accelerator
0080     //  static_cast<KPushButton*>(m_toolbar->getWidget(id))->setAccel(Qt::CTRL|Qt::Key_P);
0081     /// @todo add the separator
0082     //  m_toolbar->addSeparator();
0083 
0084     QAction *pageSetupAction = new QAction(i18n("&Page setup"), this);
0085     //                                &m_actions, "file_page_setup");
0086     connect(pageSetupAction, &QAction::triggered, this, &KGVSimplePrintPreviewWindow::slotPageSetup);
0087     m_toolbar->addAction((QAction *)pageSetupAction);
0088 
0089     //  id = m_toolbar->insertWidget(-1, 0, new KPushButton(i18n("Page Set&up..."), m_toolbar));
0090     /// @todo handle the accelerator
0091     //  m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotPageSetup()));
0092     /// @todo add the separator
0093     //  m_toolbar->addSeparator();
0094 
0095 #ifndef KGV_NO_UNFINISHED
0096 //! @todo unfinished
0097 /*  id = m_toolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("viewmag+"), i18n("Zoom In"), m_toolbar));
0098     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotZoomInClicked()));
0099     m_toolbar->addSeparator();
0100 
0101     id = m_toolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("viewmag-"), i18n("Zoom Out"), m_toolbar));
0102     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotZoomOutClicked()));
0103     m_toolbar->addSeparator();*/
0104 #endif
0105 
0106     QAction *closeAction = KStandardAction::close(this, SLOT(close()), &m_actions);
0107     m_toolbar->addAction((QAction *)closeAction);
0108 
0109     m_scrollView = new KGVSimplePrintPreviewScrollView(this);
0110     m_scrollView->setUpdatesEnabled(true);
0111     m_view = (KGVSimplePrintPreviewView *)m_scrollView->widget();
0112     m_scrollView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0113     lyr->addWidget(m_scrollView);
0114 
0115     m_navToolbar = new KToolBar(this);
0116     m_navToolbar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0117     lyr->addWidget(m_navToolbar);
0118 
0119     QAction *firstPageAction = KStandardAction::firstPage(this, SLOT(slotFirstClicked()), &m_actions);
0120     m_navToolbar->addAction((QAction *)firstPageAction);
0121     m_navToolbar->addSeparator();
0122 
0123     QAction *previousAction = new QAction(i18n("Previous Page"), this);
0124     //                                &m_actions,  "prevpage");
0125     connect(previousAction, &QAction::triggered, this, &KGVSimplePrintPreviewWindow::slotPreviousClicked);
0126     m_navToolbar->addAction((QAction *)previousAction);
0127     m_navToolbar->addSeparator();
0128 
0129     m_pageNumberLabel = new QLabel(m_navToolbar);
0130     m_navToolbar->addWidget(m_pageNumberLabel);
0131     m_navToolbar->addSeparator();
0132 
0133     QAction *nextAction = new QAction(i18n("Next Page"), this); //&m_actions, "nextpage");
0134     connect(nextAction, &QAction::triggered, this, &KGVSimplePrintPreviewWindow::slotNextClicked);
0135     m_navToolbar->addAction((QAction *)nextAction);
0136     m_navToolbar->addSeparator();
0137 
0138     QAction *lastPageAction = KStandardAction::lastPage(this, SLOT(slotLastClicked()), this);
0139     //                                       &m_actions);
0140     m_navToolbar->addAction((QAction *)lastPageAction);
0141     m_navToolbar->addSeparator();
0142 
0143     resize(width(), qApp->desktop()->height() * 4 / 5);
0144 
0145     this->setLayout(lyr);
0146     //! @todo progress bar...
0147 
0148     QTimer::singleShot(50, this, SLOT(initLater()));
0149 }
0150 
0151 void KGVSimplePrintPreviewWindow::initLater()
0152 {
0153     qCDebug(KGRAPHVIEWERLIB_LOG);
0154     setFullWidth();
0155     updatePagesCount();
0156     goToPage(0);
0157 }
0158 
0159 KGVSimplePrintPreviewWindow::~KGVSimplePrintPreviewWindow()
0160 {
0161 }
0162 
0163 void KGVSimplePrintPreviewWindow::slotPrintClicked()
0164 {
0165     hide();
0166     emit printRequested();
0167     show();
0168     raise();
0169 }
0170 
0171 void KGVSimplePrintPreviewWindow::slotPageSetup()
0172 {
0173     lower();
0174     emit pageSetupRequested();
0175 }
0176 
0177 void KGVSimplePrintPreviewWindow::slotZoomInClicked()
0178 {
0179     //! @todo
0180 }
0181 
0182 void KGVSimplePrintPreviewWindow::slotZoomOutClicked()
0183 {
0184     //! @todo
0185 }
0186 
0187 void KGVSimplePrintPreviewWindow::slotFirstClicked()
0188 {
0189     goToPage(0);
0190 }
0191 
0192 void KGVSimplePrintPreviewWindow::slotPreviousClicked()
0193 {
0194     goToPage(m_pageNumber - 1);
0195 }
0196 
0197 void KGVSimplePrintPreviewWindow::slotNextClicked()
0198 {
0199     goToPage(m_pageNumber + 1);
0200 }
0201 
0202 void KGVSimplePrintPreviewWindow::slotLastClicked()
0203 {
0204     goToPage(m_engine.pagesCount() - 1);
0205 }
0206 
0207 void KGVSimplePrintPreviewWindow::slotRedraw()
0208 {
0209     m_engine.clear();
0210     setFullWidth();
0211     updatePagesCount();
0212     m_pageNumber = 0;
0213     m_view->repaint(); // this will automatically paint a new page
0214 
0215     //   m_navToolbar->setItemEnabled(m_idNext, m_pageNumber < ((int)m_engine.pagesCount()-1));
0216     //   m_navToolbar->setItemEnabled(m_idLast, m_pageNumber < ((int)m_engine.pagesCount()-1));
0217     //   m_navToolbar->setItemEnabled(m_idPrevious, m_pageNumber > 0);
0218     //   m_navToolbar->setItemEnabled(m_idFirst, m_pageNumber > 0);
0219     m_pageNumberLabel->setText(i18nc("Page (number) of (total)", "Page %1 of %2", m_pageNumber + 1, m_engine.pagesCount()));
0220 }
0221 
0222 void KGVSimplePrintPreviewWindow::goToPage(int pageNumber)
0223 {
0224     qCDebug(KGRAPHVIEWERLIB_LOG) << pageNumber;
0225     if (pageNumber == m_pageNumber || pageNumber < 0 || pageNumber > ((int)m_engine.pagesCount() - 1))
0226         return;
0227     m_pageNumber = pageNumber;
0228 
0229     m_view->repaint(); // this will automatically paint a new page
0230     //  if (m_engine.eof())
0231     //      m_pagesCount = pageNumber+1;
0232 
0233     //  m_navToolbar->setItemEnabled(m_idNext, pageNumber < ((int)m_engine.pagesCount()-1));
0234     //  m_navToolbar->setItemEnabled(m_idLast, pageNumber < ((int)m_engine.pagesCount()-1));
0235     //  m_navToolbar->setItemEnabled(m_idPrevious, pageNumber > 0);
0236     //  m_navToolbar->setItemEnabled(m_idFirst, pageNumber > 0);
0237     m_pageNumberLabel->setText(i18nc("Page (number) of (total)", "Page %1 of %2", m_pageNumber + 1, m_engine.pagesCount()));
0238 }
0239 
0240 void KGVSimplePrintPreviewWindow::setFullWidth()
0241 {
0242     m_scrollView->setFullWidth();
0243 }
0244 
0245 void KGVSimplePrintPreviewWindow::updatePagesCount()
0246 {
0247     QPainter p(this);
0248     //   QPainter p(m_view);
0249     //   p.begin(this);
0250     m_engine.calculatePagesCount(p);
0251     //   p.end();
0252 }
0253 
0254 bool KGVSimplePrintPreviewWindow::event(QEvent *e)
0255 {
0256     QEvent::Type t = e->type();
0257     if (t == QEvent::KeyPress) {
0258         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
0259         const int k = ke->key();
0260         bool ok = true;
0261         if (k == Qt::Key_Equal || k == Qt::Key_Plus)
0262             slotZoomInClicked();
0263         else if (k == Qt::Key_Minus)
0264             slotZoomOutClicked();
0265         else if (k == Qt::Key_Home)
0266             slotFirstClicked();
0267         else if (k == Qt::Key_End)
0268             slotLastClicked();
0269         else
0270             ok = false;
0271 
0272         if (ok) {
0273             ke->accept();
0274             return true;
0275         }
0276     } else if (t == QEvent::ShortcutOverride) {
0277         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
0278         const int k = ke->key();
0279         bool ok = true;
0280         if (k == Qt::Key_PageUp)
0281             slotPreviousClicked();
0282         else if (k == Qt::Key_PageDown)
0283             slotNextClicked();
0284         else
0285             ok = false;
0286 
0287         if (ok) {
0288             ke->accept();
0289             return true;
0290         }
0291     }
0292     return QWidget::event(e);
0293 }
0294 
0295 }
0296 
0297 #include "moc_simpleprintpreviewwindow.cpp"