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