OSDN Git Service

Ver0.26
[gefu/Gefu.git] / thumbnailworker.cpp
1 #include "thumbnailworker.h"
2
3 #include <QDebug>
4 #include <QTimer>
5 #include <QThread>
6
7 const int SLEEP_TIME = 100;
8
9 ///////////////////////////////////////////////////////////////////////////////
10 /// \brief ThumbnailWorker::ThumbnailWorker
11 ///
12 /// コンストラクタ
13 ///
14 ThumbnailWorker::ThumbnailWorker() :
15     AbstractWorker(),
16     m_pathList()
17 {
18 }
19
20 ///////////////////////////////////////////////////////////////////////////////
21 /// \brief ThumbnailWorker::addPath
22 /// \param path パス
23 ///
24 /// パスを追加します。
25 ///
26 void ThumbnailWorker::addPath(const QString &path)
27 {
28     QMutexLocker locker(&m_mutex);
29     m_pathList << path;
30 }
31
32 ///////////////////////////////////////////////////////////////////////////////
33 /// \brief ThumbnailWorker::clearPath
34 ///
35 /// パスをクリアします。
36 ///
37 void ThumbnailWorker::clearPath()
38 {
39     QMutexLocker locker(&m_mutex);
40     m_pathList.clear();
41 }
42
43 ///////////////////////////////////////////////////////////////////////////////
44 /// \brief ThumbnailWorker::getPath
45 /// \return リストからパスを取得します。
46 ///
47 QString ThumbnailWorker::getPath()
48 {
49     QMutexLocker locker(&m_mutex);
50     if (m_pathList.isEmpty()) {
51         return QString();
52     }
53     return m_pathList.takeFirst();
54 }
55
56 ///////////////////////////////////////////////////////////////////////////////
57 /// \brief ThumbnailWorker::run
58 ///
59 /// サムネイルを生成します。
60 ///
61 void ThumbnailWorker::run()
62 {
63     qDebug() << "ThumbnailWorker::run() enter.";
64
65     while (!isAborted()) {
66         QString path = getPath();
67         if (!path.isEmpty()) {
68             QPixmap pixmap(path);
69             if (!pixmap.isNull()) {
70                 QSize size(256, 256);
71                 double scaleX = 1.0 * size.width() / pixmap.width();
72                 double scaleY = 1.0 * size.height() / pixmap.height();
73                 double scaleFactor = qMin(scaleX, scaleY);
74                 if (scaleFactor < 1) {
75                     pixmap = pixmap.scaled(pixmap.size() * scaleFactor,
76                                            Qt::IgnoreAspectRatio,
77                                            Qt::SmoothTransformation);
78                 }
79                 emit resultReady(path, pixmap);
80             }
81         }
82         this->thread()->msleep(SLEEP_TIME);
83     }
84
85     emit finished();
86     qDebug() << "ThumbnailWorker::run() finished.";
87 }