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, QList< QAction * > actions, QWidget *parent) :
95 , m_projectFolder(projectFolder)
100 //setAttribute(Qt::WA_DeleteOnClose);
101 QAction *analyse = new QAction(i18n("Send frames to color scopes"), this);
102 analyse->setCheckable(true);
103 analyse->setChecked(KdenliveSettings::analyse_stopmotion());
104 connect(analyse, SIGNAL(triggered(bool)), this, SLOT(slotSwitchAnalyse(bool)));
107 setWindowTitle(i18n("Stop Motion Capture"));
108 setFont(KGlobalSettings::toolBarFont());
110 live_button->setIcon(KIcon("camera-photo"));
112 m_captureAction = actions.at(0);
113 connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
114 capture_button->setDefaultAction(m_captureAction);
116 connect(actions.at(1), SIGNAL(triggered()), this, SLOT(slotSwitchLive()));
118 preview_button->setIcon(KIcon("media-playback-start"));
119 capture_button->setEnabled(false);
122 QMenu *confMenu = new QMenu;
123 m_showOverlay = actions.at(2);
124 connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
125 confMenu->addAction(m_showOverlay);
128 m_effectIndex = KdenliveSettings::blitzeffect();
129 QMenu *effectsMenu = new QMenu(i18n("Overlay effect"));
130 QActionGroup *effectGroup = new QActionGroup(this);
131 QAction *noEffect = new QAction(i18n("No Effect"), effectGroup);
132 noEffect->setData(1);
133 QAction *contrastEffect = new QAction(i18n("Contrast"), effectGroup);
134 contrastEffect->setData(2);
135 QAction *edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
136 edgeEffect->setData(3);
137 QAction *brightEffect = new QAction(i18n("Brighten"), effectGroup);
138 brightEffect->setData(4);
139 QAction *invertEffect = new QAction(i18n("Invert"), effectGroup);
140 invertEffect->setData(5);
141 QAction *thresEffect = new QAction(i18n("Threshold"), effectGroup);
142 thresEffect->setData(6);
144 effectsMenu->addAction(noEffect);
145 effectsMenu->addAction(contrastEffect);
146 effectsMenu->addAction(edgeEffect);
147 effectsMenu->addAction(brightEffect);
148 effectsMenu->addAction(invertEffect);
149 effectsMenu->addAction(thresEffect);
150 QList <QAction *> list = effectsMenu->actions();
151 for (int i = 0; i < list.count(); i++) {
152 list.at(i)->setCheckable(true);
153 if (list.at(i)->data().toInt() == m_effectIndex) {
154 list.at(i)->setChecked(true);
157 connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
158 confMenu->addMenu(effectsMenu);
161 QAction *showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
162 showThumbs->setCheckable(true);
163 showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
164 connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
166 QAction *removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
167 removeCurrent->setShortcut(Qt::Key_Delete);
168 connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
170 QAction *capInterval = new QAction(KIcon(), i18n("Set capture interval"), this);
171 connect(capInterval, SIGNAL(triggered()), this, SLOT(slotSetCaptureInterval()));
173 confMenu->addAction(showThumbs);
174 confMenu->addAction(capInterval);
175 confMenu->addAction(removeCurrent);
176 confMenu->addAction(analyse);
177 config_button->setIcon(KIcon("configure"));
178 config_button->setMenu(confMenu);
180 // Build capture menu
181 QMenu *capMenu = new QMenu;
182 m_intervalCapture = new QAction(KIcon("edit-redo"), i18n("Interval capture"), this);
183 m_intervalCapture->setCheckable(true);
184 m_intervalCapture->setChecked(false);
185 connect(m_intervalCapture, SIGNAL(triggered(bool)), this, SLOT(slotIntervalCapture(bool)));
186 capMenu->addAction(m_intervalCapture);
187 capture_button->setMenu(capMenu);
189 connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
190 connect(sequence_name, SIGNAL(currentIndexChanged(int)), live_button, SLOT(setFocus()));
192 m_layout = new QVBoxLayout;
193 if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
194 // Found a BlackMagic device
195 m_bmCapture = new BmdCaptureHandler(m_layout);
196 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
198 if (QFile::exists(KdenliveSettings::video4vdevice())) {
199 #if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
200 V4lCaptureHandler v4l(NULL);
201 // Video 4 Linux device detection
202 for (int i = 0; i < 10; i++) {
203 QString path = "/dev/video" + QString::number(i);
204 if (QFile::exists(path)) {
205 QStringList deviceInfo = v4l.getDeviceName(path);
206 if (!deviceInfo.isEmpty()) {
207 capture_device->addItem(deviceInfo.at(0), "v4l");
208 capture_device->setItemData(capture_device->count() - 1, path, Qt::UserRole + 1);
209 capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(1), Qt::UserRole + 2);
210 if (path == KdenliveSettings::video4vdevice()) capture_device->setCurrentIndex(capture_device->count() - 1);
215 /*V4lCaptureHandler v4lhandler(NULL);
216 QStringList deviceInfo = v4lhandler.getDeviceName(KdenliveSettings::video4vdevice());
217 capture_device->addItem(deviceInfo.at(0), "v4l");
218 capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(3), Qt::UserRole + 1);*/
219 if (m_bmCapture == NULL) {
220 m_bmCapture = new V4lCaptureHandler(m_layout);
221 m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
226 connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
228 connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
229 connect(m_bmCapture, SIGNAL(gotFrame(QImage)), this, SIGNAL(gotFrame(QImage)));
230 } else live_button->setEnabled(false);
231 m_frame_preview = new MyLabel(this);
232 connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
233 connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
234 m_layout->addWidget(m_frame_preview);
235 m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
236 video_preview->setLayout(m_layout);
237 live_button->setChecked(false);
238 button_addsequence->setEnabled(false);
239 connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
240 connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
241 connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
242 connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
243 connect(frame_list, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slotShowSelectedFrame()));
244 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
246 frame_list->addAction(removeCurrent);
247 frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
248 frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
249 parseExistingSequences();
252 StopmotionWidget::~StopmotionWidget()
255 m_bmCapture->stopPreview();
258 void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
261 if (act) m_effectIndex = act->data().toInt();
262 KdenliveSettings::setBlitzeffect(m_effectIndex);
263 if (m_showOverlay->isChecked()) slotUpdateOverlay();
267 void StopmotionWidget::closeEvent(QCloseEvent *e)
270 QDialog::closeEvent(e);
273 void StopmotionWidget::slotSetCaptureInterval()
275 int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
276 if (interval > 0 && interval != KdenliveSettings::captureinterval())
277 KdenliveSettings::setCaptureinterval(interval);
280 void StopmotionWidget::slotShowThumbs(bool show)
282 KdenliveSettings::setShowstopmotionthumbs(show);
285 sequenceNameChanged(sequence_name->currentText());
290 frame_list->setHidden(!show);
293 void StopmotionWidget::slotIntervalCapture(bool capture)
295 if (capture) slotCaptureFrame();
299 void StopmotionWidget::slotUpdateHandler()
301 QString data = capture_device->itemData(capture_device->currentIndex()).toString();
306 m_layout->removeWidget(m_frame_preview);
308 #if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
309 m_bmCapture = new V4lCaptureHandler(m_layout);
310 m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
313 m_bmCapture = new BmdCaptureHandler(m_layout);
314 if (m_bmCapture) connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
316 live_button->setEnabled(m_bmCapture != NULL);
317 m_layout->addWidget(m_frame_preview);
320 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
322 log_box->insertItem(0, message);
325 void StopmotionWidget::parseExistingSequences()
327 sequence_name->clear();
328 sequence_name->addItem(QString());
329 QDir dir(m_projectFolder.path());
331 filters << "*_0000.png";
332 //dir.setNameFilters(filters);
333 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
334 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
335 foreach(QString sequencename, sequences) {
336 sequence_name->addItem(sequencename.section("_", 0, -2));
340 void StopmotionWidget::slotSwitchLive()
342 setUpdatesEnabled(false);
343 if (m_frame_preview->isHidden()) {
344 if (m_bmCapture) m_bmCapture->hidePreview(true);
345 m_frame_preview->setHidden(false);
347 m_frame_preview->setHidden(true);
348 if (m_bmCapture) m_bmCapture->hidePreview(false);
349 capture_button->setEnabled(true);
351 setUpdatesEnabled(true);
354 void StopmotionWidget::slotLive(bool isOn)
356 if (isOn && m_bmCapture) {
357 //m_frame_preview->setImage(QImage());
358 m_frame_preview->setHidden(true);
359 m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
360 capture_button->setEnabled(true);
362 if (m_bmCapture) m_bmCapture->stopPreview();
363 m_frame_preview->setHidden(false);
364 capture_button->setEnabled(false);
365 live_button->setChecked(false);
369 void StopmotionWidget::slotShowOverlay(bool isOn)
372 if (live_button->isChecked() && m_sequenceFrame > 0) {
375 } else if (m_bmCapture) {
376 m_bmCapture->hideOverlay();
380 void StopmotionWidget::slotUpdateOverlay()
382 if (m_bmCapture == NULL) return;
383 QString path = getPathForFrame(m_sequenceFrame - 1);
384 if (!QFile::exists(path)) return;
387 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
392 //img = Blitz::convolveEdge(img, 0, Blitz::Low);
393 switch (m_effectIndex) {
395 img = Blitz::contrast(img, true, 6);
398 img = Blitz::edge(img);
401 img = Blitz::intensity(img, 0.5);
407 img = Blitz::threshold(img, 120, Blitz::Grayscale, qRgba(0, 0, 0, 0), qRgba(255, 0, 0, 255));
408 //img = Blitz::flatten(img, QColor(255, 0, 0, 255), QColor(0, 0, 0, 0));
415 m_bmCapture->showOverlay(img);
418 void StopmotionWidget::sequenceNameChanged(const QString &name)
420 // Get rid of frames from previous sequence
421 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
423 m_future.waitForFinished();
425 if (name.isEmpty()) {
426 button_addsequence->setEnabled(false);
428 // Check if we are editing an existing sequence
429 QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
430 m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
431 if (!m_filesList.isEmpty()) {
432 m_sequenceName = sequence_name->currentText();
433 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
434 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
435 button_addsequence->setEnabled(true);
438 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
439 button_addsequence->setEnabled(false);
441 capture_button->setEnabled(live_button->isChecked());
445 void StopmotionWidget::slotCaptureFrame()
447 if (m_bmCapture == NULL) return;
448 if (sequence_name->currentText().isEmpty()) {
449 QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
450 if (seqName.isEmpty()) return;
451 sequence_name->blockSignals(true);
452 sequence_name->setItemText(sequence_name->currentIndex(), seqName);
453 sequence_name->blockSignals(false);
455 if (m_sequenceName != sequence_name->currentText()) {
456 m_sequenceName = sequence_name->currentText();
459 //capture_button->setEnabled(false);
460 QString currentPath = getPathForFrame(m_sequenceFrame);
461 m_bmCapture->captureFrame(currentPath);
462 KNotification::event("FrameCaptured");
464 button_addsequence->setEnabled(true);
465 if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
469 void StopmotionWidget::slotNewThumb(const QString path)
471 if (!KdenliveSettings::showstopmotionthumbs()) return;
472 m_filesList.append(path);
473 if (m_showOverlay->isChecked()) slotUpdateOverlay();
474 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
477 void StopmotionWidget::slotPrepareThumbs()
479 if (m_filesList.isEmpty()) return;
480 QString path = m_filesList.takeFirst();
481 emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
485 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
488 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
492 int width = height * img.width() / img.height();
493 frame_list->setIconSize(QSize(width, height));
494 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
495 QString nb = QString::number(ix);
497 QFontInfo finfo(font());
498 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
500 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
503 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
504 item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
505 item->setData(Qt::UserRole, ix);
506 frame_list->blockSignals(true);
507 frame_list->setCurrentItem(item);
508 frame_list->blockSignals(false);
509 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
512 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
514 if (seqName.isEmpty()) seqName = m_sequenceName;
515 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
518 void StopmotionWidget::slotShowFrame(const QString &path)
522 capture_button->setEnabled(false);
524 if (m_bmCapture) m_bmCapture->hidePreview(true);
525 m_frame_preview->setImage(img);
526 m_frame_preview->setHidden(false);
527 m_frame_preview->update();
531 void StopmotionWidget::slotShowSelectedFrame()
533 QListWidgetItem *item = frame_list->currentItem();
535 //int ix = item->data(Qt::UserRole).toInt();;
536 slotShowFrame(item->toolTip());
540 void StopmotionWidget::slotAddSequence()
542 emit addOrUpdateSequence(getPathForFrame(0));
545 void StopmotionWidget::slotPlayPreview(bool animate)
549 m_animationList.clear();
552 if (KdenliveSettings::showstopmotionthumbs()) {
553 frame_list->setCurrentRow(0);
554 QTimer::singleShot(200, this, SLOT(slotAnimate()));
556 SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
561 void StopmotionWidget::slotAnimate()
563 //slotShowFrame(m_animatedIndex);
564 if (KdenliveSettings::showstopmotionthumbs()) {
566 if (frame_list->currentRow() < (frame_list->count() - 1)) {
567 frame_list->setCurrentRow(frame_list->currentRow() + 1);
568 QTimer::singleShot(100, this, SLOT(slotAnimate()));
569 } else preview_button->setChecked(false);
570 } else if (!m_animationList.isEmpty()) {
571 slotShowFrame(m_animationList.takeFirst());
572 QTimer::singleShot(100, this, SLOT(slotAnimate()));
573 } else preview_button->setChecked(false);
577 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
579 QListWidgetItem *item = NULL;
581 if (ix >= frame_list->count()) {
582 pos = frame_list->count() - 1;
585 item = frame_list->item(pos);
587 int value = item->data(Qt::UserRole).toInt();
588 if (value == ix) return item;
589 else if (value < ix) {
591 while (pos < frame_list->count()) {
592 item = frame_list->item(pos);
593 value = item->data(Qt::UserRole).toInt();
594 if (value == ix) return item;
600 item = frame_list->item(pos);
601 value = item->data(Qt::UserRole).toInt();
602 if (value == ix) return item;
610 void StopmotionWidget::selectFrame(int ix)
612 frame_list->blockSignals(true);
613 QListWidgetItem *item = getFrameFromIndex(ix);
615 frame_list->setCurrentItem(item);
616 frame_list->blockSignals(false);
619 void StopmotionWidget::slotSeekFrame(bool forward)
621 int ix = frame_list->currentRow();
623 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
624 } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
627 void StopmotionWidget::slotRemoveFrame()
629 if (frame_list->currentItem() == NULL) return;
630 QString path = frame_list->currentItem()->toolTip();
631 if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
634 QListWidgetItem *item = frame_list->takeItem(frame_list->currentRow());
639 void StopmotionWidget::slotSwitchAnalyse(bool isOn)
641 KdenliveSettings::setAnalyse_stopmotion(isOn);
642 m_bmCapture->setAnalyse(isOn);