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