summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/slideshow/slideshowloader.cpp
blob: b47088588e4d3799a93c119876d8905144b77dc9 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* ============================================================
 *
 * This file is a part of kipi-plugins project
 * http://www.kipi-plugins.org
 *
 * Date        : 2007-11-11
 * Description : a kipi plugin to slide images.
 *
 * Copyright (C) 2007 by Valerio Fuoglio <valerio dot fuoglio at gmail dot com>
 *
 * 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 <tqimage.h>
#include <tqwmatrix.h>
#include <tqpainter.h>
#include <tqvaluelist.h>

// KDE includes

#include <kdebug.h>

// Local includes

#include "slideshowloader.h"

namespace KIPISlideShowPlugin
{ 
  
  LoadThread::LoadThread(LoadedImages* loadedImages, TQMutex* imageLock, const KURL path, 
                         const int angle, int width, int height) {

    m_path  =  path;
    m_angle =  angle;
    m_swidth = width; 
    m_sheight = height; 
    m_imageLock = imageLock;
    m_loadedImages = loadedImages;
  }
  
  LoadThread::~LoadThread() {
  }
  
  void LoadThread::run() {
  
    TQImage newImage(m_path.path());
    // Rotate according to angle
    if ( m_angle != 0 ) 
    {
      TQWMatrix matrix;
      matrix.rotate((double)m_angle);
      newImage.xForm( matrix );
    }
    
  
    newImage = TQImage(newImage.smoothScale(m_swidth, m_sheight, TQ_ScaleMin));
    
    m_imageLock->lock();
    m_loadedImages->insert(m_path, newImage);
    m_imageLock->unlock();
  }

  // -----------------------------------------------------------------------------------------

  SlideShowLoader::SlideShowLoader(FileList &pathList, uint cacheSize, int width, int height, int beginAtIndex) {

    m_currIndex = beginAtIndex;
    m_cacheSize = cacheSize;
    m_pathList = pathList;
    m_swidth = width;
    m_sheight = height;
    m_loadingThreads = new LoadingThreads();
    m_loadedImages = new LoadedImages();
    m_imageLock = new TQMutex();
    m_threadLock = new TQMutex();
    
    for ( uint i = 0; i < uint(m_cacheSize/2) && i < uint(m_pathList.count()); i++ ) {
      LoadThread* newThread = new LoadThread(m_loadedImages, m_imageLock, KURL(m_pathList[i].first), 
                                             m_pathList[i].second, m_swidth, m_sheight);
      m_threadLock->lock();
      m_loadingThreads->insert(m_pathList[i].first, newThread);
      newThread->start(); 
      m_threadLock->unlock();
    }  
    for ( uint i = 0; i < ( m_cacheSize%2 == 0? (m_cacheSize%2) : uint(m_cacheSize/2)+1); i++ ) {
      int toLoad = (m_currIndex - i)%m_pathList.count();
      LoadThread* newThread = new LoadThread(m_loadedImages, m_imageLock, KURL(m_pathList[toLoad].first), 
                                             m_pathList[toLoad].second, m_swidth, m_sheight);
      m_threadLock->lock();
      m_loadingThreads->insert(m_pathList[toLoad].first, newThread);
      newThread->start(); 
      m_threadLock->unlock();
    }
    
  }

  SlideShowLoader::~SlideShowLoader() {
    
    m_threadLock->lock();
   LoadingThreads::Iterator it;
    for ( it = m_loadingThreads->begin(); it != m_loadingThreads->end(); ++it ) {
      it.data()->wait();
      delete it.data();
      m_loadingThreads->remove(it);
    }
    m_threadLock->unlock();

    
    delete(m_loadingThreads);
    delete(m_loadedImages);
 
    delete(m_imageLock);
    delete(m_threadLock);
  }
  
  void SlideShowLoader::next() {
    int victim = (m_currIndex - ( m_cacheSize%2 == 0 ? (m_cacheSize/2)-1 : int(m_cacheSize/2))) % m_pathList.count();
    int newBorn = (m_currIndex + int(m_cacheSize/2) + 1) % m_pathList.count();
    
    if ( victim == newBorn ) return;
    
    m_threadLock->lock();
    m_imageLock->lock();
    m_loadingThreads->remove(m_pathList[victim].first);
    m_loadedImages->remove(m_pathList[victim].first);
    m_imageLock->unlock();
    m_threadLock->unlock();
    
    LoadThread* newThread = new LoadThread(m_loadedImages, m_imageLock, KURL(m_pathList[newBorn].first), 
                                           m_pathList[newBorn].second, m_swidth, m_sheight);
    
    m_threadLock->lock();
    m_loadingThreads->insert(m_pathList[newBorn].first, newThread);
    newThread->start();
    m_threadLock->unlock();
    
    m_currIndex = (m_currIndex + 1) % m_pathList.count();
  }
  
  void SlideShowLoader::prev() {
    int victim = (m_currIndex + int(m_currIndex/2)) % m_pathList.count();
    int newBorn = (m_currIndex - ( m_cacheSize&2 == 0 ? (m_cacheSize/2) : int(m_cacheSize/2)+1)) % m_pathList.count();
    
    if ( victim == newBorn ) return;
    
    m_threadLock->lock();
    m_imageLock->lock();
    m_loadingThreads->remove(m_pathList[victim].first);
    m_loadedImages->remove(m_pathList[victim].first);
    m_imageLock->unlock();
    m_threadLock->unlock();
    
    LoadThread* newThread = new LoadThread(m_loadedImages, m_imageLock, KURL(m_pathList[newBorn].first), 
                                           m_pathList[newBorn].second, m_swidth, m_sheight);

    
    m_threadLock->lock();
    m_loadingThreads->insert(m_pathList[newBorn].first, newThread);
    newThread->start();
    m_threadLock->unlock();
    
    m_currIndex = (m_currIndex - 1) % m_pathList.count();
    
  }
  
  TQImage SlideShowLoader::getCurrent() {
    checkIsIn(m_currIndex);
    
    m_imageLock->lock();
    TQImage returned = (*m_loadedImages)[m_pathList[m_currIndex].first];
    m_imageLock->unlock(); 
    
    
    return returned;
  }
  
  TQString SlideShowLoader::currFileName() 
  {
    return KURL(m_pathList[m_currIndex].first).fileName();
  }
  
  KURL SlideShowLoader::currPath() 
  {
    return m_pathList[m_currIndex].first;
  }
  
  void SlideShowLoader::checkIsIn(int index) 
  {
    m_threadLock->lock();
    if (m_loadingThreads->contains(m_pathList[index].first)) 
    {
      if ( (*m_loadingThreads)[m_pathList[index].first]->running() )
        (*m_loadingThreads)[m_pathList[index].first]->wait();
      m_threadLock->unlock();
    }
    else
    {
      LoadThread* newThread = new LoadThread(m_loadedImages, m_imageLock, KURL(m_pathList[index].first), 
                                             m_pathList[index].second, m_swidth, m_sheight);
      
      m_loadingThreads->insert(m_pathList[index].first,newThread);
      newThread->start();
      (*m_loadingThreads)[m_pathList[index].first]->wait();
      m_threadLock->unlock();
    }    
  }

}  // NameSpace KIPISlideShowPlugin