File indexing completed on 2024-04-28 04:33:44

0001 /*******************************************************************************
0002  * Copyright (C) 2020 by Steve Allewell                                        *
0003  * steve.allewell@gmail.com                                                    *
0004  *                                                                             *
0005  * This program is free software; you can redistribute it and/or modify        *
0006  * it under the terms of the GNU General Public License as published by        *
0007  * the Free Software Foundation; either version 2 of the License, or           *
0008  * (at your option) any later version.                                         *
0009  ******************************************************************************/
0010 
0011 
0012 #include "MainWindow.h"
0013 
0014 #include <QFileDialog>
0015 #include <QTabWidget>
0016 #include <QUrl>
0017 
0018 #include <KActionCollection>
0019 #include <KSharedConfig>
0020 #include <KConfigGroup>
0021 #include <KLocalizedString>
0022 #include <KRecentFilesAction>
0023 
0024 #include <poppler-qt5.h>
0025 
0026 #include "ViewerTab.h"
0027 
0028 
0029 MainWindow::MainWindow()
0030     :   m_tabWidget(new QTabWidget(this))
0031 {
0032     setObjectName(QStringLiteral("MainWindow#"));
0033 
0034     m_tabWidget->setTabsClosable(true);
0035 
0036     KActionCollection *actions = actionCollection();
0037 
0038     setCentralWidget(m_tabWidget);
0039 
0040     KStandardAction::open(this, SLOT(fileOpen()), actions);
0041     KStandardAction::openRecent(this, SLOT(fileOpen(QUrl)), actions)->loadEntries(KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("RecentFiles")));
0042 
0043     QAction *action = new QAction(i18n("Export PDF..."), this);
0044     connect(action, SIGNAL(triggered()), this, SLOT(fileExport()));
0045     actions->addAction(QStringLiteral("fileExportPDF"), action);
0046     action->setEnabled(false); // initially disabled when no tabs present
0047 
0048     action = KStandardAction::print(this, SLOT(filePrint()), actions);
0049     action->setEnabled(false); // initially disabled when no tabs present
0050 
0051     KStandardAction::quit(this, SLOT(quit()), actions);
0052 
0053     setupGUI(KXmlGuiWindow::Default, QStringLiteral("pvfViewerui.rc"));
0054 
0055     connect(m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));
0056     connect(m_tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeRequested(int)));
0057 }
0058 
0059 
0060 void MainWindow::fileOpen()
0061 {
0062     QList<QUrl> urls = QFileDialog::getOpenFileUrls(this, i18n("Open file"), QUrl::fromLocalFile(QDir::homePath()), i18n("Pattern Viewer File (*.pvf)"));
0063 
0064     for (QUrl url : urls) {
0065         fileOpen(url);
0066     }
0067 }
0068 
0069 
0070 void MainWindow::fileOpen(const QUrl &url)
0071 {
0072     if (!url.isValid())
0073         return;
0074 
0075     ViewerTab *viewerTab = new ViewerTab(m_tabWidget);
0076 
0077     if (viewerTab->load(url) == 0) {
0078         KRecentFilesAction *action = static_cast<KRecentFilesAction *>(actionCollection()->action(QStringLiteral("file_open_recent")));
0079         action->addUrl(url);
0080         action->saveEntries(KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("RecentFiles")));
0081 
0082         m_tabWidget->setCurrentIndex(m_tabWidget->addTab(viewerTab, viewerTab->title()));
0083     } else {
0084         delete viewerTab;
0085     }
0086 }
0087 
0088 
0089 void MainWindow::filePrint()
0090 {
0091     static_cast<ViewerTab *>(m_tabWidget->currentWidget())->print();  // filePrint will not be enabled if there are no tabs
0092 }
0093 
0094 
0095 void MainWindow::fileExport()
0096 {
0097     if (ViewerTab *viewerTab = static_cast<ViewerTab*>(m_tabWidget->currentWidget())) {
0098         QUrl url = QFileDialog::getSaveFileUrl(this, i18n("Export As..."),
0099             QUrl::fromLocalFile(QDir::homePath()), i18n("PDF File (*.pdf)"));
0100 
0101         if (url.isValid()) {
0102             QFile file(url.toLocalFile());
0103             file.open(QIODevice::WriteOnly);
0104 
0105             QDataStream stream(&file);
0106 
0107             QByteArray pdfData = viewerTab->pdfData();
0108             stream.writeRawData(pdfData.constData(), pdfData.size());
0109 
0110             file.close();
0111         }
0112     }
0113 }
0114 
0115 
0116 void MainWindow::quit()
0117 {
0118     KXmlGuiWindow::close();
0119 }
0120 
0121 
0122 void MainWindow::currentChanged(int index)
0123 {
0124     if (index == -1) {
0125         // no more tabs, disable print and export
0126         actionCollection()->action("file_print")->setEnabled(false);
0127         actionCollection()->action("fileExportPDF")->setEnabled(false);
0128     } else {
0129         actionCollection()->action("file_print")->setEnabled(true);
0130         actionCollection()->action("fileExportPDF")->setEnabled(true);
0131     }
0132 }
0133 
0134 
0135 void MainWindow::closeRequested(int index)
0136 {
0137     QWidget *viewerTab = m_tabWidget->widget(index);
0138 
0139     m_tabWidget->removeTab(index);
0140     delete viewerTab;
0141 }
0142 
0143 #include "moc_MainWindow.cpp"