]> git.sesse.net Git - kdenlive/blob - src/stopmotion/stopmotion.cpp
New: analyse stopmotion video feed through color scopes
[kdenlive] / src / stopmotion / stopmotion.cpp
1 /***************************************************************************
2                           stopmotion.cpp  -  description
3                              -------------------
4     begin                : Feb 28 2008
5     copyright            : (C) 2010 by Jean-Baptiste Mardelle
6     email                : jb@kdenlive.org
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "stopmotion.h"
19 #include "../blackmagic/devices.h"
20 #include "../v4l/v4lcapture.h"
21 #include "../slideshowclip.h"
22 #include "kdenlivesettings.h"
23
24
25 #include <KDebug>
26 #include <KGlobalSettings>
27 #include <KFileDialog>
28 #include <KStandardDirs>
29 #include <KMessageBox>
30 #include <kdeversion.h>
31 #include <KNotification>
32
33 #ifdef QIMAGEBLITZ
34 #include <qimageblitz/qimageblitz.h>
35 #endif
36
37 #include <QtConcurrentRun>
38 #include <QInputDialog>
39 #include <QComboBox>
40 #include <QVBoxLayout>
41 #include <QTimer>
42 #include <QPainter>
43 #include <QAction>
44 #include <QWheelEvent>
45 #include <QMenu>
46
47 MyLabel::MyLabel(QWidget *parent) :
48     QLabel(parent)
49 {
50 }
51
52 void MyLabel::setImage(QImage img)
53 {
54     m_img = img;
55 }
56
57 //virtual
58 void MyLabel::wheelEvent(QWheelEvent * event)
59 {
60     if (event->delta() > 0) emit seek(true);
61     else emit seek(false);
62 }
63
64 //virtual
65 void MyLabel::mousePressEvent(QMouseEvent *)
66 {
67     emit switchToLive();
68 }
69
70 //virtual
71 void MyLabel::paintEvent(QPaintEvent * event)
72 {
73     Q_UNUSED(event);
74
75     QRect r(0, 0, width(), height());
76     QPainter p(this);
77     p.fillRect(r, QColor(KdenliveSettings::window_background()));
78     double aspect_ratio = (double) m_img.width() / m_img.height();
79     int pictureHeight = height();
80     int pictureWidth = width();
81     int calculatedWidth = aspect_ratio * height();
82     if (calculatedWidth > width()) pictureHeight = width() / aspect_ratio;
83     else {
84         int calculatedHeight = width() / aspect_ratio;
85         if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
86     }
87     p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
88     p.end();
89 }
90
91
92 StopmotionWidget::StopmotionWidget(KUrl projectFolder, QList< QAction * > actions, QWidget *parent) :
93     QDialog(parent)
94     , Ui::Stopmotion_UI()
95     , m_projectFolder(projectFolder)
96     , m_bmCapture(NULL)
97     , m_sequenceFrame(0)
98     , m_animatedIndex(-1)
99 {
100     //setAttribute(Qt::WA_DeleteOnClose);
101     QAction *analyse = new QAction(i18n("Send frames to color scopes"), this);
102     analyse->setCheckable(true);
103     analyse->setChecked(KdenliveSettings::analyse_stopmotion());
104     connect(analyse, SIGNAL(triggered(bool)), this, SLOT(slotSwitchAnalyse(bool)));
105     addActions(actions);
106     setupUi(this);
107     setWindowTitle(i18n("Stop Motion Capture"));
108     setFont(KGlobalSettings::toolBarFont());
109
110     live_button->setIcon(KIcon("camera-photo"));
111
112     m_captureAction = actions.at(0);
113     connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
114     capture_button->setDefaultAction(m_captureAction);
115
116     connect(actions.at(1), SIGNAL(triggered()), this, SLOT(slotSwitchLive()));
117
118     preview_button->setIcon(KIcon("media-playback-start"));
119     capture_button->setEnabled(false);
120
121     // Build config menu
122     QMenu *confMenu = new QMenu;
123     m_showOverlay = actions.at(2);
124     connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
125     confMenu->addAction(m_showOverlay);
126
127 #ifdef QIMAGEBLITZ
128     m_effectIndex = KdenliveSettings::blitzeffect();
129     QMenu *effectsMenu = new QMenu(i18n("Overlay effect"));
130     QActionGroup *effectGroup = new QActionGroup(this);
131     QAction *noEffect = new QAction(i18n("No Effect"), effectGroup);
132     noEffect->setData(1);
133     QAction *contrastEffect = new QAction(i18n("Contrast"), effectGroup);
134     contrastEffect->setData(2);
135     QAction *edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
136     edgeEffect->setData(3);
137     QAction *brightEffect = new QAction(i18n("Brighten"), effectGroup);
138     brightEffect->setData(4);
139     QAction *invertEffect = new QAction(i18n("Invert"), effectGroup);
140     invertEffect->setData(5);
141     QAction *thresEffect = new QAction(i18n("Threshold"), effectGroup);
142     thresEffect->setData(6);
143
144     effectsMenu->addAction(noEffect);
145     effectsMenu->addAction(contrastEffect);
146     effectsMenu->addAction(edgeEffect);
147     effectsMenu->addAction(brightEffect);
148     effectsMenu->addAction(invertEffect);
149     effectsMenu->addAction(thresEffect);
150     QList <QAction *> list = effectsMenu->actions();
151     for (int i = 0; i < list.count(); i++) {
152         list.at(i)->setCheckable(true);
153         if (list.at(i)->data().toInt() == m_effectIndex) {
154             list.at(i)->setChecked(true);
155         }
156     }
157     connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
158     confMenu->addMenu(effectsMenu);
159 #endif
160
161     QAction *showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
162     showThumbs->setCheckable(true);
163     showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
164     connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
165
166     QAction *removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
167     removeCurrent->setShortcut(Qt::Key_Delete);
168     connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
169
170     QAction *capInterval = new QAction(KIcon(), i18n("Set capture interval"), this);
171     connect(capInterval, SIGNAL(triggered()), this, SLOT(slotSetCaptureInterval()));
172
173     confMenu->addAction(showThumbs);
174     confMenu->addAction(capInterval);
175     confMenu->addAction(removeCurrent);
176     confMenu->addAction(analyse);
177     config_button->setIcon(KIcon("configure"));
178     config_button->setMenu(confMenu);
179
180     // Build capture menu
181     QMenu *capMenu = new QMenu;
182     m_intervalCapture = new QAction(KIcon("edit-redo"), i18n("Interval capture"), this);
183     m_intervalCapture->setCheckable(true);
184     m_intervalCapture->setChecked(false);
185     connect(m_intervalCapture, SIGNAL(triggered(bool)), this, SLOT(slotIntervalCapture(bool)));
186     capMenu->addAction(m_intervalCapture);
187     capture_button->setMenu(capMenu);
188
189     connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
190     connect(sequence_name, SIGNAL(currentIndexChanged(int)), live_button, SLOT(setFocus()));
191
192     m_layout = new QVBoxLayout;
193     if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
194         // Found a BlackMagic device
195         m_bmCapture = new BmdCaptureHandler(m_layout);
196         connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
197     }
198     if (QFile::exists(KdenliveSettings::video4vdevice())) {
199 #if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
200         V4lCaptureHandler v4l(NULL);
201         // Video 4 Linux device detection
202         for (int i = 0; i < 10; i++) {
203             QString path = "/dev/video" + QString::number(i);
204             if (QFile::exists(path)) {
205                 QStringList deviceInfo = v4l.getDeviceName(path);
206                 if (!deviceInfo.isEmpty()) {
207                     capture_device->addItem(deviceInfo.at(0), "v4l");
208                     capture_device->setItemData(capture_device->count() - 1, path, Qt::UserRole + 1);
209                     capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(1), Qt::UserRole + 2);
210                     if (path == KdenliveSettings::video4vdevice()) capture_device->setCurrentIndex(capture_device->count() - 1);
211                 }
212             }
213         }
214
215         /*V4lCaptureHandler v4lhandler(NULL);
216         QStringList deviceInfo = v4lhandler.getDeviceName(KdenliveSettings::video4vdevice());
217             capture_device->addItem(deviceInfo.at(0), "v4l");
218         capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(3), Qt::UserRole + 1);*/
219         if (m_bmCapture == NULL) {
220             m_bmCapture = new V4lCaptureHandler(m_layout);
221             m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
222         }
223 #endif
224     }
225
226     connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
227     if (m_bmCapture) {
228         connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
229         connect(m_bmCapture, SIGNAL(gotFrame(QImage)), this, SIGNAL(gotFrame(QImage)));
230     } else live_button->setEnabled(false);
231     m_frame_preview = new MyLabel(this);
232     connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
233     connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
234     m_layout->addWidget(m_frame_preview);
235     m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
236     video_preview->setLayout(m_layout);
237     live_button->setChecked(false);
238     button_addsequence->setEnabled(false);
239     connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
240     connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
241     connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
242     connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
243     connect(frame_list, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slotShowSelectedFrame()));
244     connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
245
246     frame_list->addAction(removeCurrent);
247     frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
248     frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
249     parseExistingSequences();
250 }
251
252 StopmotionWidget::~StopmotionWidget()
253 {
254     if (m_bmCapture)
255         m_bmCapture->stopPreview();
256 }
257
258 void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
259 {
260 #ifdef QIMAGEBLITZ
261     if (act) m_effectIndex = act->data().toInt();
262     KdenliveSettings::setBlitzeffect(m_effectIndex);
263     if (m_showOverlay->isChecked()) slotUpdateOverlay();
264 #endif
265 }
266
267 void StopmotionWidget::closeEvent(QCloseEvent *e)
268 {
269     slotLive(false);
270     QDialog::closeEvent(e);
271 }
272
273 void StopmotionWidget::slotSetCaptureInterval()
274 {
275     int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
276     if (interval > 0 && interval != KdenliveSettings::captureinterval())
277         KdenliveSettings::setCaptureinterval(interval);
278 }
279
280 void StopmotionWidget::slotShowThumbs(bool show)
281 {
282     KdenliveSettings::setShowstopmotionthumbs(show);
283     if (show) {
284         frame_list->clear();
285         sequenceNameChanged(sequence_name->currentText());
286     } else {
287         m_filesList.clear();
288         frame_list->clear();
289     }
290     frame_list->setHidden(!show);
291 }
292
293 void StopmotionWidget::slotIntervalCapture(bool capture)
294 {
295     if (capture) slotCaptureFrame();
296 }
297
298
299 void StopmotionWidget::slotUpdateHandler()
300 {
301     QString data = capture_device->itemData(capture_device->currentIndex()).toString();
302     slotLive(false);
303     if (m_bmCapture) {
304         delete m_bmCapture;
305     }
306     m_layout->removeWidget(m_frame_preview);
307     if (data == "v4l") {
308 #if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
309         m_bmCapture = new V4lCaptureHandler(m_layout);
310         m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
311 #endif
312     } else {
313         m_bmCapture = new BmdCaptureHandler(m_layout);
314         if (m_bmCapture) connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
315     }
316     live_button->setEnabled(m_bmCapture != NULL);
317     m_layout->addWidget(m_frame_preview);
318 }
319
320 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
321 {
322     log_box->insertItem(0, message);
323 }
324
325 void StopmotionWidget::parseExistingSequences()
326 {
327     sequence_name->clear();
328     sequence_name->addItem(QString());
329     QDir dir(m_projectFolder.path());
330     QStringList filters;
331     filters << "*_0000.png";
332     //dir.setNameFilters(filters);
333     QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
334     //kDebug()<<"PF: "<<<<", sm: "<<sequences;
335     foreach(QString sequencename, sequences) {
336         sequence_name->addItem(sequencename.section("_", 0, -2));
337     }
338 }
339
340 void StopmotionWidget::slotSwitchLive()
341 {
342     setUpdatesEnabled(false);
343     if (m_frame_preview->isHidden()) {
344         if (m_bmCapture) m_bmCapture->hidePreview(true);
345         m_frame_preview->setHidden(false);
346     } else {
347         m_frame_preview->setHidden(true);
348         if (m_bmCapture) m_bmCapture->hidePreview(false);
349         capture_button->setEnabled(true);
350     }
351     setUpdatesEnabled(true);
352 }
353
354 void StopmotionWidget::slotLive(bool isOn)
355 {
356     if (isOn && m_bmCapture) {
357         //m_frame_preview->setImage(QImage());
358         m_frame_preview->setHidden(true);
359         m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
360         capture_button->setEnabled(true);
361     } else {
362         if (m_bmCapture) m_bmCapture->stopPreview();
363         m_frame_preview->setHidden(false);
364         capture_button->setEnabled(false);
365         live_button->setChecked(false);
366     }
367 }
368
369 void StopmotionWidget::slotShowOverlay(bool isOn)
370 {
371     if (isOn) {
372         if (live_button->isChecked() && m_sequenceFrame > 0) {
373             slotUpdateOverlay();
374         }
375     } else if (m_bmCapture) {
376         m_bmCapture->hideOverlay();
377     }
378 }
379
380 void StopmotionWidget::slotUpdateOverlay()
381 {
382     if (m_bmCapture == NULL) return;
383     QString path = getPathForFrame(m_sequenceFrame - 1);
384     if (!QFile::exists(path)) return;
385     QImage img(path);
386     if (img.isNull()) {
387         QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
388         return;
389     }
390
391 #ifdef QIMAGEBLITZ
392     //img = Blitz::convolveEdge(img, 0, Blitz::Low);
393     switch (m_effectIndex) {
394     case 2:
395         img = Blitz::contrast(img, true, 6);
396         break;
397     case 3:
398         img = Blitz::edge(img);
399         break;
400     case 4:
401         img = Blitz::intensity(img, 0.5);
402         break;
403     case 5:
404         Blitz::invert(img);
405         break;
406     case 6:
407         img = Blitz::threshold(img, 120, Blitz::Grayscale, qRgba(0, 0, 0, 0), qRgba(255, 0, 0, 255));
408         //img = Blitz::flatten(img, QColor(255, 0, 0, 255), QColor(0, 0, 0, 0));
409         break;
410     default:
411         break;
412
413     }
414 #endif
415     m_bmCapture->showOverlay(img);
416 }
417
418 void StopmotionWidget::sequenceNameChanged(const QString &name)
419 {
420     // Get rid of frames from previous sequence
421     disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
422     m_filesList.clear();
423     m_future.waitForFinished();
424     frame_list->clear();
425     if (name.isEmpty()) {
426         button_addsequence->setEnabled(false);
427     } else {
428         // Check if we are editing an existing sequence
429         QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
430         m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
431         if (!m_filesList.isEmpty()) {
432             m_sequenceName = sequence_name->currentText();
433             connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
434             m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
435             button_addsequence->setEnabled(true);
436         } else {
437             // new sequence
438             connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
439             button_addsequence->setEnabled(false);
440         }
441         capture_button->setEnabled(live_button->isChecked());
442     }
443 }
444
445 void StopmotionWidget::slotCaptureFrame()
446 {
447     if (m_bmCapture == NULL) return;
448     if (sequence_name->currentText().isEmpty()) {
449         QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
450         if (seqName.isEmpty()) return;
451         sequence_name->blockSignals(true);
452         sequence_name->setItemText(sequence_name->currentIndex(), seqName);
453         sequence_name->blockSignals(false);
454     }
455     if (m_sequenceName != sequence_name->currentText()) {
456         m_sequenceName = sequence_name->currentText();
457         m_sequenceFrame = 0;
458     }
459     //capture_button->setEnabled(false);
460     QString currentPath = getPathForFrame(m_sequenceFrame);
461     m_bmCapture->captureFrame(currentPath);
462     KNotification::event("FrameCaptured");
463     m_sequenceFrame++;
464     button_addsequence->setEnabled(true);
465     if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
466 }
467
468
469 void StopmotionWidget::slotNewThumb(const QString path)
470 {
471     if (!KdenliveSettings::showstopmotionthumbs()) return;
472     m_filesList.append(path);
473     if (m_showOverlay->isChecked()) slotUpdateOverlay();
474     if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
475 }
476
477 void StopmotionWidget::slotPrepareThumbs()
478 {
479     if (m_filesList.isEmpty()) return;
480     QString path = m_filesList.takeFirst();
481     emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
482
483 }
484
485 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
486 {
487     if (img.isNull()) {
488         m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
489         return;
490     }
491     int height = 90;
492     int width = height * img.width() / img.height();
493     frame_list->setIconSize(QSize(width, height));
494     QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
495     QString nb = QString::number(ix);
496     QPainter p(&pix);
497     QFontInfo finfo(font());
498     p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
499     p.setPen(Qt::white);
500     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
501     p.end();
502     QIcon icon(pix);
503     QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
504     item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
505     item->setData(Qt::UserRole, ix);
506     frame_list->blockSignals(true);
507     frame_list->setCurrentItem(item);
508     frame_list->blockSignals(false);
509     m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
510 }
511
512 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
513 {
514     if (seqName.isEmpty()) seqName = m_sequenceName;
515     return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
516 }
517
518 void StopmotionWidget::slotShowFrame(const QString &path)
519 {
520     //slotLive(false);
521     QImage img(path);
522     capture_button->setEnabled(false);
523     if (!img.isNull()) {
524         if (m_bmCapture) m_bmCapture->hidePreview(true);
525         m_frame_preview->setImage(img);
526         m_frame_preview->setHidden(false);
527         m_frame_preview->update();
528     }
529 }
530
531 void StopmotionWidget::slotShowSelectedFrame()
532 {
533     QListWidgetItem *item = frame_list->currentItem();
534     if (item) {
535         //int ix = item->data(Qt::UserRole).toInt();;
536         slotShowFrame(item->toolTip());
537     }
538 }
539
540 void StopmotionWidget::slotAddSequence()
541 {
542     emit addOrUpdateSequence(getPathForFrame(0));
543 }
544
545 void StopmotionWidget::slotPlayPreview(bool animate)
546 {
547     if (!animate) {
548         // stop animation
549         m_animationList.clear();
550         return;
551     }
552     if (KdenliveSettings::showstopmotionthumbs()) {
553         frame_list->setCurrentRow(0);
554         QTimer::singleShot(200, this, SLOT(slotAnimate()));
555     } else {
556         SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
557         slotAnimate();
558     }
559 }
560
561 void StopmotionWidget::slotAnimate()
562 {
563     //slotShowFrame(m_animatedIndex);
564     if (KdenliveSettings::showstopmotionthumbs()) {
565         //TODO: loop
566         if (frame_list->currentRow() < (frame_list->count() - 1)) {
567             frame_list->setCurrentRow(frame_list->currentRow() + 1);
568             QTimer::singleShot(100, this, SLOT(slotAnimate()));
569         } else preview_button->setChecked(false);
570     } else if (!m_animationList.isEmpty()) {
571         slotShowFrame(m_animationList.takeFirst());
572         QTimer::singleShot(100, this, SLOT(slotAnimate()));
573     } else preview_button->setChecked(false);
574
575 }
576
577 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
578 {
579     QListWidgetItem *item = NULL;
580     int pos = ix;
581     if (ix >= frame_list->count()) {
582         pos = frame_list->count() - 1;
583     }
584     if (ix < 0) pos = 0;
585     item = frame_list->item(pos);
586
587     int value = item->data(Qt::UserRole).toInt();
588     if (value == ix) return item;
589     else if (value < ix) {
590         pos++;
591         while (pos < frame_list->count()) {
592             item = frame_list->item(pos);
593             value = item->data(Qt::UserRole).toInt();
594             if (value == ix) return item;
595             pos++;
596         }
597     } else {
598         pos --;
599         while (pos >= 0) {
600             item = frame_list->item(pos);
601             value = item->data(Qt::UserRole).toInt();
602             if (value == ix) return item;
603             pos --;
604         }
605     }
606     return NULL;
607 }
608
609
610 void StopmotionWidget::selectFrame(int ix)
611 {
612     frame_list->blockSignals(true);
613     QListWidgetItem *item = getFrameFromIndex(ix);
614     if (!item) return;
615     frame_list->setCurrentItem(item);
616     frame_list->blockSignals(false);
617 }
618
619 void StopmotionWidget::slotSeekFrame(bool forward)
620 {
621     int ix = frame_list->currentRow();
622     if (forward) {
623         if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
624     } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
625 }
626
627 void StopmotionWidget::slotRemoveFrame()
628 {
629     if (frame_list->currentItem() == NULL) return;
630     QString path = frame_list->currentItem()->toolTip();
631     if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
632     QFile f(path);
633     if (f.remove()) {
634         QListWidgetItem *item = frame_list->takeItem(frame_list->currentRow());
635         delete item;
636     }
637 }
638
639 void StopmotionWidget::slotSwitchAnalyse(bool isOn)
640 {
641     KdenliveSettings::setAnalyse_stopmotion(isOn);
642     m_bmCapture->setAnalyse(isOn);
643 }
644
645