]> git.sesse.net Git - kdenlive/blobdiff - src/stopmotion/stopmotion.cpp
small ui fix
[kdenlive] / src / stopmotion / stopmotion.cpp
index d6d6786df7735a1f5b5b49f86a6c21c08679e0c5..6c8f535e286ed54db9e6a27ab48188d13fd880d8 100644 (file)
@@ -19,6 +19,7 @@
 #include "../blackmagic/devices.h"
 #include "../v4l/v4lcapture.h"
 #include "../slideshowclip.h"
+#include "ui_smconfig_ui.h"
 #include "kdenlivesettings.h"
 
 
@@ -44,7 +45,7 @@
 #include <QWheelEvent>
 #include <QMenu>
 
-MyLabel::MyLabel(QWidget *parent) :
+MyLabel::MyLabel(QWidgetparent) :
     QLabel(parent)
 {
 }
@@ -55,14 +56,20 @@ void MyLabel::setImage(QImage img)
 }
 
 //virtual
-void MyLabel::wheelEvent(QWheelEvent * event)
+void MyLabel::wheelEvent(QWheelEvent* event)
 {
     if (event->delta() > 0) emit seek(true);
     else emit seek(false);
 }
 
 //virtual
-void MyLabel::paintEvent(QPaintEvent * event)
+void MyLabel::mousePressEvent(QMouseEvent*)
+{
+    emit switchToLive();
+}
+
+//virtual
+void MyLabel::paintEvent(QPaintEvent* event)
 {
     Q_UNUSED(event);
 
@@ -83,50 +90,67 @@ void MyLabel::paintEvent(QPaintEvent * event)
 }
 
 
-StopmotionWidget::StopmotionWidget(KUrl projectFolder, QWidget *parent) :
+StopmotionWidget::StopmotionWidget(KUrl projectFolder, QList< QAction* > actions, QWidget* parent) :
     QDialog(parent)
     , Ui::Stopmotion_UI()
     , m_projectFolder(projectFolder)
     , m_bmCapture(NULL)
     , m_sequenceFrame(0)
     , m_animatedIndex(-1)
