File indexing completed on 2024-05-05 11:56:11
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) 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 0050 void AnimationResultItem::update() 0051 { 0052 Q_ASSERT(m_result->type() == Cantor::AnimationResult::Type); 0053 QMovie* mov; 0054 switch(m_result->type()) { 0055 case Cantor::AnimationResult::Type: 0056 mov = static_cast<QMovie*>(m_result->data().value<QObject*>()); 0057 setMovie(mov); 0058 break; 0059 default: 0060 break; 0061 } 0062 } 0063 0064 QRectF AnimationResultItem::boundingRect() const 0065 { 0066 return QRectF(0, 0, width(), height()); 0067 } 0068 0069 double AnimationResultItem::width() const 0070 { 0071 return WorksheetImageItem::width(); 0072 } 0073 0074 double AnimationResultItem::height() const 0075 { 0076 return WorksheetImageItem::height(); 0077 } 0078 0079 0080 void AnimationResultItem::setMovie(QMovie* movie) 0081 { 0082 if (m_movie) { 0083 m_movie->disconnect(this, SLOT(updateFrame())); 0084 m_movie->disconnect(this, SLOT(updateSize())); 0085 } 0086 m_movie = movie; 0087 m_height = 0; 0088 if (m_movie) { 0089 connect(m_movie, &QMovie::frameChanged, this, &AnimationResultItem::updateFrame); 0090 connect(m_movie, &QMovie::resized, this, &AnimationResultItem::updateSize); 0091 m_movie->start(); 0092 } 0093 } 0094 0095 void AnimationResultItem::updateFrame() 0096 { 0097 setImage(m_movie->currentImage()); 0098 worksheet()->update(mapRectToScene(boundingRect())); 0099 } 0100 0101 void AnimationResultItem::updateSize(QSize size) 0102 { 0103 if (m_height != size.height()) { 0104 m_height = size.height(); 0105 emit sizeChanged(); 0106 } 0107 } 0108 0109 void AnimationResultItem::saveResult() 0110 { 0111 const QString& filename = QFileDialog::getSaveFileName(worksheet()->worksheetView(), i18n("Save animation result"), QString(), i18n("Animations (*.gif)")); 0112 result()->save(filename); 0113 } 0114 0115 void AnimationResultItem::stopMovie() 0116 { 0117 if (m_movie) { 0118 m_movie->stop(); 0119 m_movie->jumpToFrame(0); 0120 worksheet()->update(mapRectToScene(boundingRect())); 0121 } 0122 } 0123 0124 void AnimationResultItem::pauseMovie() 0125 { 0126 if (m_movie) 0127 m_movie->setPaused(true); 0128 } 0129 0130 void AnimationResultItem::deleteLater() 0131 { 0132 WorksheetImageItem::deleteLater(); 0133 }