Warning, file /multimedia/stopmotion/src/application/runanimationhandler.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2017 by Linuxstopmotion contributors;              *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "src/application/runanimationhandler.h"
0021 
0022 #include <algorithm>
0023 #include <QPushButton>
0024 #include <QTimer>
0025 #include <QStatusBar>
0026 
0027 #include "src/foundation/preferencestool.h"
0028 #include "src/foundation/uiexception.h"
0029 #include "src/domain/domainfacade.h"
0030 #include "src/presentation/frontends/selection.h"
0031 #include "src/presentation/frontends/frontend.h"
0032 
0033 RunAnimationHandler::RunAnimationHandler(QObject *parent, QStatusBar *sb,
0034         Selection *sel, const char *name )
0035         : QObject(parent), statusBar(sb), selection(sel),
0036           playButton(0), removeFramesButton(0), loopButton(0),
0037           pauseButton(0), timer(0), sceneNr(0), frameNr(0),
0038           fps(0), isLooping(false),
0039           startFrame(-1), endFrame(0) {
0040     fps = PreferencesTool::get()->getPreference("fps", 10);
0041     timer = new QTimer(this);
0042     QObject::connect( timer, SIGNAL(timeout()), this, SLOT(playNextFrame()) );
0043     setObjectName(name);
0044 }
0045 
0046 
0047 void RunAnimationHandler::setPlayButton( QPushButton * playButton ) {
0048     this->playButton = playButton;
0049 }
0050 
0051 
0052 void RunAnimationHandler::setRemoveFramesButton(
0053         QPushButton * removeFramesButton) {
0054     this->removeFramesButton = removeFramesButton;
0055 }
0056 
0057 
0058 void RunAnimationHandler::setLoopButton(QPushButton * loopButton) {
0059     this->loopButton = loopButton;
0060 }
0061 
0062 
0063 void RunAnimationHandler::toggleRunning() {
0064     if(timer->isActive()) {
0065         stopAnimation();
0066     } else if (startFrame < 0) {
0067         runAnimation();
0068     } else {
0069         resumeAnimation();
0070     }
0071 }
0072 
0073 void RunAnimationHandler::resumeAnimation() {
0074     DomainFacade *f = DomainFacade::getFacade();
0075     int sceneSize = f->getSceneSize(sceneNr);
0076     if (0 <= sceneNr && sceneNr < f->getNumberOfScenes() && sceneSize > 0) {
0077         if (sceneSize < endFrame)
0078             endFrame = sceneSize;
0079         if (endFrame <= startFrame) {
0080             startFrame = endFrame;
0081             stopAnimation();
0082         }
0083         if (endFrame <= frameNr)
0084             frameNr = startFrame;
0085         f->initAudioDevice();
0086         QObject::disconnect( playButton, SIGNAL(clicked()), this, SLOT(runAnimation()) );
0087         QObject::connect( playButton, SIGNAL(clicked()), this, SLOT(pauseAnimation()) );
0088 
0089         //playButton->setToggleButton(true);
0090         playButton->setChecked(true);
0091         playButton->toggle();
0092         removeFramesButton->setEnabled(false);
0093         statusBar->showMessage( tr("Running animation"), 2000 );
0094         timer->start( 1000/fps);
0095         timer->setSingleShot(false);
0096     }
0097 }
0098 
0099 void RunAnimationHandler::runAnimation() {
0100     sceneNr = 0;
0101     startFrame = 0;
0102     endFrame = 0;
0103     int activeFrame = selection->getActiveFrame();
0104     if (selection) {
0105         sceneNr = selection->getActiveScene();
0106         startFrame = activeFrame;
0107         int sel = selection->getSelectionAnchor();
0108         if (startFrame < sel) {
0109             endFrame = sel + 1;
0110         } else {
0111             endFrame = startFrame + 1;
0112             startFrame = sel;
0113         }
0114     }
0115     if (endFrame - startFrame <= 1) {
0116         // only one or zero frames selected. Play the entire scene from the
0117         // selected frame.
0118         startFrame = std::max(activeFrame, 0);
0119         endFrame = DomainFacade::getFacade()->getSceneSize(sceneNr) ;
0120     }
0121     frameNr = startFrame;
0122     resumeAnimation();
0123 }
0124 
0125 
0126 void RunAnimationHandler::stopAnimation() {
0127     if ( timer->isActive() ) {
0128         QObject::disconnect( playButton, SIGNAL(clicked()), this, SLOT(pauseAnimation()) );
0129         QObject::connect(playButton, SIGNAL(clicked()), this, SLOT(runAnimation()));
0130 
0131         if ( playButton->isChecked() ) {
0132             playButton->toggle();
0133         }
0134 
0135         playButton->setChecked(false);
0136         removeFramesButton->setEnabled(true);
0137 
0138         DomainFacade *f = DomainFacade::getFacade();
0139         f->shutdownAudioDevice();
0140 
0141         statusBar->clearMessage();
0142         timer->stop();
0143         if (startFrame < endFrame)
0144             emit stopped(sceneNr, startFrame, endFrame - 1);
0145         startFrame = -1;
0146     }
0147 }
0148 
0149 
0150 void RunAnimationHandler::setPauseButton(QPushButton * pauseButton) {
0151     this->pauseButton = pauseButton;
0152 }
0153 
0154 
0155 void RunAnimationHandler::pauseAnimation() {
0156     if ( timer->isActive() ) {
0157         QObject::disconnect( playButton, SIGNAL(clicked()), this, SLOT(pauseAnimation()) );
0158         QObject::connect(playButton, SIGNAL(clicked()), this, SLOT(runAnimation()));
0159 
0160         playButton->setChecked(false);
0161         removeFramesButton->setEnabled(true);
0162 
0163         emit paused();
0164         DomainFacade *f = DomainFacade::getFacade();
0165         f->shutdownAudioDevice();
0166 
0167         statusBar->clearMessage();
0168         timer->stop();
0169     }
0170 
0171 }
0172 
0173 
0174 void RunAnimationHandler::setSpeed(int fps) {
0175     this->fps = fps;
0176     if ( timer->isActive() ) {
0177         timer->setInterval(1000/this->fps);
0178     }
0179     PreferencesTool* preferences = PreferencesTool::get();
0180     preferences->setPreference("fps", fps);
0181     try {
0182         preferences->flush();
0183     } catch (UiException& ex) {
0184         DomainFacade::getFacade()->getFrontend()->handleException(ex);
0185     }
0186 }
0187 
0188 
0189 void RunAnimationHandler::toggleLooping() {
0190     isLooping = !isLooping;
0191 }
0192 
0193 
0194 void RunAnimationHandler::playNextFrame() {
0195     DomainFacade *facade = DomainFacade::getFacade();
0196     if (sceneNr >= 0) {
0197         emit playFrame(sceneNr, frameNr);
0198         facade->playSounds(sceneNr, frameNr);
0199         ++frameNr;
0200         int sceneSize = facade->getSceneSize(sceneNr);
0201         if (sceneSize < endFrame)
0202             endFrame = sceneSize;
0203         if (frameNr < endFrame)
0204             return;
0205         if (isLooping) {
0206             frameNr = startFrame;
0207             return;
0208         }
0209     }
0210     stopAnimation();
0211 }