summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/slideshow/imageloadthread.cpp
blob: 36f40c80fdbc833b90f09843334bc5de690aefac (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* ============================================================
 *
 * This file is a part of kipi-plugins project
 * http://www.kipi-plugins.org
 *
 * Date        : 2007-11-14
 * Description : a kipi plugin to slide images.
 *
 * Copyright (C) 2007 by Valerio Fuoglio <valerio dot fuoglio at gmail dot com>
 *
 * Parts of this code are based on smoothslidesaver by Carsten Weinhold 
 * <carsten dot weinhold at gmx dot de>                                           
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

// Qt includes.

#include <qdir.h>
#include <qvaluevector.h>
#include <qdeepcopy.h>
#include <qwmatrix.h>
#include <qobject.h>
             
// KDE includes.

#include <klocale.h>
             
// Local includes.

#include "slideshowkb.h"
#include "imageloadthread.h"
#include "imageloadthread.moc"

namespace KIPISlideShowPlugin
{

ImageLoadThread::ImageLoadThread(QValueList<QPair<QString, int> >& fileList, 
                                 int width, int height) 
{

    m_initialized   = false;
    m_needImage     = true;
    m_haveImages    = false;
    m_quitRequested = false;

    m_fileIndex = 0;
    m_fileList = fileList;

    m_width = width;
    m_height = height;
}

void ImageLoadThread::quit() {

    QMutexLocker locker(&m_condLock);
    
    m_quitRequested = true;
    m_imageRequest.wakeOne();
}

void ImageLoadThread::requestNewImage() {

    QMutexLocker locker(&m_condLock);
    
    if ( !m_needImage) {
        m_needImage = true;
        m_imageRequest.wakeOne();
    }
}

void ImageLoadThread::run() {

    QMutexLocker locker(&m_condLock);

    // we enter the loop with m_needImage==true, so we will immediatly
    // try to load an image

    while (true) {

        if (m_quitRequested)
            break;

        if (m_needImage) {

          if ( m_fileIndex == (int)m_fileList.count() )
          {
            m_needImage = false;
            emit(endOfShow());
            continue;
          }
          
            m_needImage = false;
            m_condLock.unlock();

            bool ok;
            do {
                ok = loadImage();
                if ( !ok)
                    invalidateCurrentImageName();
            } while ( !ok && m_fileIndex < (int)m_fileList.count());
            
            if ( m_fileIndex == (int)m_fileList.count() )
            {
              
              emit(endOfShow());
              m_condLock.lock();
              continue;
            }
 
            if ( !ok) {
                // generate a black dummy image
                m_texture = QImage(128, 128, 32);
                m_texture.fill(Qt::black.rgb());
            }

            m_condLock.lock();
            
            m_fileIndex++;
            
            if ( !m_initialized) {
                m_haveImages  = ok;
                m_initialized = true;
            }

        } else {
            // wait for new requests from the consumer
            m_imageRequest.wait(&m_condLock);
        }
    }
}

bool ImageLoadThread::loadImage() {

    QPair<QString, int> fileAngle = m_fileList[m_fileIndex];
    QString path(fileAngle.first);
    int     angle(fileAngle.second);
    QImage image(path);
    if (angle != 0)
    {
        QWMatrix wm;
        wm.rotate(angle);
        image = image.xForm(wm);
    }
    
    if (image.isNull()) {
        return false;
    }

    float aspect  = (float)image.width() / (float)image.height();    
    
    image = image.smoothScale(m_width, m_height, QImage::ScaleMin);
    
    m_imageLock.lock();
    
    // this is the critical moment, when we make the new texture and
    // aspect available to the consumer
    m_textureAspect = aspect;
    m_texture       = QGLWidget::convertToGLFormat(image);
    
    m_imageLock.unlock();

    return true;
}

void ImageLoadThread::invalidateCurrentImageName() {
    m_fileList.remove(m_fileList[m_fileIndex]);
    m_fileIndex++;
}

}  // NameSpace KIPISlideShowPlugin