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