]> git.sesse.net Git - kdenlive/blob - src/stopmotion/stopmotion.cpp
87af29ee1748092c1dbb9464a6cd3e96ef2da0fa
[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 #ifdef QIMAGEBLITZ
34 #include <qimageblitz/qimageblitz.h>
35 #endif
36
37 #include <QtConcurrentRun>
38 #include <QInputDialog>
39 #include <QComboBox>
40 #include <QVBoxLayout>
41 #include <QTimer>
42 #include <QPainter>
43 #include <QAction>
44 #include <QWheelEvent>
45 #include <QMenu>
46
47 MyLabel::MyLabel(QWidget *parent) :
48     QLabel(parent)
49 {
50 }
51
52 void MyLabel::setImage(QImage img)
53 {
54     m_img = img;
55 }
56
57 //virtual
58 void MyLabel::wheelEvent(QWheelEvent * event)
59 {
60     if (event->delta() > 0) emit seek(true);
61     else emit seek(false);
62 }
63
64 //virtual
65 void MyLabel::mousePressEvent(QMouseEvent *)
66 {
67     emit switchToLive();
68 }
69
70 //virtual
71 void MyLabel::paintEvent(QPaintEvent * event)
72 {
73     Q_UNUSED(event);
74
75     QRect r(0, 0, width(), height());
76     QPainter p(this);
77     p.fillRect(r, QColor(KdenliveSettings::window_background()));
78     double aspect_ratio = (double) m_img.width() / m_img.height();
79     int pictureHeight = height();
80     int pictureWidth = width();
81     int calculatedWidth = aspect_ratio * height();
82     if (calculatedWidth > width()) pictureHeight = width() / aspect_ratio;
83     else {
84         int calculatedHeight = width() / aspect_ratio;
85         if (calculatedHeight > height()) pictureWidth = height() * aspect_ratio;
86     }
87     p.drawImage(QRect((width() - pictureWidth) / 2, (height() - pictureHeight) / 2, pictureWidth, pictureHeight), m_img, QRect(0, 0, m_img.width(), m_img.height()));
88     p.end();
89 }
90
91
92 StopmotionWidget::StopmotionWidget(KUrl projectFolder, const QList< QAction * > actions, QWidget *parent) :
93     QDialog(parent)
94     , Ui::Stopmotion_UI()
95     , m_projectFolder(projectFolder)
96     , m_bmCapture(NULL)
97     , m_sequenceFrame(0)
98     , m_animatedIndex(-1)
99 {
100     //setAttribute(Qt::WA_DeleteOnClose);
101     addActions(actions);
102     setupUi(this);
103     setWindowTitle(i18n("Stop Motion Capture"));
104     setFont(KGlobalSettings::toolBarFont());
105
106     live_button->setIcon(KIcon("camera-photo"));
107
108     m_captureAction = actions.at(0);
109     connect(m_captureAction, SIGNAL(triggered()), this, SLOT(slotCaptureFrame()));
110     capture_button->setDefaultAction(m_captureAction);
111
112     connect(actions.at(1), SIGNAL(triggered()), this, SLOT(slotSwitchLive()));
113
114     preview_button->setIcon(KIcon("media-playback-start"));
115     capture_button->setEnabled(false);
116
117     // Build config menu
118     QMenu *confMenu = new QMenu;
119     m_showOverlay = actions.at(2);
120     connect(m_showOverlay, SIGNAL(triggered(bool)), this, SLOT(slotShowOverlay(bool)));
121     confMenu->addAction(m_showOverlay);
122
123 #ifdef QIMAGEBLITZ
124     m_effectIndex = KdenliveSettings::blitzeffect();
125     QMenu *effectsMenu = new QMenu(i18n("Overlay effect"));
126     QActionGroup *effectGroup = new QActionGroup(this);
127     QAction *noEffect = new QAction(i18n("No Effect"), effectGroup);
128     noEffect->setData(1);
129     QAction *contrastEffect = new QAction(i18n("Contrast"), effectGroup);
130     contrastEffect->setData(2);
131     QAction *edgeEffect = new QAction(i18n("Edge detect"), effectGroup);
132     edgeEffect->setData(3);
133     QAction *brightEffect = new QAction(i18n("Brighten"), effectGroup);
134     brightEffect->setData(4);
135     QAction *invertEffect = new QAction(i18n("Invert"), effectGroup);
136     invertEffect->setData(5);
137     QAction *thresEffect = new QAction(i18n("Threshold"), effectGroup);
138     thresEffect->setData(6);
139
140     effectsMenu->addAction(noEffect);
141     effectsMenu->addAction(contrastEffect);
142     effectsMenu->addAction(edgeEffect);
143     effectsMenu->addAction(brightEffect);
144     effectsMenu->addAction(invertEffect);
145     effectsMenu->addAction(thresEffect);
146     QList <QAction *> list = effectsMenu->actions();
147     for (int i = 0; i < list.count(); i++) {
148         list.at(i)->setCheckable(true);
149         if (list.at(i)->data().toInt() == m_effectIndex) {
150             list.at(i)->setChecked(true);
151         }
152     }
153     connect(effectsMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotUpdateOverlayEffect(QAction*)));
154     confMenu->addMenu(effectsMenu);
155 #endif
156
157     QAction *showThumbs = new QAction(KIcon("image-x-generic"), i18n("Show sequence thumbnails"), this);
158     showThumbs->setCheckable(true);
159     showThumbs->setChecked(KdenliveSettings::showstopmotionthumbs());
160     connect(showThumbs, SIGNAL(triggered(bool)), this, SLOT(slotShowThumbs(bool)));
161
162     QAction *removeCurrent = new QAction(KIcon("edit-delete"), i18n("Delete current frame"), this);
163     removeCurrent->setShortcut(Qt::Key_Delete);
164     connect(removeCurrent, SIGNAL(triggered()), this, SLOT(slotRemoveFrame()));
165
166     QAction *capInterval = new QAction(KIcon(), i18n("Set capture interval"), this);
167     connect(capInterval, SIGNAL(triggered()), this, SLOT(slotSetCaptureInterval()));
168
169     confMenu->addAction(showThumbs);
170     confMenu->addAction(capInterval);
171     confMenu->addAction(removeCurrent);
172     config_button->setIcon(KIcon("configure"));
173     config_button->setMenu(confMenu);
174
175     // Build capture menu
176     QMenu *capMenu = new QMenu;
177     m_intervalCapture = new QAction(KIcon("edit-redo"), i18n("Interval capture"), this);
178     m_intervalCapture->setCheckable(true);
179     m_intervalCapture->setChecked(false);
180     connect(m_intervalCapture, SIGNAL(triggered(bool)), this, SLOT(slotIntervalCapture(bool)));
181     capMenu->addAction(m_intervalCapture);
182     capture_button->setMenu(capMenu);
183
184     connect(sequence_name, SIGNAL(textChanged(const QString &)), this, SLOT(sequenceNameChanged(const QString &)));
185     connect(sequence_name, SIGNAL(currentIndexChanged(int)), live_button, SLOT(setFocus()));
186
187     m_layout = new QVBoxLayout;
188     if (BMInterface::getBlackMagicDeviceList(capture_device, NULL)) {
189         // Found a BlackMagic device
190         m_bmCapture = new BmdCaptureHandler(m_layout);
191         connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
192     }
193     if (QFile::exists(KdenliveSettings::video4vdevice())) {
194 #ifndef Q_WS_MAC
195         V4lCaptureHandler v4l(NULL);
196         // Video 4 Linux device detection
197         for (int i = 0; i < 10; i++) {
198             QString path = "/dev/video" + QString::number(i);
199             if (QFile::exists(path)) {
200                 QStringList deviceInfo = v4l.getDeviceName(path);
201                 if (!deviceInfo.isEmpty()) {
202                     capture_device->addItem(deviceInfo.at(0), "v4l");
203                     capture_device->setItemData(capture_device->count() - 1, path, Qt::UserRole + 1);
204                     capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(1), Qt::UserRole + 2);
205                     if (path == KdenliveSettings::video4vdevice()) capture_device->setCurrentIndex(capture_device->count() - 1);
206                 }
207             }
208         }
209
210         /*V4lCaptureHandler v4lhandler(NULL);
211         QStringList deviceInfo = v4lhandler.getDeviceName(KdenliveSettings::video4vdevice());
212             capture_device->addItem(deviceInfo.at(0), "v4l");
213         capture_device->setItemData(capture_device->count() - 1, deviceInfo.at(3), Qt::UserRole + 1);*/
214         if (m_bmCapture == NULL) {
215             m_bmCapture = new V4lCaptureHandler(m_layout);
216             m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
217         }
218 #endif
219     }
220
221     connect(capture_device, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHandler()));
222     if (m_bmCapture) {
223         connect(m_bmCapture, SIGNAL(frameSaved(const QString)), this, SLOT(slotNewThumb(const QString)));
224     } else live_button->setEnabled(false);
225     m_frame_preview = new MyLabel(this);
226     connect(m_frame_preview, SIGNAL(seek(bool)), this, SLOT(slotSeekFrame(bool)));
227     connect(m_frame_preview, SIGNAL(switchToLive()), this, SLOT(slotSwitchLive()));
228     m_layout->addWidget(m_frame_preview);
229     m_frame_preview->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
230     video_preview->setLayout(m_layout);
231     live_button->setChecked(false);
232     button_addsequence->setEnabled(false);
233     connect(live_button, SIGNAL(clicked(bool)), this, SLOT(slotLive(bool)));
234     connect(button_addsequence, SIGNAL(clicked(bool)), this, SLOT(slotAddSequence()));
235     connect(preview_button, SIGNAL(clicked(bool)), this, SLOT(slotPlayPreview(bool)));
236     connect(frame_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotShowSelectedFrame()));
237     connect(frame_list, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(slotShowSelectedFrame()));
238     connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
239
240     frame_list->addAction(removeCurrent);
241     frame_list->setContextMenuPolicy(Qt::ActionsContextMenu);
242     frame_list->setHidden(!KdenliveSettings::showstopmotionthumbs());
243     parseExistingSequences();
244 }
245
246 StopmotionWidget::~StopmotionWidget()
247 {
248     if (m_bmCapture)
249         m_bmCapture->stopPreview();
250 }
251
252 void StopmotionWidget::slotUpdateOverlayEffect(QAction *act)
253 {
254 #ifdef QIMAGEBLITZ
255     if (act) m_effectIndex = act->data().toInt();
256     KdenliveSettings::setBlitzeffect(m_effectIndex);
257     if (m_showOverlay->isChecked()) slotUpdateOverlay();
258 #endif
259 }
260
261 void StopmotionWidget::closeEvent(QCloseEvent *e)
262 {
263     slotLive(false);
264     QDialog::closeEvent(e);
265 }
266
267 void StopmotionWidget::slotSetCaptureInterval()
268 {
269     int interval = QInputDialog::getInteger(this, i18n("Set Capture Interval"), i18n("Interval (in seconds)"), KdenliveSettings::captureinterval(), 1);
270     if (interval > 0 && interval != KdenliveSettings::captureinterval())
271         KdenliveSettings::setCaptureinterval(interval);
272 }
273
274 void StopmotionWidget::slotShowThumbs(bool show)
275 {
276     KdenliveSettings::setShowstopmotionthumbs(show);
277     if (show) {
278         frame_list->clear();
279         sequenceNameChanged(sequence_name->currentText());
280     } else {
281         m_filesList.clear();
282         frame_list->clear();
283     }
284     frame_list->setHidden(!show);
285 }
286
287 void StopmotionWidget::slotIntervalCapture(bool capture)
288 {
289     if (capture) slotCaptureFrame();
290 }
291
292
293 void StopmotionWidget::slotUpdateHandler()
294 {
295     QString data = capture_device->itemData(capture_device->currentIndex()).toString();
296     slotLive(false);
297     if (m_bmCapture) {
298         delete m_bmCapture;
299     }
300     m_layout->removeWidget(m_frame_preview);
301     if (data == "v4l") {
302 #ifndef Q_WS_MAC
303         m_bmCapture = new V4lCaptureHandler(m_layout);
304         m_bmCapture->setDevice(capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 1).toString(), capture_device->itemData(capture_device->currentIndex(), Qt::UserRole + 2).toString());
305 #endif
306     } else {
307         m_bmCapture = new BmdCaptureHandler(m_layout);
308         if (m_bmCapture) connect(m_bmCapture, SIGNAL(gotMessage(const QString &)), this, SLOT(slotGotHDMIMessage(const QString &)));
309     }
310     live_button->setEnabled(m_bmCapture != NULL);
311     m_layout->addWidget(m_frame_preview);
312 }
313
314 void StopmotionWidget::slotGotHDMIMessage(const QString &message)
315 {
316     log_box->insertItem(0, message);
317 }
318
319 void StopmotionWidget::parseExistingSequences()
320 {
321     sequence_name->clear();
322     sequence_name->addItem(QString());
323     QDir dir(m_projectFolder.path());
324     QStringList filters;
325     filters << "*_0000.png";
326     //dir.setNameFilters(filters);
327     QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
328     //kDebug()<<"PF: "<<<<", sm: "<<sequences;
329     foreach(QString sequencename, sequences) {
330         sequence_name->addItem(sequencename.section("_", 0, -2));
331     }
332 }
333
334 void StopmotionWidget::slotSwitchLive()
335 {
336     setUpdatesEnabled(false);
337     if (m_frame_preview->isHidden()) {
338         if (m_bmCapture) m_bmCapture->hidePreview(true);
339         m_frame_preview->setHidden(false);
340     } else {
341         m_frame_preview->setHidden(true);
342         if (m_bmCapture) m_bmCapture->hidePreview(false);
343         capture_button->setEnabled(true);
344     }
345     setUpdatesEnabled(true);
346 }
347
348 void StopmotionWidget::slotLive(bool isOn)
349 {
350     if (isOn && m_bmCapture) {
351         //m_frame_preview->setImage(QImage());
352         m_frame_preview->setHidden(true);
353         m_bmCapture->startPreview(KdenliveSettings::hdmi_capturedevice(), KdenliveSettings::hdmi_capturemode(), false);
354         capture_button->setEnabled(true);
355     } else {
356         if (m_bmCapture) m_bmCapture->stopPreview();
357         m_frame_preview->setHidden(false);
358         capture_button->setEnabled(false);
359         live_button->setChecked(false);
360     }
361 }
362
363 void StopmotionWidget::slotShowOverlay(bool isOn)
364 {
365     if (isOn) {
366         if (live_button->isChecked() && m_sequenceFrame > 0) {
367             slotUpdateOverlay();
368         }
369     } else if (m_bmCapture) {
370         m_bmCapture->hideOverlay();
371     }
372 }
373
374 void StopmotionWidget::slotUpdateOverlay()
375 {
376     if (m_bmCapture == NULL) return;
377     QString path = getPathForFrame(m_sequenceFrame - 1);
378     if (!QFile::exists(path)) return;
379     QImage img(path);
380     if (img.isNull()) {
381         QTimer::singleShot(1000, this, SLOT(slotUpdateOverlay()));
382         return;
383     }
384
385 #ifdef QIMAGEBLITZ
386     //img = Blitz::convolveEdge(img, 0, Blitz::Low);
387     switch (m_effectIndex) {
388     case 2:
389         img = Blitz::contrast(img, true, 6);
390         break;
391     case 3:
392         img = Blitz::edge(img);
393         break;
394     case 4:
395         img = Blitz::intensity(img, 0.5);
396         break;
397     case 5:
398         Blitz::invert(img);
399         break;
400     case 6:
401         img = Blitz::threshold(img, 120, Blitz::Grayscale, qRgba(0, 0, 0, 0), qRgba(255, 0, 0, 255));
402         //img = Blitz::flatten(img, QColor(255, 0, 0, 255), QColor(0, 0, 0, 0));
403         break;
404     default:
405         break;
406
407     }
408 #endif
409     m_bmCapture->showOverlay(img);
410 }
411
412 void StopmotionWidget::sequenceNameChanged(const QString &name)
413 {
414     // Get rid of frames from previous sequence
415     disconnect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
416     m_filesList.clear();
417     m_future.waitForFinished();
418     frame_list->clear();
419     if (name.isEmpty()) {
420         button_addsequence->setEnabled(false);
421     } else {
422         // Check if we are editing an existing sequence
423         QString pattern = SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_filesList);
424         m_sequenceFrame = m_filesList.isEmpty() ? 0 : SlideshowClip::getFrameNumberFromPath(m_filesList.last()) + 1;
425         if (!m_filesList.isEmpty()) {
426             m_sequenceName = sequence_name->currentText();
427             connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
428             m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
429             button_addsequence->setEnabled(true);
430         } else {
431             // new sequence
432             connect(this, SIGNAL(doCreateThumbs(QImage, int)), this, SLOT(slotCreateThumbs(QImage, int)));
433             button_addsequence->setEnabled(false);
434         }
435         capture_button->setEnabled(live_button->isChecked());
436     }
437 }
438
439 void StopmotionWidget::slotCaptureFrame()
440 {
441     if (m_bmCapture == NULL) return;
442     if (sequence_name->currentText().isEmpty()) {
443         QString seqName = QInputDialog::getText(this, i18n("Create New Sequence"), i18n("Enter sequence name"));
444         if (seqName.isEmpty()) return;
445         sequence_name->blockSignals(true);
446         sequence_name->setItemText(sequence_name->currentIndex(), seqName);
447         sequence_name->blockSignals(false);
448     }
449     if (m_sequenceName != sequence_name->currentText()) {
450         m_sequenceName = sequence_name->currentText();
451         m_sequenceFrame = 0;
452     }
453     //capture_button->setEnabled(false);
454     QString currentPath = getPathForFrame(m_sequenceFrame);
455     m_bmCapture->captureFrame(currentPath);
456     KNotification::event("FrameCaptured");
457     m_sequenceFrame++;
458     button_addsequence->setEnabled(true);
459     if (m_intervalCapture->isChecked()) QTimer::singleShot(KdenliveSettings::captureinterval() * 1000, this, SLOT(slotCaptureFrame()));
460 }
461
462
463 void StopmotionWidget::slotNewThumb(const QString path)
464 {
465     if (!KdenliveSettings::showstopmotionthumbs()) return;
466     m_filesList.append(path);
467     if (m_showOverlay->isChecked()) slotUpdateOverlay();
468     if (!m_future.isRunning()) m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
469 }
470
471 void StopmotionWidget::slotPrepareThumbs()
472 {
473     if (m_filesList.isEmpty()) return;
474     QString path = m_filesList.takeFirst();
475     emit doCreateThumbs(QImage(path), SlideshowClip::getFrameNumberFromPath(path));
476
477 }
478
479 void StopmotionWidget::slotCreateThumbs(QImage img, int ix)
480 {
481     if (img.isNull()) {
482         m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
483         return;
484     }
485     int height = 90;
486     int width = height * img.width() / img.height();
487     frame_list->setIconSize(QSize(width, height));
488     QPixmap pix = QPixmap::fromImage(img).scaled(width, height);
489     QString nb = QString::number(ix);
490     QPainter p(&pix);
491     QFontInfo finfo(font());
492     p.fillRect(0, 0, finfo.pixelSize() * nb.count() + 6, finfo.pixelSize() + 6, QColor(80, 80, 80, 150));
493     p.setPen(Qt::white);
494     p.drawText(QPoint(3, finfo.pixelSize() + 3), nb);
495     p.end();
496     QIcon icon(pix);
497     QListWidgetItem *item = new QListWidgetItem(icon, QString(), frame_list);
498     item->setToolTip(getPathForFrame(ix, sequence_name->currentText()));
499     item->setData(Qt::UserRole, ix);
500     frame_list->blockSignals(true);
501     frame_list->setCurrentItem(item);
502     frame_list->blockSignals(false);
503     m_future = QtConcurrent::run(this, &StopmotionWidget::slotPrepareThumbs);
504 }
505
506 QString StopmotionWidget::getPathForFrame(int ix, QString seqName)
507 {
508     if (seqName.isEmpty()) seqName = m_sequenceName;
509     return m_projectFolder.path(KUrl::AddTrailingSlash) + seqName + "_" + QString::number(ix).rightJustified(4, '0', false) + ".png";
510 }
511
512 void StopmotionWidget::slotShowFrame(const QString &path)
513 {
514     //slotLive(false);
515     QImage img(path);
516     capture_button->setEnabled(false);
517     if (!img.isNull()) {
518         if (m_bmCapture) m_bmCapture->hidePreview(true);
519         m_frame_preview->setImage(img);
520         m_frame_preview->setHidden(false);
521         m_frame_preview->update();
522     }
523 }
524
525 void StopmotionWidget::slotShowSelectedFrame()
526 {
527     QListWidgetItem *item = frame_list->currentItem();
528     if (item) {
529         //int ix = item->data(Qt::UserRole).toInt();;
530         slotShowFrame(item->toolTip());
531     }
532 }
533
534 void StopmotionWidget::slotAddSequence()
535 {
536     emit addOrUpdateSequence(getPathForFrame(0));
537 }
538
539 void StopmotionWidget::slotPlayPreview(bool animate)
540 {
541     if (!animate) {
542         // stop animation
543         m_animationList.clear();
544         return;
545     }
546     if (KdenliveSettings::showstopmotionthumbs()) {
547         frame_list->setCurrentRow(0);
548         QTimer::singleShot(200, this, SLOT(slotAnimate()));
549     } else {
550         SlideshowClip::selectedPath(getPathForFrame(0, sequence_name->currentText()), false, QString(), &m_animationList);
551         slotAnimate();
552     }
553 }
554
555 void StopmotionWidget::slotAnimate()
556 {
557     //slotShowFrame(m_animatedIndex);
558     if (KdenliveSettings::showstopmotionthumbs()) {
559         //TODO: loop
560         if (frame_list->currentRow() < (frame_list->count() - 1)) {
561             frame_list->setCurrentRow(frame_list->currentRow() + 1);
562             QTimer::singleShot(100, this, SLOT(slotAnimate()));
563         } else preview_button->setChecked(false);
564     } else if (!m_animationList.isEmpty()) {
565         slotShowFrame(m_animationList.takeFirst());
566         QTimer::singleShot(100, this, SLOT(slotAnimate()));
567     } else preview_button->setChecked(false);
568
569 }
570
571 QListWidgetItem *StopmotionWidget::getFrameFromIndex(int ix)
572 {
573     QListWidgetItem *item = NULL;
574     int pos = ix;
575     if (ix >= frame_list->count()) {
576         pos = frame_list->count() - 1;
577     }
578     if (ix < 0) pos = 0;
579     item = frame_list->item(pos);
580
581     int value = item->data(Qt::UserRole).toInt();
582     if (value == ix) return item;
583     else if (value < ix) {
584         pos++;
585         while (pos < frame_list->count()) {
586             item = frame_list->item(pos);
587             value = item->data(Qt::UserRole).toInt();
588             if (value == ix) return item;
589             pos++;
590         }
591     } else {
592         pos --;
593         while (pos >= 0) {
594             item = frame_list->item(pos);
595             value = item->data(Qt::UserRole).toInt();
596             if (value == ix) return item;
597             pos --;
598         }
599     }
600     return NULL;
601 }
602
603
604 void StopmotionWidget::selectFrame(int ix)
605 {
606     frame_list->blockSignals(true);
607     QListWidgetItem *item = getFrameFromIndex(ix);
608     if (!item) return;
609     frame_list->setCurrentItem(item);
610     frame_list->blockSignals(false);
611 }
612
613 void StopmotionWidget::slotSeekFrame(bool forward)
614 {
615     int ix = frame_list->currentRow();
616     if (forward) {
617         if (ix < frame_list->count() - 1) frame_list->setCurrentRow(ix + 1);
618     } else if (ix > 0) frame_list->setCurrentRow(ix - 1);
619 }
620
621 void StopmotionWidget::slotRemoveFrame()
622 {
623     if (frame_list->currentItem() == NULL) return;
624     QString path = frame_list->currentItem()->toolTip();
625     if (KMessageBox::questionYesNo(this, i18n("Delete frame %1 from disk?", path), i18n("Delete Frame")) != KMessageBox::Yes) return;
626     QFile f(path);
627     if (f.remove()) {
628         QListWidgetItem *item = frame_list->takeItem(frame_list->currentRow());
629         delete item;
630     }
631 }