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