+    , m_animate(false)
 {
+    //setAttribute(Qt::WA_DeleteOnClose);
+    QAction* analyse = new QAction(i18n("Send frames to color scopes"), this);
+    analyse->setCheckable(true);
+    analyse->setChecked(KdenliveSettings::analyse_stopmotion());
+    connect(analyse, SIGNAL(triggered(bool)), this, SLOT(slotSwitchAnalyse(bool)));
+    addActions(actions);
     setupUi(this);
     setWindowTitle(i18n("Stop Motion Capture"));
     setFont(KGlobalSettings::toolBarFont());
 
     live_button->setIcon(KIcon("camera-photo"));
-    m_captureAction = new QAction(KIcon("media-record"), i18n("Capture frame"), this);
-    m_captureAction->setShortcut(QKeySequence(Qt::Key_Space));
+
+    m_captureAction = actions.at(0);
     connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
+    m_captureAction->setCheckable(true);
+    m_captureAction->setChecked(false);
     capture_button->setDefaultAction(m_captureAction);
 
+    connect(actions.at(1), SIGNAL(triggered()), this, SLOT(slotSwitchLive()));
+
+    QAction *intervalCapture = new QAction(i18n("Interval capture"), this);
+    intervalCapture->setIcon(KIcon("chronometer"));
+    intervalCapture->setCheckable(true);
+    intervalCapture->setChecked(false);
+    capture_interval->setDefaultAction(intervalCapture);
+        
     preview_button->setIcon(KIcon("media-playback-start"));
     capture_button->setEnabled(false);
+    
 
     // Build config menu
-    QMenu *confMenu = new QMenu;
-    m_showOverlay = new QAction(KIcon("edit-paste"), i18n("Show last frame over video"), this);
-    m_showOverlay->setCheckable(true);
-    m_showOverlay->setChecked(false);
+    QMenu* confMenu = new QMenu;
+    m_showOverlay = actions.at(2);
     connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
-    confMenu->addAction(m_showOverlay);
+    overlay_button->setDefaultAction(m_showOverlay);
+    //confMenu->addAction(m_showOverlay);
 
 #ifdef QIMAGEBLITZ
     m_effectIndex = KdenliveSettings::blitzeffect();
-    QMenu *effectsMenu = new QMenu(i18n("Overlay effect"));
-    QActionGroup *effectGroup = new QActionGroup(this);
-    QAction *noEffect = new QAction(i18n("No Effect"), effectGroup);
+    QMenueffectsMenu = new QMenu(i18n("Overlay effect"));
+    QActionGroupeffectGroup = new QActionGroup(this);
+    QActionnoEffect = new QAction(i18n("No Effect"), effectGroup);
     noEffect->setData(1);
-    QAction *contrastEffect = new QAction(i18n("Contrast"), effectGroup);
+    QActioncontrastEffect = new QAction(i18n("Contrast"), effectGroup);
     contrastEffect->setData(2);
-    QAction *edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
+    QActionedgeEffect = new QAction(i18n("Edge detect"), effectGroup);
     edgeEffect->setData(3);
-    QAction *brightEffect = new QAction(i18n("Brighten"), effectGroup);
+    QActionbrightEffect = new QAction(i18n("Brighten"), effectGroup);
     brightEffect->setData(4);
-    QAction *invertEffect = new QAction(i18n("Invert"), effectGroup);
+    QActioninvertEffect = new QAction(i18n("Invert"), effectGroup);
     invertEffect->setData(5);
-    QAction *thresEffect = new QAction(i18n("Threshold"), effectGroup);
+    QActionthresEffect = new QAction(i18n("Threshold"), effectGroup);
     thresEffect->setData(6);
 
     effectsMenu->addAction(noEffect);
@@ -135,7 +159,7 @@ StopmotionWidget::StopmotionWidget(KUrl projectFolder, QWidget *parent) :
     effectsMenu->addAction(brightEffect);
     effectsMenu->addAction(invertEffect);
     effectsMenu->addAction(thresEffect);
-    QList <QAction *> list = effectsMenu->actions();
+    QList <QAction*> list = effectsMenu->actions();
     for (int i = 0; i < list.count(); i++) {
         list.at(i)->setCheckable(true);
         if (list.at(i)->data().toInt() == m_effectIndex) {
@@ -146,70 +170,99 @@ StopmotionWidget::StopmotionWidget(KUrl projectFolder, QWidget *parent) :
     confMenu->addMenu(effectsMenu);
 #endif
 
-    QAction *showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
+    QActionshowThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
     showThumbs->setCheckable(true);
     showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
     connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
 
-    QAction *removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
-    //TODO: implement frame deletion
-    //connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
+    QActionremoveCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
+    removeCurrent->setShortcut(Qt::Key_Delete);
+    connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
 
-    QAction *capInterval = new QAction(KIcon(), i18n("Set capture interval"), this);
-    connect(capInterval, SIGNAL(triggered()), this, SLOT(slotSetCaptureInterval()));
+    QAction* conf = new QAction(KIcon("configure"), i18n("Configure"), this);
+    connect(conf, SIGNAL(triggered()), this, SLOT(slotConfigure()));
 
     confMenu->addAction(showThumbs);
-    confMenu->addAction(capInterval);
     confMenu->addAction(removeCurrent);
+    confMenu->addAction(analyse);
+    confMenu->addAction(conf);
     config_button->setIcon(KIcon("configure"));
     config_button->setMenu(confMenu);
 
-    // Build capture menu
-    QMenu *capMenu = new QMenu;
-    m_intervalCapture = new QAction(KIcon("edit-redo"), i18n("Interval capture"), this);
-    m_intervalCapture->setCheckable(true);
-    m_intervalCapture->setChecked(false);
-    connect(m_intervalCapture, SIGNAL(triggered(bool)), this, SLOT(slotIntervalCapture(bool)));
-    capMenu->addAction(m_intervalCapture);
-    capture_button->setMenu(capMenu);
+    connect(sequence_name, SIGNAL(textChanged(const QString&)), this, SLOT(sequenceNameChanged(const QString&)));
+    connect(sequence_name, SIGNAL(currentIndexChanged(int)), live_button, SLOT(setFocus()));
 
-    connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
     m_layout = new QVBoxLayout;
     if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
         // Found a BlackMagic device
         m_bmCapture = new BmdCaptureHandler(m_layout);
-        connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
+        connect(m_bmCapture, SIGNAL(gotMessage(const QString&)), this, SLOT(slotGotHDMIMessage(const QString&)));
     }
     if (QFile::exists(KdenliveSettings::video4vdevice())) {
-        if (m_bmCapture == NULL) m_bmCapture = new V4lCaptureHandler(m_layout);
-        capture_device->addItem(KdenliveSettings::video4vdevice(), "v4l");
+#if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
+        V4lCaptureHandler v4l(NULL);
+        // Video 4 Linux device detection
+        for (int i = 0; i < 10; i++) {
+            QString path = "/dev/video" + QString::number(i);
+            if (QFile::exists(path)) {
+                QStringList deviceInfo = v4l.getDeviceName(path);
+                if (!deviceInfo.isEmpty()) {
+                    capture_device->addItem(deviceInfo.at(0), "v4l");
+                    capture_device->setItemData(capture_device->count() - 1, path, Qt::UserRole + 1);
+                    capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(1), Qt::UserRole + 2);
+                    if (path == KdenliveSettings::video4vdevice()) capture_device->setCurrentIndex(capture_device->count() - 1);
+                }
+            }
+        }
+
+        /*V4lCaptureHandler v4lhandler(NULL);
+        QStringList deviceInfo = v4lhandler.getDeviceName(KdenliveSettings::video4vdevice());
+            capture_device->addItem(deviceInfo.at(0), "v4l");
+        capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(3), Qt::UserRole + 1);*/
+        if (m_bmCapture == NULL) {
+            m_bmCapture = new V4lCaptureHandler(m_layout);
+            m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
+        }
+#endif
     }
 
-    connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
     connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
+    if (m_bmCapture) {
+        connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
+        connect(m_bmCapture, SIGNAL(gotFrame(QImage)), this, SIGNAL(gotFrame(QImage)));
+    } else live_button->setEnabled(false);
     m_frame_preview = new MyLabel(this);
     connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
+    connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
     m_layout->addWidget(m_frame_preview);
     m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
     video_preview->setLayout(m_layout);
     live_button->setChecked(false);
     button_addsequence->setEnabled(false);
-    connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
+    connect(live_button, SIGNAL(toggled(bool)), this, SLOT(slotLive(bool)));
     connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
     connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
     connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
+    connect(frame_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotShowSelectedFrame()));
     connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
 
