File indexing completed on 2024-05-12 16:28:28

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 KO GmbH <jos.van.den.oever@kogmbh.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 #include "combinedview.h"
0020 #include "dirslideloader.h"
0021 #include "kpresenterslideloader.h"
0022 #include "slideview.h"
0023 #include "oothread.h"
0024 #include <PptToOdp.h>
0025 #include <QGridLayout>
0026 #include <QDragEnterEvent>
0027 #include <QCoreApplication>
0028 #include <kmessagebox.h>
0029 #include <kmimetype.h>
0030 
0031 CombinedView::CombinedView(QWidget* parent) :QWidget(parent),
0032         ooodploader(new DirSlideLoader(this)),
0033         koodploader(new KPresenterSlideLoader(this)),
0034         oopptloader(new DirSlideLoader(this)),
0035         kopptloader(new KPresenterSlideLoader(this)),
0036         ooodpview(new SlideView(ooodploader, this)),
0037         koodpview(new SlideView(koodploader, this)),
0038         oopptview(new SlideView(oopptloader, this)),
0039         kopptview(new SlideView(kopptloader, this)),
0040         oothread(new OoThread(this)),
0041         layout(new QGridLayout(this)) {
0042 
0043     ooodploader->setSlideNamePattern("img%1.png");
0044     oopptloader->setSlideNamePattern("img%1.png");
0045 
0046     addSlideView(ooodpview);
0047     addSlideView(koodpview);
0048     addSlideView(oopptview);
0049     addSlideView(kopptview);
0050 
0051     layout->addWidget(ooodpview, 0, 0);
0052     layout->addWidget(koodpview, 0, 1);
0053     layout->addWidget(oopptview, 1, 0);
0054     layout->addWidget(kopptview, 1, 1);
0055     layout->setColumnStretch(0, 1);
0056     layout->setColumnStretch(1, 1);
0057     layout->setRowStretch(0, 1);
0058     layout->setRowStretch(1, 1);
0059     layout->setSpacing(0);
0060     layout->setContentsMargins(0, 0, 0, 0);
0061     setLayout(layout);
0062 
0063     connect(oothread, SIGNAL(toOdpDone(QString)),
0064         this, SLOT(slotHandleOoOdp(QString)));
0065     connect(oothread, SIGNAL(toPngDone(QString)),
0066         this, SLOT(slotHandleOoPng(QString)));
0067     setAcceptDrops(true);
0068 }
0069 
0070 CombinedView::~CombinedView() {
0071     oothread->stop();
0072     oothread->wait();
0073 }
0074 
0075 void CombinedView::slotSetView(qreal zoomFactor, int h, int v) {
0076     for (int i=0; i<slideViews.size(); ++i) {
0077         slideViews[i]->setView(zoomFactor, h, v);
0078     }
0079 }
0080 void CombinedView::addSlideView(SlideView* slideview) {
0081     slideViews.push_back(slideview);
0082     connect(slideview, SIGNAL(viewChanged(qreal,int,int)),
0083         this, SLOT(slotSetView(qreal,int,int)));
0084 }
0085 QString
0086 koppttoodp(const QString& from) {
0087     QDir d(QDir::temp().filePath("slidecompare-" + QDir::home().dirName()));
0088     QString dirpath = d.absolutePath();
0089     d.mkpath(dirpath);
0090     QString to = dirpath + QDir::separator()
0091                  + QFileInfo(from).baseName() + ".odp";
0092     QFile::remove(to);
0093     PptToOdp ppttoodp(0, 0);
0094     ppttoodp.convert(from, to, KoStore::Zip);
0095     return to;
0096 }
0097 void
0098 CombinedView::openFile(const QString& path) {
0099     bool odp = path.endsWith(QLatin1String(".odp"), Qt::CaseInsensitive);
0100     oopptview->setVisible(!odp);
0101     kopptview->setVisible(!odp);
0102     layout->setRowStretch(1, (odp)?0:1);
0103     // update view now for more pleasing user experience, later renderings
0104     // may be slow
0105     qApp->processEvents();
0106     oopptloader->setSlideDir("");
0107     kopptloader->close();
0108 
0109     if (!QFileInfo(path).exists()) {
0110          KMessageBox::error(this,
0111                        QString("File %1 does not exist.").arg(path));
0112          return;
0113     }
0114 
0115     if (!odp) {
0116         nextodpfile = koppttoodp(path);
0117         if (!QFileInfo(nextodpfile).exists()) {
0118             KMessageBox::error(this, QString(
0119                     "File %1 cannot be converted by PptToOdp.").arg(path));
0120             return;
0121         }
0122         koodploader->open(nextodpfile);
0123     } else {
0124         nextodpfile.clear();
0125         koodploader->open(path);
0126     }
0127     quint32 nslides = koodploader->numberOfSlides();
0128     if (nslides == 0) {
0129         return;
0130     }
0131     ooodploader->setSlideSize(koodploader->slideSize());
0132     ooodploader->setNumberOfSlides(nslides);
0133 
0134     if (!odp) {
0135         // start conversion to odp
0136         ooodpresult = oothread->toOdp(path);
0137         oopptloader->setSlideSize(koodploader->slideSize());
0138         oopptloader->setNumberOfSlides(nslides);
0139     } else {
0140         oopptloader->setNumberOfSlides(0);
0141     }
0142     QString dir = oothread->toPng(path, koodploader->slideSize().width());
0143     ooodploader->setSlideDir(dir);
0144     // if ppt, convert to odp with calligra and put in queue for conversion to
0145     // png
0146 
0147     // adapt zoom level to number of slides
0148     qreal zoomlevel = (nslides > 3 || nslides == 0) ?0.25 :1.0/nslides;
0149     ooodpview->setView(zoomlevel, 0, 0);
0150     koodpview->setView(zoomlevel, 0, 0);
0151     oopptview->setView(zoomlevel, 0, 0);
0152     kopptview->setView(zoomlevel, 0, 0);
0153 }
0154 void
0155 CombinedView::slotHandleOoOdp(const QString& path) {
0156     if (path == ooodpresult) {
0157         kopptloader->open(path);
0158     }
0159 }
0160 void
0161 CombinedView::slotHandleOoPng(const QString& /*path*/) {
0162     if (!nextodpfile.isEmpty()) {
0163         QString dir = oothread->toPng(nextodpfile,
0164             koodploader->slideSize().width());
0165         oopptloader->setSlideDir(dir);
0166         nextodpfile.clear();
0167     }
0168 }
0169 void CombinedView::dragEnterEvent(QDragEnterEvent *event)
0170 {
0171     foreach (const QUrl& url, event->mimeData()->urls()) {
0172         const QString path(url.toLocalFile());
0173         if (path.isEmpty()) { // url is not local
0174             event->acceptProposedAction();
0175         } else {
0176             QString mimetype = KMimeType::findByUrl(url)->name();
0177             if (mimetype == "application/vnd.oasis.opendocument.presentation"
0178                 || mimetype == "application/vnd.ms-powerpoint") {
0179                 event->acceptProposedAction();
0180             }
0181         }
0182     }
0183 }
0184 void CombinedView::dropEvent(QDropEvent *event)
0185 {
0186     if (event->mimeData()->urls().size()) {
0187         openFile(event->mimeData()->urls().first().toLocalFile());
0188     }
0189 }