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>
33 #include <QtConcurrentRun>
34 #include <QInputDialog>
36 #include <QVBoxLayout>
40 #include <QWheelEvent>
42 MyLabel::MyLabel(QWidget *parent) :
47 void MyLabel::setImage(QImage img)
53 void MyLabel::wheelEvent(QWheelEvent * event)
55 if (event->delta() > 0) emit seek(true);
56 else emit seek(false);
60 void MyLabel::paintEvent(QPaintEvent * event)
64 QRect r(0, 0, width(), height());
66 p.fillRect(r, QColor(KdenliveSettings::window_background()));
67 double aspect_ratio = (double) m_img.width() / m_img.height();
68 int pictureHeight = height();
69 int pictureWidth = width();
70 int calculatedWidth = aspect_ratio * height();
71 if (calculatedWidth > width()) pictureHeight = width() / aspect_ratio;
73 int calculatedHeight = width() / aspect_ratio;
74 if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
76 p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
81 StopmotionWidget::StopmotionWidget(KUrl projectFolder, QWidget *parent) :
84 , m_projectFolder(projectFolder)
90 setWindowTitle(i18n("Stop Motion Capture"));
91 setFont(KGlobalSettings::toolBarFont());
93 live_button->setIcon(KIcon("camera-photo"));
94 frameoverlay_button->setIcon(KIcon("edit-paste"));
95 m_captureAction = new QAction(KIcon("media-record"), i18n("Capture frame"), this);
96 m_captureAction->setShortcut(QKeySequence(Qt::Key_Space));
97 connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
98 capture_button->setDefaultAction(m_captureAction);
100 preview_button->setIcon(KIcon("media-playback-start"));
101 removelast_button->setIcon(KIcon("edit-delete"));
102 frameoverlay_button->setEnabled(false);
103 removelast_button->setEnabled(false);
104 capture_button->setEnabled(false);
106 connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
107 m_layout = new QVBoxLayout;
108 if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
109 // Found a BlackMagic device
110 m_bmCapture = new BmdCaptureHandler(m_layout);
111 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
113 if (QFile::exists(KdenliveSettings::video4vdevice())) {
114 if (m_bmCapture == NULL) m_bmCapture = new V4lCaptureHandler(m_layout);
115 capture_device->addItem(KdenliveSettings::video4vdevice(), "v4l");
117 connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
118 m_frame_preview = new MyLabel(this);
119 connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
120 m_layout->addWidget(m_frame_preview);
121 m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
122 video_preview->setLayout(m_layout);
123 live_button->setChecked(false);
124 frameoverlay_button->setChecked(false);
125 button_addsequence->setEnabled(false);
126 connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
127 connect(frameoverlay_button, SIGNAL(clicked(bool)), this, SLOT(slotShowOverlay(bool)));
128 connect(frame_number, SIGNAL(valueChanged(int)), this, SLOT(slotShowFrame(int)));
129 connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
130 connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview()));
131 connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
132 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
134 parseExistingSequences();
137 StopmotionWidget::~StopmotionWidget()
139 m_bmCapture->stopPreview();
142 void StopmotionWidget::slotUpdateHandler()
144 QString data = capture_device->itemData(capture_device->currentIndex()).toString();
145 m_bmCapture->stopPreview();
147 m_layout->removeWidget(m_frame_preview);
149 m_bmCapture = new V4lCaptureHandler(m_layout);
151 m_bmCapture = new BmdCaptureHandler(m_layout);
152 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
154 m_layout->addWidget(m_frame_preview);
157 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
159 log_box->insertItem(0, message);
162 void StopmotionWidget::parseExistingSequences()
164 sequence_name->clear();
165 sequence_name->addItem(QString());
166 QDir dir(m_projectFolder.path());
168 filters << "*_0000.png";
169 //dir.setNameFilters(filters);
170 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
171 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
172 foreach(QString sequencename, sequences) {
173 sequence_name->addItem(sequencename.section("_", 0, -2));
177 void StopmotionWidget::slotLive(bool isOn)
180 m_frame_preview->setImage(QImage());
181 m_frame_preview->setHidden(true);
182 m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode());
183 capture_button->setEnabled(true);
185 m_bmCapture->stopPreview();
186 capture_button->setEnabled(false);
190 void StopmotionWidget::slotShowOverlay(bool isOn)
193 if (live_button->isChecked() && m_sequenceFrame > 0) {
197 m_bmCapture->hideOverlay();
201 void StopmotionWidget::slotUpdateOverlay()
203 QString path = getPathForFrame(m_sequenceFrame - 1);
204 if (!QFile::exists(path)) return;
207 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
210 m_bmCapture->showOverlay(img);
213 void StopmotionWidget::sequenceNameChanged(const QString &name)
215 // Get rid of frames from previous sequence
216 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
218 m_future.waitForFinished();
220 if (name.isEmpty()) {
221 button_addsequence->setEnabled(false);
222 frame_number->blockSignals(true);
223 frame_number->setValue(m_sequenceFrame);
224 frame_number->blockSignals(false);
225 frameoverlay_button->setEnabled(false);
226 removelast_button->setEnabled(false);
228 // Check if we are editing an existing sequence
229 QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
230 m_sequenceFrame = m_filesList.size();
231 frame_number->blockSignals(true);
232 frame_number->setValue(0);
233 frame_number->blockSignals(false);
235 if (!m_filesList.isEmpty()) {
236 m_sequenceName = sequence_name->currentText();
237 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
238 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
239 button_addsequence->setEnabled(true);
240 frameoverlay_button->setEnabled(true);
242 button_addsequence->setEnabled(false);
243 frameoverlay_button->setEnabled(false);
245 frame_number->setRange(0, m_sequenceFrame);
246 capture_button->setEnabled(true);
250 void StopmotionWidget::slotCaptureFrame()
252 if (sequence_name->currentText().isEmpty()) {
253 QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
254 if (seqName.isEmpty()) return;
255 sequence_name->blockSignals(true);
256 sequence_name->setItemText(sequence_name->currentIndex(), seqName);
257 sequence_name->blockSignals(false);
259 if (m_sequenceName != sequence_name->currentText()) {
260 m_sequenceName = sequence_name->currentText();
263 capture_button->setEnabled(false);
264 m_bmCapture->captureFrame(getPathForFrame(m_sequenceFrame));
265 KNotification::event("FrameCaptured");
266 frameoverlay_button->setEnabled(true);
268 frame_number->setRange(0, m_sequenceFrame);
269 frame_number->blockSignals(true);
270 frame_number->setValue(m_sequenceFrame);
271 frame_number->blockSignals(false);
272 button_addsequence->setEnabled(true);
273 //if (frameoverlay_button->isChecked()) QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
274 QTimer::singleShot(1000, this, SLOT(slotUpdateFrameList()));
277 void StopmotionWidget::slotPrepareThumbs()
279 if (m_filesList.isEmpty()) return;
280 QString path = m_filesList.takeFirst();
281 emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
285 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
288 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
292 int width = height * img.width() / img.height();
293 frame_list->setIconSize(QSize(width, height));
294 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
295 QString nb = QString::number(ix);
297 QFontInfo finfo(font());
298 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
300 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
303 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
304 item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
305 item->setData(Qt::UserRole, ix);
306 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
309 void StopmotionWidget::slotUpdateFrameList(int ix)
311 kDebug() << "// GET FRAME: " << ix;
312 if (ix == -1) ix = m_sequenceFrame - 1;
313 QString path = getPathForFrame(ix);
314 if (!QFile::exists(path)) {
315 capture_button->setEnabled(true);
320 if (ix == m_sequenceFrame - 1) QTimer::singleShot(1000, this, SLOT(slotUpdateFrameList()));
324 int width = height * img.width() / img.height();
325 frame_list->setIconSize(QSize(width, height));
326 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
327 QString nb = QString::number(ix);
329 QFontInfo finfo(font());
330 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
332 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
335 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
336 item->setData(Qt::UserRole, ix);
337 frame_list->blockSignals(true);
338 frame_list->setCurrentItem(item);
339 frame_list->blockSignals(true);
340 capture_button->setEnabled(true);
343 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
345 if (seqName.isEmpty()) seqName = m_sequenceName;
346 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
349 void StopmotionWidget::slotShowFrame(int ix)
351 if (m_sequenceFrame == 0) {
352 //there are no images in sequence
355 frameoverlay_button->blockSignals(true);
356 frameoverlay_button->setChecked(false);
357 frameoverlay_button->blockSignals(false);
358 if (ix < m_sequenceFrame) {
359 // Show previous frame
361 live_button->setChecked(false);
362 QImage img(getPathForFrame(ix));
363 capture_button->setEnabled(false);
365 //m_bmCapture->showOverlay(img, false);
366 m_bmCapture->hidePreview(true);
367 m_frame_preview->setImage(img);
368 m_frame_preview->setHidden(false);
369 m_frame_preview->update();
375 m_frame_preview->setImage(QImage());
376 m_frame_preview->setHidden(true);
377 m_bmCapture->hideOverlay();
378 m_bmCapture->hidePreview(false);
379 capture_button->setEnabled(true);
383 void StopmotionWidget::slotShowSelectedFrame()
385 QListWidgetItem *item = frame_list->currentItem();
387 int ix = item->data(Qt::UserRole).toInt();
388 //frame_number->blockSignals(true);
389 frame_number->setValue(ix);
390 //frame_number->blockSignals(false);
395 void StopmotionWidget::slotAddSequence()
397 emit addOrUpdateSequence(getPathForFrame(0));
400 void StopmotionWidget::slotPlayPreview()
402 if (m_animatedIndex != -1) {
404 m_animatedIndex = -1;
407 QListWidgetItem *item = frame_list->currentItem();
409 m_animatedIndex = item->data(Qt::UserRole).toInt();
411 QTimer::singleShot(200, this, SLOT(slotAnimate()));
414 void StopmotionWidget::slotAnimate()
416 slotShowFrame(m_animatedIndex);
418 if (m_animatedIndex < m_sequenceFrame) QTimer::singleShot(200, this, SLOT(slotAnimate()));
419 else m_animatedIndex = -1;
422 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
424 QListWidgetItem *item = NULL;
426 if (ix >= frame_list->count()) {
427 pos = frame_list->count() - 1;
430 item = frame_list->item(pos);
432 int value = item->data(Qt::UserRole).toInt();
433 if (value == ix) return item;
434 else if (value < ix) {
436 while (pos < frame_list->count()) {
437 item = frame_list->item(pos);
438 value = item->data(Qt::UserRole).toInt();
439 if (value == ix) return item;
445 item = frame_list->item(pos);
446 value = item->data(Qt::UserRole).toInt();
447 if (value == ix) return item;
455 void StopmotionWidget::selectFrame(int ix)
457 frame_list->blockSignals(true);
458 QListWidgetItem *item = getFrameFromIndex(ix);
460 frame_list->setCurrentItem(item);
461 frame_list->blockSignals(false);
464 void StopmotionWidget::slotSeekFrame(bool forward)
466 int ix = frame_list->currentRow();
468 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
469 } else if (ix > 0) frame_list->setCurrentRow(ix - 1);