+    frame_list->addAction(removeCurrent);
+    frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
     frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
     parseExistingSequences();
+    QTimer::singleShot(500, this, SLOT(slotLive()));
+    connect(&m_intervalTimer, SIGNAL(timeout()), this, SLOT(slotCaptureFrame()));
+    m_intervalTimer.setSingleShot(true);
+    m_intervalTimer.setInterval(KdenliveSettings::captureinterval() * 1000);
 }
 
 StopmotionWidget::~StopmotionWidget()
 {
-    m_bmCapture->stopPreview();
+    if (m_bmCapture)
+        m_bmCapture->stopPreview();
 }
 
-void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
+void StopmotionWidget::slotUpdateOverlayEffect(QActionact)
 {
 #ifdef QIMAGEBLITZ
     if (act) m_effectIndex = act->data().toInt();
@@ -218,17 +271,35 @@ void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
 #endif
 }
 
-void StopmotionWidget::closeEvent(QCloseEvent *e)
+void StopmotionWidget::closeEvent(QCloseEvente)
 {
     slotLive(false);
     QDialog::closeEvent(e);
 }
 
-void StopmotionWidget::slotSetCaptureInterval()
+void StopmotionWidget::slotConfigure()
 {
-    int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
-    if (interval > 0 && interval != KdenliveSettings::captureinterval())
-        KdenliveSettings::setCaptureinterval(interval);
+    QDialog d;
+    Ui::SmConfig_UI ui;
+    ui.setupUi(&d);
+    d.setWindowTitle(i18n("Configure Stop Motion"));
+    ui.sm_interval->setValue(KdenliveSettings::captureinterval());
+    ui.sm_interval->setSuffix(ki18np(" second", " seconds"));
+    ui.sm_notifytime->setSuffix(ki18np(" second", " seconds"));
+    ui.sm_notifytime->setValue(KdenliveSettings::sm_notifytime());
+    connect(ui.sm_prenotify, SIGNAL(toggled(bool)), ui.sm_notifytime, SLOT(setEnabled(bool)));
+    ui.sm_prenotify->setChecked(KdenliveSettings::sm_prenotify());
+    ui.sm_loop->setChecked(KdenliveSettings::sm_loop());
+    ui.sm_framesplayback->setValue(KdenliveSettings::sm_framesplayback());
+    
+    if (d.exec() == QDialog::Accepted) {
+        KdenliveSettings::setSm_loop(ui.sm_loop->isChecked());
+        KdenliveSettings::setCaptureinterval(ui.sm_interval->value());
+        KdenliveSettings::setSm_framesplayback(ui.sm_framesplayback->value());
+        KdenliveSettings::setSm_notifytime(ui.sm_notifytime->value());
+        KdenliveSettings::setSm_prenotify(ui.sm_prenotify->isChecked());
+        m_intervalTimer.setInterval(KdenliveSettings::captureinterval() * 1000);
+    }
 }
 
 void StopmotionWidget::slotShowThumbs(bool show)
