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"
21 #include "v4l/v4lcapture.h"
23 #include "slideshowclip.h"
24 #include "profilesdialog.h"
25 #include "mltdevicecapture.h"
26 #include "recmonitor.h"
27 #include "monitormanager.h"
28 #include "ui_smconfig_ui.h"
29 #include "kdenlivesettings.h"
32 #include <KGlobalSettings>
33 #include <KFileDialog>
34 #include <KStandardDirs>
35 #include <KMessageBox>
36 #include <kdeversion.h>
37 #include <KNotification>
39 #include <QtConcurrentRun>
40 #include <QInputDialog>
42 #include <QVBoxLayout>
46 #include <QWheelEvent>
49 MyLabel::MyLabel(QWidget* parent) :
54 void MyLabel::setImage(QImage img)
60 void MyLabel::wheelEvent(QWheelEvent* event)
62 if (event->delta() > 0) emit seek(true);
63 else emit seek(false);
67 void MyLabel::mousePressEvent(QMouseEvent*)
73 void MyLabel::paintEvent(QPaintEvent* event)
77 QRect r(0, 0, width(), height());
79 p.fillRect(r, QColor(KdenliveSettings::window_background()));
80 double aspect_ratio = (double) m_img.width() / m_img.height();
81 int pictureHeight = height();
82 int pictureWidth = width();
83 int calculatedWidth = aspect_ratio * height();
84 if (calculatedWidth > width()) pictureHeight = width() / aspect_ratio;
86 int calculatedHeight = width() / aspect_ratio;
87 if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
89 p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
94 StopmotionMonitor::StopmotionMonitor(QWidget *parent) :
95 AbstractMonitor(parent),
100 StopmotionMonitor::~StopmotionMonitor()
104 void StopmotionMonitor::setRender(MltDeviceCapture *render)
106 m_captureDevice = render;
109 AbstractRender *StopmotionMonitor::abstractRender()
111 return m_captureDevice;
114 const QString StopmotionMonitor::name() const
116 return QString("stopmotion");
120 void StopmotionMonitor::stop()
122 if (m_captureDevice) m_captureDevice->stop();
126 void StopmotionMonitor::start()
130 StopmotionWidget::StopmotionWidget(MonitorManager *manager, KUrl projectFolder, QList< QAction* > actions, QWidget* parent) :
132 , Ui::Stopmotion_UI()
133 , m_projectFolder(projectFolder)
134 , m_captureDevice(NULL)
136 , m_animatedIndex(-1)
139 , m_monitor(new StopmotionMonitor(this))
141 //setAttribute(Qt::WA_DeleteOnClose);
142 //HACK: the monitor widget is hidden, it is just used to control the capturedevice from monitormanager
143 m_monitor->setHidden(true);
144 connect(m_monitor, SIGNAL(stopCapture()), this, SLOT(slotStopCapture()));
145 m_manager->appendMonitor(m_monitor);
146 QAction* analyse = new QAction(i18n("Send frames to color scopes"), this);
147 analyse->setCheckable(true);
148 analyse->setChecked(KdenliveSettings::analyse_stopmotion());
149 connect(analyse, SIGNAL(triggered(bool)), this, SLOT(slotSwitchAnalyse(bool)));
151 QAction* mirror = new QAction(i18n("Mirror display"), this);
152 mirror->setCheckable(true);
153 //mirror->setChecked(KdenliveSettings::analyse_stopmotion());
154 connect(mirror, SIGNAL(triggered(bool)), this, SLOT(slotSwitchMirror(bool)));
158 setWindowTitle(i18n("Stop Motion Capture"));
159 setFont(KGlobalSettings::toolBarFont());
161 live_button->setIcon(KIcon("camera-photo"));
163 m_captureAction = actions.at(0);
164 connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
165 m_captureAction->setCheckable(true);
166 m_captureAction->setChecked(false);
167 capture_button->setDefaultAction(m_captureAction);
169 connect(actions.at(1), SIGNAL(triggered()), this, SLOT(slotSwitchLive()));
171 QAction *intervalCapture = new QAction(i18n("Interval capture"), this);
172 intervalCapture->setIcon(KIcon("chronometer"));
173 intervalCapture->setCheckable(true);
174 intervalCapture->setChecked(false);
175 capture_interval->setDefaultAction(intervalCapture);
177 preview_button->setIcon(KIcon("media-playback-start"));
178 capture_button->setEnabled(false);
182 QMenu* confMenu = new QMenu;
183 m_showOverlay = actions.at(2);
184 connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
185 overlay_button->setDefaultAction(m_showOverlay);
186 //confMenu->addAction(m_showOverlay);
188 m_effectIndex = KdenliveSettings::stopmotioneffect();
189 QMenu* effectsMenu = new QMenu(i18n("Overlay effect"));
190 QActionGroup* effectGroup = new QActionGroup(this);
191 QAction* noEffect = new QAction(i18n("No Effect"), effectGroup);
192 noEffect->setData(0);
193 QAction* contrastEffect = new QAction(i18n("Contrast"), effectGroup);
194 contrastEffect->setData(1);
195 QAction* edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
196 edgeEffect->setData(2);
197 QAction* brightEffect = new QAction(i18n("Brighten"), effectGroup);
198 brightEffect->setData(3);
199 QAction* invertEffect = new QAction(i18n("Invert"), effectGroup);
200 invertEffect->setData(4);
201 QAction* thresEffect = new QAction(i18n("Threshold"), effectGroup);
202 thresEffect->setData(5);
204 effectsMenu->addAction(noEffect);
205 effectsMenu->addAction(contrastEffect);
206 effectsMenu->addAction(edgeEffect);
207 effectsMenu->addAction(brightEffect);
208 effectsMenu->addAction(invertEffect);
209 effectsMenu->addAction(thresEffect);
210 QList <QAction*> list = effectsMenu->actions();
211 for (int i = 0; i < list.count(); i++) {
212 list.at(i)->setCheckable(true);
213 if (list.at(i)->data().toInt() == m_effectIndex) {
214 list.at(i)->setChecked(true);
217 connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
218 confMenu->addMenu(effectsMenu);
220 QAction* showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
221 showThumbs->setCheckable(true);
222 showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
223 connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
225 QAction* removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
226 removeCurrent->setShortcut(Qt::Key_Delete);
227 connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
229 QAction* conf = new QAction(KIcon("configure"), i18n("Configure"), this);
230 connect(conf, SIGNAL(triggered()), this, SLOT(slotConfigure()));
232 confMenu->addAction(showThumbs);
233 confMenu->addAction(removeCurrent);
234 confMenu->addAction(analyse);
235 confMenu->addAction(mirror);
236 confMenu->addAction(conf);
237 config_button->setIcon(KIcon("configure"));
238 config_button->setMenu(confMenu);
240 connect(sequence_name, SIGNAL(textChanged(const QString&)), this, SLOT(sequenceNameChanged(const QString&)));
241 connect(sequence_name, SIGNAL(currentIndexChanged(int)), live_button, SLOT(setFocus()));
243 // Video widget holder
244 QVBoxLayout *layout = new QVBoxLayout;
245 layout->setContentsMargins(0, 0, 0, 0);
246 layout->setSpacing(0);
247 m_videoBox = new VideoPreviewContainer();
248 m_videoBox->setContentsMargins(0, 0, 0, 0);
249 m_videoBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
250 //m_videoBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
251 m_videoBox->setLineWidth(4);
252 layout->addWidget(m_videoBox);
255 if (BMInterface::getBlackMagicDeviceList(capture_device)) {
256 // Found a BlackMagic device
258 if (QFile::exists(KdenliveSettings::video4vdevice())) {
260 // Video 4 Linux device detection
261 for (int i = 0; i < 10; i++) {
262 QString path = "/dev/video" + QString::number(i);
263 if (QFile::exists(path)) {
264 QStringList deviceInfo = V4lCaptureHandler::getDeviceName(path);
265 if (!deviceInfo.isEmpty()) {
266 capture_device->addItem(deviceInfo.at(0), "v4l");
267 capture_device->setItemData(capture_device->count() - 1, path, Qt::UserRole + 1);
268 capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(1), Qt::UserRole + 2);
269 if (path == KdenliveSettings::video4vdevice()) capture_device->setCurrentIndex(capture_device->count() - 1);
276 connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDeviceHandler()));
278 connect(m_bmCapture, SIGNAL(frameSaved(const QString &)), this, SLOT(slotNewThumb(const QString &)));
279 connect(m_bmCapture, SIGNAL(gotFrame(QImage)), this, SIGNAL(gotFrame(QImage)));
280 } else live_button->setEnabled(false);*/
282 m_frame_preview = new MyLabel(this);
283 connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
284 connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
285 layout->addWidget(m_frame_preview);
286 m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
287 video_preview->setLayout(layout);
289 //kDebug()<<video_preview->winId();
292 // Create MLT producer data
293 if (capture_device->itemData(capture_device->currentIndex()) == "v4l") {
294 // Capture using a video4linux device
295 profilePath = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
299 profilePath = KdenliveSettings::current_profile();
302 m_captureDevice = new MltDeviceCapture(profilePath, m_videoBox, this);
303 m_captureDevice->sendFrameForAnalysis = KdenliveSettings::analyse_stopmotion();
304 m_monitor->setRender(m_captureDevice);
305 connect(m_captureDevice, SIGNAL(frameSaved(const QString &)), this, SLOT(slotNewThumb(const QString &)));
307 live_button->setChecked(false);
308 button_addsequence->setEnabled(false);
309 connect(live_button, SIGNAL(toggled(bool)), this, SLOT(slotLive(bool)));
310 connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
311 connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
312 connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
313 connect(frame_list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotShowSelectedFrame()));
314 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
316 frame_list->addAction(removeCurrent);
317 frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
318 frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
319 parseExistingSequences();
320 QTimer::singleShot(500, this, SLOT(slotLive()));
321 connect(&m_intervalTimer, SIGNAL(timeout()), this, SLOT(slotCaptureFrame()));
322 m_intervalTimer.setSingleShot(true);
323 m_intervalTimer.setInterval(KdenliveSettings::captureinterval() * 1000);
326 StopmotionWidget::~StopmotionWidget()
328 m_manager->removeMonitor(m_monitor);
329 if (m_captureDevice) {
330 m_captureDevice->stop();
331 delete m_captureDevice;
332 m_captureDevice = NULL;
338 void StopmotionWidget::slotUpdateOverlayEffect(QAction* act)
340 if (act) m_effectIndex = act->data().toInt();
341 KdenliveSettings::setStopmotioneffect(m_effectIndex);
345 void StopmotionWidget::closeEvent(QCloseEvent* e)
348 QDialog::closeEvent(e);
351 void StopmotionWidget::slotConfigure()
356 d.setWindowTitle(i18n("Configure Stop Motion"));
357 ui.sm_interval->setValue(KdenliveSettings::captureinterval());
358 ui.sm_interval->setSuffix(ki18np(" second", " seconds"));
359 ui.sm_notifytime->setSuffix(ki18np(" second", " seconds"));
360 ui.sm_notifytime->setValue(KdenliveSettings::sm_notifytime());
361 connect(ui.sm_prenotify, SIGNAL(toggled(bool)), ui.sm_notifytime, SLOT(setEnabled(bool)));
362 ui.sm_prenotify->setChecked(KdenliveSettings::sm_prenotify());
363 ui.sm_loop->setChecked(KdenliveSettings::sm_loop());
364 ui.sm_framesplayback->setValue(KdenliveSettings::sm_framesplayback());
366 if (d.exec() == QDialog::Accepted) {
367 KdenliveSettings::setSm_loop(ui.sm_loop->isChecked());
368 KdenliveSettings::setCaptureinterval(ui.sm_interval->value());
369 KdenliveSettings::setSm_framesplayback(ui.sm_framesplayback->value());
370 KdenliveSettings::setSm_notifytime(ui.sm_notifytime->value());
371 KdenliveSettings::setSm_prenotify(ui.sm_prenotify->isChecked());
372 m_intervalTimer.setInterval(KdenliveSettings::captureinterval() * 1000);
376 void StopmotionWidget::slotShowThumbs(bool show)
378 KdenliveSettings::setShowstopmotionthumbs(show);
381 sequenceNameChanged(sequence_name->currentText());
386 frame_list->setHidden(!show);
390 void StopmotionWidget::slotUpdateDeviceHandler()
393 delete m_captureDevice;
394 m_captureDevice = NULL;
395 /*QString data = capture_device->itemData(capture_device->currentIndex()).toString();
400 m_layout->removeWidget(m_frame_preview);
403 m_bmCapture = new V4lCaptureHandler(m_layout);
404 m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
407 m_bmCapture = new BmdCaptureHandler(m_layout);
408 if (m_bmCapture) connect(m_bmCapture, SIGNAL(gotMessage(const QString&)), this, SLOT(slotGotHDMIMessage(const QString&)));
410 live_button->setEnabled(m_bmCapture != NULL);
411 m_layout->addWidget(m_frame_preview);*/
414 void StopmotionWidget::slotGotHDMIMessage(const QString& message)
416 log_box->insertItem(0, message);
419 void StopmotionWidget::parseExistingSequences()
421 sequence_name->clear();
422 sequence_name->addItem(QString());
423 QDir dir(m_projectFolder.path());
425 filters << "*_0000.png";
426 //dir.setNameFilters(filters);
427 QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
428 //kDebug()<<"PF: "<<<<", sm: "<<sequences;
429 foreach(QString sequencename, sequences) {
430 sequence_name->addItem(sequencename.section("_", 0, -2));
434 void StopmotionWidget::slotSwitchLive()
436 setUpdatesEnabled(false);
437 slotLive(!live_button->isChecked());
438 /*if (m_frame_preview->isHidden()) {
439 //if (m_bmCapture) m_bmCapture->hidePreview(true);
440 m_videoBox->setHidden(true);
441 m_frame_preview->setHidden(false);
443 m_frame_preview->setHidden(true);
444 //if (m_bmCapture) m_bmCapture->hidePreview(false);
445 m_videoBox->setHidden(false);
446 capture_button->setEnabled(true);
448 setUpdatesEnabled(true);
451 void StopmotionWidget::slotStopCapture()
456 void StopmotionWidget::slotLive(bool isOn)
458 live_button->blockSignals(true);
459 capture_button->setEnabled(false);
461 m_frame_preview->setHidden(true);
462 m_videoBox->setHidden(false);
465 MltVideoProfile profile;
469 // Create MLT producer data
470 if (capture_device->itemData(capture_device->currentIndex()) == "v4l") {
471 // Capture using a video4linux device
472 profilePath = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
473 profile = ProfilesDialog::getVideoProfile(profilePath);
474 service = "avformat-novalidate";
475 QString devicePath = capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString();
476 resource = QString("video4linux2:%1?width:%2&height:%3&frame_rate:%4").arg(devicePath).arg(profile.width).arg(profile.height).arg((double) profile.frame_rate_num / profile.frame_rate_den);
480 profilePath = KdenliveSettings::current_profile();
481 profile = ProfilesDialog::getVideoProfile(profilePath);
482 service = "decklink";
483 resource = capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString();
486 if (m_captureDevice == NULL) {
487 m_captureDevice = new MltDeviceCapture(profilePath, m_videoBox, this);
488 m_captureDevice->sendFrameForAnalysis = KdenliveSettings::analyse_stopmotion();
489 m_monitor->setRender(m_captureDevice);
490 connect(m_captureDevice, SIGNAL(frameSaved(const QString &)), this, SLOT(slotNewThumb(const QString &)));
493 m_manager->activateMonitor("stopmotion");
494 QString producer = createProducer(profile, service, resource);
495 if (m_captureDevice->slotStartPreview(producer, true)) {
496 if (m_showOverlay->isChecked()) {
500 capture_button->setEnabled(true);
501 live_button->setChecked(true);
502 log_box->insertItem(-1, i18n("Playing %1x%2 (%3 fps)", profile.width, profile.height, locale.toString((double)profile.frame_rate_num/profile.frame_rate_den).rightJustified(2, '0')));
503 log_box->setCurrentIndex(0);
506 kDebug()<<"// problem starting stopmo";
507 log_box->insertItem(-1, i18n("Failed to start device"));
508 log_box->setCurrentIndex(0);
512 m_frame_preview->setHidden(false);
513 live_button->setChecked(false);
514 if (m_captureDevice) {
515 m_captureDevice->stop();
516 m_videoBox->setHidden(true);
517 log_box->insertItem(-1, i18n("Stopped"));
518 log_box->setCurrentIndex(0);
519 //delete m_captureDevice;
520 //m_captureDevice = NULL;
525 if (isOn && m_bmCapture) {
526 //m_frame_preview->setImage(QImage());
527 m_frame_preview->setHidden(true);
528 m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
529 capture_button->setEnabled(true);
530 live_button->setChecked(true);
532 if (m_bmCapture) m_bmCapture->stopPreview();
533 m_frame_preview->setHidden(false);
534 capture_button->setEnabled(false);
535 live_button->setChecked(false);
537 live_button->blockSignals(false);
540 void StopmotionWidget::slotShowOverlay(bool isOn)
543 // Overlay last frame of the sequence
549 m_captureDevice->setOverlay(QString());
553 void StopmotionWidget::reloadOverlay()
555 QString path = getPathForFrame(m_sequenceFrame - 1);
556 if (!QFile::exists(path)) {
557 log_box->insertItem(-1, i18n("No previous frame found"));
558 log_box->setCurrentIndex(0);
561 m_captureDevice->setOverlay(path);
564 void StopmotionWidget::slotUpdateOverlay()
566 if (m_captureDevice == NULL) return;
571 switch (m_effectIndex) {
573 tag = "frei0r.contrast0r";
574 params << "Contrast=1.2";
578 params << "x_scatter=4" << "y_scatter=4" << "scale=1" << "mix=0";
581 tag = "frei0r.brightness";
582 params << "Brightness=0.7";
589 params << "midpoint=125";
595 m_captureDevice->setOverlayEffect(tag, params);
598 void StopmotionWidget::sequenceNameChanged(const QString& name)
600 // Get rid of frames from previous sequence
601 disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
603 m_future.waitForFinished();
605 if (name.isEmpty()) {
606 button_addsequence->setEnabled(false);
608 // Check if we are editing an existing sequence
609 QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
610 m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
611 if (!m_filesList.isEmpty()) {
612 m_sequenceName = sequence_name->currentText();
613 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
614 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
615 button_addsequence->setEnabled(true);
618 connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
619 button_addsequence->setEnabled(false);
621 capture_button->setEnabled(live_button->isChecked());
625 void StopmotionWidget::slotCaptureFrame()
627 if (m_captureDevice == NULL) return;
628 if (sequence_name->currentText().isEmpty()) {
629 QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
630 if (seqName.isEmpty()) {
631 m_captureAction->setChecked(false);
634 sequence_name->blockSignals(true);
635 sequence_name->setItemText(sequence_name->currentIndex(), seqName);
636 sequence_name->blockSignals(false);
638 if (m_sequenceName != sequence_name->currentText()) {
639 m_sequenceName = sequence_name->currentText();
642 //capture_button->setEnabled(false);
643 if (m_intervalTimer.isActive()) {
644 // stop interval capture
645 m_intervalTimer.stop();
648 QString currentPath = getPathForFrame(m_sequenceFrame);
649 m_captureDevice->captureFrame(currentPath);
650 KNotification::event("FrameCaptured", i18n("Frame Captured"), QPixmap(), this);
652 button_addsequence->setEnabled(true);
653 if (capture_interval->isChecked()) {
654 if (KdenliveSettings::sm_prenotify()) QTimer::singleShot((KdenliveSettings::captureinterval() - KdenliveSettings::sm_notifytime()) * 1000, this, SLOT(slotPreNotify()));
655 m_intervalTimer.start();
657 else m_captureAction->setChecked(false);
660 void StopmotionWidget::slotPreNotify()
662 if (m_captureAction->isChecked()) KNotification::event("ReadyToCapture", i18n("Going to Capture Frame"), QPixmap(), this);
666 void StopmotionWidget::slotNewThumb(const QString &path)
668 if (!KdenliveSettings::showstopmotionthumbs()) return;
669 m_filesList.append(path);
670 if (m_showOverlay->isChecked()) reloadOverlay();
671 if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
675 void StopmotionWidget::slotPrepareThumbs()
677 if (m_filesList.isEmpty()) return;
678 QString path = m_filesList.takeFirst();
679 emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
683 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
686 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
690 int width = height * img.width() / img.height();
691 frame_list->setIconSize(QSize(width, height));
692 QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
693 QString nb = QString::number(ix);
695 QFontInfo finfo(font());
696 p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
698 p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
701 QListWidgetItem* item = new QListWidgetItem(icon, QString(), frame_list);
702 item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
703 item->setData(Qt::UserRole, ix);
704 frame_list->blockSignals(true);
705 frame_list->setCurrentItem(item);
706 frame_list->blockSignals(false);
707 m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
710 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
712 if (seqName.isEmpty()) seqName = m_sequenceName;
713 return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
716 void StopmotionWidget::slotShowFrame(const QString& path)
720 capture_button->setEnabled(false);
723 //m_videoBox->setHidden(true);
725 m_frame_preview->setImage(img);
726 m_frame_preview->setHidden(false);
727 m_frame_preview->update();
731 void StopmotionWidget::slotShowSelectedFrame()
733 QListWidgetItem* item = frame_list->currentItem();
735 //int ix = item->data(Qt::UserRole).toInt();;
736 slotShowFrame(item->toolTip());
740 void StopmotionWidget::slotAddSequence()
742 emit addOrUpdateSequence(getPathForFrame(0));
745 void StopmotionWidget::slotPlayPreview(bool animate)
750 m_animationList.clear();
753 if (KdenliveSettings::showstopmotionthumbs()) {
754 if (KdenliveSettings::sm_framesplayback() == 0) frame_list->setCurrentRow(0);
755 else frame_list->setCurrentRow(frame_list->count() - KdenliveSettings::sm_framesplayback());
756 QTimer::singleShot(200, this, SLOT(slotAnimate()));
758 SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
759 if (KdenliveSettings::sm_framesplayback() > 0) {
760 // only play the last x frames
761 while (m_animationList.count() > KdenliveSettings::sm_framesplayback() + 1) {
762 m_animationList.removeFirst();
770 void StopmotionWidget::slotAnimate()
773 if (KdenliveSettings::showstopmotionthumbs()) {
774 int newRow = frame_list->currentRow() + 1;
775 if (KdenliveSettings::sm_loop() || newRow < frame_list->count()) {
776 if (newRow >= frame_list->count()) {
777 if (KdenliveSettings::sm_framesplayback() == 0) newRow = 0;
779 // seek to correct frame
780 newRow = frame_list->count() - KdenliveSettings::sm_framesplayback();
783 frame_list->setCurrentRow(newRow);
784 QTimer::singleShot(100, this, SLOT(slotAnimate()));
788 if (m_animatedIndex >= m_animationList.count()) {
789 if (KdenliveSettings::sm_loop()) m_animatedIndex = 0;
790 else m_animatedIndex = -1;
792 if (m_animatedIndex > -1) {
793 slotShowFrame(m_animationList.at(m_animatedIndex));
795 QTimer::singleShot(100, this, SLOT(slotAnimate()));
801 preview_button->setChecked(false);
805 QListWidgetItem* StopmotionWidget::getFrameFromIndex(int ix)
807 QListWidgetItem* item = NULL;
809 if (ix >= frame_list->count()) {
810 pos = frame_list->count() - 1;
813 item = frame_list->item(pos);
815 int value = item->data(Qt::UserRole).toInt();
816 if (value == ix) return item;
817 else if (value < ix) {
819 while (pos < frame_list->count()) {
820 item = frame_list->item(pos);
821 value = item->data(Qt::UserRole).toInt();
822 if (value == ix) return item;
828 item = frame_list->item(pos);
829 value = item->data(Qt::UserRole).toInt();
830 if (value == ix) return item;
838 void StopmotionWidget::selectFrame(int ix)
840 frame_list->blockSignals(true);
841 QListWidgetItem* item = getFrameFromIndex(ix);
843 frame_list->setCurrentItem(item);
844 frame_list->blockSignals(false);
847 void StopmotionWidget::slotSeekFrame(bool forward)
849 int ix = frame_list->currentRow();
851 if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
852 } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
855 void StopmotionWidget::slotRemoveFrame()
857 if (frame_list->currentItem() == NULL) return;
858 QString path = frame_list->currentItem()->toolTip();
859 if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
862 QListWidgetItem* item = frame_list->takeItem(frame_list->currentRow());
863 int ix = item->data(Qt::UserRole).toInt();
864 if (ix == m_sequenceFrame - 1) {
865 // We are removing the last frame, update counter
866 QListWidgetItem* item2 = frame_list->item(frame_list->count() - 1);
867 if (item2) m_sequenceFrame = item2->data(Qt::UserRole).toInt() + 1;
873 void StopmotionWidget::slotSwitchAnalyse(bool isOn)
875 KdenliveSettings::setAnalyse_stopmotion(isOn);
876 if (m_captureDevice) m_captureDevice->sendFrameForAnalysis = isOn;
879 void StopmotionWidget::slotSwitchMirror(bool isOn)
881 //KdenliveSettings::setAnalyse_stopmotion(isOn);
882 if (m_captureDevice) m_captureDevice->mirror(isOn);
885 const QString StopmotionWidget::createProducer(MltVideoProfile profile, const QString &service, const QString &resource)
889 QString playlist = "<mlt title=\"capture\"><producer id=\"producer0\" in=\"0\" out=\"99999\"><property name=\"mlt_type\">producer</property><property name=\"length\">100000</property><property name=\"eof\">pause</property><property name=\"resource\">" + resource + "</property><property name=\"mlt_service\">" + service + "</property></producer>";
892 playlist.append("<playlist id=\"playlist0\"></playlist>");
895 playlist.append("<playlist id=\"playlist1\"><entry producer=\"producer0\" in=\"0\" out=\"99999\"/></playlist>");
897 playlist.append("<tractor id=\"tractor0\" title=\"video0\" global_feed=\"1\" in=\"0\" out=\"99999\">");
898 playlist.append("<track producer=\"playlist0\"/>");
899 playlist.append("<track producer=\"playlist1\"/>");
900 playlist.append("</tractor></mlt>");