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 QVBoxLayout *lay = new QVBoxLayout;
108 if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
109 // Found a BlackMagic device
110 kDebug()<<"CREATE BM DEVICE";
111 m_bmCapture = new BmdCaptureHandler(lay);
112 connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
115 kDebug()<<"CREATE V4L DEVICE";
116 m_bmCapture = new V4lCaptureHandler(lay);
117 capture_device->addItem(KdenliveSettings::video4vdevice());
119 m_frame_preview = new MyLabel(this);
120 connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
121 lay->addWidget(m_frame_preview);
122 m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
123 video_preview->setLayout(lay);
124 live_button->setChecked(false);
125 frameoverlay_button->setChecked(false);
126 button_addsequence->setEnabled(false);
127 connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
128 connect(frameoverlay_button, SIGNAL(clicked(bool)), this, SLOT(slotShowOverlay(bool)));
129 connect(frame_number, SIGNAL(valueChanged(int)), this, SLOT(slotShowFrame(int)));
130 connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
131 connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview()));
132 connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
133 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage,int)));
135 parseExistingSequences();
138 StopmotionWidget::~StopmotionWidget()
140 m_bmCapture->stopPreview();
143 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
145 log_box->insertItem(0, message);
148 void StopmotionWidget::parseExistingSequences()
150 sequence_name->clear();
151 sequence_name->addItem(QString());
152 QDir dir(m_projectFolder.path());
154 filters << "*_0000.png";
155 //dir.setNameFilters(filters);
156 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
157 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
158 foreach(QString sequencename, sequences) {
159 sequence_name->addItem(sequencename.section("_", 0, -2));
163 void StopmotionWidget::slotLive(bool isOn)
166 m_frame_preview->setImage(QImage());
167 m_frame_preview->setHidden(true);
168 m_bmCapture->startPreview(KdenliveSettings::hdmicapturedevice(), KdenliveSettings::hdmicapturemode());
169 capture_button->setEnabled(true);
172 m_bmCapture->stopPreview();
173 capture_button->setEnabled(false);
177 void StopmotionWidget::slotShowOverlay(bool isOn)
180 if (live_button->isChecked() && m_sequenceFrame > 0) {
185 m_bmCapture->hideOverlay();
189 void StopmotionWidget::slotUpdateOverlay()
191 QString path = getPathForFrame(m_sequenceFrame - 1);
192 if (!QFile::exists(path)) return;
195 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
198 m_bmCapture->showOverlay(img);
201 void StopmotionWidget::sequenceNameChanged(const QString &name)
203 // Get rid of frames from previous sequence
204 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage,int)));
206 m_future.waitForFinished();
208 if (name.isEmpty()) {
209 button_addsequence->setEnabled(false);
210 frame_number->blockSignals(true);
211 frame_number->setValue(m_sequenceFrame);
212 frame_number->blockSignals(false);
213 frameoverlay_button->setEnabled(false);
214 removelast_button->setEnabled(false);
217 // Check if we are editing an existing sequence
219 QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &count);
220 m_sequenceFrame = count;
221 frame_number->blockSignals(true);
222 frame_number->setValue(0);
223 frame_number->blockSignals(false);
226 m_sequenceName = sequence_name->currentText();
227 //TODO: Do the thumbnail stuff in a thread
228 for (int i = 0; i < count; i++) {
229 //slotUpdateFrameList(i);
230 m_filesList.append(getPathForFrame(i));
232 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage,int)));
233 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
234 button_addsequence->setEnabled(true);
235 frameoverlay_button->setEnabled(true);
238 button_addsequence->setEnabled(false);
239 frameoverlay_button->setEnabled(false);
241 frame_number->setRange(0, m_sequenceFrame);
242 capture_button->setEnabled(true);
246 void StopmotionWidget::slotCaptureFrame()
248 if (sequence_name->currentText().isEmpty()) {
249 QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
250 if (seqName.isEmpty()) return;
251 sequence_name->blockSignals(true);
252 sequence_name->setItemText(sequence_name->currentIndex(), seqName);
253 sequence_name->blockSignals(false);
255 if (m_sequenceName != sequence_name->currentText()) {
256 m_sequenceName = sequence_name->currentText();
259 capture_button->setEnabled(false);
260 m_bmCapture->captureFrame(getPathForFrame(m_sequenceFrame));
261 KNotification::event("FrameCaptured");
262 frameoverlay_button->setEnabled(true);
264 frame_number->setRange(0, m_sequenceFrame);
265 frame_number->blockSignals(true);
266 frame_number->setValue(m_sequenceFrame);
267 frame_number->blockSignals(false);
268 button_addsequence->setEnabled(true);
269 //if (frameoverlay_button->isChecked()) QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
270 QTimer::singleShot(1000, this, SLOT(slotUpdateFrameList()));
273 void StopmotionWidget::slotPrepareThumbs()
275 if (m_filesList.isEmpty()) return;
276 QString path = m_filesList.takeFirst();
277 emit doCreateThumbs(QImage(path), m_currentIndex++);
281 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
284 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
288 int width = height * img.width() / img.height();
289 frame_list->setIconSize(QSize(width, height));
290 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
291 QString nb = QString::number(ix);
293 QFontInfo finfo(font());
294 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
296 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
299 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
300 item->setData(Qt::UserRole, ix);
301 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
304 void StopmotionWidget::slotUpdateFrameList(int ix)
306 kDebug()<< "// GET FRAME: "<<ix;
307 if (ix == -1) ix = m_sequenceFrame - 1;
308 QString path = getPathForFrame(ix);
309 if (!QFile::exists(path)) {
310 capture_button->setEnabled(true);
315 if (ix == m_sequenceFrame - 1) QTimer::singleShot(1000, this, SLOT(slotUpdateFrameList()));
319 int width = height * img.width() / img.height();
320 frame_list->setIconSize(QSize(width, height));
321 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
322 QString nb = QString::number(ix);
324 QFontInfo finfo(font());
325 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
327 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
330 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
331 item->setData(Qt::UserRole, ix);
332 capture_button->setEnabled(true);
335 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
337 if (seqName.isEmpty()) seqName = m_sequenceName;
338 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
341 void StopmotionWidget::slotShowFrame(int ix)
343 if (m_sequenceFrame == 0) {
344 //there are no images in sequence
347 frameoverlay_button->blockSignals(true);
348 frameoverlay_button->setChecked(false);
349 frameoverlay_button->blockSignals(false);
350 if (ix < m_sequenceFrame) {
351 // Show previous frame
353 live_button->setChecked(false);
354 QImage img(getPathForFrame(ix));
355 capture_button->setEnabled(false);
357 //m_bmCapture->showOverlay(img, false);
358 m_bmCapture->hidePreview(true);
359 m_frame_preview->setImage(img);
360 m_frame_preview->setHidden(false);
361 m_frame_preview->update();
367 m_frame_preview->setImage(QImage());
368 m_frame_preview->setHidden(true);
369 m_bmCapture->hideOverlay();
370 m_bmCapture->hidePreview(false);
371 capture_button->setEnabled(true);
375 void StopmotionWidget::slotShowSelectedFrame()
377 QListWidgetItem *item = frame_list->currentItem();
379 int ix = item->data(Qt::UserRole).toInt();
380 //frame_number->blockSignals(true);
381 frame_number->setValue(ix);
382 //frame_number->blockSignals(false);
387 void StopmotionWidget::slotAddSequence()
389 emit addOrUpdateSequence(getPathForFrame(0));
392 void StopmotionWidget::slotPlayPreview()
394 if (m_animatedIndex != -1) {
396 m_animatedIndex = -1;
399 QListWidgetItem *item = frame_list->currentItem();
401 m_animatedIndex = item->data(Qt::UserRole).toInt();
403 QTimer::singleShot(200, this, SLOT(slotAnimate()));
406 void StopmotionWidget::slotAnimate()
408 slotShowFrame(m_animatedIndex);
410 if (m_animatedIndex < m_sequenceFrame) QTimer::singleShot(200, this, SLOT(slotAnimate()));
411 else m_animatedIndex = -1;
414 void StopmotionWidget::selectFrame(int ix)
416 frame_list->blockSignals(true);
417 QListWidgetItem *item = frame_list->item(ix);
418 int current = item->data(Qt::UserRole).toInt();
420 frame_list->setCurrentItem(item);
422 else if (current < ix) {
423 for (int i = ix; i < frame_list->count(); i++) {
424 item = frame_list->item(i);
425 current = item->data(Qt::UserRole).toInt();
427 frame_list->setCurrentItem(item);
433 for (int i = ix; i >= 0; i--) {
434 item = frame_list->item(i);
435 current = item->data(Qt::UserRole).toInt();
437 frame_list->setCurrentItem(item);
442 frame_list->blockSignals(false);
445 void StopmotionWidget::slotSeekFrame(bool forward)
447 int ix = frame_list->currentRow();
449 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
451 else if (ix > 0) frame_list->setCurrentRow(ix - 1);