]> git.sesse.net Git - kdenlive/blob - src/stopmotion/stopmotion.cpp
webcam capture: Try to get webcam name instead of displaying /dev/video0
[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 "kdenlivesettings.h"
23
24
25 #include <KDebug>
26 #include <KGlobalSettings>
27 #include <KFileDialog>
28 #include <KStandardDirs>
29 #include <KMessageBox>
30 #include <kdeversion.h>
31 #include <KNotification>
32
33 #ifdef QIMAGEBLITZ
34 #include <qimageblitz/qimageblitz.h>
35 #endif
36
37 #include <QtConcurrentRun>
38 #include <QInputDialog>
39 #include <QComboBox>
40 #include <QVBoxLayout>
41 #include <QTimer>
42 #include <QPainter>
43 #include <QAction>
44 #include <QWheelEvent>
45 #include <QMenu>
46
47 MyLabel::MyLabel(QWidget *parent) :
48     QLabel(parent)
49 {
50 }
51
52 void MyLabel::setImage(QImage img)
53 {
54     m_img = img;
55 }
56
57 //virtual
58 void MyLabel::wheelEvent(QWheelEvent * event)
59 {
60     if (event->delta() > 0) emit seek(true);
61     else emit seek(false);
62 }
63
64 //virtual
65 void MyLabel::paintEvent(QPaintEvent * event)
66 {
67     Q_UNUSED(event);
68
69     QRect r(0, 0, width(), height());
70     QPainter p(this);
71     p.fillRect(r, QColor(KdenliveSettings::window_background()));
72     double aspect_ratio = (double) m_img.width() / m_img.height();
73     int pictureHeight = height();
74     int pictureWidth = width();
75     int calculatedWidth = aspect_ratio * height();
76     if (calculatedWidth > width()) pictureHeight = width() / aspect_ratio;
77     else {
78         int calculatedHeight = width() / aspect_ratio;
79         if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
80     }
81     p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
82     p.end();
83 }
84
85
86 StopmotionWidget::StopmotionWidget(KUrl projectFolder, QWidget *parent) :
87     QDialog(parent)
88     , Ui::Stopmotion_UI()
89     , m_projectFolder(projectFolder)
90     , m_bmCapture(NULL)
91     , m_sequenceFrame(0)
92     , m_animatedIndex(-1)
93 {
94     setupUi(this);
95     setWindowTitle(i18n("Stop Motion Capture"));
96     setFont(KGlobalSettings::toolBarFont());
97
98     live_button->setIcon(KIcon("camera-photo"));
99     m_captureAction = new QAction(KIcon("media-record"), i18n("Capture frame"), this);
100     m_captureAction->setShortcut(QKeySequence(Qt::Key_Space));
101     connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
102     capture_button->setDefaultAction(m_captureAction);
103
104     preview_button->setIcon(KIcon("media-playback-start"));
105     capture_button->setEnabled(false);
106
107     // Build config menu
108     QMenu *confMenu = new QMenu;
109     m_showOverlay = new QAction(KIcon("edit-paste"), i18n("Show last frame over video"), this);
110     m_showOverlay->setCheckable(true);
111     m_showOverlay->setChecked(false);
112     connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
113     confMenu->addAction(m_showOverlay);
114
115 #ifdef QIMAGEBLITZ
116     m_effectIndex = KdenliveSettings::blitzeffect();
117     QMenu *effectsMenu = new QMenu(i18n("Overlay effect"));
118     QActionGroup *effectGroup = new QActionGroup(this);
119     QAction *noEffect = new QAction(i18n("No Effect"), effectGroup);
120     noEffect->setData(1);
121     QAction *contrastEffect = new QAction(i18n("Contrast"), effectGroup);
122     contrastEffect->setData(2);
123     QAction *edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
124     edgeEffect->setData(3);
125     QAction *brightEffect = new QAction(i18n("Brighten"), effectGroup);
126     brightEffect->setData(4);
127     QAction *invertEffect = new QAction(i18n("Invert"), effectGroup);
128     invertEffect->setData(5);
129     QAction *thresEffect = new QAction(i18n("Threshold"), effectGroup);
130     thresEffect->setData(6);
131
132     effectsMenu->addAction(noEffect);
133     effectsMenu->addAction(contrastEffect);
134     effectsMenu->addAction(edgeEffect);
135     effectsMenu->addAction(brightEffect);
136     effectsMenu->addAction(invertEffect);
137     effectsMenu->addAction(thresEffect);
138     QList <QAction *> list = effectsMenu->actions();
139     for (int i = 0; i < list.count(); i++) {
140         list.at(i)->setCheckable(true);
141         if (list.at(i)->data().toInt() == m_effectIndex) {
142             list.at(i)->setChecked(true);
143         }
144     }
145     connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
146     confMenu->addMenu(effectsMenu);
147 #endif
148
149     QAction *showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
150     showThumbs->setCheckable(true);
151     showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
152     connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
153
154     QAction *removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
155     //TODO: implement frame deletion
156     //connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
157
158     QAction *capInterval = new QAction(KIcon(), i18n("Set capture interval"), this);
159     connect(capInterval, SIGNAL(triggered()), this, SLOT(slotSetCaptureInterval()));
160
161     confMenu->addAction(showThumbs);
162     confMenu->addAction(capInterval);
163     confMenu->addAction(removeCurrent);
164     config_button->setIcon(KIcon("configure"));
165     config_button->setMenu(confMenu);
166
167     // Build capture menu
168     QMenu *capMenu = new QMenu;
169     m_intervalCapture = new QAction(KIcon("edit-redo"), i18n("Interval capture"), this);
170     m_intervalCapture->setCheckable(true);
171     m_intervalCapture->setChecked(false);
172     connect(m_intervalCapture, SIGNAL(triggered(bool)), this, SLOT(slotIntervalCapture(bool)));
173     capMenu->addAction(m_intervalCapture);
174     capture_button->setMenu(capMenu);
175
176     connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
177     m_layout = new QVBoxLayout;
178     if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
179         // Found a BlackMagic device
180         m_bmCapture = new BmdCaptureHandler(m_layout);
181         connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
182     }
183     if (QFile::exists(KdenliveSettings::video4vdevice())) {
184         if (m_bmCapture == NULL) m_bmCapture = new V4lCaptureHandler(m_layout);
185         capture_device->addItem(m_bmCapture->getDeviceName(KdenliveSettings::video4vdevice()), "v4l");
186     }
187
188     connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
189     connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
190     m_frame_preview = new MyLabel(this);
191     connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
192     m_layout->addWidget(m_frame_preview);
193     m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
194     video_preview->setLayout(m_layout);
195     live_button->setChecked(false);
196     button_addsequence->setEnabled(false);
197     connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
198     connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
199     connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
200     connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
201     connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
202
203     frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
204     parseExistingSequences();
205 }
206
207 StopmotionWidget::~StopmotionWidget()
208 {
209     m_bmCapture->stopPreview();
210 }
211
212 void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
213 {
214 #ifdef QIMAGEBLITZ
215     if (act) m_effectIndex = act->data().toInt();
216     KdenliveSettings::setBlitzeffect(m_effectIndex);
217     if (m_showOverlay->isChecked()) slotUpdateOverlay();
218 #endif
219 }
220
221 void StopmotionWidget::closeEvent(QCloseEvent *e)
222 {
223     slotLive(false);
224     QDialog::closeEvent(e);
225 }
226
227 void StopmotionWidget::slotSetCaptureInterval()
228 {
229     int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
230     if (interval > 0 && interval != KdenliveSettings::captureinterval())
231         KdenliveSettings::setCaptureinterval(interval);
232 }
233
234 void StopmotionWidget::slotShowThumbs(bool show)
235 {
236     KdenliveSettings::setShowstopmotionthumbs(show);
237     if (show) {
238         frame_list->clear();
239         sequenceNameChanged(sequence_name->currentText());
240     } else {
241         m_filesList.clear();
242         frame_list->clear();
243     }
244     frame_list->setHidden(!show);
245 }
246
247 void StopmotionWidget::slotIntervalCapture(bool capture)
248 {
249     if (capture) slotCaptureFrame();
250 }
251
252
253 void StopmotionWidget::slotUpdateHandler()
254 {
255     QString data = capture_device->itemData(capture_device->currentIndex()).toString();
256     m_bmCapture->stopPreview();
257     delete m_bmCapture;
258     m_layout->removeWidget(m_frame_preview);
259     if (data == "v4l") {
260         m_bmCapture = new V4lCaptureHandler(m_layout);
261     } else {
262         m_bmCapture = new BmdCaptureHandler(m_layout);
263         connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
264     }
265     m_layout->addWidget(m_frame_preview);
266 }
267
268 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
269 {
270     log_box->insertItem(0, message);
271 }
272
273 void StopmotionWidget::parseExistingSequences()
274 {
275     sequence_name->clear();
276     sequence_name->addItem(QString());
277     QDir dir(m_projectFolder.path());
278     QStringList filters;
279     filters << "*_0000.png";
280     //dir.setNameFilters(filters);
281     QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
282     //kDebug()<<"PF: "<<<<", sm: "<<sequences;
283     foreach(QString sequencename, sequences) {
284         sequence_name->addItem(sequencename.section("_", 0, -2));
285     }
286 }
287
288 void StopmotionWidget::slotLive(bool isOn)
289 {
290     if (isOn) {
291         m_frame_preview->setImage(QImage());
292         m_frame_preview->setHidden(true);
293         m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode());
294         capture_button->setEnabled(true);
295     } else {
296         m_bmCapture->stopPreview();
297         capture_button->setEnabled(false);
298         live_button->setChecked(false);
299     }
300 }
301
302 void StopmotionWidget::slotShowOverlay(bool isOn)
303 {
304     if (isOn) {
305         if (live_button->isChecked() && m_sequenceFrame > 0) {
306             slotUpdateOverlay();
307         }
308     } else {
309         m_bmCapture->hideOverlay();
310     }
311 }
312
313 void StopmotionWidget::slotUpdateOverlay()
314 {
315     QString path = getPathForFrame(m_sequenceFrame - 1);
316     if (!QFile::exists(path)) return;
317     QImage img(path);
318     if (img.isNull()) {
319         QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
320         return;
321     }
322
323 #ifdef QIMAGEBLITZ
324     //img = Blitz::convolveEdge(img, 0, Blitz::Low);
325     switch (m_effectIndex) {
326     case 2:
327         img = Blitz::contrast(img, true, 6);
328         break;
329     case 3:
330         img = Blitz::edge(img);
331         break;
332     case 4:
333         img = Blitz::intensity(img, 0.5);
334         break;
335     case 5:
336         Blitz::invert(img);
337         break;
338     case 6:
339         img = Blitz::threshold(img, 120, Blitz::Grayscale, qRgba(0, 0, 0, 0), qRgba(255, 0, 0, 255));
340         //img = Blitz::flatten(img, QColor(255, 0, 0, 255), QColor(0, 0, 0, 0));
341         break;
342     default:
343         break;
344
345     }
346 #endif
347     m_bmCapture->showOverlay(img);
348 }
349
350 void StopmotionWidget::sequenceNameChanged(const QString &name)
351 {
352     // Get rid of frames from previous sequence
353     disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
354     m_filesList.clear();
355     m_future.waitForFinished();
356     frame_list->clear();
357     if (name.isEmpty()) {
358         button_addsequence->setEnabled(false);
359     } else {
360         // Check if we are editing an existing sequence
361         QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
362         m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
363         if (!m_filesList.isEmpty()) {
364             m_sequenceName = sequence_name->currentText();
365             connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
366             m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
367             button_addsequence->setEnabled(true);
368         } else {
369             // new sequence
370             connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
371             button_addsequence->setEnabled(false);
372         }
373         capture_button->setEnabled(live_button->isChecked());
374     }
375 }
376
377 void StopmotionWidget::slotCaptureFrame()
378 {
379     if (sequence_name->currentText().isEmpty()) {
380         QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
381         if (seqName.isEmpty()) return;
382         sequence_name->blockSignals(true);
383         sequence_name->setItemText(sequence_name->currentIndex(), seqName);
384         sequence_name->blockSignals(false);
385     }
386     if (m_sequenceName != sequence_name->currentText()) {
387         m_sequenceName = sequence_name->currentText();
388         m_sequenceFrame = 0;
389     }
390     //capture_button->setEnabled(false);
391     QString currentPath = getPathForFrame(m_sequenceFrame);
392     kDebug() << "Capture FRame NB: " << m_sequenceFrame;
393     m_bmCapture->captureFrame(currentPath);
394     KNotification::event("FrameCaptured");
395     m_sequenceFrame++;
396     button_addsequence->setEnabled(true);
397     if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
398 }
399
400
401 void StopmotionWidget::slotNewThumb(const QString path)
402 {
403     if (!KdenliveSettings::showstopmotionthumbs()) return;
404     m_filesList.append(path);
405     if (m_showOverlay->isChecked()) slotUpdateOverlay();
406     if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
407 }
408
409 void StopmotionWidget::slotPrepareThumbs()
410 {
411     if (m_filesList.isEmpty()) return;
412     QString path = m_filesList.takeFirst();
413     emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
414
415 }
416
417 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
418 {
419     if (img.isNull()) {
420         m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
421         return;
422     }
423     int height = 90;
424     int width = height * img.width() / img.height();
425     frame_list->setIconSize(QSize(width, height));
426     QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
427     QString nb = QString::number(ix);
428     QPainter p(&pix);
429     QFontInfo finfo(font());
430     p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
431     p.setPen(Qt::white);
432     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
433     p.end();
434     QIcon icon(pix);
435     QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
436     item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
437     item->setData(Qt::UserRole, ix);
438     m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
439 }
440
441 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
442 {
443     if (seqName.isEmpty()) seqName = m_sequenceName;
444     return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
445 }
446
447 void StopmotionWidget::slotShowFrame(const QString &path)
448 {
449     slotLive(false);
450     QImage img(path);
451     capture_button->setEnabled(false);
452     if (!img.isNull()) {
453         m_bmCapture->hidePreview(true);
454         m_frame_preview->setImage(img);
455         m_frame_preview->setHidden(false);
456         m_frame_preview->update();
457     }
458 }
459
460 void StopmotionWidget::slotShowSelectedFrame()
461 {
462     QListWidgetItem *item = frame_list->currentItem();
463     if (item) {
464         //int ix = item->data(Qt::UserRole).toInt();;
465         slotShowFrame(item->toolTip());
466     }
467 }
468
469 void StopmotionWidget::slotAddSequence()
470 {
471     emit addOrUpdateSequence(getPathForFrame(0));
472 }
473
474 void StopmotionWidget::slotPlayPreview(bool animate)
475 {
476     if (!animate) {
477         // stop animation
478         m_animationList.clear();
479         return;
480     }
481     if (KdenliveSettings::showstopmotionthumbs()) {
482         frame_list->setCurrentRow(0);
483         QTimer::singleShot(200, this, SLOT(slotAnimate()));
484     } else {
485         SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
486         slotAnimate();
487     }
488 }
489
490 void StopmotionWidget::slotAnimate()
491 {
492     //slotShowFrame(m_animatedIndex);
493     if (KdenliveSettings::showstopmotionthumbs()) {
494         //TODO: loop
495         if (frame_list->currentRow() < (frame_list->count() - 1)) {
496             frame_list->setCurrentRow(frame_list->currentRow() + 1);
497             QTimer::singleShot(200, this, SLOT(slotAnimate()));
498         } else preview_button->setChecked(false);
499     } else if (!m_animationList.isEmpty()) {
500         slotShowFrame(m_animationList.takeFirst());
501         QTimer::singleShot(200, this, SLOT(slotAnimate()));
502     } else preview_button->setChecked(false);
503
504 }
505
506 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
507 {
508     QListWidgetItem *item = NULL;
509     int pos = ix;
510     if (ix >= frame_list->count()) {
511         pos = frame_list->count() - 1;
512     }
513     if (ix < 0) pos = 0;
514     item = frame_list->item(pos);
515
516     int value = item->data(Qt::UserRole).toInt();
517     if (value == ix) return item;
518     else if (value < ix) {
519         pos++;
520         while (pos < frame_list->count()) {
521             item = frame_list->item(pos);
522             value = item->data(Qt::UserRole).toInt();
523             if (value == ix) return item;
524             pos++;
525         }
526     } else {
527         pos --;
528         while (pos >= 0) {
529             item = frame_list->item(pos);
530             value = item->data(Qt::UserRole).toInt();
531             if (value == ix) return item;
532             pos --;
533         }
534     }
535     return NULL;
536 }
537
538
539 void StopmotionWidget::selectFrame(int ix)
540 {
541     frame_list->blockSignals(true);
542     QListWidgetItem *item = getFrameFromIndex(ix);
543     if (!item) return;
544     frame_list->setCurrentItem(item);
545     frame_list->blockSignals(false);
546 }
547
548 void StopmotionWidget::slotSeekFrame(bool forward)
549 {
550     int ix = frame_list->currentRow();
551     if (forward) {
552         if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
553     } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
554 }
555
556