@@ -244,28 +315,29 @@ void StopmotionWidget::slotShowThumbs(bool show)
     frame_list->setHidden(!show);
 }
 
-void StopmotionWidget::slotIntervalCapture(bool capture)
-{
-    if (capture) slotCaptureFrame();
-}
-
 
 void StopmotionWidget::slotUpdateHandler()
 {
     QString data = capture_device->itemData(capture_device->currentIndex()).toString();
-    m_bmCapture->stopPreview();
-    delete m_bmCapture;
+    slotLive(false);
+    if (m_bmCapture) {
+        delete m_bmCapture;
+    }
     m_layout->removeWidget(m_frame_preview);
     if (data == "v4l") {
+#if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
         m_bmCapture = new V4lCaptureHandler(m_layout);
+        m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
+#endif
     } else {
         m_bmCapture = new BmdCaptureHandler(m_layout);
-        connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
+        if (m_bmCapture) connect(m_bmCapture, SIGNAL(gotMessage(const QString&)), this, SLOT(slotGotHDMIMessage(const QString&)));
     }
+    live_button->setEnabled(m_bmCapture != NULL);
     m_layout->addWidget(m_frame_preview);
 }
 
-void StopmotionWidget::slotGotHDMIMessage(const QString &message)
+void StopmotionWidget::slotGotHDMIMessage(const QStringmessage)
 {
     log_box->insertItem(0, message);
 }
@@ -285,18 +357,36 @@ void StopmotionWidget::parseExistingSequences()
     }
 }
 
+void StopmotionWidget::slotSwitchLive()
+{
+    setUpdatesEnabled(false);
+    if (m_frame_preview->isHidden()) {
+        if (m_bmCapture) m_bmCapture->hidePreview(true);
+        m_frame_preview->setHidden(false);
+    } else {
+        m_frame_preview->setHidden(true);
+        if (m_bmCapture) m_bmCapture->hidePreview(false);
+        capture_button->setEnabled(true);
+    }
+    setUpdatesEnabled(true);
+}
+
 void StopmotionWidget::slotLive(bool isOn)
 {
-    if (isOn) {
-        m_frame_preview->setImage(QImage());
+    live_button->blockSignals(true);
+    if (isOn && m_bmCapture) {
+        //m_frame_preview->setImage(QImage());
         m_frame_preview->setHidden(true);
-        m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode());
+        m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
         capture_button->setEnabled(true);
+        live_button->setChecked(true);
     } else {
-        m_bmCapture->stopPreview();
+        if (m_bmCapture) m_bmCapture->stopPreview();
+        m_frame_preview->setHidden(false);
         capture_button->setEnabled(false);
         live_button->setChecked(false);
     }
+    live_button->blockSignals(false);
 }
 
 void StopmotionWidget::slotShowOverlay(bool isOn)
@@ -305,13 +395,14 @@ void StopmotionWidget::slotShowOverlay(bool isOn)
         if (live_button->isChecked() && m_sequenceFrame > 0) {
             slotUpdateOverlay();
         }
