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