]> git.sesse.net Git - kdenlive/blob - src/stopmotion/stopmotion.cpp
Progress on stopmotion widget (make it possible to switch between HDMI and V4L)
[kdenlive] / src / stopmotion / stopmotion.cpp
1 /***************************************************************************
2                           stopmotion.cpp  -  description
3                              -------------------
4     begin                : Feb 28 2008
5     copyright            : (C) 2010 by Jean-Baptiste Mardelle
6     email                : jb@kdenlive.org
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "stopmotion.h"
19 #include "../blackmagic/devices.h"
20 #include "../v4l/v4lcapture.h"
21 #include "../slideshowclip.h"
22 #include "kdenlivesettings.h"
23
24
25 #include <KDebug>
26 #include <KGlobalSettings>
27 #include <KFileDialog>
28 #include <KStandardDirs>
29 #include <KMessageBox>
30 #include <kdeversion.h>
31 #include <KNotification>
32
33 #include <QtConcurrentRun>
34 #include <QInputDialog>
35 #include <QComboBox>
36 #include <QVBoxLayout>
37 #include <QTimer>
38 #include <QPainter>
39 #include <QAction>
40 #include <QWheelEvent>
41
42 MyLabel::MyLabel(QWidget *parent) :
43     QLabel(parent)
44 {
45 }
46
47 void MyLabel::setImage(QImage img)
48 {
49     m_img = img;
50 }
51
52 //virtual
53 void MyLabel::wheelEvent(QWheelEvent * event)
54 {
55     if (event->delta() > 0) emit seek(true);
56     else emit seek(false);
57 }
58
59 //virtual
60 void MyLabel::paintEvent(QPaintEvent * event)
61 {
62     Q_UNUSED(event);
63
64     QRect r(0, 0, width(), height());
65     QPainter p(this);
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;
72     else {
73         int calculatedHeight = width() / aspect_ratio;
74         if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
75     }
76     p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
77     p.end();
78 }
79
80
81 StopmotionWidget::StopmotionWidget(KUrl projectFolder, QWidget *parent) :
82     QDialog(parent)
83     , Ui::Stopmotion_UI()
84     , m_projectFolder(projectFolder)
85     , m_bmCapture(NULL)
86     , m_sequenceFrame(0)
87     , m_animatedIndex(-1)
88 {
89     setupUi(this);
90     setWindowTitle(i18n("Stop Motion Capture"));
91     setFont(KGlobalSettings::toolBarFont());
92
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);
99
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);
105
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 &)));
112     }
113     if (QFile::exists(KdenliveSettings::video4vdevice())) {
114         if (m_bmCapture == NULL) m_bmCapture = new V4lCaptureHandler(m_layout);
115         capture_device->addItem(KdenliveSettings::video4vdevice(), "v4l");
116     }
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)));
133
134     parseExistingSequences();
135 }
136
137 StopmotionWidget::~StopmotionWidget()
138 {
139     m_bmCapture->stopPreview();
140 }
141
142 void StopmotionWidget::slotUpdateHandler()
143 {
144     QString data = capture_device->itemData(capture_device->currentIndex()).toString();
145     m_bmCapture->stopPreview();
146     delete m_bmCapture;
147     m_layout->removeWidget(m_frame_preview);
148     if (data == "v4l") {
149         m_bmCapture = new V4lCaptureHandler(m_layout);
150     } else {
151         m_bmCapture = new BmdCaptureHandler(m_layout);
152         connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
153     }
154     m_layout->addWidget(m_frame_preview);
155 }
156
157 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
158 {
159     log_box->insertItem(0, message);
160 }
161
162 void StopmotionWidget::parseExistingSequences()
163 {
164     sequence_name->clear();
165     sequence_name->addItem(QString());
166     QDir dir(m_projectFolder.path());
167     QStringList filters;
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));
174     }
175 }
176
177 void StopmotionWidget::slotLive(bool isOn)
178 {
179     if (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);
184     } else {
185         m_bmCapture->stopPreview();
186         capture_button->setEnabled(false);
187     }
188 }
189
190 void StopmotionWidget::slotShowOverlay(bool isOn)
191 {
192     if (isOn) {
193         if (live_button->isChecked() && m_sequenceFrame > 0) {
194             slotUpdateOverlay();
195         }
196     } else {
197         m_bmCapture->hideOverlay();
198     }
199 }
200
201 void StopmotionWidget::slotUpdateOverlay()
202 {
203     QString path = getPathForFrame(m_sequenceFrame - 1);
204     if (!QFile::exists(path)) return;
205     QImage img(path);
206     if (img.isNull()) {
207         QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
208         return;
209     }
210     m_bmCapture->showOverlay(img);
211 }
212
213 void StopmotionWidget::sequenceNameChanged(const QString &name)
214 {
215     // Get rid of frames from previous sequence
216     disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
217     m_filesList.clear();
218     m_future.waitForFinished();
219     frame_list->clear();
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);
227     } else {
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);
234         m_currentIndex = 0;
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);
241         } else {
242             button_addsequence->setEnabled(false);
243             frameoverlay_button->setEnabled(false);
244         }
245         frame_number->setRange(0, m_sequenceFrame);
246         capture_button->setEnabled(true);
247     }
248 }
249
250 void StopmotionWidget::slotCaptureFrame()
251 {
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);
258     }
259     if (m_sequenceName != sequence_name->currentText()) {
260         m_sequenceName = sequence_name->currentText();
261         m_sequenceFrame = 0;
262     }
263     capture_button->setEnabled(false);
264     m_bmCapture->captureFrame(getPathForFrame(m_sequenceFrame));
265     KNotification::event("FrameCaptured");
266     frameoverlay_button->setEnabled(true);
267     m_sequenceFrame++;
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()));
275 }
276
277 void StopmotionWidget::slotPrepareThumbs()
278 {
279     if (m_filesList.isEmpty()) return;
280     QString path = m_filesList.takeFirst();
281     emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
282
283 }
284
285 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
286 {
287     if (img.isNull()) {
288         m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
289         return;
290     }
291     int height = 90;
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);
296     QPainter p(&pix);
297     QFontInfo finfo(font());
298     p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
299     p.setPen(Qt::white);
300     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
301     p.end();
302     QIcon icon(pix);
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);
307 }
308
309 void StopmotionWidget::slotUpdateFrameList(int ix)
310 {
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);
316         return;
317     }
318     QImage img(path);
319     if (img.isNull()) {
320         if (ix == m_sequenceFrame - 1) QTimer::singleShot(1000, this, SLOT(slotUpdateFrameList()));
321         return;
322     }
323     int height = 90;
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);
328     QPainter p(&pix);
329     QFontInfo finfo(font());
330     p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
331     p.setPen(Qt::white);
332     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
333     p.end();
334     QIcon icon(pix);
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);
341 }
342
343 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
344 {
345     if (seqName.isEmpty()) seqName = m_sequenceName;
346     return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
347 }
348
349 void StopmotionWidget::slotShowFrame(int ix)
350 {
351     if (m_sequenceFrame == 0) {
352         //there are no images in sequence
353         return;
354     }
355     frameoverlay_button->blockSignals(true);
356     frameoverlay_button->setChecked(false);
357     frameoverlay_button->blockSignals(false);
358     if (ix < m_sequenceFrame) {
359         // Show previous frame
360         slotLive(false);
361         live_button->setChecked(false);
362         QImage img(getPathForFrame(ix));
363         capture_button->setEnabled(false);
364         if (!img.isNull()) {
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();
370             selectFrame(ix);
371         }
372     }
373     /*else {
374     slotLive(true);
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);
380     }*/
381 }
382
383 void StopmotionWidget::slotShowSelectedFrame()
384 {
385     QListWidgetItem *item = frame_list->currentItem();
386     if (item) {
387         int ix = item->data(Qt::UserRole).toInt();
388         //frame_number->blockSignals(true);
389         frame_number->setValue(ix);
390         //frame_number->blockSignals(false);
391         //slotShowFrame(ix);
392     }
393 }
394
395 void StopmotionWidget::slotAddSequence()
396 {
397     emit addOrUpdateSequence(getPathForFrame(0));
398 }
399
400 void StopmotionWidget::slotPlayPreview()
401 {
402     if (m_animatedIndex != -1) {
403         // stop animation
404         m_animatedIndex = -1;
405         return;
406     }
407     QListWidgetItem *item = frame_list->currentItem();
408     if (item) {
409         m_animatedIndex = item->data(Qt::UserRole).toInt();
410     }
411     QTimer::singleShot(200, this, SLOT(slotAnimate()));
412 }
413
414 void StopmotionWidget::slotAnimate()
415 {
416     slotShowFrame(m_animatedIndex);
417     m_animatedIndex++;
418     if (m_animatedIndex < m_sequenceFrame) QTimer::singleShot(200, this, SLOT(slotAnimate()));
419     else m_animatedIndex = -1;
420 }
421
422 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
423 {
424     QListWidgetItem *item = NULL;
425     int pos = ix;
426     if (ix >= frame_list->count()) {
427         pos = frame_list->count() - 1;
428     }
429     if (ix < 0) pos = 0;
430     item = frame_list->item(pos);
431
432     int value = item->data(Qt::UserRole).toInt();
433     if (value == ix) return item;
434     else if (value < ix) {
435         pos++;
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;
440             pos++;
441         }
442     } else {
443         pos --;
444         while (pos >= 0) {
445             item = frame_list->item(pos);
446             value = item->data(Qt::UserRole).toInt();
447             if (value == ix) return item;
448             pos --;
449         }
450     }
451     return NULL;
452 }
453
454
455 void StopmotionWidget::selectFrame(int ix)
456 {
457     frame_list->blockSignals(true);
458     QListWidgetItem *item = getFrameFromIndex(ix);
459     if (!item) return;
460     frame_list->setCurrentItem(item);
461     frame_list->blockSignals(false);
462 }
463
464 void StopmotionWidget::slotSeekFrame(bool forward)
465 {
466     int ix = frame_list->currentRow();
467     if (forward) {
468         if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
469     } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
470 }
471
472