-    } else {
+    } else if (m_bmCapture) {
         m_bmCapture->hideOverlay();
     }
 }
 
 void StopmotionWidget::slotUpdateOverlay()
 {
+    if (m_bmCapture == NULL) return;
     QString path = getPathForFrame(m_sequenceFrame - 1);
     if (!QFile::exists(path)) return;
     QImage img(path);
@@ -347,7 +438,7 @@ void StopmotionWidget::slotUpdateOverlay()
     m_bmCapture->showOverlay(img);
 }
 
-void StopmotionWidget::sequenceNameChanged(const QString &name)
+void StopmotionWidget::sequenceNameChanged(const QStringname)
 {
     // Get rid of frames from previous sequence
     disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
@@ -376,9 +467,13 @@ void StopmotionWidget::sequenceNameChanged(const QString &name)
 
 void StopmotionWidget::slotCaptureFrame()
 {
+    if (m_bmCapture == NULL) return;
     if (sequence_name->currentText().isEmpty()) {
         QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
-        if (seqName.isEmpty()) return;
+        if (seqName.isEmpty()) {
+            m_captureAction->setChecked(false);
+            return;
+        }
         sequence_name->blockSignals(true);
         sequence_name->setItemText(sequence_name->currentIndex(), seqName);
         sequence_name->blockSignals(false);
@@ -388,13 +483,26 @@ void StopmotionWidget::slotCaptureFrame()
         m_sequenceFrame = 0;
     }
     //capture_button->setEnabled(false);
+    if (m_intervalTimer.isActive()) {
+        // stop interval capture
+        m_intervalTimer.stop();
+        return;
+    }
     QString currentPath = getPathForFrame(m_sequenceFrame);
-    kDebug() << "Capture FRame NB: " << m_sequenceFrame;
     m_bmCapture->captureFrame(currentPath);
-    KNotification::event("FrameCaptured");
+    KNotification::event("FrameCaptured", i18n("Frame Captured"), QPixmap(), this);
     m_sequenceFrame++;
     button_addsequence->setEnabled(true);
-    if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
+    if (capture_interval->isChecked()) {
+        if (KdenliveSettings::sm_prenotify()) QTimer::singleShot((KdenliveSettings::captureinterval() - KdenliveSettings::sm_notifytime()) * 1000, this, SLOT(slotPreNotify()));
+        m_intervalTimer.start();
+    }
+    else m_captureAction->setChecked(false);
+}
+
+void StopmotionWidget::slotPreNotify()
+{
+    if (m_captureAction->isChecked()) KNotification::event("ReadyToCapture", i18n("Going to Capture Frame"), QPixmap(), this);
 }
 
 
@@ -432,9 +540,12 @@ void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
     p.end();
     QIcon icon(pix);
-    QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
+    QListWidgetItemitem = new QListWidgetItem(icon, QString(), frame_list);
     item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
     item->setData(Qt::UserRole, ix);
+    frame_list->blockSignals(true);
+    frame_list->setCurrentItem(item);
+    frame_list->blockSignals(false);
     m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
 }
 
@@ -444,13 +555,14 @@ QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
     return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
 }
 
