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