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(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()));
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 live_button->setChecked(false);
225 QDialog::closeEvent(e);
228 void StopmotionWidget::slotSetCaptureInterval()
230 int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
231 if(interval > 0 && interval != KdenliveSettings::captureinterval())
232 KdenliveSettings::setCaptureinterval(interval);
235 void StopmotionWidget::slotShowThumbs(bool show)
237 KdenliveSettings::setShowstopmotionthumbs(show);
238 kDebug() << "SHOW THUMBS: " << show;
241 sequenceNameChanged(sequence_name->currentText());
246 frame_list->setHidden(!show);
249 void StopmotionWidget::slotIntervalCapture(bool capture)
251 if(capture) slotCaptureFrame();
255 void StopmotionWidget::slotUpdateHandler()
257 QString data = capture_device->itemData(capture_device->currentIndex()).toString();
258 m_bmCapture->stopPreview();
260 m_layout->removeWidget(m_frame_preview);
262 m_bmCapture = new V4lCaptureHandler(m_layout);
264 m_bmCapture = new BmdCaptureHandler(m_layout);
265 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
267 m_layout->addWidget(m_frame_preview);
270 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
272 log_box->insertItem(0, message);
275 void StopmotionWidget::parseExistingSequences()
277 sequence_name->clear();
278 sequence_name->addItem(QString());
279 QDir dir(m_projectFolder.path());
281 filters << "*_0000.png";
282 //dir.setNameFilters(filters);
283 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
284 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
285 foreach(QString sequencename, sequences) {
286 sequence_name->addItem(sequencename.section("_", 0, -2));
290 void StopmotionWidget::slotLive(bool isOn)
293 m_frame_preview->setImage(QImage());
294 m_frame_preview->setHidden(true);
295 m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode());
296 capture_button->setEnabled(true);
298 m_bmCapture->stopPreview();
299 capture_button->setEnabled(false);
303 void StopmotionWidget::slotShowOverlay(bool isOn)
306 if(live_button->isChecked() && m_sequenceFrame > 0) {
310 m_bmCapture->hideOverlay();
314 void StopmotionWidget::slotUpdateOverlay()
316 QString path = getPathForFrame(m_sequenceFrame - 1);
317 if(!QFile::exists(path)) return;
320 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
325 //img = Blitz::convolveEdge(img, 0, Blitz::Low);
326 switch(m_effectIndex) {
328 img = Blitz::contrast(img, true);
331 img = Blitz::edge(img);
334 img = Blitz::intensity(img, 0.5);
340 img = Blitz::threshold(img, 200, Blitz::Grayscale, qRgba(255, 0, 0, 255), qRgba(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();
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(int ix)
449 if(m_sequenceFrame == 0) {
450 //there are no images in sequence
453 if(ix < m_sequenceFrame) {
454 // Show previous frame
456 live_button->setChecked(false);
457 QImage img(getPathForFrame(ix));
458 capture_button->setEnabled(false);
460 //m_bmCapture->showOverlay(img, false);
461 m_bmCapture->hidePreview(true);
462 m_frame_preview->setImage(img);
463 m_frame_preview->setHidden(false);
464 m_frame_preview->update();
470 m_frame_preview->setImage(QImage());
471 m_frame_preview->setHidden(true);
472 m_bmCapture->hideOverlay();
473 m_bmCapture->hidePreview(false);
474 capture_button->setEnabled(true);
478 void StopmotionWidget::slotShowSelectedFrame()
480 QListWidgetItem *item = frame_list->currentItem();
482 int ix = item->data(Qt::UserRole).toInt();;
487 void StopmotionWidget::slotAddSequence()
489 emit addOrUpdateSequence(getPathForFrame(0));
492 void StopmotionWidget::slotPlayPreview()
494 if(m_animatedIndex != -1) {
496 m_animatedIndex = -1;
499 QListWidgetItem *item = frame_list->currentItem();
501 m_animatedIndex = item->data(Qt::UserRole).toInt();
503 QTimer::singleShot(200, this, SLOT(slotAnimate()));
506 void StopmotionWidget::slotAnimate()
508 slotShowFrame(m_animatedIndex);
510 if(m_animatedIndex < m_sequenceFrame) QTimer::singleShot(200, this, SLOT(slotAnimate()));
511 else m_animatedIndex = -1;
514 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
516 QListWidgetItem *item = NULL;
518 if(ix >= frame_list->count()) {
519 pos = frame_list->count() - 1;
522 item = frame_list->item(pos);
524 int value = item->data(Qt::UserRole).toInt();
525 if(value == ix) return item;
526 else if(value < ix) {
528 while(pos < frame_list->count()) {
529 item = frame_list->item(pos);
530 value = item->data(Qt::UserRole).toInt();
531 if(value == ix) return item;
537 item = frame_list->item(pos);
538 value = item->data(Qt::UserRole).toInt();
539 if(value == ix) return item;
547 void StopmotionWidget::selectFrame(int ix)
549 frame_list->blockSignals(true);
550 QListWidgetItem *item = getFrameFromIndex(ix);
552 frame_list->setCurrentItem(item);
553 frame_list->blockSignals(false);
556 void StopmotionWidget::slotSeekFrame(bool forward)
558 int ix = frame_list->currentRow();
560 if(ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
561 } else if(ix > 0) frame_list->setCurrentRow(ix - 1);