OSDN Git Service

サムネイル生成スレッドの改良
[gefu/Gefu.git] / thumbnailworker.cpp
index 497577b..cda15e7 100644 (file)
@@ -1,28 +1,70 @@
 #include "thumbnailworker.h"
 
+#include <QTimer>
+#include <QThread>
+
 ThumbnailWorker::ThumbnailWorker(QObject *parent) :
-    QObject(parent)
+    QObject(parent),
+    m_mutex(),
+    m_pathList(),
+    m_loop(true)
 {
 }
 
-void ThumbnailWorker::doWork()
+void ThumbnailWorker::addPath(const QString &path)
+{
+    QMutexLocker locker(&m_mutex);
+    m_pathList << path;
+}
+
+void ThumbnailWorker::clearPath()
+{
+    QMutexLocker locker(&m_mutex);
+    m_pathList.clear();
+}
+
+void ThumbnailWorker::finish()
 {
-    QPixmap pixmap(m_path);
-    if (pixmap.isNull()) {
-        emit finished();
-        return;
+    QMutexLocker locker(&m_mutex);
+    m_loop = false;
+}
+
+QString ThumbnailWorker::getPath()
+{
+    QMutexLocker locker(&m_mutex);
+    if (m_pathList.isEmpty()) {
+        return QString();
     }
+    return m_pathList.takeFirst();
+}
 
-    if (pixmap.width() > m_size.width() || pixmap.height() > m_size.height()) {
-        double scaleX = 1.0 * m_size.width() / pixmap.width();
-        double scaleY = 1.0 * m_size.height() / pixmap.height();
-        double scaleFactor = (scaleX > scaleY) ? scaleY : scaleX;
+bool ThumbnailWorker::loop()
+{
+    QMutexLocker locker(&m_mutex);
+    return m_loop;
+}
 
-        pixmap = pixmap.scaled(pixmap.size() * scaleFactor,
-                               Qt::IgnoreAspectRatio,
-                               Qt::SmoothTransformation);
+void ThumbnailWorker::doWork()
+{
+    while (loop()) {
+        QString path = getPath();
+        if (path.isEmpty()) {
+            QThread::sleep(1);
+        }
+        else {
+            QPixmap pixmap(path);
+            if (!pixmap.isNull()) {
+                QSize size(256, 256);
+                double scaleX = 1.0 * size.width() / pixmap.width();
+                double scaleY = 1.0 * size.height() / pixmap.height();
+                double scaleFactor = (scaleX > scaleY) ? scaleY : scaleX;
+                pixmap = pixmap.scaled(pixmap.size() * scaleFactor,
+                                       Qt::IgnoreAspectRatio,
+                                       Qt::SmoothTransformation);
+                emit resultReady(path, pixmap);
+            }
+        }
     }
 
-    emit resultReady(m_path, pixmap);
     emit finished();
 }