Commit 42b4e6c8 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

Observer: Fixed synchronization of animation speed and paused state

The menus of the Observer were not being updated correctly when
animation speed was changed or when the animations were paused.

Now the whole thing looks a bit cleaner and the Observer menu
uses the QDeclarativeViewObserver to change these settings and stays in
sync when they change from other places.

Reviewed-by: Kai Koehne
parent bc8d61ef
......@@ -46,9 +46,8 @@ namespace QmlJSDebugger {
QmlToolBar::QmlToolBar(QWidget *parent)
: QToolBar(parent)
, m_emitSignals(true)
, m_isRunning(false)
, m_paused(false)
, m_animationSpeed(1.0f)
, m_previousAnimationSpeed(0.0f)
, ui(new Ui)
{
ui->playIcon = QIcon(QLatin1String(":/qml/images/play-24.png"));
......@@ -99,40 +98,35 @@ QmlToolBar::QmlToolBar(QWidget *parent)
setWindowFlags(Qt::Tool);
QMenu *playSpeedMenu = new QMenu(this);
QActionGroup *playSpeedMenuActions = new QActionGroup(this);
playSpeedMenuActions->setExclusive(true);
playSpeedMenu->addAction(tr("Animation Speed"));
playSpeedMenu->addSeparator();
ui->defaultAnimSpeedAction = playSpeedMenu->addAction(tr("1x"), this,
SLOT(changeToDefaultAnimSpeed()));
ui->defaultAnimSpeedAction->setCheckable(true);
ui->defaultAnimSpeedAction->setChecked(true);
playSpeedMenuActions->addAction(ui->defaultAnimSpeedAction);
ui->halfAnimSpeedAction = playSpeedMenu->addAction(tr("0.5x"), this,
SLOT(changeToHalfAnimSpeed()));
ui->halfAnimSpeedAction->setCheckable(true);
playSpeedMenuActions->addAction(ui->halfAnimSpeedAction);
ui->fourthAnimSpeedAction = playSpeedMenu->addAction(tr("0.25x"), this,
SLOT(changeToFourthAnimSpeed()));
ui->fourthAnimSpeedAction->setCheckable(true);
playSpeedMenuActions->addAction(ui->fourthAnimSpeedAction);
ui->eighthAnimSpeedAction = playSpeedMenu->addAction(tr("0.125x"), this,
SLOT(changeToEighthAnimSpeed()));
ui->eighthAnimSpeedAction->setCheckable(true);
playSpeedMenuActions->addAction(ui->eighthAnimSpeedAction);
ui->tenthAnimSpeedAction = playSpeedMenu->addAction(tr("0.1x"), this,
SLOT(changeToTenthAnimSpeed()));
ui->tenthAnimSpeedAction->setCheckable(true);
playSpeedMenuActions->addAction(ui->tenthAnimSpeedAction);
ui->menuPauseAction = playSpeedMenu->addAction(tr("Pause"), this, SLOT(updatePauseAction()));
ui->menuPauseAction->setCheckable(true);
ui->menuPauseAction->setIcon(ui->pauseIcon);
playSpeedMenuActions->addAction(ui->menuPauseAction);
ui->playSpeedMenuActions = new QActionGroup(this);
ui->playSpeedMenuActions->setExclusive(true);
QAction *speedAction = playSpeedMenu->addAction(tr("1x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setChecked(true);
speedAction->setData(1.0f);
ui->playSpeedMenuActions->addAction(speedAction);
speedAction = playSpeedMenu->addAction(tr("0.5x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setData(2.0f);
ui->playSpeedMenuActions->addAction(speedAction);
speedAction = playSpeedMenu->addAction(tr("0.25x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setData(4.0f);
ui->playSpeedMenuActions->addAction(speedAction);
speedAction = playSpeedMenu->addAction(tr("0.125x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setData(8.0f);
ui->playSpeedMenuActions->addAction(speedAction);
speedAction = playSpeedMenu->addAction(tr("0.1x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setData(10.0f);
ui->playSpeedMenuActions->addAction(speedAction);
ui->play->setMenu(playSpeedMenu);
connect(ui->designmode, SIGNAL(toggled(bool)), SLOT(setDesignModeBehaviorOnClick(bool)));
......@@ -183,63 +177,40 @@ void QmlToolBar::activateZoom()
m_emitSignals = true;
}
void QmlToolBar::setAnimationSpeed(qreal slowdownFactor)
void QmlToolBar::setAnimationSpeed(qreal slowDownFactor)
{
if (m_animationSpeed == slowDownFactor)
return;
m_emitSignals = false;
if (slowdownFactor != 0) {
m_animationSpeed = slowdownFactor;
if (slowdownFactor == 1.0f) {
ui->defaultAnimSpeedAction->setChecked(true);
} else if (slowdownFactor == 2.0f) {
ui->halfAnimSpeedAction->setChecked(true);
} else if (slowdownFactor == 4.0f) {
ui->fourthAnimSpeedAction->setChecked(true);
} else if (slowdownFactor == 8.0f) {
ui->eighthAnimSpeedAction->setChecked(true);
} else if (slowdownFactor == 10.0f) {
ui->tenthAnimSpeedAction->setChecked(true);
m_animationSpeed = slowDownFactor;
foreach (QAction *action, ui->playSpeedMenuActions->actions()) {
if (action->data().toReal() == slowDownFactor) {
action->setChecked(true);
break;
}
updatePlayAction();
} else {
ui->menuPauseAction->setChecked(true);
updatePauseAction();
}
m_emitSignals = true;
}
void QmlToolBar::changeToDefaultAnimSpeed()
{
m_animationSpeed = 1.0f;
updatePlayAction();
}
void QmlToolBar::changeToHalfAnimSpeed()
{
m_animationSpeed = 2.0f;
updatePlayAction();
}
void QmlToolBar::changeToFourthAnimSpeed()
void QmlToolBar::setExecutionPaused(bool paused)
{
m_animationSpeed = 4.0f;
updatePlayAction();
}
if (m_paused == paused)
return;
void QmlToolBar::changeToEighthAnimSpeed()
{
m_animationSpeed = 8.0f;
m_paused = paused;
updatePlayAction();
}
void QmlToolBar::changeToTenthAnimSpeed()
void QmlToolBar::changeAnimationSpeed()
{
m_animationSpeed = 10.0f;
updatePlayAction();
QAction *action = qobject_cast<QAction*>(sender());
m_animationSpeed = action->data().toReal();
emit animationSpeedChanged(m_animationSpeed);
}
void QmlToolBar::setDesignModeBehavior(bool inDesignMode)
{
m_emitSignals = false;
......@@ -268,30 +239,14 @@ void QmlToolBar::setColorBoxColor(const QColor &color)
void QmlToolBar::activatePlayOnClick()
{
if (m_isRunning) {
updatePauseAction();
} else {
updatePlayAction();
}
m_paused = !m_paused;
emit executionPausedChanged(m_paused);
updatePlayAction();
}
void QmlToolBar::updatePlayAction()
{
m_isRunning = true;
ui->play->setIcon(ui->pauseIcon);
if (m_animationSpeed != m_previousAnimationSpeed)
m_previousAnimationSpeed = m_animationSpeed;
if (m_emitSignals)
emit animationSpeedChanged(m_animationSpeed);
}
void QmlToolBar::updatePauseAction()
{
m_isRunning = false;
ui->play->setIcon(ui->playIcon);
if (m_emitSignals)
emit animationSpeedChanged(0.0f);
ui->play->setIcon(m_paused ? ui->playIcon : ui->pauseIcon);
}
void QmlToolBar::activateColorPickerOnClick()
......
......@@ -39,6 +39,8 @@
#include "qmlobserverconstants.h"
QT_FORWARD_DECLARE_CLASS(QActionGroup)
namespace QmlJSDebugger {
class ToolBarColorBox;
......@@ -58,10 +60,13 @@ public slots:
void activateSelectTool();
void activateMarqueeSelectTool();
void activateZoom();
void setAnimationSpeed(qreal slowdownFactor = 0.0f);
void setAnimationSpeed(qreal slowDownFactor);
void setExecutionPaused(bool paused);
signals:
void animationSpeedChanged(qreal slowdownFactor = 1.0f);
void animationSpeedChanged(qreal factor);
void executionPausedChanged(bool paused);
void designModeBehaviorChanged(bool inDesignMode);
void colorPickerSelected();
......@@ -83,14 +88,9 @@ private slots:
void activateFromQml();
void activateToQml();
void changeToDefaultAnimSpeed();
void changeToHalfAnimSpeed();
void changeToFourthAnimSpeed();
void changeToEighthAnimSpeed();
void changeToTenthAnimSpeed();
void changeAnimationSpeed();
void updatePlayAction();
void updatePauseAction();
private:
class Ui {
......@@ -107,18 +107,12 @@ private:
QIcon pauseIcon;
ToolBarColorBox *colorBox;
QAction *defaultAnimSpeedAction;
QAction *halfAnimSpeedAction;
QAction *fourthAnimSpeedAction;
QAction *eighthAnimSpeedAction;
QAction *tenthAnimSpeedAction;
QAction *menuPauseAction;
QActionGroup *playSpeedMenuActions;
};
bool m_emitSignals;
bool m_isRunning;
bool m_paused;
qreal m_animationSpeed;
qreal m_previousAnimationSpeed;
Constants::DesignTool m_activeTool;
......
......@@ -73,9 +73,8 @@ public Q_SLOTS:
void setShowAppOnTop(bool appOnTop);
void changeAnimationSpeed(qreal slowdownFactor);
void continueExecution(qreal slowdownFactor = 1.0f);
void pauseExecution();
void setAnimationSpeed(qreal factor);
void setExecutionPaused(bool paused);
void setObserverContext(int contextIndex);
......@@ -89,8 +88,8 @@ Q_SIGNALS:
void colorPickerActivated();
void selectedColorChanged(const QColor &color);
void executionStarted(qreal slowdownFactor);
void executionPaused();
void animationSpeedChanged(qreal factor);
void executionPausedChanged(bool paused);
void inspectorContextCleared();
void inspectorContextPushed(const QString &contextTitle);
......@@ -110,6 +109,9 @@ protected:
void setSelectedItemsForTools(QList<QGraphicsItem *> items);
private slots:
void onAnimationSpeedChangeRequested(qreal factor);
private:
Q_DISABLE_COPY(QDeclarativeViewObserver)
......
......@@ -224,7 +224,6 @@ void QDeclarativeObserverService::setCurrentTool(QmlJSDebugger::Constants::Desig
void QDeclarativeObserverService::setAnimationSpeed(qreal slowdownFactor)
{
QByteArray message;
QDataStream ds(&message, QIODevice::WriteOnly);
......
......@@ -106,7 +106,7 @@ QDeclarativeViewObserverPrivate::QDeclarativeViewObserverPrivate(QDeclarativeVie
designModeBehavior(false),
showAppOnTop(false),
executionPaused(false),
slowdownFactor(1.0f),
slowDownFactor(1.0f),
toolBox(0)
{
}
......@@ -147,7 +147,7 @@ QDeclarativeViewObserver::QDeclarativeViewObserver(QDeclarativeView *view, QObje
connect(data->debugService, SIGNAL(currentObjectsChanged(QList<QObject*>)),
data.data(), SLOT(_q_onCurrentObjectsChanged(QList<QObject*>)));
connect(data->debugService, SIGNAL(animationSpeedChangeRequested(qreal)),
SLOT(changeAnimationSpeed(qreal)));
SLOT(onAnimationSpeedChangeRequested(qreal)));
connect(data->debugService, SIGNAL(colorPickerToolRequested()),
data.data(), SLOT(_q_changeToColorPickerTool()));
connect(data->debugService, SIGNAL(selectMarqueeToolRequested()),
......@@ -384,11 +384,7 @@ bool QDeclarativeViewObserver::keyReleaseEvent(QKeyEvent *event)
data->subcomponentEditorTool->setCurrentItem(data->selectedItems().first());
break;
case Qt::Key_Space:
if (data->executionPaused) {
continueExecution(data->slowdownFactor);
} else {
pauseExecution();
}
setExecutionPaused(!data->executionPaused);
break;
default:
break;
......@@ -757,44 +753,47 @@ void QDeclarativeViewObserverPrivate::_q_changeContextPathIndex(int index)
subcomponentEditorTool->setContext(index);
}
void QDeclarativeViewObserver::changeAnimationSpeed(qreal slowdownFactor)
void QDeclarativeViewObserver::setAnimationSpeed(qreal factor)
{
data->slowdownFactor = slowdownFactor;
Q_ASSERT(factor > 0);
if (data->slowDownFactor == factor)
return;
if (data->slowdownFactor != 0)
continueExecution(data->slowdownFactor);
else
pauseExecution();
data->slowDownFactor = factor;
emit animationSpeedChanged(factor);
const float effectiveFactor = data->executionPaused ? 0 : factor;
onAnimationSpeedChangeRequested(effectiveFactor);
data->debugService->setAnimationSpeed(effectiveFactor);
}
void QDeclarativeViewObserver::continueExecution(qreal slowdownFactor)
void QDeclarativeViewObserver::setExecutionPaused(bool paused)
{
Q_ASSERT(slowdownFactor > 0);
data->slowdownFactor = slowdownFactor;
static const qreal animSpeedSnapDelta = 0.01f;
qreal slowDownFactor = data->slowdownFactor;
if (qAbs(1.0f - slowDownFactor) < animSpeedSnapDelta) {
slowDownFactor = 1.0f;
}
QDeclarativeDebugHelper::setAnimationSlowDownFactor(slowDownFactor);
data->executionPaused = false;
if (data->executionPaused == paused)
return;
emit executionStarted(data->slowdownFactor);
data->debugService->setAnimationSpeed(data->slowdownFactor);
const float effectiveFactor = paused ? 0 : data->slowDownFactor;
onAnimationSpeedChangeRequested(effectiveFactor);
data->debugService->setAnimationSpeed(effectiveFactor);
}
void QDeclarativeViewObserver::pauseExecution()
void QDeclarativeViewObserver::onAnimationSpeedChangeRequested(qreal factor)
{
QDeclarativeDebugHelper::setAnimationSlowDownFactor(0.0f);
data->executionPaused = true;
const bool paused = factor == 0;
if (!paused && data->slowDownFactor != factor) {
data->slowDownFactor = factor;
emit animationSpeedChanged(factor);
}
if (data->executionPaused != paused) {
data->executionPaused = paused;
emit executionPausedChanged(paused);
}
emit executionPaused();
data->debugService->setAnimationSpeed(0);
QDeclarativeDebugHelper::setAnimationSlowDownFactor(factor);
}
void QDeclarativeViewObserverPrivate::_q_applyChangesFromClient()
{
}
......@@ -850,8 +849,6 @@ void QDeclarativeViewObserverPrivate::_q_onStatusChanged(QDeclarativeView::Statu
if (subcomponentEditorTool->contextIndex() != -1)
subcomponentEditorTool->clear();
subcomponentEditorTool->pushContext(view->rootObject());
emit q->executionStarted(1.0f);
}
debugService->reloaded();
}
......@@ -917,8 +914,8 @@ void QDeclarativeViewObserverPrivate::createToolBox()
QObject::connect(toolBar, SIGNAL(designModeBehaviorChanged(bool)),
q, SLOT(setDesignModeBehavior(bool)));
QObject::connect(toolBar, SIGNAL(animationSpeedChanged(qreal)),
q, SLOT(changeAnimationSpeed(qreal)));
QObject::connect(toolBar, SIGNAL(animationSpeedChanged(qreal)), q, SLOT(setAnimationSpeed(qreal)));
QObject::connect(toolBar, SIGNAL(executionPausedChanged(bool)), q, SLOT(setExecutionPaused(bool)));
QObject::connect(toolBar, SIGNAL(colorPickerSelected()), this, SLOT(_q_changeToColorPickerTool()));
QObject::connect(toolBar, SIGNAL(zoomToolSelected()), this, SLOT(_q_changeToZoomTool()));
QObject::connect(toolBar, SIGNAL(selectToolSelected()), this, SLOT(_q_changeToSingleSelectTool()));
......@@ -928,8 +925,8 @@ void QDeclarativeViewObserverPrivate::createToolBox()
QObject::connect(toolBar, SIGNAL(applyChangesFromQmlFileSelected()),
this, SLOT(_q_applyChangesFromClient()));
QObject::connect(q, SIGNAL(executionStarted(qreal)), toolBar, SLOT(setAnimationSpeed(qreal)));
QObject::connect(q, SIGNAL(executionPaused()), toolBar, SLOT(setAnimationSpeed()));
QObject::connect(q, SIGNAL(animationSpeedChanged(qreal)), toolBar, SLOT(setAnimationSpeed(qreal)));
QObject::connect(q, SIGNAL(executionPausedChanged(bool)), toolBar, SLOT(setExecutionPaused(bool)));
QObject::connect(q, SIGNAL(selectToolActivated()), toolBar, SLOT(activateSelectTool()));
......
......@@ -89,7 +89,7 @@ public:
bool showAppOnTop;
bool executionPaused;
qreal slowdownFactor;
qreal slowDownFactor;
ToolBox *toolBox;
......
......@@ -114,7 +114,6 @@
#include <qdeclarativetester.h>
#include <qt_private/qdeclarativedebughelper_p.h>
#include "jsdebuggeragent.h"
QT_BEGIN_NAMESPACE
......@@ -759,13 +758,12 @@ void QDeclarativeViewer::createMenu()
connect(recordOptions, SIGNAL(triggered()), this, SLOT(chooseRecordingOptions()));
QMenu *playSpeedMenu = new QMenu(tr("Animation Speed"), this);
QActionGroup *playSpeedMenuActions = new QActionGroup(this);
playSpeedMenuActions = new QActionGroup(this);
playSpeedMenuActions->setExclusive(true);
QAction *speedAction = playSpeedMenu->addAction(tr("1x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setChecked(true);
animationSpeed = 1.0f;
speedAction->setData(1.0f);
playSpeedMenuActions->addAction(speedAction);
......@@ -776,7 +774,6 @@ void QDeclarativeViewer::createMenu()
speedAction = playSpeedMenu->addAction(tr("0.25x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setCheckable(true);
speedAction->setData(4.0f);
playSpeedMenuActions->addAction(speedAction);
......@@ -790,7 +787,7 @@ void QDeclarativeViewer::createMenu()
speedAction->setData(10.0f);
playSpeedMenuActions->addAction(speedAction);
pauseAnimationsAction = playSpeedMenu->addAction(tr("Pause"), this, SLOT(setAnimationsPaused(bool)));
pauseAnimationsAction = playSpeedMenu->addAction(tr("Pause"), observer, SLOT(setExecutionPaused(bool)));
pauseAnimationsAction->setCheckable(true);
pauseAnimationsAction->setShortcut(QKeySequence("Ctrl+."));
......@@ -803,6 +800,9 @@ void QDeclarativeViewer::createMenu()
QAction *playSpeedAction = new QAction(tr("Animations"), this);
playSpeedAction->setMenu(playSpeedMenu);
connect(observer, SIGNAL(animationSpeedChanged(qreal)), SLOT(animationSpeedChanged(qreal)));
connect(observer, SIGNAL(executionPausedChanged(bool)), pauseAnimationsAction, SLOT(setChecked(bool)));
showWarningsWindow = new QAction(tr("Show Warnings"), this);
showWarningsWindow->setCheckable((true));
showWarningsWindow->setChecked(loggerWindow->isVisible());
......@@ -1078,24 +1078,15 @@ void QDeclarativeViewer::toggleRecording()
#endif
}
void QDeclarativeViewer::setAnimationsPaused(bool enable)
void QDeclarativeViewer::pauseAnimations()
{
if (enable) {
setAnimationSpeed(0.0);
} else {
setAnimationSpeed(animationSpeed);
}
}
void QDeclarativeViewer::pauseAnimations() {
pauseAnimationsAction->setChecked(true);
setAnimationsPaused(true);
observer->setExecutionPaused(true);
}
void QDeclarativeViewer::stepAnimations()
{
setAnimationSpeed(1.0);
QTimer::singleShot(m_stepSize, this, SLOT(pauseAnimations()));
observer->setExecutionPaused(false);
QTimer::singleShot(m_stepSize, this, SLOT(pauseAnimations()));
}
void QDeclarativeViewer::setAnimationStep()
......@@ -1108,13 +1099,8 @@ void QDeclarativeViewer::setAnimationStep()
void QDeclarativeViewer::changeAnimationSpeed()
{
QAction *action = qobject_cast<QAction*>(sender());
if (action) {
float f = action->data().toFloat();
animationSpeed = f;
if (!pauseAnimationsAction->isChecked())
setAnimationSpeed(animationSpeed);
}
if (QAction *action = qobject_cast<QAction*>(sender()))
observer->setAnimationSpeed(action->data().toFloat());
}
void QDeclarativeViewer::addLibraryPath(const QString& lib)
......@@ -1574,6 +1560,16 @@ void QDeclarativeViewer::orientationChanged()
updateSizeHints();
}
void QDeclarativeViewer::animationSpeedChanged(qreal factor)
{
foreach (QAction *action, playSpeedMenuActions->actions()) {
if (action->data().toFloat() == factor) {
action->setChecked(true);
break;
}
}
}
void QDeclarativeViewer::setDeviceKeys(bool on)
{
devicemode = on;
......@@ -1625,11 +1621,6 @@ void QDeclarativeViewer::setStayOnTop(bool stayOnTop)
appOnTopAction->setChecked(stayOnTop);
}
void QDeclarativeViewer::setAnimationSpeed(float f)
{
QDeclarativeDebugHelper::setAnimationSlowDownFactor(f);
}
void QDeclarativeViewer::updateSizeHints(bool initial)
{
static bool isRecursive = false;
......
......@@ -130,7 +130,6 @@ public slots:
void proxySettingsChanged ();
void rotateOrientation();
void statusChanged();
void setAnimationsPaused(bool);
void pauseAnimations();
void stepAnimations();
void setAnimationStep();
......@@ -154,12 +153,13 @@ private slots:
void changeOrientation(QAction*);
void orientationChanged();
void animationSpeedChanged(qreal factor);
void showWarnings(bool show);
void warningsWidgetOpened();
void warningsWidgetClosed();
private:
void setAnimationSpeed(float f);
void updateSizeHints(bool initial = false);
QString getVideoFileName();
......@@ -192,8 +192,8 @@ private:
bool ffmpegAvailable;
bool convertAvailable;
float animationSpeed;