File indexing completed on 2022-12-06 18:50:09

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0004 */
0005 
0006 #include "animationresultitem.h"
0007 #include "commandentry.h"
0008 #include "worksheetview.h"
0009 #include "lib/result.h"
0010 #include "lib/animationresult.h"
0011 
0012 #include <QFileDialog>
0013 #include <QMovie>
0014 
0015 #include <KLocalizedString>
0016 
0017 AnimationResultItem::AnimationResultItem(QGraphicsObject* parent, Cantor::Result* result)
0018     : WorksheetImageItem(parent), ResultItem(result)
0019 {
0020     update();
0021 }
0022 
0023 double AnimationResultItem::setGeometry(double x, double y, double w)
0024 {
0025     Q_UNUSED(w);
0026     setPos(x,y);
0027 
0028     return m_height;
0029 }
0030 
0031 void AnimationResultItem::populateMenu(QMenu* menu, QPointF pos)
0032 {
0033     ResultItem::addCommonActions(this, menu);
0034 
0035     menu->addSeparator();
0036     if (m_movie) {
0037         if (m_movie->state() == QMovie::Running)
0038             menu->addAction(QIcon::fromTheme(QLatin1String("media-playback-pause")), i18n("Pause"),
0039                             this, SLOT(pauseMovie()));
0040         else
0041             menu->addAction(QIcon::fromTheme(QLatin1String("media-playback-start")), i18n("Start"),
0042                             m_movie, SLOT(start()));
0043         if (m_movie->state() == QMovie::Running ||
0044             m_movie->state() == QMovie::Paused)
0045             menu->addAction(QIcon::fromTheme(QLatin1String("media-playback-stop")), i18n("Stop"),
0046                             this, SLOT(stopMovie()));
0047     }
0048 
0049     emit menuCreated(menu, mapToParent(pos));
0050 }
0051 
0052 void AnimationResultItem::update()
0053 {
0054     Q_ASSERT(m_result->type() == Cantor::AnimationResult::Type);
0055     QMovie* mov;
0056     switch(m_result->type()) {
0057     case Cantor::AnimationResult::Type:
0058         mov = static_cast<QMovie*>(m_result->data().value<QObject*>());
0059         setMovie(mov);
0060         break;
0061     default:
0062         break;
0063     }
0064 }
0065 
0066 QRectF AnimationResultItem::boundingRect() const
0067 {
0068     return QRectF(0, 0, width(), height());
0069 }
0070 
0071 double AnimationResultItem::width() const
0072 {
0073     return WorksheetImageItem::width();
0074 }
0075 
0076 double AnimationResultItem::height() const
0077 {
0078     return WorksheetImageItem::height();
0079 }
0080 
0081 
0082 void AnimationResultItem::setMovie(QMovie* movie)
0083 {
0084     if (m_movie) {
0085         m_movie->disconnect(this, SLOT(updateFrame()));
0086         m_movie->disconnect(this, SLOT(updateSize()));
0087     }
0088     m_movie = movie;
0089     m_height = 0;
0090     if (m_movie) {
0091         connect(m_movie, &QMovie::frameChanged, this, &AnimationResultItem::updateFrame);
0092         connect(m_movie, &QMovie::resized, this, &AnimationResultItem::updateSize);
0093         m_movie->start();
0094     }
0095 }
0096 
0097 void AnimationResultItem::updateFrame()
0098 {
0099     setImage(m_movie->currentImage());
0100     worksheet()->update(mapRectToScene(boundingRect()));
0101 }
0102 
0103 void AnimationResultItem::updateSize(QSize size)
0104 {
0105     if (m_height != size.height()) {
0106         m_height = size.height();
0107         emit sizeChanged();
0108     }
0109 }
0110 
0111 void AnimationResultItem::saveResult()
0112 {
0113     const QString& filename = QFileDialog::getSaveFileName(worksheet()->worksheetView(), i18n("Save animation result"), QString(), i18n("Animations (*.gif)"));
0114     result()->save(filename);
0115 }
0116 
0117 void AnimationResultItem::stopMovie()
0118 {
0119     if (m_movie) {
0120         m_movie->stop();
0121         m_movie->jumpToFrame(0);
0122         worksheet()->update(mapRectToScene(boundingRect()));
0123     }
0124 }
0125 
0126 void AnimationResultItem::pauseMovie()
0127 {
0128     if (m_movie)
0129         m_movie->setPaused(true);
0130 }
0131 
0132 void AnimationResultItem::deleteLater()
0133 {
0134     WorksheetImageItem::deleteLater();
0135 }