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