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 "../slideshowclip.h"
21 #include "kdenlivesettings.h"
25 #include <KGlobalSettings>
26 #include <KFileDialog>
27 #include <KStandardDirs>
28 #include <KMessageBox>
29 #include <kdeversion.h>
30 #include <KNotification>
32 #include <QtConcurrentRun>
33 #include <QInputDialog>
35 #include <QVBoxLayout>
39 #include <QWheelEvent>
41 MyLabel::MyLabel(QWidget *parent) :
46 void MyLabel::setImage(QImage img)
52 void MyLabel::wheelEvent(QWheelEvent * event)
54 if (event->delta() > 0) emit seek(true);
55 else emit seek(false);
59 void MyLabel::paintEvent( QPaintEvent * event)
63 QRect r(0, 0, width(), height());
65 p.fillRect(r, QColor(KdenliveSettings::window_background()));
66 double aspect_ratio = (double) m_img.width() / m_img.height();
67 int pictureHeight = height();
68 int pictureWidth = width();
69 int calculatedWidth = aspect_ratio * height();
70 if (calculatedWidth > width()) pictureHeight = width() / aspect_ratio;
72 int calculatedHeight = width() / aspect_ratio;
73 if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
75 p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
80 StopmotionWidget::StopmotionWidget(KUrl projectFolder, QWidget *parent) :
83 , m_projectFolder(projectFolder)
88 setWindowTitle(i18n("Stop Motion Capture"));
89 setFont(KGlobalSettings::toolBarFont());
91 live_button->setIcon(KIcon("camera-photo"));
92 frameoverlay_button->setIcon(KIcon("edit-paste"));
93 m_captureAction = new QAction(KIcon("media-record"), i18n("Capture frame"), this);
94 m_captureAction->setShortcut(QKeySequence(Qt::Key_Space));
95 connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
96 capture_button->setDefaultAction(m_captureAction);
98 preview_button->setIcon(KIcon("media-playback-start"));
99 removelast_button->setIcon(KIcon("edit-delete"));
100 frameoverlay_button->setEnabled(false);
101 removelast_button->setEnabled(false);
102 capture_button->setEnabled(false);
104 connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
105 BMInterface::getBlackMagicDeviceList(capture_device, NULL);
106 QVBoxLayout *lay = new QVBoxLayout;
107 m_bmCapture = new CaptureHandler(lay);
108 m_frame_preview = new MyLabel(this);
109 connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
110 lay->addWidget(m_frame_preview);
111 m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
112 video_preview->setLayout(lay);
113 live_button->setChecked(false);
114 frameoverlay_button->setChecked(false);
115 button_addsequence->setEnabled(false);
116 connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
117 connect(frameoverlay_button, SIGNAL(clicked(bool)), this, SLOT(slotShowOverlay(bool)));
118 connect(frame_number, SIGNAL(valueChanged(int)), this, SLOT(slotShowFrame(int)));
119 connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
120 connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview()));
121 connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
122 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage,int)));
124 parseExistingSequences();
127 StopmotionWidget::~StopmotionWidget()
129 m_bmCapture->stopPreview();
132 void StopmotionWidget::parseExistingSequences()
134 sequence_name->clear();
135 sequence_name->addItem(QString());
136 QDir dir(m_projectFolder.path());
138 filters << "*_0000.png";
139 //dir.setNameFilters(filters);
140 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
141 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
142 foreach(QString sequencename, sequences) {
143 sequence_name->addItem(sequencename.section("_", 0, -2));
147 void StopmotionWidget::slotLive(bool isOn)
150 m_frame_preview->setImage(QImage());
151 m_frame_preview->setHidden(true);
152 m_bmCapture->startPreview(KdenliveSettings::hdmicapturedevice(), KdenliveSettings::hdmicapturemode());
153 capture_button->setEnabled(true);
156 m_bmCapture->stopPreview();
157 capture_button->setEnabled(false);
161 void StopmotionWidget::slotShowOverlay(bool isOn)
164 if (live_button->isChecked() && m_sequenceFrame > 0) {
169 m_bmCapture->hideOverlay();
173 void StopmotionWidget::slotUpdateOverlay()
175 QString path = getPathForFrame(m_sequenceFrame - 1);
176 if (!QFile::exists(path)) return;
179 QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
182 m_bmCapture->showOverlay(img);
185 void StopmotionWidget::sequenceNameChanged(const QString &name)
187 // Get rid of frames from previous sequence
188 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage,int)));
190 m_future.waitForFinished();
192 if (name.isEmpty()) {
193 button_addsequence->setEnabled(false);
194 frame_number->blockSignals(true);
195 frame_number->setValue(m_sequenceFrame);
196 frame_number->blockSignals(false);
197 frameoverlay_button->setEnabled(false);
198 removelast_button->setEnabled(false);
201 // Check if we are editing an existing sequence
203 QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &count);
204 m_sequenceFrame = count;
205 frame_number->blockSignals(true);
206 frame_number->setValue(0);
207 frame_number->blockSignals(false);
210 m_sequenceName = sequence_name->currentText();
211 //TODO: Do the thumbnail stuff in a thread
212 for (int i = 0; i < count; i++) {
213 //slotUpdateFrameList(i);
214 m_filesList.append(getPathForFrame(i));
216 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage,int)));
217 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
218 button_addsequence->setEnabled(true);
219 frameoverlay_button->setEnabled(true);
222 button_addsequence->setEnabled(false);
223 frameoverlay_button->setEnabled(false);
225 frame_number->setRange(0, m_sequenceFrame);
226 capture_button->setEnabled(true);
230 void StopmotionWidget::slotCaptureFrame()
232 if (sequence_name->currentText().isEmpty()) {
233 QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
234 if (seqName.isEmpty()) return;
235 sequence_name->blockSignals(true);
236 sequence_name->setItemText(sequence_name->currentIndex(), seqName);
237 sequence_name->blockSignals(false);
239 if (m_sequenceName != sequence_name->currentText()) {
240 m_sequenceName = sequence_name->currentText();
243 capture_button->setEnabled(false);
244 m_bmCapture->captureFrame(getPathForFrame(m_sequenceFrame));
245 KNotification::event("FrameCaptured");
246 frameoverlay_button->setEnabled(true);
248 frame_number->setRange(0, m_sequenceFrame);
249 frame_number->blockSignals(true);
250 frame_number->setValue(m_sequenceFrame);
251 frame_number->blockSignals(false);
252 button_addsequence->setEnabled(true);
253 //if (frameoverlay_button->isChecked()) QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
254 QTimer::singleShot(1000, this, SLOT(slotUpdateFrameList()));
257 void StopmotionWidget::slotPrepareThumbs()
259 if (m_filesList.isEmpty()) return;
260 QString path = m_filesList.takeFirst();
261 emit doCreateThumbs(QImage(path), m_currentIndex++);
265 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
268 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
272 int width = height * img.width() / img.height();
273 frame_list->setIconSize(QSize(width, height));
274 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
275 QString nb = QString::number(ix);
277 QFontInfo finfo(font());
278 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
280 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
283 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
284 item->setData(Qt::UserRole, ix);
285 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
288 void StopmotionWidget::slotUpdateFrameList(int ix)
290 kDebug()<< "// GET FRAME: "<<ix;
291 if (ix == -1) ix = m_sequenceFrame - 1;
292 QString path = getPathForFrame(ix);
293 if (!QFile::exists(path)) {
294 capture_button->setEnabled(true);
299 if (ix == m_sequenceFrame - 1) QTimer::singleShot(1000, this, SLOT(slotUpdateFrameList()));
303 int width = height * img.width() / img.height();
304 frame_list->setIconSize(QSize(width, height));
305 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
306 QString nb = QString::number(ix);
308 QFontInfo finfo(font());
309 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
311 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
314 QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
315 item->setData(Qt::UserRole, ix);
316 capture_button->setEnabled(true);
319 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
321 if (seqName.isEmpty()) seqName = m_sequenceName;
322 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
325 void StopmotionWidget::slotShowFrame(int ix)
327 if (m_sequenceFrame == 0) {
328 //there are no images in sequence
331 frameoverlay_button->blockSignals(true);
332 frameoverlay_button->setChecked(false);
333 frameoverlay_button->blockSignals(false);
334 if (ix < m_sequenceFrame) {
335 // Show previous frame
337 live_button->setChecked(false);
338 QImage img(getPathForFrame(ix));
339 capture_button->setEnabled(false);
341 //m_bmCapture->showOverlay(img, false);
342 m_bmCapture->hidePreview(true);
343 m_frame_preview->setImage(img);
344 m_frame_preview->setHidden(false);
345 m_frame_preview->update();
351 m_frame_preview->setImage(QImage());
352 m_frame_preview->setHidden(true);
353 m_bmCapture->hideOverlay();
354 m_bmCapture->hidePreview(false);
355 capture_button->setEnabled(true);
359 void StopmotionWidget::slotShowSelectedFrame()
361 QListWidgetItem *item = frame_list->currentItem();
363 int ix = item->data(Qt::UserRole).toInt();
364 //frame_number->blockSignals(true);
365 frame_number->setValue(ix);
366 //frame_number->blockSignals(false);
371 void StopmotionWidget::slotAddSequence()
373 emit addOrUpdateSequence(getPathForFrame(0));
376 void StopmotionWidget::slotPlayPreview()
378 if (m_animatedIndex != -1) {
380 m_animatedIndex = -1;
383 QListWidgetItem *item = frame_list->currentItem();
385 m_animatedIndex = item->data(Qt::UserRole).toInt();
387 QTimer::singleShot(200, this, SLOT(slotAnimate()));
390 void StopmotionWidget::slotAnimate()
392 slotShowFrame(m_animatedIndex);
394 if (m_animatedIndex < m_sequenceFrame) QTimer::singleShot(200, this, SLOT(slotAnimate()));
395 else m_animatedIndex = -1;
398 void StopmotionWidget::selectFrame(int ix)
400 frame_list->blockSignals(true);
401 QListWidgetItem *item = frame_list->item(ix);
402 int current = item->data(Qt::UserRole).toInt();
404 frame_list->setCurrentItem(item);
406 else if (current < ix) {
407 for (int i = ix; i < frame_list->count(); i++) {
408 item = frame_list->item(i);
409 current = item->data(Qt::UserRole).toInt();
411 frame_list->setCurrentItem(item);
417 for (int i = ix; i >= 0; i--) {
418 item = frame_list->item(i);
419 current = item->data(Qt::UserRole).toInt();
421 frame_list->setCurrentItem(item);
426 frame_list->blockSignals(false);
429 void StopmotionWidget::slotSeekFrame(bool forward)
431 int ix = frame_list->currentRow();
433 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
435 else if (ix > 0) frame_list->setCurrentRow(ix - 1);