File: slideshow.cpp

package info (click to toggle)
kdeplasma-addons 4%3A4.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 8,792 kB
  • ctags: 7,686
  • sloc: cpp: 61,927; python: 1,240; xml: 795; sh: 77; makefile: 4
file content (211 lines) | stat: -rw-r--r-- 5,784 bytes parent folder | download
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
/***************************************************************************
 *   Copyright  2008 by Thomas Coopman <thomas.coopman@gmail.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 of the License, 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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

#include "slideshow.h"

#include <QDir>
#include <QDirIterator>
#include <QTimer>

#include <KDebug>
#include <KRandomSequence>

#include "picture.h"

SlideShow::SlideShow(QObject *parent)
    : QObject(parent)
{
    m_filters << "*.jpeg" << "*.jpg" << "*.png" << "*.svg" << "*.svgz" << "*.bmp" << "*.tif"; // use mime types?
    m_slideNumber = 0;
    m_useRandom = false;

    m_picture = new Picture(this);
    connect(m_picture, SIGNAL(pictureLoaded(QPixmap)), this, SLOT(pictureLoaded(QPixmap)));

    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(nextPicture()));
}

SlideShow::~SlideShow()
{
}

void SlideShow::setRandom(bool useRandom)
{
    m_useRandom = useRandom;
}

void SlideShow::setDirs(const QStringList &slideShowPath, bool recursive)
{
    QDateTime setDirStart = QDateTime::currentDateTime();

    m_image = QPixmap();
    m_picturePaths.clear();
    foreach(const QString &path, slideShowPath) {
        addDir(KUrl(path).path(), recursive);
    }
    
    KRandomSequence randomSequence;
    m_indexList.clear();

    for (int j = 0; j < m_picturePaths.count(); j++) {
        m_indexList.append(j);
    }
    randomSequence.randomize(m_indexList);

    // select 1st picture
    firstPicture();
    
    // TODO do something meaningful if m_picturePaths.empty()
    
    kDebug() << "Loaded " << m_picturePaths.size() << " pictures in " << setDirStart.secsTo(QDateTime::currentDateTime()) << " seconds";
}

void SlideShow::setImage(const QString &imagePath)
{
    m_image = QPixmap();
    m_picturePaths.clear();
    addImage(imagePath);
    m_currentUrl = url();
}

void SlideShow::addImage(const QString &imagePath)
{
    m_picturePaths.append(imagePath);
}

void SlideShow::addDir(const QString &path, bool recursive)
{
    QDirIterator dirIterator(path, m_filters, QDir::Files, (recursive ? QDirIterator::Subdirectories | QDirIterator::FollowSymlinks : QDirIterator::NoIteratorFlags));
    QStringList dirPicturePaths;

    while (dirIterator.hasNext()) {
        dirIterator.next();
        dirPicturePaths.append(dirIterator.filePath());
    }
    
    // the pictures have to be sorted before adding them to the list,
    // because the QDirIterator sorts them in a different way than QDir::entryList
    dirPicturePaths.sort();
    m_picturePaths.append(dirPicturePaths);
}

QPixmap SlideShow::image() const 
{
    if (m_image.isNull() || m_currentUrl != m_picture->url()) {
        kDebug() << "reloading from Picture" << m_currentUrl;
        //m_currentUrl = m_picture->url();
        m_picture->setPicture(m_currentUrl);
    }
    kDebug();
    return m_image;
}

KUrl SlideShow::url(int offset)
{
    if (!m_picturePaths.isEmpty()) {

        m_slideNumber += offset;

        if (m_slideNumber <= -1) {
            m_slideNumber = m_picturePaths.count() - 1;

        } else if (m_slideNumber >= m_picturePaths.count()) {
            m_slideNumber = 0;
        }

        if (m_useRandom) {
            return KUrl(m_picturePaths.at(m_indexList.at(m_slideNumber)));
        } else {
            return KUrl(m_picturePaths.at(m_slideNumber));
        }
    }

    return KUrl();
}

void SlideShow::firstPicture()
{
    m_slideNumber = 0;
    m_currentUrl = url(0);
    m_image = image();
    emit pictureUpdated();
}

void SlideShow::nextPicture()
{
    m_currentUrl = url(1);
    m_image = image();
    emit pictureUpdated();
}

void SlideShow::previousPicture()
{
    m_currentUrl = url(-1);
    m_image = image();
    emit pictureUpdated();
}

KUrl SlideShow::currentUrl() const
{
    return m_currentUrl;
}

void SlideShow::setUpdateInterval(int msec)
{
    m_timer->stop();
    if (msec > 1) {
        if (m_currentUrl.isEmpty()) {
            m_currentUrl = url();
        }
        m_timer->start(msec);
    }
}

QString SlideShow::message() const 
{
    return m_picture->message();
}

void SlideShow::pictureLoaded(QPixmap image)
{
    m_image = image;
    emit pictureUpdated();
}

void SlideShow::clearPicture()
{
    m_image = QPixmap();
}

void SlideShow::dataUpdated(const QString &name, const Plasma::DataEngine::Data &data)
{
    if (data.isEmpty()) {
        m_image = QPixmap();
        m_picture->setMessage(i18n("No Picture from this Provider."));
        return;
    }

    m_image = data[name].value<QPixmap>();
    m_picture->setMessage(QString());
    emit pictureUpdated();
}

#include "slideshow.moc"