-void StopmotionWidget::slotShowFrame(const QString &path)
+void StopmotionWidget::slotShowFrame(const QStringpath)
 {
-    slotLive(false);
+    //slotLive(false);
     QImage img(path);
     capture_button->setEnabled(false);
+    slotLive(false);
     if (!img.isNull()) {
-        m_bmCapture->hidePreview(true);
+        if (m_bmCapture) m_bmCapture->hidePreview(true);
         m_frame_preview->setImage(img);
         m_frame_preview->setHidden(false);
         m_frame_preview->update();
@@ -459,7 +571,7 @@ void StopmotionWidget::slotShowFrame(const QString &path)
 
 void StopmotionWidget::slotShowSelectedFrame()
 {
-    QListWidgetItem *item = frame_list->currentItem();
+    QListWidgetItemitem = frame_list->currentItem();
     if (item) {
         //int ix = item->data(Qt::UserRole).toInt();;
         slotShowFrame(item->toolTip());
@@ -473,39 +585,67 @@ void StopmotionWidget::slotAddSequence()
 
 void StopmotionWidget::slotPlayPreview(bool animate)
 {
+    m_animate = animate;
     if (!animate) {
         // stop animation
         m_animationList.clear();
         return;
     }
     if (KdenliveSettings::showstopmotionthumbs()) {
-        frame_list->setCurrentRow(0);
+        if (KdenliveSettings::sm_framesplayback() == 0) frame_list->setCurrentRow(0);
+        else frame_list->setCurrentRow(frame_list->count() - KdenliveSettings::sm_framesplayback());
         QTimer::singleShot(200, this, SLOT(slotAnimate()));
     } else {
         SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
+        if (KdenliveSettings::sm_framesplayback() > 0) {
+            // only play the last x frames
+            while (m_animationList.count() > KdenliveSettings::sm_framesplayback() + 1) {
+                m_animationList.removeFirst();
+            }
+        }
+        m_animatedIndex = 0;
         slotAnimate();
     }
 }
 
 void StopmotionWidget::slotAnimate()
 {
-    //slotShowFrame(m_animatedIndex);
-    if (KdenliveSettings::showstopmotionthumbs()) {
-        //TODO: loop
-        if (frame_list->currentRow() < (frame_list->count() - 1)) {
-            frame_list->setCurrentRow(frame_list->currentRow() + 1);
-            QTimer::singleShot(200, this, SLOT(slotAnimate()));
-        } else preview_button->setChecked(false);
-    } else if (!m_animationList.isEmpty()) {
-        slotShowFrame(m_animationList.takeFirst());
-        QTimer::singleShot(200, this, SLOT(slotAnimate()));
-    } else preview_button->setChecked(false);
+    if (m_animate) {
+        if (KdenliveSettings::showstopmotionthumbs()) {
+            int newRow = frame_list->currentRow() + 1;
+            if (KdenliveSettings::sm_loop() || newRow < frame_list->count()) {
+                if (newRow >= frame_list->count()) {
+                    if (KdenliveSettings::sm_framesplayback() == 0) newRow = 0;
+                    else {
+                        // seek to correct frame
+                        newRow = frame_list->count() - KdenliveSettings::sm_framesplayback();
+                    }
+                }
+                frame_list->setCurrentRow(newRow);
+                QTimer::singleShot(100, this, SLOT(slotAnimate()));
+                return;
+            }
+        } else {
+            if (m_animatedIndex >= m_animationList.count()) {
+                if (KdenliveSettings::sm_loop()) m_animatedIndex = 0;
+                else m_animatedIndex = -1;
+            }
+            if (m_animatedIndex > -1) {
+                slotShowFrame(m_animationList.at(m_animatedIndex));
+                m_animatedIndex++;
+                QTimer::singleShot(100, this, SLOT(slotAnimate()));
+                return;
+            }
+        }
+    }
+    m_animate = false;
+    preview_button->setChecked(false);
 
 }
 
-QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
+QListWidgetItemStopmotionWidget::getFrameFromIndex(int ix)
 {
-    QListWidgetItem *item = NULL;
+    QListWidgetItemitem = NULL;
     int pos = ix;
     if (ix >= frame_list->count()) {
         pos = frame_list->count() - 1;
@@ -539,7 +679,7 @@ QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
 void StopmotionWidget::selectFrame(int ix)
 {
     frame_list->blockSignals(true);
-    QListWidgetItem *item = getFrameFromIndex(ix);
+    QListWidgetItemitem = getFrameFromIndex(ix);
     if (!item) return;
     frame_list->setCurrentItem(item);
     frame_list->blockSignals(false);
@@ -553,4 +693,28 @@ void StopmotionWidget::slotSeekFrame(bool forward)
     } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
 }
 
+void StopmotionWidget::slotRemoveFrame()
+{
+    if (frame_list->currentItem() == NULL) return;
+    QString path = frame_list->currentItem()->toolTip();
+    if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
+    QFile f(path);
+    if (f.remove()) {
+        QListWidgetItem* item = frame_list->takeItem(frame_list->currentRow());
+        int ix = item->data(Qt::UserRole).toInt();
+        if (ix == m_sequenceFrame - 1) {
+            // We are removing the last frame, update counter
+            QListWidgetItem* item2 = frame_list->item(frame_list->count() - 1);
+            if (item2) m_sequenceFrame = item2->data(Qt::UserRole).toInt() + 1;
+        }
+        delete item;
+    }
+}
+
+void StopmotionWidget::slotSwitchAnalyse(bool isOn)
+{
+    KdenliveSettings::setAnalyse_stopmotion(isOn);
+    m_bmCapture->setAnalyse(isOn);
+}
+