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)
102 setWindowTitle(i18n("Stop Motion Capture"));
103 setFont(KGlobalSettings::toolBarFont());
105 live_button->setIcon(KIcon("camera-photo"));
107 m_captureAction = actions.at(0);
108 connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
109 capture_button->setDefaultAction(m_captureAction);
111 connect(actions.at(1), SIGNAL(triggered()), this, SLOT(slotSwitchLive()));
113 preview_button->setIcon(KIcon("media-playback-start"));
114 capture_button->setEnabled(false);
117 QMenu *confMenu = new QMenu;
118 m_showOverlay = actions.at(2);
119 connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
120 confMenu->addAction(m_showOverlay);
123 m_effectIndex = KdenliveSettings::blitzeffect();
124 QMenu *effectsMenu = new QMenu(i18n("Overlay effect"));
125 QActionGroup *effectGroup = new QActionGroup(this);
126 QAction *noEffect = new QAction(i18n("No Effect"), effectGroup);
127 noEffect->setData(1);
128 QAction *contrastEffect = new QAction(i18n("Contrast"), effectGroup);
129 contrastEffect->setData(2);
130 QAction *edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
131 edgeEffect->setData(3);
132 QAction *brightEffect = new QAction(i18n("Brighten"), effectGroup);
133 brightEffect->setData(4);
134 QAction *invertEffect = new QAction(i18n("Invert"), effectGroup);
135 invertEffect->setData(5);
136 QAction *thresEffect = new QAction(i18n("Threshold"), effectGroup);
137 thresEffect->setData(6);
139 effectsMenu->addAction(noEffect);
140 effectsMenu->addAction(contrastEffect);
141 effectsMenu->addAction(edgeEffect);
142 effectsMenu->addAction(brightEffect);
143 effectsMenu->addAction(invertEffect);
144 effectsMenu->addAction(thresEffect);
145 QList <QAction *> list = effectsMenu->actions();
146 for (int i = 0; i < list.count(); i++) {
147 list.at(i)->setCheckable(true);
148 if (list.at(i)->data().toInt() == m_effectIndex) {
149 list.at(i)->setChecked(true);
152 connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
153 confMenu->addMenu(effectsMenu);
156 QAction *showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
157 showThumbs->setCheckable(true);
158 showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
159 connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
161 QAction *removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
162 removeCurrent->setShortcut(Qt::Key_Delete);
163 connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
165 QAction *capInterval = new QAction(KIcon(), i18n("Set capture interval"), this);
166 connect(capInterval, SIGNAL(triggered()), this, SLOT(slotSetCaptureInterval()));
168 confMenu->addAction(showThumbs);
169 confMenu->addAction(capInterval);
170 confMenu->addAction(removeCurrent);
171 config_button->setIcon(KIcon("configure"));
172 config_button->setMenu(confMenu);
174 // Build capture menu
175 QMenu *capMenu = new QMenu;
176 m_intervalCapture = new QAction(KIcon("edit-redo"), i18n("Interval capture"), this);
177 m_intervalCapture->setCheckable(true);
178 m_intervalCapture->setChecked(false);
179 connect(m_intervalCapture, SIGNAL(triggered(bool)), this, SLOT(slotIntervalCapture(bool)));
180 capMenu->addAction(m_intervalCapture);
181 capture_button->setMenu(capMenu);
183 connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
184 connect(sequence_name, SIGNAL(currentIndexChanged(int)), live_button, SLOT(setFocus()));
186 m_layout = new QVBoxLayout;
187 if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
188 // Found a BlackMagic device
189 m_bmCapture = new BmdCaptureHandler(m_layout);
190 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
192 if (QFile::exists(KdenliveSettings::video4vdevice())) {
194 if (m_bmCapture == NULL) m_bmCapture = new V4lCaptureHandler(m_layout);
195 capture_device->addItem(m_bmCapture->getDeviceName(KdenliveSettings::video4vdevice()).at(0), "v4l");
199 connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
200 connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
201 m_frame_preview = new MyLabel(this);
202 connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
203 connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
204 m_layout->addWidget(m_frame_preview);
205 m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
206 video_preview->setLayout(m_layout);
207 live_button->setChecked(false);
208 button_addsequence->setEnabled(false);
209 connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
210 connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
211 connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
212 connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
213 connect(frame_list, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slotShowSelectedFrame()));
214 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
216 frame_list->addAction(removeCurrent);
217 frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
218 frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
219 parseExistingSequences();
222 StopmotionWidget::~StopmotionWidget()
225 m_bmCapture->stopPreview();
228 void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
231 if (act) m_effectIndex = act->data().toInt();
232 KdenliveSettings::setBlitzeffect(m_effectIndex);
233 if (m_showOverlay->isChecked()) slotUpdateOverlay();
237 void StopmotionWidget::closeEvent(QCloseEvent *e)
240 QDialog::closeEvent(e);
243 void StopmotionWidget::slotSetCaptureInterval()
245 int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
246 if (interval > 0 && interval != KdenliveSettings::captureinterval())
247 KdenliveSettings::setCaptureinterval(interval);
250 void StopmotionWidget::slotShowThumbs(bool show)
252 KdenliveSettings::setShowstopmotionthumbs(show);
255 sequenceNameChanged(sequence_name->currentText());
260 frame_list->setHidden(!show);
263 void StopmotionWidget::slotIntervalCapture(bool capture)
265 if (capture) slotCaptureFrame();
269 void StopmotionWidget::slotUpdateHandler()
271 QString data = capture_device->itemData(capture_device->currentIndex()).toString();
272 m_bmCapture->stopPreview();
274 m_layout->removeWidget(m_frame_preview);
277 m_bmCapture = new V4lCaptureHandler(m_layout);
280 m_bmCapture = new BmdCaptureHandler(m_layout);
281 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
283 m_layout->addWidget(m_frame_preview);
286 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
288 log_box->insertItem(0, message);
291 void StopmotionWidget::parseExistingSequences()
293 sequence_name->clear();
294 sequence_name->addItem(QString());
295 QDir dir(m_projectFolder.path());
297 filters << "*_0000.png";
298 //dir.setNameFilters(filters);
299 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
300 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
301 foreach(QString sequencename, sequences) {
302 sequence_name->addItem(sequencename.section("_", 0, -2));
306 void StopmotionWidget::slotSwitchLive()
308 setUpdatesEnabled(false);
309 if (m_frame_preview->isHidden()) {
310 m_bmCapture->hidePreview(true);
311 m_frame_preview->setHidden(false);
313 m_frame_preview->setHidden(true);
314 m_bmCapture->hidePreview(false);
316 setUpdatesEnabled(true);
319 void StopmotionWidget::slotLive(bool isOn)
322 //m_frame_preview->setImage(QImage());
323 m_frame_preview->setHidden(true);
324 m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
325 capture_button->setEnabled(true);
327 m_bmCapture->stopPreview();
328 m_frame_preview->setHidden(false);
329 capture_button->setEnabled(false);
330 live_button->setChecked(false);
334 void StopmotionWidget::slotShowOverlay(bool isOn)
337 if (live_button->isChecked() && m_sequenceFrame > 0) {
341 m_bmCapture->hideOverlay();
345 void StopmotionWidget::slotUpdateOverlay()
347 QString path = getPathForFrame(m_sequenceFrame - 1);
348 if (!QFile::exists(path)) return;
351 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
356 //img = Blitz::convolveEdge(img, 0, Blitz::Low);
357 switch (m_effectIndex) {
359 img = Blitz::contrast(img, true, 6);
362 img = Blitz::edge(img);
365 img = Blitz::intensity(img, 0.5);
371 img = Blitz::threshold(img, 120, Blitz::Grayscale, qRgba(0, 0, 0, 0), qRgba(255, 0, 0, 255));
372 //img = Blitz::flatten(img, QColor(255, 0, 0, 255), QColor(0, 0, 0, 0));
379 m_bmCapture->showOverlay(img);
382 void StopmotionWidget::sequenceNameChanged(const QString &name)
384 // Get rid of frames from previous sequence
385 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
387 m_future.waitForFinished();
389 if (name.isEmpty()) {
390 button_addsequence->setEnabled(false);
392 // Check if we are editing an existing sequence
393 QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
394 m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
395 if (!m_filesList.isEmpty()) {
396 m_sequenceName = sequence_name->currentText();
397 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
398 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
399 button_addsequence->setEnabled(true);
402 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
403 button_addsequence->setEnabled(false);
405 capture_button->setEnabled(live_button->isChecked());
409 void StopmotionWidget::slotCaptureFrame()
411 if (sequence_name->currentText().isEmpty()) {
412 QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
413 if (seqName.isEmpty()) return;
414 sequence_name->blockSignals(true);
415 sequence_name->setItemText(sequence_name->currentIndex(), seqName);
416 sequence_name->blockSignals(false);
418 if (m_sequenceName != sequence_name->currentText()) {
419 m_sequenceName = sequence_name->currentText();
422 //capture_button->setEnabled(false);
423 QString currentPath = getPathForFrame(m_sequenceFrame);
424 m_bmCapture->captureFrame(currentPath);
425 KNotification::event("FrameCaptured");
427 button_addsequence->setEnabled(true);
428 if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
432 void StopmotionWidget::slotNewThumb(const QString path)
434 if (!KdenliveSettings::showstopmotionthumbs()) return;
435 m_filesList.append(path);
436 if (m_showOverlay->isChecked()) slotUpdateOverlay();
437 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
440 void StopmotionWidget::slotPrepareThumbs()
442 if (m_filesList.isEmpty()) return;
443 QString path = m_filesList.takeFirst();
444 emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
448 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
451 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
455 int width = height * img.width() / img.height();
456 frame_list->setIconSize(QSize(width, height));
457 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
458 QString nb = QString::number(ix);
460 QFontInfo finfo(font());
461 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
463 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
466 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
467 item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
468 item->setData(Qt::UserRole, ix);
469 frame_list->blockSignals(true);
470 frame_list->setCurrentItem(item);
471 frame_list->blockSignals(false);
472 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
475 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
477 if (seqName.isEmpty()) seqName = m_sequenceName;
478 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
481 void StopmotionWidget::slotShowFrame(const QString &path)
485 capture_button->setEnabled(false);
487 m_bmCapture->hidePreview(true);
488 m_frame_preview->setImage(img);
489 m_frame_preview->setHidden(false);
490 m_frame_preview->update();
494 void StopmotionWidget::slotShowSelectedFrame()
496 QListWidgetItem *item = frame_list->currentItem();
498 //int ix = item->data(Qt::UserRole).toInt();;
499 slotShowFrame(item->toolTip());
503 void StopmotionWidget::slotAddSequence()
505 emit addOrUpdateSequence(getPathForFrame(0));
508 void StopmotionWidget::slotPlayPreview(bool animate)
512 m_animationList.clear();
515 if (KdenliveSettings::showstopmotionthumbs()) {
516 frame_list->setCurrentRow(0);
517 QTimer::singleShot(200, this, SLOT(slotAnimate()));
519 SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
524 void StopmotionWidget::slotAnimate()
526 //slotShowFrame(m_animatedIndex);
527 if (KdenliveSettings::showstopmotionthumbs()) {
529 if (frame_list->currentRow() < (frame_list->count() - 1)) {
530 frame_list->setCurrentRow(frame_list->currentRow() + 1);
531 QTimer::singleShot(100, this, SLOT(slotAnimate()));
532 } else preview_button->setChecked(false);
533 } else if (!m_animationList.isEmpty()) {
534 slotShowFrame(m_animationList.takeFirst());
535 QTimer::singleShot(100, this, SLOT(slotAnimate()));
536 } else preview_button->setChecked(false);
540 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
542 QListWidgetItem *item = NULL;
544 if (ix >= frame_list->count()) {
545 pos = frame_list->count() - 1;
548 item = frame_list->item(pos);
550 int value = item->data(Qt::UserRole).toInt();
551 if (value == ix) return item;
552 else if (value < ix) {
554 while (pos < frame_list->count()) {
555 item = frame_list->item(pos);
556 value = item->data(Qt::UserRole).toInt();
557 if (value == ix) return item;
563 item = frame_list->item(pos);
564 value = item->data(Qt::UserRole).toInt();
565 if (value == ix) return item;
573 void StopmotionWidget::selectFrame(int ix)
575 frame_list->blockSignals(true);
576 QListWidgetItem *item = getFrameFromIndex(ix);
578 frame_list->setCurrentItem(item);
579 frame_list->blockSignals(false);
582 void StopmotionWidget::slotSeekFrame(bool forward)
584 int ix = frame_list->currentRow();
586 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
587 } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
590 void StopmotionWidget::slotRemoveFrame()
592 if (frame_list->currentItem() == NULL) return;
593 QString path = frame_list->currentItem()->toolTip();
594 if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
597 QListWidgetItem *item = frame_list->takeItem(frame_list->currentRow());