]> git.sesse.net Git - kdenlive/blob - src/stopmotion/stopmotion.cpp
Improve stopmotion widget:
[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(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()));
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     live_button->setChecked(false);
225     QDialog::closeEvent(e);
226 }
227
228 void StopmotionWidget::slotSetCaptureInterval()
229 {
230     int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
231     if(interval > 0 && interval != KdenliveSettings::captureinterval())
232         KdenliveSettings::setCaptureinterval(interval);
233 }
234
235 void StopmotionWidget::slotShowThumbs(bool show)
236 {
237     KdenliveSettings::setShowstopmotionthumbs(show);
238     kDebug() << "SHOW THUMBS: " << show;
239     if(show) {
240         frame_list->clear();
241         sequenceNameChanged(sequence_name->currentText());
242     } else {
243         m_filesList.clear();
244         frame_list->clear();
245     }
246     frame_list->setHidden(!show);
247 }
248
249 void StopmotionWidget::slotIntervalCapture(bool capture)
250 {
251     if(capture) slotCaptureFrame();
252 }
253
254
255 void StopmotionWidget::slotUpdateHandler()
256 {
257     QString data = capture_device->itemData(capture_device->currentIndex()).toString();
258     m_bmCapture->stopPreview();
259     delete m_bmCapture;
260     m_layout->removeWidget(m_frame_preview);
261     if(data == "v4l") {
262         m_bmCapture = new V4lCaptureHandler(m_layout);
263     } else {
264         m_bmCapture = new BmdCaptureHandler(m_layout);
265         connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
266     }
267     m_layout->addWidget(m_frame_preview);
268 }
269
270 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
271 {
272     log_box->insertItem(0, message);
273 }
274
275 void StopmotionWidget::parseExistingSequences()
276 {
277     sequence_name->clear();
278     sequence_name->addItem(QString());
279     QDir dir(m_projectFolder.path());
280     QStringList filters;
281     filters << "*_0000.png";
282     //dir.setNameFilters(filters);
283     QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
284     //kDebug()<<"PF: "<<<<", sm: "<<sequences;
285     foreach(QString sequencename, sequences) {
286         sequence_name->addItem(sequencename.section("_", 0, -2));
287     }
288 }
289
290 void StopmotionWidget::slotLive(bool isOn)
291 {
292     if(isOn) {
293         m_frame_preview->setImage(QImage());
294         m_frame_preview->setHidden(true);
295         m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode());
296         capture_button->setEnabled(true);
297     } else {
298         m_bmCapture->stopPreview();
299         capture_button->setEnabled(false);
300     }
301 }
302
303 void StopmotionWidget::slotShowOverlay(bool isOn)
304 {
305     if(isOn) {
306         if(live_button->isChecked() && m_sequenceFrame > 0) {
307             slotUpdateOverlay();
308         }
309     } else {
310         m_bmCapture->hideOverlay();
311     }
312 }
313
314 void StopmotionWidget::slotUpdateOverlay()
315 {
316     QString path = getPathForFrame(m_sequenceFrame - 1);
317     if(!QFile::exists(path)) return;
318     QImage img(path);
319     if(img.isNull()) {
320         QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
321         return;
322     }
323
324 #ifdef QIMAGEBLITZ
325     //img = Blitz::convolveEdge(img, 0, Blitz::Low);
326     switch(m_effectIndex) {
327     case 2:
328         img = Blitz::contrast(img, true);
329         break;
330     case 3:
331         img = Blitz::edge(img);
332         break;
333     case 4:
334         img = Blitz::intensity(img, 0.5);
335         break;
336     case 5:
337         Blitz::invert(img);
338         break;
339     case 6:
340         img = Blitz::threshold(img, 200, Blitz::Grayscale, qRgba(255, 0, 0, 255), qRgba(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(int ix)
448 {
449     if(m_sequenceFrame == 0) {
450         //there are no images in sequence
451         return;
452     }
453     if(ix < m_sequenceFrame) {
454         // Show previous frame
455         slotLive(false);
456         live_button->setChecked(false);
457         QImage img(getPathForFrame(ix));
458         capture_button->setEnabled(false);
459         if(!img.isNull()) {
460             //m_bmCapture->showOverlay(img, false);
461             m_bmCapture->hidePreview(true);
462             m_frame_preview->setImage(img);
463             m_frame_preview->setHidden(false);
464             m_frame_preview->update();
465             selectFrame(ix);
466         }
467     }
468     /*else {
469     slotLive(true);
470     m_frame_preview->setImage(QImage());
471     m_frame_preview->setHidden(true);
472     m_bmCapture->hideOverlay();
473     m_bmCapture->hidePreview(false);
474     capture_button->setEnabled(true);
475     }*/
476 }
477
478 void StopmotionWidget::slotShowSelectedFrame()
479 {
480     QListWidgetItem *item = frame_list->currentItem();
481     if(item) {
482         int ix = item->data(Qt::UserRole).toInt();;
483         //slotShowFrame(ix);
484     }
485 }
486
487 void StopmotionWidget::slotAddSequence()
488 {
489     emit addOrUpdateSequence(getPathForFrame(0));
490 }
491
492 void StopmotionWidget::slotPlayPreview()
493 {
494     if(m_animatedIndex != -1) {
495         // stop animation
496         m_animatedIndex = -1;
497         return;
498     }
499     QListWidgetItem *item = frame_list->currentItem();
500     if(item) {
501         m_animatedIndex = item->data(Qt::UserRole).toInt();
502     }
503     QTimer::singleShot(200, this, SLOT(slotAnimate()));
504 }
505
506 void StopmotionWidget::slotAnimate()
507 {
508     slotShowFrame(m_animatedIndex);
509     m_animatedIndex++;
510     if(m_animatedIndex < m_sequenceFrame) QTimer::singleShot(200, this, SLOT(slotAnimate()));
511     else m_animatedIndex = -1;
512 }
513
514 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
515 {
516     QListWidgetItem *item = NULL;
517     int pos = ix;
518     if(ix >= frame_list->count()) {
519         pos = frame_list->count() - 1;
520     }
521     if(ix < 0) pos = 0;
522     item = frame_list->item(pos);
523
524     int value = item->data(Qt::UserRole).toInt();
525     if(value == ix) return item;
526     else if(value < ix) {
527         pos++;
528         while(pos < frame_list->count()) {
529             item = frame_list->item(pos);
530             value = item->data(Qt::UserRole).toInt();
531             if(value == ix) return item;
532             pos++;
533         }
534     } else {
535         pos --;
536         while(pos >= 0) {
537             item = frame_list->item(pos);
538             value = item->data(Qt::UserRole).toInt();
539             if(value == ix) return item;
540             pos --;
541         }
542     }
543     return NULL;
544 }
545
546
547 void StopmotionWidget::selectFrame(int ix)
548 {
549     frame_list->blockSignals(true);
550     QListWidgetItem *item = getFrameFromIndex(ix);
551     if(!item) return;
552     frame_list->setCurrentItem(item);
553     frame_list->blockSignals(false);
554 }
555
556 void StopmotionWidget::slotSeekFrame(bool forward)
557 {
558     int ix = frame_list->currentRow();
559     if(forward) {
560         if(ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
561     } else if(ix > 0) frame_list->setCurrentRow(ix - 1);
562 }
563
564