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())) {
193 if (m_bmCapture == NULL) m_bmCapture = new V4lCaptureHandler(m_layout);
194 capture_device->addItem(m_bmCapture->getDeviceName(KdenliveSettings::video4vdevice(), 0, 0), "v4l");
197 connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
198 connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
199 m_frame_preview = new MyLabel(this);
200 connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
201 connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
202 m_layout->addWidget(m_frame_preview);
203 m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
204 video_preview->setLayout(m_layout);
205 live_button->setChecked(false);
206 button_addsequence->setEnabled(false);
207 connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
208 connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
209 connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
210 connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
211 connect(frame_list, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slotShowSelectedFrame()));
212 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
214 frame_list->addAction(removeCurrent);
215 frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
216 frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
217 parseExistingSequences();
220 StopmotionWidget::~StopmotionWidget()
222 m_bmCapture->stopPreview();
225 void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
228 if (act) m_effectIndex = act->data().toInt();
229 KdenliveSettings::setBlitzeffect(m_effectIndex);
230 if (m_showOverlay->isChecked()) slotUpdateOverlay();
234 void StopmotionWidget::closeEvent(QCloseEvent *e)
237 QDialog::closeEvent(e);
240 void StopmotionWidget::slotSetCaptureInterval()
242 int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
243 if (interval > 0 && interval != KdenliveSettings::captureinterval())
244 KdenliveSettings::setCaptureinterval(interval);
247 void StopmotionWidget::slotShowThumbs(bool show)
249 KdenliveSettings::setShowstopmotionthumbs(show);
252 sequenceNameChanged(sequence_name->currentText());
257 frame_list->setHidden(!show);
260 void StopmotionWidget::slotIntervalCapture(bool capture)
262 if (capture) slotCaptureFrame();
266 void StopmotionWidget::slotUpdateHandler()
268 QString data = capture_device->itemData(capture_device->currentIndex()).toString();
269 m_bmCapture->stopPreview();
271 m_layout->removeWidget(m_frame_preview);
273 m_bmCapture = new V4lCaptureHandler(m_layout);
275 m_bmCapture = new BmdCaptureHandler(m_layout);
276 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
278 m_layout->addWidget(m_frame_preview);
281 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
283 log_box->insertItem(0, message);
286 void StopmotionWidget::parseExistingSequences()
288 sequence_name->clear();
289 sequence_name->addItem(QString());
290 QDir dir(m_projectFolder.path());
292 filters << "*_0000.png";
293 //dir.setNameFilters(filters);
294 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
295 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
296 foreach(QString sequencename, sequences) {
297 sequence_name->addItem(sequencename.section("_", 0, -2));
301 void StopmotionWidget::slotSwitchLive()
303 setUpdatesEnabled(false);
304 if (m_frame_preview->isHidden()) {
305 m_bmCapture->hidePreview(true);
306 m_frame_preview->setHidden(false);
308 m_frame_preview->setHidden(true);
309 m_bmCapture->hidePreview(false);
311 setUpdatesEnabled(true);
314 void StopmotionWidget::slotLive(bool isOn)
317 //m_frame_preview->setImage(QImage());
318 m_frame_preview->setHidden(true);
319 m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
320 capture_button->setEnabled(true);
322 m_bmCapture->stopPreview();
323 m_frame_preview->setHidden(false);
324 capture_button->setEnabled(false);
325 live_button->setChecked(false);
329 void StopmotionWidget::slotShowOverlay(bool isOn)
332 if (live_button->isChecked() && m_sequenceFrame > 0) {
336 m_bmCapture->hideOverlay();
340 void StopmotionWidget::slotUpdateOverlay()
342 QString path = getPathForFrame(m_sequenceFrame - 1);
343 if (!QFile::exists(path)) return;
346 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
351 //img = Blitz::convolveEdge(img, 0, Blitz::Low);
352 switch (m_effectIndex) {
354 img = Blitz::contrast(img, true, 6);
357 img = Blitz::edge(img);
360 img = Blitz::intensity(img, 0.5);
366 img = Blitz::threshold(img, 120, Blitz::Grayscale, qRgba(0, 0, 0, 0), qRgba(255, 0, 0, 255));
367 //img = Blitz::flatten(img, QColor(255, 0, 0, 255), QColor(0, 0, 0, 0));
374 m_bmCapture->showOverlay(img);
377 void StopmotionWidget::sequenceNameChanged(const QString &name)
379 // Get rid of frames from previous sequence
380 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
382 m_future.waitForFinished();
384 if (name.isEmpty()) {
385 button_addsequence->setEnabled(false);
387 // Check if we are editing an existing sequence
388 QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
389 m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
390 if (!m_filesList.isEmpty()) {
391 m_sequenceName = sequence_name->currentText();
392 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
393 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
394 button_addsequence->setEnabled(true);
397 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
398 button_addsequence->setEnabled(false);
400 capture_button->setEnabled(live_button->isChecked());
404 void StopmotionWidget::slotCaptureFrame()
406 if (sequence_name->currentText().isEmpty()) {
407 QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
408 if (seqName.isEmpty()) return;
409 sequence_name->blockSignals(true);
410 sequence_name->setItemText(sequence_name->currentIndex(), seqName);
411 sequence_name->blockSignals(false);
413 if (m_sequenceName != sequence_name->currentText()) {
414 m_sequenceName = sequence_name->currentText();
417 //capture_button->setEnabled(false);
418 QString currentPath = getPathForFrame(m_sequenceFrame);
419 m_bmCapture->captureFrame(currentPath);
420 KNotification::event("FrameCaptured");
422 button_addsequence->setEnabled(true);
423 if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
427 void StopmotionWidget::slotNewThumb(const QString path)
429 if (!KdenliveSettings::showstopmotionthumbs()) return;
430 m_filesList.append(path);
431 if (m_showOverlay->isChecked()) slotUpdateOverlay();
432 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
435 void StopmotionWidget::slotPrepareThumbs()
437 if (m_filesList.isEmpty()) return;
438 QString path = m_filesList.takeFirst();
439 emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
443 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
446 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
450 int width = height * img.width() / img.height();
451 frame_list->setIconSize(QSize(width, height));
452 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
453 QString nb = QString::number(ix);
455 QFontInfo finfo(font());
456 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
458 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
461 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
462 item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
463 item->setData(Qt::UserRole, ix);
464 frame_list->blockSignals(true);
465 frame_list->setCurrentItem(item);
466 frame_list->blockSignals(false);
467 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
470 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
472 if (seqName.isEmpty()) seqName = m_sequenceName;
473 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
476 void StopmotionWidget::slotShowFrame(const QString &path)
480 capture_button->setEnabled(false);
482 m_bmCapture->hidePreview(true);
483 m_frame_preview->setImage(img);
484 m_frame_preview->setHidden(false);
485 m_frame_preview->update();
489 void StopmotionWidget::slotShowSelectedFrame()
491 QListWidgetItem *item = frame_list->currentItem();
493 //int ix = item->data(Qt::UserRole).toInt();;
494 slotShowFrame(item->toolTip());
498 void StopmotionWidget::slotAddSequence()
500 emit addOrUpdateSequence(getPathForFrame(0));
503 void StopmotionWidget::slotPlayPreview(bool animate)
507 m_animationList.clear();
510 if (KdenliveSettings::showstopmotionthumbs()) {
511 frame_list->setCurrentRow(0);
512 QTimer::singleShot(200, this, SLOT(slotAnimate()));
514 SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
519 void StopmotionWidget::slotAnimate()
521 //slotShowFrame(m_animatedIndex);
522 if (KdenliveSettings::showstopmotionthumbs()) {
524 if (frame_list->currentRow() < (frame_list->count() - 1)) {
525 frame_list->setCurrentRow(frame_list->currentRow() + 1);
526 QTimer::singleShot(100, this, SLOT(slotAnimate()));
527 } else preview_button->setChecked(false);
528 } else if (!m_animationList.isEmpty()) {
529 slotShowFrame(m_animationList.takeFirst());
530 QTimer::singleShot(100, this, SLOT(slotAnimate()));
531 } else preview_button->setChecked(false);
535 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
537 QListWidgetItem *item = NULL;
539 if (ix >= frame_list->count()) {
540 pos = frame_list->count() - 1;
543 item = frame_list->item(pos);
545 int value = item->data(Qt::UserRole).toInt();
546 if (value == ix) return item;
547 else if (value < ix) {
549 while (pos < frame_list->count()) {
550 item = frame_list->item(pos);
551 value = item->data(Qt::UserRole).toInt();
552 if (value == ix) return item;
558 item = frame_list->item(pos);
559 value = item->data(Qt::UserRole).toInt();
560 if (value == ix) return item;
568 void StopmotionWidget::selectFrame(int ix)
570 frame_list->blockSignals(true);
571 QListWidgetItem *item = getFrameFromIndex(ix);
573 frame_list->setCurrentItem(item);
574 frame_list->blockSignals(false);
577 void StopmotionWidget::slotSeekFrame(bool forward)
579 int ix = frame_list->currentRow();
581 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
582 } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
585 void StopmotionWidget::slotRemoveFrame()
587 if (frame_list->currentItem() == NULL) return;
588 QString path = frame_list->currentItem()->toolTip();
589 if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
592 QListWidgetItem *item = frame_list->takeItem(frame_list->currentRow());