Debian Codegraph @

src/imports/Lomiri/Contacts/imagescalethread.cpp

raw
1 /*
2 * Copyright (C) 2012-2015 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "imagescalethread.h"
18
19 #include <QImage>
20 #include <QImageReader>
21 #include <QFile>
22 #include <QDir>
23 #include <QUuid>
24 #include <QDebug>
25
26 ImageScaleThread::ImageScaleThread(const QUrl &imageUrl, QObject *listener)
27 : m_imageUrl(imageUrl),
28 m_id(QUuid::createUuid().toString()),
29 m_listener(listener),
30 m_tmpFile(0)
31 {
32 }
33
34 ImageScaleThread::~ImageScaleThread()
35 {
36 if (m_tmpFile) {
37 m_tmpFile->setAutoRemove(false);
38 m_tmpFile->deleteLater();
39 m_tmpFile = 0;
40 }
41 }
42
43 QString ImageScaleThread::id()
44 {
45 return m_id;
46 }
47
48 QString ImageScaleThread::outputFile() const
49 {
50 if (m_tmpFile) {
51 return m_tmpFile->fileName();
52 } else {
53 return QString();
54 }
55 }
56
57 void ImageScaleThread::run()
58 {
59 // make sure that the old image get deleted
60 if (m_tmpFile) {
61 qDebug() << "Delete previous avatar" << m_tmpFile->fileName();
62 m_tmpFile->setAutoRemove(true);
63 m_tmpFile->close();
64 delete m_tmpFile;
65 }
66
67 // Create the temporary file
68 m_tmpFile = new QTemporaryFile(QString("%1/avatar_XXXXXX.png").arg(QDir::tempPath()));
69 if (!m_tmpFile->open()) {
70 qWarning() << "Fail to create avatar temporary file";
71 return;
72 }
73
74 // try using the Qt's image reader to speedup the scaling
75 QImage scaledAvatar;
76 QImageReader reader(m_imageUrl.toLocalFile());
77 if (reader.canRead()) {
78 reader.setAutoTransform( true ); // to avoid rotation issue
79 QSize size = reader.size();
80 if ((size.height() > 720) && (size.width() > 720)) {
81 size.scale(720, 720, Qt::KeepAspectRatioByExpanding);
82 }
83 reader.setScaledSize(size);
84 scaledAvatar = reader.read();
85 }
86
87 // fallback to use a QImage to load the avatar if the image reader failed
88 if (scaledAvatar.isNull()) {
89 QImage img(m_imageUrl.toLocalFile());
90 if (!img.isNull()) {
91 if ((img.height() > 720) && (img.width() > 720)) {
92 scaledAvatar = img.scaled(720, 720, Qt::KeepAspectRatioByExpanding, Qt::FastTransformation);
93 } else {
94 scaledAvatar = img;
95 }
96 }
97 }
98
99 // and finally, save the image
100 if (!scaledAvatar.isNull()) {
101 scaledAvatar.save(m_tmpFile, "png");
102 }
103
104 m_tmpFile->close();
105
106 if (m_listener) {
107 QMetaObject::invokeMethod(m_listener, "imageCopyDone",
108 Q_ARG(QString, m_id), Q_ARG(QString, m_tmpFile->fileName()));
109 }
110 }