summaryrefslogtreecommitdiffstats
path: root/weather/src/pixmaploader.cpp
blob: e60cef4ac4d86a6315b56f4c5506d11a8921d3a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "pixmaploader.h"
#include "settings.h"
#include <QMutexLocker>

#include "settings.h"

// PixmapLoaderThread

PixmapLoaderThread::PixmapLoaderThread(PixmapLoader *loader)
    : QThread(loader)
    , m_loader(loader)
{
}

void PixmapLoaderThread::run()
{
    static const qreal widthFactor = Settings::widthFactor();
    static const qreal heightFactor = Settings::heightFactor();
    QString name = m_loader->dequeue();
    while (!name.isEmpty()) {
        QImage image(":images/weather_elements/" + name + ".png");
        if (!image.isNull()) {
            QSize size(widthFactor * image.size().width(), heightFactor * image.size().height());
            if (size.width() && size.height())
                image = image.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        }
        emit imageIsReady(name, image);
        name = m_loader->dequeue();
    }
    deleteLater();
}

// PixmapLoader

PixmapLoader::PixmapLoader(QObject *parent)
    : QObject(parent)
    , m_thread(0)
{
}

void PixmapLoader::load(const QString &name)
{
    instance()->enqueue(name);
}

PixmapLoader *PixmapLoader::instance()
{
    static PixmapLoader * const result(new PixmapLoader());
    return result;
}

void PixmapLoader::connectToOnIdleSignal(QObject *receiver, const char *method)
{
    QObject::connect(instance(), SIGNAL(onIdle()), receiver, method, Qt::QueuedConnection);
}

void PixmapLoader::disconnectReceiver(QObject *receiver)
{
    instance()->disconnect(receiver);
}

QPixmap PixmapLoader::getPic(const QString &name)
{
    PixmapLoader *obj = instance();
    QMutexLocker locker(&obj->m_mutex);
    return obj->m_store.contains(name) ? obj->m_store[name] : QPixmap();
}

void PixmapLoader::enqueue(const QString &name)
{
    QMutexLocker locker(&m_mutex);

    if (m_queue.indexOf(name) >= 0 || m_currentImages.indexOf(name) >= 0 || m_store.contains(name))
        return;

    m_queue.append(name);

    if (!m_thread) {
        m_thread = new PixmapLoaderThread(this);
        connect(m_thread, SIGNAL(imageIsReady(QString,QImage)),
                this, SLOT(imageIsReady(QString,QImage)));
        m_thread->start();
    }
    m_condition.wakeOne();
}

QString PixmapLoader::doDequeue()
{
    QString name = m_queue.isEmpty() ? QString() : m_queue.takeAt(0);

    while (!name.isEmpty() && (m_currentImages.indexOf(name) >= 0 || m_store.contains(name)))
        name = m_queue.isEmpty() ? QString() : m_queue.takeAt(0);

    if (!name.isEmpty())
        m_currentImages.append(name);

    return name;
}

QString PixmapLoader::dequeue()
{
    static const int waitTime = 60000;
    QMutexLocker locker(&m_mutex);
    QString result;
    do
        result = doDequeue();
    while (result.isEmpty() && m_condition.wait(&m_mutex, waitTime));
    if (result.isEmpty()) {
        m_thread = 0;
    }
    return result;
}

void PixmapLoader::imageIsReady(const QString &name, QImage image)
{
    QMutexLocker locker(&m_mutex);

    QPixmap pixmap = QPixmap::fromImage(image, Qt::ColorOnly);
    if (!pixmap.isNull())
        m_store[name] = pixmap;

    m_currentImages.removeAll(name);
    if (m_currentImages.isEmpty() && m_queue.isEmpty())
        emit onIdle();
}