1 /***************************************************************************
2 stopmotion.cpp - description
5 copyright : (C) 2010 by Jean-Baptiste Mardelle
6 email : jb@kdenlive.org
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include "stopmotion.h"
19 #include "../blackmagic/devices.h"
20 #include "../v4l/v4lcapture.h"
21 #include "../slideshowclip.h"
22 #include "kdenlivesettings.h"
26 #include <KGlobalSettings>
27 #include <KFileDialog>
28 #include <KStandardDirs>
29 #include <KMessageBox>
30 #include <kdeversion.h>
31 #include <KNotification>
34 #include <qimageblitz/qimageblitz.h>
37 #include <QtConcurrentRun>
38 #include <QInputDialog>
40 #include <QVBoxLayout>
44 #include <QWheelEvent>
47 MyLabel::MyLabel(QWidget *parent) :
52 void MyLabel::setImage(QImage img)
58 void MyLabel::wheelEvent(QWheelEvent * event)
60 if (event->delta() > 0) emit seek(true);
61 else emit seek(false);
65 void MyLabel::paintEvent(QPaintEvent * event)
69 QRect r(0, 0, width(), height());
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;
78 int calculatedHeight = width() / aspect_ratio;
79 if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
81 p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
86 StopmotionWidget::StopmotionWidget(KUrl projectFolder, QWidget *parent) :
89 , m_projectFolder(projectFolder)
95 setWindowTitle(i18n("Stop Motion Capture"));
96 setFont(KGlobalSettings::toolBarFont());
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);
104 preview_button->setIcon(KIcon("media-playback-start"));
105 capture_button->setEnabled(false);
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);
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);
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);
145 connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
146 confMenu->addMenu(effectsMenu);
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)));
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()));
158 QAction *capInterval = new QAction(KIcon(), i18n("Set capture interval"), this);
159 connect(capInterval, SIGNAL(triggered()), this, SLOT(slotSetCaptureInterval()));
161 confMenu->addAction(showThumbs);
162 confMenu->addAction(capInterval);
163 confMenu->addAction(removeCurrent);
164 config_button->setIcon(KIcon("configure"));
165 config_button->setMenu(confMenu);
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);
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 &)));
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");
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)));
203 frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
204 parseExistingSequences();
207 StopmotionWidget::~StopmotionWidget()
209 m_bmCapture->stopPreview();
212 void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
215 if (act) m_effectIndex = act->data().toInt();
216 KdenliveSettings::setBlitzeffect(m_effectIndex);
217 if (m_showOverlay->isChecked()) slotUpdateOverlay();
221 void StopmotionWidget::closeEvent(QCloseEvent *e)
224 QDialog::closeEvent(e);
227 void StopmotionWidget::slotSetCaptureInterval()
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);
234 void StopmotionWidget::slotShowThumbs(bool show)
236 KdenliveSettings::setShowstopmotionthumbs(show);
239 sequenceNameChanged(sequence_name->currentText());
244 frame_list->setHidden(!show);
247 void StopmotionWidget::slotIntervalCapture(bool capture)
249 if (capture) slotCaptureFrame();
253 void StopmotionWidget::slotUpdateHandler()
255 QString data = capture_device->itemData(capture_device->currentIndex()).toString();
256 m_bmCapture->stopPreview();
258 m_layout->removeWidget(m_frame_preview);
260 m_bmCapture = new V4lCaptureHandler(m_layout);
262 m_bmCapture = new BmdCaptureHandler(m_layout);
263 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
265 m_layout->addWidget(m_frame_preview);
268 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
270 log_box->insertItem(0, message);
273 void StopmotionWidget::parseExistingSequences()
275 sequence_name->clear();
276 sequence_name->addItem(QString());
277 QDir dir(m_projectFolder.path());
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));
288 void StopmotionWidget::slotLive(bool 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);
296 m_bmCapture->stopPreview();
297 capture_button->setEnabled(false);
298 live_button->setChecked(false);
302 void StopmotionWidget::slotShowOverlay(bool isOn)
305 if (live_button->isChecked() && m_sequenceFrame > 0) {
309 m_bmCapture->hideOverlay();
313 void StopmotionWidget::slotUpdateOverlay()
315 QString path = getPathForFrame(m_sequenceFrame - 1);
316 if (!QFile::exists(path)) return;
319 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
324 //img = Blitz::convolveEdge(img, 0, Blitz::Low);
325 switch (m_effectIndex) {
327 img = Blitz::contrast(img, true, 6);
330 img = Blitz::edge(img);
333 img = Blitz::intensity(img, 0.5);
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));
347 m_bmCapture->showOverlay(img);
350 void StopmotionWidget::sequenceNameChanged(const QString &name)
352 // Get rid of frames from previous sequence
353 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
355 m_future.waitForFinished();
357 if (name.isEmpty()) {
358 button_addsequence->setEnabled(false);
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);
370 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
371 button_addsequence->setEnabled(false);
373 capture_button->setEnabled(live_button->isChecked());
377 void StopmotionWidget::slotCaptureFrame()
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);
386 if (m_sequenceName != sequence_name->currentText()) {
387 m_sequenceName = sequence_name->currentText();
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");
396 button_addsequence->setEnabled(true);
397 if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
401 void StopmotionWidget::slotNewThumb(const QString path)
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);
409 void StopmotionWidget::slotPrepareThumbs()
411 if (m_filesList.isEmpty()) return;
412 QString path = m_filesList.takeFirst();
413 emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
417 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
420 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
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);
429 QFontInfo finfo(font());
430 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
432 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
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);
441 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
443 if (seqName.isEmpty()) seqName = m_sequenceName;
444 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
447 void StopmotionWidget::slotShowFrame(const QString &path)
451 capture_button->setEnabled(false);
453 m_bmCapture->hidePreview(true);
454 m_frame_preview->setImage(img);
455 m_frame_preview->setHidden(false);
456 m_frame_preview->update();
460 void StopmotionWidget::slotShowSelectedFrame()
462 QListWidgetItem *item = frame_list->currentItem();
464 //int ix = item->data(Qt::UserRole).toInt();;
465 slotShowFrame(item->toolTip());
469 void StopmotionWidget::slotAddSequence()
471 emit addOrUpdateSequence(getPathForFrame(0));
474 void StopmotionWidget::slotPlayPreview(bool animate)
478 m_animationList.clear();
481 if (KdenliveSettings::showstopmotionthumbs()) {
482 frame_list->setCurrentRow(0);
483 QTimer::singleShot(200, this, SLOT(slotAnimate()));
485 SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
490 void StopmotionWidget::slotAnimate()
492 //slotShowFrame(m_animatedIndex);
493 if (KdenliveSettings::showstopmotionthumbs()) {
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);
506 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
508 QListWidgetItem *item = NULL;
510 if (ix >= frame_list->count()) {
511 pos = frame_list->count() - 1;
514 item = frame_list->item(pos);
516 int value = item->data(Qt::UserRole).toInt();
517 if (value == ix) return item;
518 else if (value < ix) {
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;
529 item = frame_list->item(pos);
530 value = item->data(Qt::UserRole).toInt();
531 if (value == ix) return item;
539 void StopmotionWidget::selectFrame(int ix)
541 frame_list->blockSignals(true);
542 QListWidgetItem *item = getFrameFromIndex(ix);
544 frame_list->setCurrentItem(item);
545 frame_list->blockSignals(false);
548 void StopmotionWidget::slotSeekFrame(bool forward)
550 int ix = frame_list->currentRow();
552 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
553 } else if (ix > 0) frame_list->setCurrentRow(ix - 1);