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::mousePressEvent(QMouseEvent *)
71 void MyLabel::paintEvent(QPaintEvent * event)
75 QRect r(0, 0, width(), height());
77 p.fillRect(r, QColor(KdenliveSettings::window_background()));
78 double aspect_ratio = (double) m_img.width() / m_img.height();
79 int pictureHeight = height();
80 int pictureWidth = width();
81 int calculatedWidth = aspect_ratio * height();
82 if (calculatedWidth > width()) pictureHeight = width() / aspect_ratio;
84 int calculatedHeight = width() / aspect_ratio;
85 if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
87 p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
92 StopmotionWidget::StopmotionWidget(KUrl projectFolder, const QList< QAction * > actions, QWidget *parent) :
95 , m_projectFolder(projectFolder)
100 //setAttribute(Qt::WA_DeleteOnClose);
103 setWindowTitle(i18n("Stop Motion Capture"));
104 setFont(KGlobalSettings::toolBarFont());
106 live_button->setIcon(KIcon("camera-photo"));
108 m_captureAction = actions.at(0);
109 connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
110 capture_button->setDefaultAction(m_captureAction);
112 connect(actions.at(1), SIGNAL(triggered()), this, SLOT(slotSwitchLive()));
114 preview_button->setIcon(KIcon("media-playback-start"));
115 capture_button->setEnabled(false);
118 QMenu *confMenu = new QMenu;
119 m_showOverlay = actions.at(2);
120 connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
121 confMenu->addAction(m_showOverlay);
124 m_effectIndex = KdenliveSettings::blitzeffect();
125 QMenu *effectsMenu = new QMenu(i18n("Overlay effect"));
126 QActionGroup *effectGroup = new QActionGroup(this);
127 QAction *noEffect = new QAction(i18n("No Effect"), effectGroup);
128 noEffect->setData(1);
129 QAction *contrastEffect = new QAction(i18n("Contrast"), effectGroup);
130 contrastEffect->setData(2);
131 QAction *edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
132 edgeEffect->setData(3);
133 QAction *brightEffect = new QAction(i18n("Brighten"), effectGroup);
134 brightEffect->setData(4);
135 QAction *invertEffect = new QAction(i18n("Invert"), effectGroup);
136 invertEffect->setData(5);
137 QAction *thresEffect = new QAction(i18n("Threshold"), effectGroup);
138 thresEffect->setData(6);
140 effectsMenu->addAction(noEffect);
141 effectsMenu->addAction(contrastEffect);
142 effectsMenu->addAction(edgeEffect);
143 effectsMenu->addAction(brightEffect);
144 effectsMenu->addAction(invertEffect);
145 effectsMenu->addAction(thresEffect);
146 QList <QAction *> list = effectsMenu->actions();
147 for (int i = 0; i < list.count(); i++) {
148 list.at(i)->setCheckable(true);
149 if (list.at(i)->data().toInt() == m_effectIndex) {
150 list.at(i)->setChecked(true);
153 connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
154 confMenu->addMenu(effectsMenu);
157 QAction *showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
158 showThumbs->setCheckable(true);
159 showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
160 connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
162 QAction *removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
163 removeCurrent->setShortcut(Qt::Key_Delete);
164 connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
166 QAction *capInterval = new QAction(KIcon(), i18n("Set capture interval"), this);
167 connect(capInterval, SIGNAL(triggered()), this, SLOT(slotSetCaptureInterval()));
169 confMenu->addAction(showThumbs);
170 confMenu->addAction(capInterval);
171 confMenu->addAction(removeCurrent);
172 config_button->setIcon(KIcon("configure"));
173 config_button->setMenu(confMenu);
175 // Build capture menu
176 QMenu *capMenu = new QMenu;
177 m_intervalCapture = new QAction(KIcon("edit-redo"), i18n("Interval capture"), this);
178 m_intervalCapture->setCheckable(true);
179 m_intervalCapture->setChecked(false);
180 connect(m_intervalCapture, SIGNAL(triggered(bool)), this, SLOT(slotIntervalCapture(bool)));
181 capMenu->addAction(m_intervalCapture);
182 capture_button->setMenu(capMenu);
184 connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
185 connect(sequence_name, SIGNAL(currentIndexChanged(int)), live_button, SLOT(setFocus()));
187 m_layout = new QVBoxLayout;
188 if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
189 // Found a BlackMagic device
190 m_bmCapture = new BmdCaptureHandler(m_layout);
191 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
193 if (QFile::exists(KdenliveSettings::video4vdevice())) {
195 V4lCaptureHandler v4l(NULL);
196 // Video 4 Linux device detection
197 for (int i = 0; i < 10; i++) {
198 QString path = "/dev/video" + QString::number(i);
199 if (QFile::exists(path)) {
200 QStringList deviceInfo = v4l.getDeviceName(path);
201 if (!deviceInfo.isEmpty()) {
202 capture_device->addItem(deviceInfo.at(0), "v4l");
203 capture_device->setItemData(capture_device->count() - 1, path, Qt::UserRole + 1);
204 capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(1), Qt::UserRole + 2);
205 if (path == KdenliveSettings::video4vdevice()) capture_device->setCurrentIndex(capture_device->count() - 1);
210 /*V4lCaptureHandler v4lhandler(NULL);
211 QStringList deviceInfo = v4lhandler.getDeviceName(KdenliveSettings::video4vdevice());
212 capture_device->addItem(deviceInfo.at(0), "v4l");
213 capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(3), Qt::UserRole + 1);*/
214 if (m_bmCapture == NULL) {
215 m_bmCapture = new V4lCaptureHandler(m_layout);
216 m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
221 connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
223 connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
224 } else live_button->setEnabled(false);
225 m_frame_preview = new MyLabel(this);
226 connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
227 connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
228 m_layout->addWidget(m_frame_preview);
229 m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
230 video_preview->setLayout(m_layout);
231 live_button->setChecked(false);
232 button_addsequence->setEnabled(false);
233 connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
234 connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
235 connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
236 connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
237 connect(frame_list, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slotShowSelectedFrame()));
238 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
240 frame_list->addAction(removeCurrent);
241 frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
242 frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
243 parseExistingSequences();
246 StopmotionWidget::~StopmotionWidget()
249 m_bmCapture->stopPreview();
252 void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
255 if (act) m_effectIndex = act->data().toInt();
256 KdenliveSettings::setBlitzeffect(m_effectIndex);
257 if (m_showOverlay->isChecked()) slotUpdateOverlay();
261 void StopmotionWidget::closeEvent(QCloseEvent *e)
264 QDialog::closeEvent(e);
267 void StopmotionWidget::slotSetCaptureInterval()
269 int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
270 if (interval > 0 && interval != KdenliveSettings::captureinterval())
271 KdenliveSettings::setCaptureinterval(interval);
274 void StopmotionWidget::slotShowThumbs(bool show)
276 KdenliveSettings::setShowstopmotionthumbs(show);
279 sequenceNameChanged(sequence_name->currentText());
284 frame_list->setHidden(!show);
287 void StopmotionWidget::slotIntervalCapture(bool capture)
289 if (capture) slotCaptureFrame();
293 void StopmotionWidget::slotUpdateHandler()
295 QString data = capture_device->itemData(capture_device->currentIndex()).toString();
300 m_layout->removeWidget(m_frame_preview);
303 m_bmCapture = new V4lCaptureHandler(m_layout);
304 m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
307 m_bmCapture = new BmdCaptureHandler(m_layout);
308 if (m_bmCapture) connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
310 live_button->setEnabled(m_bmCapture != NULL);
311 m_layout->addWidget(m_frame_preview);
314 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
316 log_box->insertItem(0, message);
319 void StopmotionWidget::parseExistingSequences()
321 sequence_name->clear();
322 sequence_name->addItem(QString());
323 QDir dir(m_projectFolder.path());
325 filters << "*_0000.png";
326 //dir.setNameFilters(filters);
327 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
328 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
329 foreach(QString sequencename, sequences) {
330 sequence_name->addItem(sequencename.section("_", 0, -2));
334 void StopmotionWidget::slotSwitchLive()
336 setUpdatesEnabled(false);
337 if (m_frame_preview->isHidden()) {
338 if (m_bmCapture) m_bmCapture->hidePreview(true);
339 m_frame_preview->setHidden(false);
341 m_frame_preview->setHidden(true);
342 if (m_bmCapture) m_bmCapture->hidePreview(false);
344 setUpdatesEnabled(true);
347 void StopmotionWidget::slotLive(bool isOn)
349 if (isOn && m_bmCapture) {
350 //m_frame_preview->setImage(QImage());
351 m_frame_preview->setHidden(true);
352 m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
353 capture_button->setEnabled(true);
355 if (m_bmCapture) m_bmCapture->stopPreview();
356 m_frame_preview->setHidden(false);
357 capture_button->setEnabled(false);
358 live_button->setChecked(false);
362 void StopmotionWidget::slotShowOverlay(bool isOn)
365 if (live_button->isChecked() && m_sequenceFrame > 0) {
368 } else if (m_bmCapture) {
369 m_bmCapture->hideOverlay();
373 void StopmotionWidget::slotUpdateOverlay()
375 if (m_bmCapture == NULL) return;
376 QString path = getPathForFrame(m_sequenceFrame - 1);
377 if (!QFile::exists(path)) return;
380 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
385 //img = Blitz::convolveEdge(img, 0, Blitz::Low);
386 switch (m_effectIndex) {
388 img = Blitz::contrast(img, true, 6);
391 img = Blitz::edge(img);
394 img = Blitz::intensity(img, 0.5);
400 img = Blitz::threshold(img, 120, Blitz::Grayscale, qRgba(0, 0, 0, 0), qRgba(255, 0, 0, 255));
401 //img = Blitz::flatten(img, QColor(255, 0, 0, 255), QColor(0, 0, 0, 0));
408 m_bmCapture->showOverlay(img);
411 void StopmotionWidget::sequenceNameChanged(const QString &name)
413 // Get rid of frames from previous sequence
414 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
416 m_future.waitForFinished();
418 if (name.isEmpty()) {
419 button_addsequence->setEnabled(false);
421 // Check if we are editing an existing sequence
422 QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
423 m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
424 if (!m_filesList.isEmpty()) {
425 m_sequenceName = sequence_name->currentText();
426 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
427 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
428 button_addsequence->setEnabled(true);
431 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
432 button_addsequence->setEnabled(false);
434 capture_button->setEnabled(live_button->isChecked());
438 void StopmotionWidget::slotCaptureFrame()
440 if (m_bmCapture == NULL) return;
441 if (sequence_name->currentText().isEmpty()) {
442 QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
443 if (seqName.isEmpty()) return;
444 sequence_name->blockSignals(true);
445 sequence_name->setItemText(sequence_name->currentIndex(), seqName);
446 sequence_name->blockSignals(false);
448 if (m_sequenceName != sequence_name->currentText()) {
449 m_sequenceName = sequence_name->currentText();
452 //capture_button->setEnabled(false);
453 QString currentPath = getPathForFrame(m_sequenceFrame);
454 m_bmCapture->captureFrame(currentPath);
455 KNotification::event("FrameCaptured");
457 button_addsequence->setEnabled(true);
458 if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
462 void StopmotionWidget::slotNewThumb(const QString path)
464 if (!KdenliveSettings::showstopmotionthumbs()) return;
465 m_filesList.append(path);
466 if (m_showOverlay->isChecked()) slotUpdateOverlay();
467 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
470 void StopmotionWidget::slotPrepareThumbs()
472 if (m_filesList.isEmpty()) return;
473 QString path = m_filesList.takeFirst();
474 emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
478 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
481 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
485 int width = height * img.width() / img.height();
486 frame_list->setIconSize(QSize(width, height));
487 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
488 QString nb = QString::number(ix);
490 QFontInfo finfo(font());
491 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
493 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
496 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
497 item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
498 item->setData(Qt::UserRole, ix);
499 frame_list->blockSignals(true);
500 frame_list->setCurrentItem(item);
501 frame_list->blockSignals(false);
502 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
505 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
507 if (seqName.isEmpty()) seqName = m_sequenceName;
508 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
511 void StopmotionWidget::slotShowFrame(const QString &path)
515 capture_button->setEnabled(false);
517 if (m_bmCapture) m_bmCapture->hidePreview(true);
518 m_frame_preview->setImage(img);
519 m_frame_preview->setHidden(false);
520 m_frame_preview->update();
524 void StopmotionWidget::slotShowSelectedFrame()
526 QListWidgetItem *item = frame_list->currentItem();
528 //int ix = item->data(Qt::UserRole).toInt();;
529 slotShowFrame(item->toolTip());
533 void StopmotionWidget::slotAddSequence()
535 emit addOrUpdateSequence(getPathForFrame(0));
538 void StopmotionWidget::slotPlayPreview(bool animate)
542 m_animationList.clear();
545 if (KdenliveSettings::showstopmotionthumbs()) {
546 frame_list->setCurrentRow(0);
547 QTimer::singleShot(200, this, SLOT(slotAnimate()));
549 SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
554 void StopmotionWidget::slotAnimate()
556 //slotShowFrame(m_animatedIndex);
557 if (KdenliveSettings::showstopmotionthumbs()) {
559 if (frame_list->currentRow() < (frame_list->count() - 1)) {
560 frame_list->setCurrentRow(frame_list->currentRow() + 1);
561 QTimer::singleShot(100, this, SLOT(slotAnimate()));
562 } else preview_button->setChecked(false);
563 } else if (!m_animationList.isEmpty()) {
564 slotShowFrame(m_animationList.takeFirst());
565 QTimer::singleShot(100, this, SLOT(slotAnimate()));
566 } else preview_button->setChecked(false);
570 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
572 QListWidgetItem *item = NULL;
574 if (ix >= frame_list->count()) {
575 pos = frame_list->count() - 1;
578 item = frame_list->item(pos);
580 int value = item->data(Qt::UserRole).toInt();
581 if (value == ix) return item;
582 else if (value < ix) {
584 while (pos < frame_list->count()) {
585 item = frame_list->item(pos);
586 value = item->data(Qt::UserRole).toInt();
587 if (value == ix) return item;
593 item = frame_list->item(pos);
594 value = item->data(Qt::UserRole).toInt();
595 if (value == ix) return item;
603 void StopmotionWidget::selectFrame(int ix)
605 frame_list->blockSignals(true);
606 QListWidgetItem *item = getFrameFromIndex(ix);
608 frame_list->setCurrentItem(item);
609 frame_list->blockSignals(false);
612 void StopmotionWidget::slotSeekFrame(bool forward)
614 int ix = frame_list->currentRow();
616 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
617 } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
620 void StopmotionWidget::slotRemoveFrame()
622 if (frame_list->currentItem() == NULL) return;
623 QString path = frame_list->currentItem()->toolTip();
624 if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
627 QListWidgetItem *item = frame_list->takeItem(frame_list->currentRow());