59
60
* This class generate random numbers which are not the same between two runs
60
61
* of Gwenview. See bug #132334
62
class RandomNumberGenerator {
63
class RandomNumberGenerator
64
RandomNumberGenerator()
66
RandomNumberGenerator()
68
int operator()(int n) {
69
return rand_r(&mSeed) % n;
73
return rand_r(&mSeed) % n;
76
80
struct SlideShowPrivate {
80
QVector<KUrl> mShuffledUrls;
81
QVector<KUrl>::ConstIterator mStartIt;
83
KUrl mLastShuffledUrl;
86
QAction* mRandomAction;
89
if (GwenviewConfig::random()) {
90
return findNextRandomUrl();
92
return findNextOrderedUrl();
97
KUrl findNextOrderedUrl() {
98
QVector<KUrl>::ConstIterator it = qFind(mUrls.constBegin(), mUrls.constEnd(), mCurrentUrl);
99
if (it == mUrls.constEnd()) {
100
kWarning() << "Current url not found in list. This should not happen.\n";
105
if (GwenviewConfig::loop()) {
106
// Looping, if we reach the end, start again
107
if (it == mUrls.constEnd()) {
108
it = mUrls.constBegin();
111
// Not looping, have we reached the end?
113
if (/*(it==mUrls.end() && GwenviewConfig::stopAtEnd()) ||*/ it == mStartIt) {
114
it = mUrls.constEnd();
118
if (it != mUrls.constEnd()) {
126
void initShuffledUrls() {
127
mShuffledUrls = mUrls;
128
RandomNumberGenerator generator;
129
std::random_shuffle(mShuffledUrls.begin(), mShuffledUrls.end(), generator);
130
// Ensure the first url is different from the previous last one, so that
131
// last url does not stay visible twice longer than usual
132
if (mLastShuffledUrl == mShuffledUrls.first() && mShuffledUrls.count() > 1) {
133
qSwap(mShuffledUrls[0], mShuffledUrls[1]);
135
mLastShuffledUrl = mShuffledUrls.last();
139
KUrl findNextRandomUrl() {
140
if (mShuffledUrls.empty()) {
141
if (GwenviewConfig::loop()) {
148
KUrl url = mShuffledUrls.last();
149
mShuffledUrls.pop_back();
155
void updateTimerInterval() {
156
mTimer->setInterval(int(GwenviewConfig::interval() * 1000));
161
if (MimeTypeUtils::urlKind(mCurrentUrl) == MimeTypeUtils::KIND_VIDEO) {
162
LOG("mState = WaitForEndOfUrl");
165
mState = WaitForEndOfUrl;
167
LOG("mState = Started");
84
QVector<KUrl> mShuffledUrls;
85
QVector<KUrl>::ConstIterator mStartIt;
87
KUrl mLastShuffledUrl;
90
QAction* mRandomAction;
94
if (GwenviewConfig::random()) {
95
return findNextRandomUrl();
97
return findNextOrderedUrl();
101
KUrl findNextOrderedUrl()
103
QVector<KUrl>::ConstIterator it = qFind(mUrls.constBegin(), mUrls.constEnd(), mCurrentUrl);
104
if (it == mUrls.constEnd()) {
105
kWarning() << "Current url not found in list. This should not happen.\n";
110
if (GwenviewConfig::loop()) {
111
// Looping, if we reach the end, start again
112
if (it == mUrls.constEnd()) {
113
it = mUrls.constBegin();
116
// Not looping, have we reached the end?
118
if (/*(it==mUrls.end() && GwenviewConfig::stopAtEnd()) ||*/ it == mStartIt) {
119
it = mUrls.constEnd();
123
if (it != mUrls.constEnd()) {
130
void initShuffledUrls()
132
mShuffledUrls = mUrls;
133
RandomNumberGenerator generator;
134
std::random_shuffle(mShuffledUrls.begin(), mShuffledUrls.end(), generator);
135
// Ensure the first url is different from the previous last one, so that
136
// last url does not stay visible twice longer than usual
137
if (mLastShuffledUrl == mShuffledUrls.first() && mShuffledUrls.count() > 1) {
138
qSwap(mShuffledUrls[0], mShuffledUrls[1]);
140
mLastShuffledUrl = mShuffledUrls.last();
143
KUrl findNextRandomUrl()
145
if (mShuffledUrls.empty()) {
146
if (GwenviewConfig::loop()) {
153
KUrl url = mShuffledUrls.last();
154
mShuffledUrls.pop_back();
159
void updateTimerInterval()
161
mTimer->setInterval(int(GwenviewConfig::interval() * 1000));
166
if (MimeTypeUtils::urlKind(mCurrentUrl) == MimeTypeUtils::KIND_VIDEO) {
167
LOG("mState = WaitForEndOfUrl");
170
mState = WaitForEndOfUrl;
172
LOG("mState = Started");
177
179
SlideShow::SlideShow(QObject* parent)
178
180
: QObject(parent)
179
, d(new SlideShowPrivate) {
182
d->mTimer = new QTimer(this);
183
connect(d->mTimer, SIGNAL(timeout()),
184
this, SLOT(goToNextUrl()) );
186
d->mLoopAction = new QAction(this);
187
d->mLoopAction->setText(i18nc("@item:inmenu toggle loop in slideshow", "Loop"));
188
d->mLoopAction->setCheckable(true);
189
connect(d->mLoopAction, SIGNAL(triggered()), SLOT(updateConfig()) );
191
d->mRandomAction = new QAction(this);
192
d->mRandomAction->setText(i18nc("@item:inmenu toggle random order in slideshow", "Random"));
193
d->mRandomAction->setCheckable(true);
194
connect(d->mRandomAction, SIGNAL(toggled(bool)), SLOT(slotRandomActionToggled(bool)) );
195
connect(d->mRandomAction, SIGNAL(triggered()), SLOT(updateConfig()) );
197
d->mLoopAction->setChecked(GwenviewConfig::loop());
198
d->mRandomAction->setChecked(GwenviewConfig::random());
202
SlideShow::~SlideShow() {
203
GwenviewConfig::self()->writeConfig();
208
QAction* SlideShow::loopAction() const {
209
return d->mLoopAction;
213
QAction* SlideShow::randomAction() const {
214
return d->mRandomAction;
218
void SlideShow::start(const QList<KUrl>& urls) {
219
d->mUrls.resize(urls.size());
220
qCopy(urls.begin(), urls.end(), d->mUrls.begin());
222
d->mStartIt=qFind(d->mUrls.constBegin(), d->mUrls.constEnd(), d->mCurrentUrl);
223
if (d->mStartIt==d->mUrls.constEnd()) {
224
kWarning() << "Current url not found in list, aborting.\n";
228
if (GwenviewConfig::random()) {
229
d->initShuffledUrls();
232
d->updateTimerInterval();
233
d->mTimer->setSingleShot(false);
239
void SlideShow::setInterval(int intervalInSeconds) {
240
GwenviewConfig::setInterval(double(intervalInSeconds));
241
d->updateTimerInterval();
245
void SlideShow::stop() {
246
LOG("Stopping timer");
253
void SlideShow::resumeAndGoToNextUrl() {
255
if (d->mState == WaitForEndOfUrl) {
261
void SlideShow::goToNextUrl() {
263
KUrl url = d->findNextUrl();
265
if (!url.isValid()) {
273
void SlideShow::setCurrentUrl(const KUrl& url) {
275
if (d->mCurrentUrl == url) {
278
d->mCurrentUrl = url;
279
// Restart timer to avoid showing new url for the remaining time of the old
281
if (d->mState != Stopped) {
287
bool SlideShow::isRunning() const {
288
return d->mState != Stopped;
292
void SlideShow::updateConfig() {
293
GwenviewConfig::setLoop(d->mLoopAction->isChecked());
294
GwenviewConfig::setRandom(d->mRandomAction->isChecked());
298
void SlideShow::slotRandomActionToggled(bool on) {
299
if (on && d->mState != Stopped) {
300
d->initShuffledUrls();
181
, d(new SlideShowPrivate)
185
d->mTimer = new QTimer(this);
186
connect(d->mTimer, SIGNAL(timeout()),
187
this, SLOT(goToNextUrl()));
189
d->mLoopAction = new QAction(this);
190
d->mLoopAction->setText(i18nc("@item:inmenu toggle loop in slideshow", "Loop"));
191
d->mLoopAction->setCheckable(true);
192
connect(d->mLoopAction, SIGNAL(triggered()), SLOT(updateConfig()));
194
d->mRandomAction = new QAction(this);
195
d->mRandomAction->setText(i18nc("@item:inmenu toggle random order in slideshow", "Random"));
196
d->mRandomAction->setCheckable(true);
197
connect(d->mRandomAction, SIGNAL(toggled(bool)), SLOT(slotRandomActionToggled(bool)));
198
connect(d->mRandomAction, SIGNAL(triggered()), SLOT(updateConfig()));
200
d->mLoopAction->setChecked(GwenviewConfig::loop());
201
d->mRandomAction->setChecked(GwenviewConfig::random());
204
SlideShow::~SlideShow()
206
GwenviewConfig::self()->writeConfig();
210
QAction* SlideShow::loopAction() const
212
return d->mLoopAction;
215
QAction* SlideShow::randomAction() const
217
return d->mRandomAction;
220
void SlideShow::start(const QList<KUrl>& urls)
222
d->mUrls.resize(urls.size());
223
qCopy(urls.begin(), urls.end(), d->mUrls.begin());
225
d->mStartIt = qFind(d->mUrls.constBegin(), d->mUrls.constEnd(), d->mCurrentUrl);
226
if (d->mStartIt == d->mUrls.constEnd()) {
227
kWarning() << "Current url not found in list, aborting.\n";
231
if (GwenviewConfig::random()) {
232
d->initShuffledUrls();
235
d->updateTimerInterval();
236
d->mTimer->setSingleShot(false);
241
void SlideShow::setInterval(int intervalInSeconds)
243
GwenviewConfig::setInterval(double(intervalInSeconds));
244
d->updateTimerInterval();
247
void SlideShow::stop()
249
LOG("Stopping timer");
255
void SlideShow::resumeAndGoToNextUrl()
258
if (d->mState == WaitForEndOfUrl) {
263
void SlideShow::goToNextUrl()
266
KUrl url = d->findNextUrl();
268
if (!url.isValid()) {
275
void SlideShow::setCurrentUrl(const KUrl& url)
278
if (d->mCurrentUrl == url) {
281
d->mCurrentUrl = url;
282
// Restart timer to avoid showing new url for the remaining time of the old
284
if (d->mState != Stopped) {
289
bool SlideShow::isRunning() const
291
return d->mState != Stopped;
294
void SlideShow::updateConfig()
296
GwenviewConfig::setLoop(d->mLoopAction->isChecked());
297
GwenviewConfig::setRandom(d->mRandomAction->isChecked());
300
void SlideShow::slotRandomActionToggled(bool on)
302
if (on && d->mState != Stopped) {
303
d->initShuffledUrls();