OSDN Git Service

29015e4ff1bd2de19d8f3f549aa19a2bf5f8d9ae
[gefu/Gefu.git] / thumbnailworker.cpp
1 #include "thumbnailworker.h"
2
3 #include <QDebug>
4 #include <QTimer>
5 #include <QThread>
6
7 ThumbnailWorker::ThumbnailWorker(QObject *parent) :
8     QObject(parent),
9     m_mutex(),
10     m_pathList(),
11     m_loop(true)
12 {
13 }
14
15 void ThumbnailWorker::addPath(const QString &path)
16 {
17     QMutexLocker locker(&m_mutex);
18     m_pathList << path;
19 }
20
21 void ThumbnailWorker::clearPath()
22 {
23     QMutexLocker locker(&m_mutex);
24     m_pathList.clear();
25 }
26
27 void ThumbnailWorker::finish()
28 {
29     QMutexLocker locker(&m_mutex);
30     m_loop = false;
31     qDebug() << "ThumbnailWorker::finish()";
32 }
33
34 QString ThumbnailWorker::getPath()
35 {
36     QMutexLocker locker(&m_mutex);
37     if (m_pathList.isEmpty()) {
38         return QString();
39     }
40     return m_pathList.takeFirst();
41 }
42
43 bool ThumbnailWorker::loop()
44 {
45     QMutexLocker locker(&m_mutex);
46     return m_loop;
47 }
48
49 void ThumbnailWorker::doWork()
50 {
51     while (loop()) {
52         QString path = getPath();
53         if (!path.isEmpty()) {
54             QPixmap pixmap(path);
55             if (!pixmap.isNull()) {
56                 QSize size(256, 256);
57                 double scaleX = 1.0 * size.width() / pixmap.width();
58                 double scaleY = 1.0 * size.height() / pixmap.height();
59                 double scaleFactor = qMin(scaleX, scaleY);
60                 if (scaleFactor < 1) {
61                     pixmap = pixmap.scaled(pixmap.size() * scaleFactor,
62                                            Qt::IgnoreAspectRatio,
63                                            Qt::SmoothTransformation);
64                 }
65                 emit resultReady(path, pixmap);
66             }
67         }
68         QThread::msleep(100);
69     }
70
71     emit finished();
72     qDebug() << "ThumbnailWorker::doWork() finished.";
73 }