Warning, file /office/calligra/gemini/ThumbnailHelperImpl.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Assists in creating thumbnails for Gemini's file views
0003  * SPDX-FileCopyrightText: 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  *
0007  */
0008 
0009 #include "ThumbnailHelperImpl.h"
0010 #include <QMimeDatabase>
0011 
0012 #include <KoDocument.h>
0013 #include <KoDocumentEntry.h>
0014 #include <KoPart.h>
0015 #include <KoStore.h>
0016 
0017 #include <QTimer>
0018 #include <QImage>
0019 #include <QApplication>
0020 #include <QPainter>
0021 #include <QMimeType>
0022 
0023 // static const int minThumbnailSize = 400;
0024 
0025 ThumbnailHelperImpl::ThumbnailHelperImpl(QObject* parent)
0026     : QObject(parent)
0027     , m_part(0)
0028     , m_doc(0)
0029 {
0030 }
0031 
0032 ThumbnailHelperImpl::~ThumbnailHelperImpl()
0033 {
0034     if(m_doc)
0035         m_doc->deleteLater();
0036 }
0037 
0038 bool ThumbnailHelperImpl::convert(const QString& in, const QString& out, int width, int height)
0039 {
0040     // Other locations for thumbnails use the embedded thumbnails. This
0041     // application specifically exists to ensure less crashes in situations
0042     // where that embedded image doesn't exist or is invalid for whatever
0043     // reason. As a direct consequence, we do not attempt to load that image.
0044     QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
0045 
0046     QMimeDatabase db;
0047     const QString mimetype = db.mimeTypeForFile(in).name();
0048     QString error;
0049     KoDocumentEntry documentEntry = KoDocumentEntry::queryByMimeType(mimetype);
0050     m_part = documentEntry.createKoPart(&error);
0051 
0052     if (!m_part)
0053         return false;
0054 
0055     m_doc = m_part->document();
0056 
0057     // prepare the document object
0058     m_doc->setCheckAutoSaveFile(false);
0059     m_doc->setAutoErrorHandlingEnabled(false); // don't show message boxes
0060 
0061     // load the document content
0062     QUrl url = QUrl::fromLocalFile(in);
0063     if (!m_doc->openUrl(url)) {
0064         delete m_doc;
0065         qDebug() << "Load failure! Document did not open" << url;
0066         return false;
0067     }
0068 
0069     while(m_doc->isLoading()) {
0070         qApp->processEvents();
0071     }
0072 
0073     // render the page on a bigger pixmap and use smoothScale,
0074     // looks better than directly scaling with the QPainter (malte)
0075     //const bool usePassedSize = (width > minThumbnailSize && height > minThumbnailSize);
0076     //const QSize size = usePassedSize ? QSize(width, height) : QSize(minThumbnailSize, minThumbnailSize);
0077     const QSize size = QSize(width, height);
0078     image = m_doc->generatePreview(size * 2).toImage().scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0079 
0080     m_doc->closeUrl();
0081 
0082     image.save(out);
0083 
0084     return m_loadingCompleted;
0085 }