]> git.sesse.net Git - kdenlive/blob - src/stopmotion/stopmotion.cpp
db1244c5fc2a9cb82676b02333d5a36a684ddf41
[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     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 &)));
113     }
114     else {
115         kDebug()<<"CREATE V4L DEVICE";
116         m_bmCapture = new V4lCaptureHandler(lay);
117         capture_device->addItem(KdenliveSettings::video4vdevice());
118     }
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)));
134     
135     parseExistingSequences();
136 }
137
138 StopmotionWidget::~StopmotionWidget()
139 {
140     m_bmCapture->stopPreview();
141 }
142
143 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
144 {
145     log_box->insertItem(0, message);
146 }
147
148 void StopmotionWidget::parseExistingSequences()
149 {
150     sequence_name->clear();
151     sequence_name->addItem(QString());
152     QDir dir(m_projectFolder.path());
153     QStringList filters;
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));
160     }
161 }
162
163 void StopmotionWidget::slotLive(bool isOn)
164 {
165     if (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);
170     }
171     else {
172         m_bmCapture->stopPreview();
173         capture_button->setEnabled(false);
174     }
175 }
176
177 void StopmotionWidget::slotShowOverlay(bool isOn)
178 {
179     if (isOn) {
180         if (live_button->isChecked() && m_sequenceFrame > 0) {
181             slotUpdateOverlay();
182         }
183     }
184     else {
185         m_bmCapture->hideOverlay();
186     }
187 }
188
189 void StopmotionWidget::slotUpdateOverlay()
190 {
191     QString path = getPathForFrame(m_sequenceFrame - 1);
192     if (!QFile::exists(path)) return;
193     QImage img(path);
194     if (img.isNull()) {
195         QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
196         return;
197     }
198     m_bmCapture->showOverlay(img);
199 }
200
201 void StopmotionWidget::sequenceNameChanged(const QString &name)
202 {
203     // Get rid of frames from previous sequence
204     disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage,int)));
205     m_filesList.clear();
206     m_future.waitForFinished();
207     frame_list->clear();
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);
215     }
216     else {
217         // Check if we are editing an existing sequence
218         int count = 0;
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);
224         m_currentIndex = 0;
225         if (count > 0) {
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));
231             }
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);
236         }
237         else {
238             button_addsequence->setEnabled(false);
239             frameoverlay_button->setEnabled(false);
240         }
241         frame_number->setRange(0, m_sequenceFrame);
242         capture_button->setEnabled(true);
243     }
244 }
245
246 void StopmotionWidget::slotCaptureFrame()
247 {
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);
254     }
255     if (m_sequenceName != sequence_name->currentText()) {
256         m_sequenceName = sequence_name->currentText();
257         m_sequenceFrame = 0;
258     }
259     capture_button->setEnabled(false);
260     m_bmCapture->captureFrame(getPathForFrame(m_sequenceFrame));
261     KNotification::event("FrameCaptured");
262     frameoverlay_button->setEnabled(true);
263     m_sequenceFrame++;
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()));
271 }
272
273 void StopmotionWidget::slotPrepareThumbs()
274 {
275     if (m_filesList.isEmpty()) return;
276     QString path = m_filesList.takeFirst();
277     emit doCreateThumbs(QImage(path), m_currentIndex++);
278     
279 }
280
281 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
282 {
283     if (img.isNull()) {
284         m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
285         return;
286     }
287     int height = 90;
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);
292     QPainter p(&pix);
293     QFontInfo finfo(font());
294     p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
295     p.setPen(Qt::white);
296     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
297     p.end();
298     QIcon icon(pix);
299     QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
300     item->setData(Qt::UserRole, ix);
301     m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
302 }
303
304 void StopmotionWidget::slotUpdateFrameList(int ix)
305 {
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);
311         return;
312     }
313     QImage img(path);
314     if (img.isNull()) {
315         if (ix == m_sequenceFrame - 1) QTimer::singleShot(1000, this, SLOT(slotUpdateFrameList()));
316         return;
317     }
318     int height = 90;
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);
323     QPainter p(&pix);
324     QFontInfo finfo(font());
325     p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(0, 0, 0, 150));
326     p.setPen(Qt::white);
327     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
328     p.end();
329     QIcon icon(pix);
330     QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
331     item->setData(Qt::UserRole, ix);
332     capture_button->setEnabled(true);
333 }
334
335 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
336 {
337     if (seqName.isEmpty()) seqName = m_sequenceName;
338     return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
339 }
340
341 void StopmotionWidget::slotShowFrame(int ix)
342 {
343     if (m_sequenceFrame == 0) {
344         //there are no images in sequence
345         return;
346     }
347     frameoverlay_button->blockSignals(true);
348     frameoverlay_button->setChecked(false);
349     frameoverlay_button->blockSignals(false);
350     if (ix < m_sequenceFrame) {
351         // Show previous frame
352         slotLive(false);
353         live_button->setChecked(false);
354         QImage img(getPathForFrame(ix));
355         capture_button->setEnabled(false);
356         if (!img.isNull()) {
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();
362             selectFrame(ix);
363         }
364     }
365     /*else {
366         slotLive(true);
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);
372     }*/   
373 }
374
375 void StopmotionWidget::slotShowSelectedFrame()
376 {
377     QListWidgetItem *item = frame_list->currentItem();
378     if (item) {
379         int ix = item->data(Qt::UserRole).toInt();
380         //frame_number->blockSignals(true);
381         frame_number->setValue(ix);
382         //frame_number->blockSignals(false);
383         //slotShowFrame(ix);
384     }
385 }
386
387 void StopmotionWidget::slotAddSequence()
388 {
389     emit addOrUpdateSequence(getPathForFrame(0));
390 }
391
392 void StopmotionWidget::slotPlayPreview()
393 {
394     if (m_animatedIndex != -1) {
395         // stop animation
396         m_animatedIndex = -1;
397         return;
398     }
399     QListWidgetItem *item = frame_list->currentItem();
400     if (item) {
401         m_animatedIndex = item->data(Qt::UserRole).toInt();
402     }
403     QTimer::singleShot(200, this, SLOT(slotAnimate()));
404 }
405
406 void StopmotionWidget::slotAnimate()
407 {
408     slotShowFrame(m_animatedIndex);
409     m_animatedIndex++;
410     if (m_animatedIndex < m_sequenceFrame) QTimer::singleShot(200, this, SLOT(slotAnimate()));
411     else m_animatedIndex = -1;
412 }
413
414 void StopmotionWidget::selectFrame(int ix)
415 {
416     frame_list->blockSignals(true);
417     QListWidgetItem *item = frame_list->item(ix);
418     int current = item->data(Qt::UserRole).toInt();
419     if (current == ix) {
420         frame_list->setCurrentItem(item);
421     }
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();
426             if (current == ix) {
427                 frame_list->setCurrentItem(item);
428                 break;
429             }
430         }
431     }
432     else {
433         for (int i = ix; i >= 0; i--) {
434             item = frame_list->item(i);
435             current = item->data(Qt::UserRole).toInt();
436             if (current == ix) {
437                 frame_list->setCurrentItem(item);
438                 break;
439             }
440         }
441     }
442     frame_list->blockSignals(false);
443 }
444
445 void StopmotionWidget::slotSeekFrame(bool forward)
446 {
447     int ix = frame_list->currentRow();
448     if (forward) {
449         if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
450     }
451     else if (ix > 0) frame_list->setCurrentRow(ix - 1);
452 }
453
454