feat: animation pause and manually step to next frame

Resolve: https://github.com/BLumia/pineapple-pictures/issues/85
This commit is contained in:
Gary Wang 2024-11-06 20:28:39 +08:00
parent a993437983
commit 42e3d4c691
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
6 changed files with 59 additions and 1 deletions

View File

@ -61,6 +61,9 @@ void ActionManager::setupAction(MainWindow *mainWindow)
CREATE_NEW_ACTION(mainWindow, actionPrevPicture);
CREATE_NEW_ACTION(mainWindow, actionNextPicture);
CREATE_NEW_ACTION(mainWindow, actionTogglePauseAnimation);
CREATE_NEW_ACTION(mainWindow, actionAnimationNextFrame);
CREATE_NEW_THEMEICON_ACTION(mainWindow, actionOpen, document-open);
CREATE_NEW_ACTION(mainWindow, actionHorizontalFlip);
CREATE_NEW_ACTION(mainWindow, actionFitInView);
@ -102,6 +105,9 @@ void ActionManager::retranslateUi(MainWindow *mainWindow)
actionPrevPicture->setText(QCoreApplication::translate("MainWindow", "Previous image", nullptr));
actionNextPicture->setText(QCoreApplication::translate("MainWindow", "Next image", nullptr));
actionTogglePauseAnimation->setText(QCoreApplication::translate("MainWindow", "Pause/Resume Animation", nullptr));
actionAnimationNextFrame->setText(QCoreApplication::translate("MainWindow", "Animation Go to Next Frame", nullptr));
actionHorizontalFlip->setText(QCoreApplication::translate("MainWindow", "Flip &Horizontally", nullptr));
actionFitInView->setText(QCoreApplication::translate("MainWindow", "Fit to view", nullptr));
actionFitByWidth->setText(QCoreApplication::translate("MainWindow", "Fit to width", nullptr));

View File

@ -35,6 +35,9 @@ public:
QAction *actionPrevPicture;
QAction *actionNextPicture;
QAction *actionTogglePauseAnimation;
QAction *actionAnimationNextFrame;
QAction *actionHorizontalFlip;
QAction *actionFitInView;
QAction *actionFitByWidth;

View File

@ -21,6 +21,9 @@ public:
: QGraphicsPixmapItem(pixmap, parent)
{}
enum { Type = UserType + 1 };
int type() const override { return Type; }
void setScaleHint(float scaleHint) {
m_scaleHint = scaleHint;
}
@ -61,6 +64,9 @@ class PGraphicsMovieItem : public QGraphicsItem
public:
PGraphicsMovieItem(QGraphicsItem *parent = nullptr) : QGraphicsItem(parent) {}
enum { Type = UserType + 2 };
int type() const override { return Type; }
void setMovie(QMovie* movie) {
if (m_movie) m_movie->disconnect();
m_movie.reset(movie);
@ -80,6 +86,10 @@ public:
}
}
inline QMovie * movie() const {
return m_movie.data();
}
private:
QScopedPointer<QMovie> m_movie;
};
@ -158,6 +168,29 @@ bool GraphicsScene::trySetTransformationMode(Qt::TransformationMode mode, float
return false;
}
bool GraphicsScene::togglePauseAnimation()
{
PGraphicsMovieItem * animatedItem = qgraphicsitem_cast<PGraphicsMovieItem *>(m_theThing);
if (animatedItem) {
animatedItem->movie()->setPaused(animatedItem->movie()->state() != QMovie::Paused);
return true;
}
return false;
}
bool GraphicsScene::skipAnimationFrame(int delta)
{
PGraphicsMovieItem * animatedItem = qgraphicsitem_cast<PGraphicsMovieItem *>(m_theThing);
if (animatedItem) {
const int frameCount = animatedItem->movie()->frameCount();
const int currentFrame = animatedItem->movie()->currentFrameNumber();
const int targetFrame = (currentFrame + delta) % frameCount;
animatedItem->movie()->setPaused(true);
return animatedItem->movie()->jumpToFrame(targetFrame);
}
return false;
}
QPixmap GraphicsScene::renderToPixmap()
{
PGraphicsPixmapItem * pixmapItem = qgraphicsitem_cast<PGraphicsPixmapItem *>(m_theThing);

View File

@ -21,10 +21,13 @@ public:
bool trySetTransformationMode(Qt::TransformationMode mode, float scaleHint);
bool togglePauseAnimation();
bool skipAnimationFrame(int delta = 1);
QPixmap renderToPixmap();
private:
QGraphicsItem * m_theThing;
QGraphicsItem * m_theThing = nullptr;
};
#endif // GRAPHICSSCENE_H

View File

@ -801,6 +801,16 @@ void MainWindow::on_actionNextPicture_triggered()
galleryNext();
}
void MainWindow::on_actionTogglePauseAnimation_triggered()
{
m_graphicsView->scene()->togglePauseAnimation();
}
void MainWindow::on_actionAnimationNextFrame_triggered()
{
m_graphicsView->scene()->skipAnimationFrame(1);
}
void MainWindow::on_actionToggleStayOnTop_triggered()
{
toggleStayOnTop();

View File

@ -92,6 +92,9 @@ private slots:
void on_actionPrevPicture_triggered();
void on_actionNextPicture_triggered();
void on_actionTogglePauseAnimation_triggered();
void on_actionAnimationNextFrame_triggered();
void on_actionHorizontalFlip_triggered();
void on_actionFitInView_triggered();
void on_actionFitByWidth_triggered();