]> git.sesse.net Git - kdenlive/blob - src/stopmotion/stopmotion.cpp
6d4732778210c34edc2367e54c54b6bda8f44ca6
[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 "../profilesdialog.h"
23 #include "../mltdevicecapture.h"
24 #include "../recmonitor.h"
25 #include "../monitormanager.h"
26 #include "ui_smconfig_ui.h"
27 #include "kdenlivesettings.h"
28
29
30 #include <KDebug>
31 #include <KGlobalSettings>
32 #include <KFileDialog>
33 #include <KStandardDirs>
34 #include <KMessageBox>
35 #include <kdeversion.h>
36 #include <KNotification>
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 StopmotionMonitor::StopmotionMonitor(QWidget *parent) :
94     AbstractMonitor(parent),
95     m_captureDevice(NULL)
96 {
97 }
98
99 StopmotionMonitor::~StopmotionMonitor()
100 {
101 }
102
103 void StopmotionMonitor::setRender(MltDeviceCapture *render)
104 {
105     m_captureDevice = render;
106 }
107
108 AbstractRender *StopmotionMonitor::abstractRender()
109 {
110     return m_captureDevice;
111 }
112
113 const QString StopmotionMonitor::name() const
114 {
115     return QString("stopmotion");
116 }
117
118
119 void StopmotionMonitor::stop()
120 {
121     if (m_captureDevice) m_captureDevice->stop();
122     emit stopCapture();
123 }
124
125 void StopmotionMonitor::start()
126 {
127 }
128
129 StopmotionWidget::StopmotionWidget(MonitorManager *manager, KUrl projectFolder, QList< QAction* > actions, QWidget* parent) :
130     QDialog(parent)
131     , Ui::Stopmotion_UI()
132     , m_projectFolder(projectFolder)
133     , m_captureDevice(NULL)
134     , m_sequenceFrame(0)
135     , m_animatedIndex(-1)
136     , m_animate(false)
137     , m_manager(manager)
138     , m_monitor(new StopmotionMonitor(this))
139 {
140     //setAttribute(Qt::WA_DeleteOnClose);
141     //HACK: the monitor widget is hidden, it is just used to control the capturedevice from monitormanager
142     m_monitor->setHidden(true);
143     connect(m_monitor, SIGNAL(stopCapture()), this, SLOT(slotStopCapture()));
144     m_manager->appendMonitor(m_monitor);
145     QAction* analyse = new QAction(i18n("Send frames to color scopes"), this);
146     analyse->setCheckable(true);
147     analyse->setChecked(KdenliveSettings::analyse_stopmotion());
148     connect(analyse, SIGNAL(triggered(bool)), this, SLOT(slotSwitchAnalyse(bool)));
149
150     QAction* mirror = new QAction(i18n("Mirror display"), this);
151     mirror->setCheckable(true);
152     //mirror->setChecked(KdenliveSettings::analyse_stopmotion());
153     connect(mirror, SIGNAL(triggered(bool)), this, SLOT(slotSwitchMirror(bool)));
154
155     addActions(actions);
156     setupUi(this);
157     setWindowTitle(i18n("Stop Motion Capture"));
158     setFont(KGlobalSettings::toolBarFont());
159
160     live_button->setIcon(KIcon("camera-photo"));
161
162     m_captureAction = actions.at(0);
163     connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
164     m_captureAction->setCheckable(true);
165     m_captureAction->setChecked(false);
166     capture_button->setDefaultAction(m_captureAction);
167
168     connect(actions.at(1), SIGNAL(triggered()), this, SLOT(slotSwitchLive()));
169
170     QAction *intervalCapture = new QAction(i18n("Interval capture"), this);
171     intervalCapture->setIcon(KIcon("chronometer"));
172     intervalCapture->setCheckable(true);
173     intervalCapture->setChecked(false);
174     capture_interval->setDefaultAction(intervalCapture);
175         
176     preview_button->setIcon(KIcon("media-playback-start"));
177     capture_button->setEnabled(false);
178     
179
180     // Build config menu
181     QMenu* confMenu = new QMenu;
182     m_showOverlay = actions.at(2);
183     connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
184     overlay_button->setDefaultAction(m_showOverlay);
185     //confMenu->addAction(m_showOverlay);
186
187     m_effectIndex = KdenliveSettings::stopmotioneffect();    
188     QMenu* effectsMenu = new QMenu(i18n("Overlay effect"));
189     QActionGroup* effectGroup = new QActionGroup(this);
190     QAction* noEffect = new QAction(i18n("No Effect"), effectGroup);
191     noEffect->setData(0);
192     QAction* contrastEffect = new QAction(i18n("Contrast"), effectGroup);
193     contrastEffect->setData(1);
194     QAction* edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
195     edgeEffect->setData(2);
196     QAction* brightEffect = new QAction(i18n("Brighten"), effectGroup);
197     brightEffect->setData(3);
198     QAction* invertEffect = new QAction(i18n("Invert"), effectGroup);
199     invertEffect->setData(4);
200     QAction* thresEffect = new QAction(i18n("Threshold"), effectGroup);
201     thresEffect->setData(5);
202
203     effectsMenu->addAction(noEffect);
204     effectsMenu->addAction(contrastEffect);
205     effectsMenu->addAction(edgeEffect);
206     effectsMenu->addAction(brightEffect);
207     effectsMenu->addAction(invertEffect);
208     effectsMenu->addAction(thresEffect);
209     QList <QAction*> list = effectsMenu->actions();
210     for (int i = 0; i < list.count(); i++) {
211         list.at(i)->setCheckable(true);
212         if (list.at(i)->data().toInt() == m_effectIndex) {
213             list.at(i)->setChecked(true);
214         }
215     }
216     connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
217     confMenu->addMenu(effectsMenu);
218
219     QAction* showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
220     showThumbs->setCheckable(true);
221     showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
222     connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
223
224     QAction* removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
225     removeCurrent->setShortcut(Qt::Key_Delete);
226     connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
227
228     QAction* conf = new QAction(KIcon("configure"), i18n("Configure"), this);
229     connect(conf, SIGNAL(triggered()), this, SLOT(slotConfigure()));
230
231     confMenu->addAction(showThumbs);
232     confMenu->addAction(removeCurrent);
233     confMenu->addAction(analyse);
234     confMenu->addAction(mirror);
235     confMenu->addAction(conf);
236     config_button->setIcon(KIcon("configure"));
237     config_button->setMenu(confMenu);
238
239     connect(sequence_name, SIGNAL(textChanged(const QString&)), this, SLOT(sequenceNameChanged(const QString&)));
240     connect(sequence_name, SIGNAL(currentIndexChanged(int)), live_button, SLOT(setFocus()));
241
242     // Video widget holder
243     QVBoxLayout *layout = new QVBoxLayout;
244     layout->setContentsMargins(0, 0, 0, 0);
245     layout->setSpacing(0);
246     m_videoBox = new VideoPreviewContainer();
247     m_videoBox->setContentsMargins(0, 0, 0, 0);
248     m_videoBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
249     //m_videoBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
250     m_videoBox->setLineWidth(4);
251     layout->addWidget(m_videoBox);
252     
253     
254     if (BMInterface::getBlackMagicDeviceList(capture_device)) {
255         // Found a BlackMagic device
256     }
257     if (QFile::exists(KdenliveSettings::video4vdevice())) {
258 #if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
259         // Video 4 Linux device detection
260         for (int i = 0; i < 10; i++) {
261             QString path = "/dev/video" + QString::number(i);
262             if (QFile::exists(path)) {
263                 QStringList deviceInfo = V4lCaptureHandler::getDeviceName(path);
264                 if (!deviceInfo.isEmpty()) {
265                     capture_device->addItem(deviceInfo.at(0), "v4l");
266                     capture_device->setItemData(capture_device->count() - 1, path, Qt::UserRole + 1);
267                     capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(1), Qt::UserRole + 2);
268                     if (path == KdenliveSettings::video4vdevice()) capture_device->setCurrentIndex(capture_device->count() - 1);
269                 }
270             }
271         }
272 #endif
273     }
274
275     connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDeviceHandler()));
276     /*if (m_bmCapture) {
277         connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
278         connect(m_bmCapture, SIGNAL(gotFrame(QImage)), this, SIGNAL(gotFrame(QImage)));
279     } else live_button->setEnabled(false);*/
280     
281     m_frame_preview = new MyLabel(this);
282     connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
283     connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
284     layout->addWidget(m_frame_preview);
285     m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
286     video_preview->setLayout(layout);
287
288     //kDebug()<<video_preview->winId();
289
290     QString profilePath;
291     // Create MLT producer data
292     if (capture_device->itemData(capture_device->currentIndex()) == "v4l") {
293         // Capture using a video4linux device
294         profilePath = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
295     }
296     else {
297         // Decklink capture
298         profilePath = KdenliveSettings::current_profile();
299     }
300     
301     m_captureDevice = new MltDeviceCapture(profilePath, m_videoBox, this);
302     m_captureDevice->sendFrameForAnalysis = KdenliveSettings::analyse_stopmotion();
303     m_monitor->setRender(m_captureDevice);
304     connect(m_captureDevice, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
305     
306     live_button->setChecked(false);
307     button_addsequence->setEnabled(false);
308     connect(live_button, SIGNAL(toggled(bool)), this, SLOT(slotLive(bool)));
309     connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
310     connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
311     connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
312     connect(frame_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotShowSelectedFrame()));
313     connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
314
315     frame_list->addAction(removeCurrent);
316     frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
317     frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
318     parseExistingSequences();
319     QTimer::singleShot(500, this, SLOT(slotLive()));
320     connect(&m_intervalTimer, SIGNAL(timeout()), this, SLOT(slotCaptureFrame()));
321     m_intervalTimer.setSingleShot(true);
322     m_intervalTimer.setInterval(KdenliveSettings::captureinterval() * 1000);
323 }
324
325 StopmotionWidget::~StopmotionWidget()
326 {
327     /*if (m_bmCapture)
328         m_bmCapture->stopPreview();*/
329     if (m_captureDevice) {
330         m_captureDevice->stop();
331         delete m_captureDevice;
332         m_captureDevice = NULL;
333     }
334
335     delete m_monitor;
336 }
337
338 void StopmotionWidget::slotUpdateOverlayEffect(QAction* act)
339 {
340     if (act) m_effectIndex = act->data().toInt();
341     KdenliveSettings::setStopmotioneffect(m_effectIndex);
342     slotUpdateOverlay();
343 }
344
345 void StopmotionWidget::closeEvent(QCloseEvent* e)
346 {
347     slotLive(false);
348     QDialog::closeEvent(e);
349 }
350
351 void StopmotionWidget::slotConfigure()
352 {
353     QDialog d;
354     Ui::SmConfig_UI ui;
355     ui.setupUi(&d);
356     d.setWindowTitle(i18n("Configure Stop Motion"));
357     ui.sm_interval->setValue(KdenliveSettings::captureinterval());
358     ui.sm_interval->setSuffix(ki18np(" second", " seconds"));
359     ui.sm_notifytime->setSuffix(ki18np(" second", " seconds"));
360     ui.sm_notifytime->setValue(KdenliveSettings::sm_notifytime());
361     connect(ui.sm_prenotify, SIGNAL(toggled(bool)), ui.sm_notifytime, SLOT(setEnabled(bool)));
362     ui.sm_prenotify->setChecked(KdenliveSettings::sm_prenotify());
363     ui.sm_loop->setChecked(KdenliveSettings::sm_loop());
364     ui.sm_framesplayback->setValue(KdenliveSettings::sm_framesplayback());
365     
366     if (d.exec() == QDialog::Accepted) {
367         KdenliveSettings::setSm_loop(ui.sm_loop->isChecked());
368         KdenliveSettings::setCaptureinterval(ui.sm_interval->value());
369         KdenliveSettings::setSm_framesplayback(ui.sm_framesplayback->value());
370         KdenliveSettings::setSm_notifytime(ui.sm_notifytime->value());
371         KdenliveSettings::setSm_prenotify(ui.sm_prenotify->isChecked());
372         m_intervalTimer.setInterval(KdenliveSettings::captureinterval() * 1000);
373     }
374 }
375
376 void StopmotionWidget::slotShowThumbs(bool show)
377 {
378     KdenliveSettings::setShowstopmotionthumbs(show);
379     if (show) {
380         frame_list->clear();
381         sequenceNameChanged(sequence_name->currentText());
382     } else {
383         m_filesList.clear();
384         frame_list->clear();
385     }
386     frame_list->setHidden(!show);
387 }
388
389
390 void StopmotionWidget::slotUpdateDeviceHandler()
391 {
392     slotLive(false);
393     delete m_captureDevice;
394     m_captureDevice = NULL;
395     /*QString data = capture_device->itemData(capture_device->currentIndex()).toString();
396     slotLive(false);
397     if (m_bmCapture) {
398         delete m_bmCapture;
399     }
400     m_layout->removeWidget(m_frame_preview);
401     if (data == "v4l") {
402 #if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
403         m_bmCapture = new V4lCaptureHandler(m_layout);
404         m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
405 #endif
406     } else {
407         m_bmCapture = new BmdCaptureHandler(m_layout);
408         if (m_bmCapture) connect(m_bmCapture, SIGNAL(gotMessage(const QString&)), this, SLOT(slotGotHDMIMessage(const QString&)));
409     }
410     live_button->setEnabled(m_bmCapture != NULL);
411     m_layout->addWidget(m_frame_preview);*/
412 }
413
414 void StopmotionWidget::slotGotHDMIMessage(const QString& message)
415 {
416     log_box->insertItem(0, message);
417 }
418
419 void StopmotionWidget::parseExistingSequences()
420 {
421     sequence_name->clear();
422     sequence_name->addItem(QString());
423     QDir dir(m_projectFolder.path());
424     QStringList filters;
425     filters << "*_0000.png";
426     //dir.setNameFilters(filters);
427     QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
428     //kDebug()<<"PF: "<<<<", sm: "<<sequences;
429     foreach(QString sequencename, sequences) {
430         sequence_name->addItem(sequencename.section("_", 0, -2));
431     }
432 }
433
434 void StopmotionWidget::slotSwitchLive()
435 {
436     setUpdatesEnabled(false);
437     slotLive(!live_button->isChecked());
438     /*if (m_frame_preview->isHidden()) {
439         //if (m_bmCapture) m_bmCapture->hidePreview(true);
440         m_videoBox->setHidden(true);
441         m_frame_preview->setHidden(false);
442     } else {
443         m_frame_preview->setHidden(true);
444         //if (m_bmCapture) m_bmCapture->hidePreview(false);
445         m_videoBox->setHidden(false);
446         capture_button->setEnabled(true);
447     }*/
448     setUpdatesEnabled(true);
449 }
450
451 void StopmotionWidget::slotStopCapture()
452 {
453     slotLive(false);
454 }
455
456 void StopmotionWidget::slotLive(bool isOn)
457 {
458     live_button->blockSignals(true);
459     capture_button->setEnabled(false);
460     if (isOn) {
461         m_frame_preview->setHidden(true);
462         m_videoBox->setHidden(false);
463
464         MltVideoProfile profile;
465         QString resource;
466         QString service;
467         QString profilePath;
468         // Create MLT producer data
469         if (capture_device->itemData(capture_device->currentIndex()) == "v4l") {
470             // Capture using a video4linux device
471             profilePath = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
472             profile = ProfilesDialog::getVideoProfile(profilePath);
473             service = "avformat-novalidate";
474             QString devicePath = capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString();
475             resource = QString("video4linux2:%1?width:%2&amp;height:%3&amp;frame_rate:%4").arg(devicePath).arg(profile.width).arg(profile.height).arg((double) profile.frame_rate_num / profile.frame_rate_den);
476         }
477         else {
478             // Decklink capture
479             profilePath = KdenliveSettings::current_profile();
480             profile = ProfilesDialog::getVideoProfile(profilePath);
481             service = "decklink";
482             resource = capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString();
483         }
484
485         if (m_captureDevice == NULL) {
486             m_captureDevice = new MltDeviceCapture(profilePath, m_videoBox, this);
487             m_captureDevice->sendFrameForAnalysis = KdenliveSettings::analyse_stopmotion();
488             m_monitor->setRender(m_captureDevice);
489             connect(m_captureDevice, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
490         }
491         
492         m_manager->activateMonitor("stopmotion");
493         QString producer = createProducer(profile, service, resource);
494         if (m_captureDevice->slotStartPreview(producer, true)) {
495             if (m_showOverlay->isChecked()) {
496                 reloadOverlay();
497                 slotUpdateOverlay();
498             }
499             capture_button->setEnabled(true);
500             live_button->setChecked(true);
501             log_box->insertItem(-1, i18n("Playing %1x%2 (%3 fps)", profile.width, profile.height, QString::number((double)profile.frame_rate_num/profile.frame_rate_den).rightJustified(2, '0')));
502             log_box->setCurrentIndex(0);
503         }
504         else {
505             kDebug()<<"// problem starting stopmo";
506             log_box->insertItem(-1, i18n("Failed to start device"));
507             log_box->setCurrentIndex(0);
508         }
509     }
510     else {
511         m_frame_preview->setHidden(false);
512         live_button->setChecked(false);
513         if (m_captureDevice) {
514             m_captureDevice->stop();
515             m_videoBox->setHidden(true);
516             log_box->insertItem(-1, i18n("Stopped"));
517             log_box->setCurrentIndex(0);
518             //delete m_captureDevice;
519             //m_captureDevice = NULL;
520         }
521     }
522             
523     /*
524     if (isOn && m_bmCapture) {
525         //m_frame_preview->setImage(QImage());
526         m_frame_preview->setHidden(true);
527         m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
528         capture_button->setEnabled(true);
529         live_button->setChecked(true);
530     } else {
531         if (m_bmCapture) m_bmCapture->stopPreview();
532         m_frame_preview->setHidden(false);
533         capture_button->setEnabled(false);
534         live_button->setChecked(false);
535     }*/
536     live_button->blockSignals(false);
537 }
538
539 void StopmotionWidget::slotShowOverlay(bool isOn)
540 {
541     if (isOn) {
542         // Overlay last frame of the sequence
543         reloadOverlay();
544         slotUpdateOverlay();
545     }
546     else {
547         // Remove overlay
548         m_captureDevice->setOverlay(QString());
549     }
550 }
551
552 void StopmotionWidget::reloadOverlay()
553 {
554     QString path = getPathForFrame(m_sequenceFrame - 1);
555     if (!QFile::exists(path)) {
556         log_box->insertItem(-1, i18n("No previous frame found"));
557         log_box->setCurrentIndex(0);
558         return;
559     }
560     m_captureDevice->setOverlay(path);
561 }
562
563 void StopmotionWidget::slotUpdateOverlay()
564 {
565     if (m_captureDevice == NULL) return;
566
567     QString tag;
568     QStringList params;
569
570     switch (m_effectIndex) {
571     case 1:
572         tag = "frei0r.contrast0r";
573         params << "Contrast=1.2";
574         break;
575     case 2:
576         tag = "charcoal";
577         params << "x_scatter=4" << "y_scatter=4" << "scale=1" << "mix=0";
578         break;
579     case 3:
580         tag = "frei0r.brightness";
581         params << "Brightness=0.7";
582         break;
583     case 4:
584         tag = "invert";
585         break;
586     case 5:
587         tag = "threshold";
588         params << "midpoint=125";
589         break;
590     default:
591         break;
592
593     }
594     m_captureDevice->setOverlayEffect(tag, params);
595 }
596
597 void StopmotionWidget::sequenceNameChanged(const QString& name)
598 {
599     // Get rid of frames from previous sequence
600     disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
601     m_filesList.clear();
602     m_future.waitForFinished();
603     frame_list->clear();
604     if (name.isEmpty()) {
605         button_addsequence->setEnabled(false);
606     } else {
607         // Check if we are editing an existing sequence
608         QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
609         m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
610         if (!m_filesList.isEmpty()) {
611             m_sequenceName = sequence_name->currentText();
612             connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
613             m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
614             button_addsequence->setEnabled(true);
615         } else {
616             // new sequence
617             connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
618             button_addsequence->setEnabled(false);
619         }
620         capture_button->setEnabled(live_button->isChecked());
621     }
622 }
623
624 void StopmotionWidget::slotCaptureFrame()
625 {
626     if (m_captureDevice == NULL) return;
627     if (sequence_name->currentText().isEmpty()) {
628         QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
629         if (seqName.isEmpty()) {
630             m_captureAction->setChecked(false);
631             return;
632         }
633         sequence_name->blockSignals(true);
634         sequence_name->setItemText(sequence_name->currentIndex(), seqName);
635         sequence_name->blockSignals(false);
636     }
637     if (m_sequenceName != sequence_name->currentText()) {
638         m_sequenceName = sequence_name->currentText();
639         m_sequenceFrame = 0;
640     }
641     //capture_button->setEnabled(false);
642     if (m_intervalTimer.isActive()) {
643         // stop interval capture
644         m_intervalTimer.stop();
645         return;
646     }
647     QString currentPath = getPathForFrame(m_sequenceFrame);
648     m_captureDevice->captureFrame(currentPath);
649     KNotification::event("FrameCaptured", i18n("Frame Captured"), QPixmap(), this);
650     m_sequenceFrame++;
651     button_addsequence->setEnabled(true);
652     if (capture_interval->isChecked()) {
653         if (KdenliveSettings::sm_prenotify()) QTimer::singleShot((KdenliveSettings::captureinterval() - KdenliveSettings::sm_notifytime()) * 1000, this, SLOT(slotPreNotify()));
654         m_intervalTimer.start();
655     }
656     else m_captureAction->setChecked(false);
657 }
658
659 void StopmotionWidget::slotPreNotify()
660 {
661     if (m_captureAction->isChecked()) KNotification::event("ReadyToCapture", i18n("Going to Capture Frame"), QPixmap(), this);
662 }
663
664
665 void StopmotionWidget::slotNewThumb(const QString path)
666 {
667     if (!KdenliveSettings::showstopmotionthumbs()) return;
668     m_filesList.append(path);
669     if (m_showOverlay->isChecked()) reloadOverlay();
670     if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
671     
672 }
673
674 void StopmotionWidget::slotPrepareThumbs()
675 {
676     if (m_filesList.isEmpty()) return;
677     QString path = m_filesList.takeFirst();
678     emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
679
680 }
681
682 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
683 {
684     if (img.isNull()) {
685         m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
686         return;
687     }
688     int height = 90;
689     int width = height * img.width() / img.height();
690     frame_list->setIconSize(QSize(width, height));
691     QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
692     QString nb = QString::number(ix);
693     QPainter p(&pix);
694     QFontInfo finfo(font());
695     p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
696     p.setPen(Qt::white);
697     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
698     p.end();
699     QIcon icon(pix);
700     QListWidgetItem* item = new QListWidgetItem(icon, QString(), frame_list);
701     item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
702     item->setData(Qt::UserRole, ix);
703     frame_list->blockSignals(true);
704     frame_list->setCurrentItem(item);
705     frame_list->blockSignals(false);
706     m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
707 }
708
709 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
710 {
711     if (seqName.isEmpty()) seqName = m_sequenceName;
712     return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
713 }
714
715 void StopmotionWidget::slotShowFrame(const QString& path)
716 {
717     //slotLive(false);
718     QImage img(path);
719     capture_button->setEnabled(false);
720     slotLive(false);
721     if (!img.isNull()) {
722         //m_videoBox->setHidden(true);
723         
724         m_frame_preview->setImage(img);
725         m_frame_preview->setHidden(false);
726         m_frame_preview->update();
727     }
728 }
729
730 void StopmotionWidget::slotShowSelectedFrame()
731 {
732     QListWidgetItem* item = frame_list->currentItem();
733     if (item) {
734         //int ix = item->data(Qt::UserRole).toInt();;
735         slotShowFrame(item->toolTip());
736     }
737 }
738
739 void StopmotionWidget::slotAddSequence()
740 {
741     emit addOrUpdateSequence(getPathForFrame(0));
742 }
743
744 void StopmotionWidget::slotPlayPreview(bool animate)
745 {
746     m_animate = animate;
747     if (!animate) {
748         // stop animation
749         m_animationList.clear();
750         return;
751     }
752     if (KdenliveSettings::showstopmotionthumbs()) {
753         if (KdenliveSettings::sm_framesplayback() == 0) frame_list->setCurrentRow(0);
754         else frame_list->setCurrentRow(frame_list->count() - KdenliveSettings::sm_framesplayback());
755         QTimer::singleShot(200, this, SLOT(slotAnimate()));
756     } else {
757         SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
758         if (KdenliveSettings::sm_framesplayback() > 0) {
759             // only play the last x frames
760             while (m_animationList.count() > KdenliveSettings::sm_framesplayback() + 1) {
761                 m_animationList.removeFirst();
762             }
763         }
764         m_animatedIndex = 0;
765         slotAnimate();
766     }
767 }
768
769 void StopmotionWidget::slotAnimate()
770 {
771     if (m_animate) {
772         if (KdenliveSettings::showstopmotionthumbs()) {
773             int newRow = frame_list->currentRow() + 1;
774             if (KdenliveSettings::sm_loop() || newRow < frame_list->count()) {
775                 if (newRow >= frame_list->count()) {
776                     if (KdenliveSettings::sm_framesplayback() == 0) newRow = 0;
777                     else {
778                         // seek to correct frame
779                         newRow = frame_list->count() - KdenliveSettings::sm_framesplayback();
780                     }
781                 }
782                 frame_list->setCurrentRow(newRow);
783                 QTimer::singleShot(100, this, SLOT(slotAnimate()));
784                 return;
785             }
786         } else {
787             if (m_animatedIndex >= m_animationList.count()) {
788                 if (KdenliveSettings::sm_loop()) m_animatedIndex = 0;
789                 else m_animatedIndex = -1;
790             }
791             if (m_animatedIndex > -1) {
792                 slotShowFrame(m_animationList.at(m_animatedIndex));
793                 m_animatedIndex++;
794                 QTimer::singleShot(100, this, SLOT(slotAnimate()));
795                 return;
796             }
797         }
798     }
799     m_animate = false;
800     preview_button->setChecked(false);
801
802 }
803
804 QListWidgetItem* StopmotionWidget::getFrameFromIndex(int ix)
805 {
806     QListWidgetItem* item = NULL;
807     int pos = ix;
808     if (ix >= frame_list->count()) {
809         pos = frame_list->count() - 1;
810     }
811     if (ix < 0) pos = 0;
812     item = frame_list->item(pos);
813
814     int value = item->data(Qt::UserRole).toInt();
815     if (value == ix) return item;
816     else if (value < ix) {
817         pos++;
818         while (pos < frame_list->count()) {
819             item = frame_list->item(pos);
820             value = item->data(Qt::UserRole).toInt();
821             if (value == ix) return item;
822             pos++;
823         }
824     } else {
825         pos --;
826         while (pos >= 0) {
827             item = frame_list->item(pos);
828             value = item->data(Qt::UserRole).toInt();
829             if (value == ix) return item;
830             pos --;
831         }
832     }
833     return NULL;
834 }
835
836
837 void StopmotionWidget::selectFrame(int ix)
838 {
839     frame_list->blockSignals(true);
840     QListWidgetItem* item = getFrameFromIndex(ix);
841     if (!item) return;
842     frame_list->setCurrentItem(item);
843     frame_list->blockSignals(false);
844 }
845
846 void StopmotionWidget::slotSeekFrame(bool forward)
847 {
848     int ix = frame_list->currentRow();
849     if (forward) {
850         if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
851     } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
852 }
853
854 void StopmotionWidget::slotRemoveFrame()
855 {
856     if (frame_list->currentItem() == NULL) return;
857     QString path = frame_list->currentItem()->toolTip();
858     if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
859     QFile f(path);
860     if (f.remove()) {
861         QListWidgetItem* item = frame_list->takeItem(frame_list->currentRow());
862         int ix = item->data(Qt::UserRole).toInt();
863         if (ix == m_sequenceFrame - 1) {
864             // We are removing the last frame, update counter
865             QListWidgetItem* item2 = frame_list->item(frame_list->count() - 1);
866             if (item2) m_sequenceFrame = item2->data(Qt::UserRole).toInt() + 1;
867         }
868         delete item;
869     }
870 }
871
872 void StopmotionWidget::slotSwitchAnalyse(bool isOn)
873 {
874     KdenliveSettings::setAnalyse_stopmotion(isOn);
875     if (m_captureDevice) m_captureDevice->sendFrameForAnalysis = isOn;
876 }
877
878 void StopmotionWidget::slotSwitchMirror(bool isOn)
879 {
880     //KdenliveSettings::setAnalyse_stopmotion(isOn);
881     if (m_captureDevice) m_captureDevice->mirror(isOn);
882 }
883
884 const QString StopmotionWidget::createProducer(MltVideoProfile profile, const QString service, const QString resource)
885 {   
886     
887     QString playlist = "<mlt title=\"capture\"><producer id=\"producer0\" in=\"0\" out=\"99999\"><property name=\"mlt_type\">producer</property><property name=\"length\">100000</property><property name=\"eof\">pause</property><property name=\"resource\">" + resource + "</property><property name=\"mlt_service\">" + service + "</property></producer>";
888
889     // overlay track
890     playlist.append("<playlist id=\"playlist0\"></playlist>");
891
892     // video4linux track
893     playlist.append("<playlist id=\"playlist1\"><entry producer=\"producer0\" in=\"0\" out=\"99999\"/></playlist>");
894
895     playlist.append("<tractor id=\"tractor0\" title=\"video0\" global_feed=\"1\" in=\"0\" out=\"99999\">");
896     playlist.append("<track producer=\"playlist0\"/>");
897     playlist.append("<track producer=\"playlist1\"/>");
898     playlist.append("</tractor></mlt>");
899
900     
901     return playlist;
902 }
903
904
905