Commit 0a813cd6 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

QmlJSDebugger: Separated animation speed and paused state

This separates animation speed and paused state in the communication
protocol between the Qt Creator QmlJSInspector plugin and the
QmlJSDebugger server.

Point is to allow changing the speed of the animation before resuming
execution. These two things were already separated in the QML Observer,
but not in Qt Creator.

Reviewed-by: Kai Koehne
parent 1716d9bb
......@@ -65,7 +65,8 @@ public:
void setDesignModeBehavior(bool inDesignMode);
void setCurrentObjects(QList<QObject*> items);
void setAnimationSpeed(qreal slowdownFactor);
void setAnimationSpeed(qreal slowDownFactor);
void setExecutionPaused(bool paused);
void setCurrentTool(QmlJSDebugger::Constants::DesignTool toolId);
void reloaded();
void setShowAppOnTop(bool showAppOnTop);
......@@ -95,9 +96,9 @@ Q_SIGNALS:
void objectReparentRequested(QObject *object, QObject *newParent);
// 1 = normal speed,
// 0 = paused,
// 1 < x < 16 = slowdown by some factor
void animationSpeedChangeRequested(qreal speedFactor);
void executionPauseChangeRequested(bool paused);
void contextPathIndexChanged(int contextPathIndex);
void clearComponentCacheRequested();
......
......@@ -110,7 +110,8 @@ protected:
void setSelectedItemsForTools(QList<QGraphicsItem *> items);
private slots:
void onAnimationSpeedChangeRequested(qreal factor);
void animationSpeedChangeRequested(qreal factor);
void executionPausedChangeRequested(bool paused);
private:
Q_DISABLE_COPY(QDeclarativeViewObserver)
......
......@@ -48,24 +48,26 @@ class ObserverProtocol : public QObject
public:
enum Message {
AnimationSpeedChanged,
ChangeTool,
ClearComponentCache,
ColorChanged,
ContextPathUpdated,
CreateObject,
CurrentObjectsChanged,
DestroyObject,
MoveObject,
ObjectIdList,
Reload,
Reloaded,
SetAnimationSpeed,
SetContextPathIdx,
SetCurrentObjects,
SetDesignMode,
ShowAppOnTop,
ToolChanged
AnimationSpeedChanged = 0,
ExecutionPausedChanged = 19, // highest value
ChangeTool = 1,
ClearComponentCache = 2,
ColorChanged = 3,
ContextPathUpdated = 4,
CreateObject = 5,
CurrentObjectsChanged = 6,
DestroyObject = 7,
MoveObject = 8,
ObjectIdList = 9,
Reload = 10,
Reloaded = 11,
SetAnimationSpeed = 12,
SetExecutionPaused = 18,
SetContextPathIdx = 13,
SetCurrentObjects = 14,
SetDesignMode = 15,
ShowAppOnTop = 16,
ToolChanged = 17
};
enum Tool {
......
......@@ -100,6 +100,12 @@ void QDeclarativeObserverService::messageReceived(const QByteArray &message)
emit animationSpeedChangeRequested(speed);
break;
}
case ObserverProtocol::SetExecutionPaused: {
bool paused;
ds >> paused;
emit executionPauseChangeRequested(paused);
break;
}
case ObserverProtocol::ChangeTool: {
ObserverProtocol::Tool tool;
ds >> tool;
......@@ -222,13 +228,24 @@ void QDeclarativeObserverService::setCurrentTool(QmlJSDebugger::Constants::Desig
sendMessage(message);
}
void QDeclarativeObserverService::setAnimationSpeed(qreal slowdownFactor)
void QDeclarativeObserverService::setAnimationSpeed(qreal slowDownFactor)
{
QByteArray message;
QDataStream ds(&message, QIODevice::WriteOnly);
ds << ObserverProtocol::AnimationSpeedChanged
<< slowdownFactor;
<< slowDownFactor;
sendMessage(message);
}
void QDeclarativeObserverService::setExecutionPaused(bool paused)
{
QByteArray message;
QDataStream ds(&message, QIODevice::WriteOnly);
ds << ObserverProtocol::ExecutionPausedChanged
<< paused;
sendMessage(message);
}
......
......@@ -147,7 +147,9 @@ 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(onAnimationSpeedChangeRequested(qreal)));
SLOT(animationSpeedChangeRequested(qreal)));
connect(data->debugService, SIGNAL(executionPauseChangeRequested(bool)),
SLOT(executionPausedChangeRequested(bool)));
connect(data->debugService, SIGNAL(colorPickerToolRequested()),
data.data(), SLOT(_q_changeToColorPickerTool()));
connect(data->debugService, SIGNAL(selectMarqueeToolRequested()),
......@@ -753,18 +755,14 @@ void QDeclarativeViewObserverPrivate::_q_changeContextPathIndex(int index)
subcomponentEditorTool->setContext(index);
}
void QDeclarativeViewObserver::setAnimationSpeed(qreal factor)
void QDeclarativeViewObserver::setAnimationSpeed(qreal slowDownFactor)
{
Q_ASSERT(factor > 0);
if (data->slowDownFactor == factor)
Q_ASSERT(slowDownFactor > 0);
if (data->slowDownFactor == slowDownFactor)
return;
data->slowDownFactor = factor;
emit animationSpeedChanged(factor);
const float effectiveFactor = data->executionPaused ? 0 : factor;
onAnimationSpeedChangeRequested(effectiveFactor);
data->debugService->setAnimationSpeed(effectiveFactor);
animationSpeedChangeRequested(slowDownFactor);
data->debugService->setAnimationSpeed(slowDownFactor);
}
void QDeclarativeViewObserver::setExecutionPaused(bool paused)
......@@ -772,25 +770,30 @@ void QDeclarativeViewObserver::setExecutionPaused(bool paused)
if (data->executionPaused == paused)
return;
const float effectiveFactor = paused ? 0 : data->slowDownFactor;
onAnimationSpeedChangeRequested(effectiveFactor);
data->debugService->setAnimationSpeed(effectiveFactor);
executionPausedChangeRequested(paused);
data->debugService->setExecutionPaused(paused);
}
void QDeclarativeViewObserver::onAnimationSpeedChangeRequested(qreal factor)
void QDeclarativeViewObserver::animationSpeedChangeRequested(qreal factor)
{
const bool paused = factor == 0;
if (!paused && data->slowDownFactor != factor) {
if (data->slowDownFactor != factor) {
data->slowDownFactor = factor;
emit animationSpeedChanged(factor);
}
const float effectiveFactor = data->executionPaused ? 0 : factor;
QDeclarativeDebugHelper::setAnimationSlowDownFactor(effectiveFactor);
}
void QDeclarativeViewObserver::executionPausedChangeRequested(bool paused)
{
if (data->executionPaused != paused) {
data->executionPaused = paused;
emit executionPausedChanged(paused);
}
QDeclarativeDebugHelper::setAnimationSlowDownFactor(factor);
const float effectiveFactor = paused ? 0 : data->slowDownFactor;
QDeclarativeDebugHelper::setAnimationSlowDownFactor(effectiveFactor);
}
......
......@@ -73,19 +73,13 @@ QmlInspectorToolBar::QmlInspectorToolBar(QObject *parent) :
m_zoomAction(0),
m_colorPickerAction(0),
m_showAppOnTopAction(0),
m_defaultAnimSpeedAction(0),
m_halfAnimSpeedAction(0),
m_fourthAnimSpeedAction(0),
m_eighthAnimSpeedAction(0),
m_tenthAnimSpeedAction(0),
m_menuPauseAction(0),
m_playSpeedMenuActions(0),
m_playIcon(QIcon(QLatin1String(":/qml/images/play-small.png"))),
m_pauseIcon(QIcon(QLatin1String(":/qml/images/pause-small.png"))),
m_colorBox(0),
m_emitSignals(true),
m_isRunning(false),
m_paused(false),
m_animationSpeed(1.0f),
m_previousAnimationSpeed(0.0f),
m_activeTool(NoTool),
m_barWidget(0)
{
......@@ -142,32 +136,33 @@ void QmlInspectorToolBar::activateZoomTool()
m_emitSignals = true;
}
void QmlInspectorToolBar::setAnimationSpeed(qreal slowdownFactor)
void QmlInspectorToolBar::setAnimationSpeed(qreal slowDownFactor)
{
if (m_animationSpeed == slowDownFactor)
return;
m_emitSignals = false;
if (slowdownFactor != 0) {
m_animationSpeed = slowdownFactor;
if (slowdownFactor == 1.0f) {
m_defaultAnimSpeedAction->setChecked(true);
} else if (slowdownFactor == 2.0f) {
m_halfAnimSpeedAction->setChecked(true);
} else if (slowdownFactor == 4.0f) {
m_fourthAnimSpeedAction->setChecked(true);
} else if (slowdownFactor == 8.0f) {
m_eighthAnimSpeedAction->setChecked(true);
} else if (slowdownFactor == 10.0f) {
m_tenthAnimSpeedAction->setChecked(true);
m_animationSpeed = slowDownFactor;
foreach (QAction *action, m_playSpeedMenuActions->actions()) {
if (action->data().toReal() == slowDownFactor) {
action->setChecked(true);
break;
}
updatePlayAction();
} else {
m_menuPauseAction->setChecked(true);
updatePauseAction();
}
m_emitSignals = true;
}
void QmlInspectorToolBar::setExecutionPaused(bool paused)
{
if (m_paused == paused)
return;
m_paused = paused;
updatePlayAction();
}
void QmlInspectorToolBar::setDesignModeBehavior(bool inDesignMode)
{
m_emitSignals = false;
......@@ -220,7 +215,8 @@ void QmlInspectorToolBar::createActions(const Core::Context &context)
m_colorPickerAction->setCheckable(true);
am->registerAction(m_observerModeAction, Constants::DESIGNMODE_ACTION, context);
am->registerAction(m_playAction, Constants::PLAY_ACTION, context);
Core::Command *command = am->registerAction(m_playAction, Constants::PLAY_ACTION, context);
command->setAttribute(Core::Command::CA_UpdateIcon);
am->registerAction(m_selectAction, Constants::SELECT_ACTION, context);
am->registerAction(m_zoomAction, Constants::ZOOM_ACTION, context);
am->registerAction(m_colorPickerAction, Constants::COLOR_PICKER_ACTION, context);
......@@ -232,40 +228,33 @@ void QmlInspectorToolBar::createActions(const Core::Context &context)
m_barWidget->setProperty("topBorder", true);
QMenu *playSpeedMenu = new QMenu(m_barWidget);
QActionGroup *playSpeedMenuActions = new QActionGroup(this);
playSpeedMenuActions->setExclusive(true);
playSpeedMenu->addAction(tr("Animation Speed"));
playSpeedMenu->addSeparator();
m_defaultAnimSpeedAction =
playSpeedMenu->addAction(tr("1x"), this, SLOT(changeToDefaultAnimSpeed()));
m_defaultAnimSpeedAction->setCheckable(true);
m_defaultAnimSpeedAction->setChecked(true);
playSpeedMenuActions->addAction(m_defaultAnimSpeedAction);
m_halfAnimSpeedAction =
playSpeedMenu->addAction(tr("0.5x"), this, SLOT(changeToHalfAnimSpeed()));
m_halfAnimSpeedAction->setCheckable(true);
playSpeedMenuActions->addAction(m_halfAnimSpeedAction);
m_fourthAnimSpeedAction =
playSpeedMenu->addAction(tr("0.25x"), this, SLOT(changeToFourthAnimSpeed()));
m_fourthAnimSpeedAction->setCheckable(true);
playSpeedMenuActions->addAction(m_fourthAnimSpeedAction);
m_eighthAnimSpeedAction =
playSpeedMenu->addAction(tr("0.125x"), this, SLOT(changeToEighthAnimSpeed()));
m_eighthAnimSpeedAction->setCheckable(true);
playSpeedMenuActions->addAction(m_eighthAnimSpeedAction);
m_tenthAnimSpeedAction =
playSpeedMenu->addAction(tr("0.1x"), this, SLOT(changeToTenthAnimSpeed()));
m_tenthAnimSpeedAction->setCheckable(true);
playSpeedMenuActions->addAction(m_tenthAnimSpeedAction);
m_menuPauseAction = playSpeedMenu->addAction(tr("Pause"), this, SLOT(updatePauseAction()));
m_menuPauseAction->setCheckable(true);
m_menuPauseAction->setIcon(m_pauseIcon);
playSpeedMenuActions->addAction(m_menuPauseAction);
m_playSpeedMenuActions = new QActionGroup(this);
m_playSpeedMenuActions->setExclusive(true);
QAction *speedAction = playSpeedMenu->addAction(tr("1x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setChecked(true);
speedAction->setData(1.0f);
m_playSpeedMenuActions->addAction(speedAction);
speedAction = playSpeedMenu->addAction(tr("0.5x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setData(2.0f);
m_playSpeedMenuActions->addAction(speedAction);
speedAction = playSpeedMenu->addAction(tr("0.25x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setData(4.0f);
m_playSpeedMenuActions->addAction(speedAction);
speedAction = playSpeedMenu->addAction(tr("0.125x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setData(8.0f);
m_playSpeedMenuActions->addAction(speedAction);
speedAction = playSpeedMenu->addAction(tr("0.1x"), this, SLOT(changeAnimationSpeed()));
speedAction->setCheckable(true);
speedAction->setData(10.0f);
m_playSpeedMenuActions->addAction(speedAction);
QHBoxLayout *toolBarLayout = new QHBoxLayout(m_barWidget);
toolBarLayout->setMargin(0);
......@@ -324,33 +313,13 @@ QWidget *QmlInspectorToolBar::widget() const
return m_barWidget;
}
void QmlInspectorToolBar::changeToDefaultAnimSpeed()
void QmlInspectorToolBar::changeAnimationSpeed()
{
m_animationSpeed = 1.0f;
updatePlayAction();
}
QAction *action = static_cast<QAction*>(sender());
void QmlInspectorToolBar::changeToHalfAnimSpeed()
{
m_animationSpeed = 2.0f;
updatePlayAction();
}
m_animationSpeed = action->data().toReal();
emit animationSpeedChanged(m_animationSpeed);
void QmlInspectorToolBar::changeToFourthAnimSpeed()
{
m_animationSpeed = 4.0f;
updatePlayAction();
}
void QmlInspectorToolBar::changeToEighthAnimSpeed()
{
m_animationSpeed = 8.0f;
updatePlayAction();
}
void QmlInspectorToolBar::changeToTenthAnimSpeed()
{
m_animationSpeed = 10.0f;
updatePlayAction();
}
......@@ -368,34 +337,14 @@ void QmlInspectorToolBar::activateDesignModeOnClick()
void QmlInspectorToolBar::activatePlayOnClick()
{
if (m_isRunning) {
updatePauseAction();
} else {
updatePlayAction();
}
m_paused = !m_paused;
emit executionPausedChanged(m_paused);
updatePlayAction();
}
void QmlInspectorToolBar::updatePlayAction()
{
m_isRunning = true;
m_playAction->setIcon(m_pauseIcon);
if (m_animationSpeed != m_previousAnimationSpeed)
m_previousAnimationSpeed = m_animationSpeed;
if (m_emitSignals)
emit animationSpeedChanged(m_animationSpeed);
m_playButton->setDefaultAction(m_playAction);
}
void QmlInspectorToolBar::updatePauseAction()
{
m_isRunning = false;
m_playAction->setIcon(m_playIcon);
if (m_emitSignals)
emit animationSpeedChanged(0.0f);
m_playButton->setDefaultAction(m_playAction);
m_playAction->setIcon(m_paused ? m_playIcon : m_pauseIcon);
}
void QmlInspectorToolBar::activateColorPickerOnClick()
......
......@@ -40,6 +40,7 @@
#include <QtGui/QIcon>
QT_FORWARD_DECLARE_CLASS(QAction)
QT_FORWARD_DECLARE_CLASS(QActionGroup)
QT_FORWARD_DECLARE_CLASS(QColor)
QT_FORWARD_DECLARE_CLASS(QToolButton)
......@@ -84,7 +85,10 @@ public slots:
void activateColorPicker();
void activateSelectTool();
void activateZoomTool();
void setAnimationSpeed(qreal slowdownFactor);
void setAnimationSpeed(qreal slowDownFactor);
void setExecutionPaused(bool paused);
void setDesignModeBehavior(bool inDesignMode);
void setShowAppOnTop(bool showAppOnTop);
void setSelectedColor(const QColor &color);
......@@ -99,7 +103,9 @@ signals:
void zoomToolSelected();
void showAppOnTopSelected(bool isChecked);
void animationSpeedChanged(qreal slowdownFactor = 1.0f);
void animationSpeedChanged(qreal slowdownFactor);
void executionPausedChanged(bool paused);
private slots:
void activateDesignModeOnClick();
......@@ -110,16 +116,11 @@ private slots:
void showAppOnTopClick();
void changeToDefaultAnimSpeed();
void changeToHalfAnimSpeed();
void changeToFourthAnimSpeed();
void changeToEighthAnimSpeed();
void changeToTenthAnimSpeed();
void changeAnimationSpeed();
void activateFromQml();
void updatePlayAction();
void updatePauseAction();
void activeDebugLanguagesChanged(Debugger::DebuggerLanguages languages);
......@@ -135,12 +136,7 @@ private:
QAction *m_showAppOnTopAction;
QAction *m_defaultAnimSpeedAction;
QAction *m_halfAnimSpeedAction;
QAction *m_fourthAnimSpeedAction;
QAction *m_eighthAnimSpeedAction;
QAction *m_tenthAnimSpeedAction;
QAction *m_menuPauseAction;
QActionGroup *m_playSpeedMenuActions;
QToolButton *m_playButton;
QIcon m_playIcon;
......@@ -149,9 +145,8 @@ private:
ToolBarColorBox *m_colorBox;
bool m_emitSignals;
bool m_isRunning;
bool m_paused;
qreal m_animationSpeed;
qreal m_previousAnimationSpeed;
DesignTool m_activeTool;
......
......@@ -86,6 +86,8 @@ void ClientProxy::connectToServer()
SIGNAL(selectMarqueeToolActivated()));
connect(m_observerClient, SIGNAL(animationSpeedChanged(qreal)),
SIGNAL(animationSpeedChanged(qreal)));
connect(m_observerClient, SIGNAL(executionPausedChanged(bool)),
SIGNAL(executionPausedChanged(bool)));
connect(m_observerClient, SIGNAL(designModeBehaviorChanged(bool)),
SIGNAL(designModeBehaviorChanged(bool)));
connect(m_observerClient, SIGNAL(showAppOnTopChanged(bool)),
......@@ -545,10 +547,16 @@ void ClientProxy::setDesignModeBehavior(bool inDesignMode)
m_observerClient->setDesignModeBehavior(inDesignMode);
}
void ClientProxy::setAnimationSpeed(qreal slowdownFactor)
void ClientProxy::setAnimationSpeed(qreal slowDownFactor)
{
if (isConnected())
m_observerClient->setAnimationSpeed(slowdownFactor);
m_observerClient->setAnimationSpeed(slowDownFactor);
}
void ClientProxy::setExecutionPaused(bool paused)
{
if (isConnected())
m_observerClient->setExecutionPaused(paused);
}
void ClientProxy::changeToColorPickerTool()
......
......@@ -106,7 +106,8 @@ signals:
void selectToolActivated();
void selectMarqueeToolActivated();
void zoomToolActivated();
void animationSpeedChanged(qreal slowdownFactor);
void animationSpeedChanged(qreal slowDownFactor);
void executionPausedChanged(bool paused);
void designModeBehaviorChanged(bool inDesignMode);
void showAppOnTopChanged(bool showAppOnTop);
void serverReloaded();
......@@ -120,7 +121,8 @@ public slots:
void reloadQmlViewer();
void setDesignModeBehavior(bool inDesignMode);
void setAnimationSpeed(qreal slowdownFactor = 1.0f);
void setAnimationSpeed(qreal slowDownFactor);
void setExecutionPaused(bool paused);
void changeToColorPickerTool();
void changeToZoomTool();
void changeToSelectTool();
......
......@@ -884,6 +884,8 @@ void InspectorUi::connectSignals()
m_toolBar, SLOT(setSelectedColor(QColor)));
connect(m_clientProxy, SIGNAL(animationSpeedChanged(qreal)),
m_toolBar, SLOT(setAnimationSpeed(qreal)));
connect(m_clientProxy, SIGNAL(executionPausedChanged(bool)),
m_toolBar, SLOT(setExecutionPaused(bool)));
connect(m_toolBar, SIGNAL(applyChangesFromQmlFileTriggered(bool)),
this, SLOT(setApplyChangesToQmlObserver(bool)));
......@@ -894,6 +896,8 @@ void InspectorUi::connectSignals()
m_clientProxy, SLOT(reloadQmlViewer()));
connect(m_toolBar, SIGNAL(animationSpeedChanged(qreal)),
m_clientProxy, SLOT(setAnimationSpeed(qreal)));
connect(m_toolBar, SIGNAL(executionPausedChanged(bool)),
m_clientProxy, SLOT(setExecutionPaused(bool)));
connect(m_toolBar, SIGNAL(colorPickerSelected()),
m_clientProxy, SLOT(changeToColorPickerTool()));
connect(m_toolBar, SIGNAL(zoomToolSelected()),
......
......@@ -106,12 +106,21 @@ void QmlJSObserverClient::messageReceived(const QByteArray &message)
break;
}
case ObserverProtocol::AnimationSpeedChanged: {
qreal slowdownFactor;
ds >> slowdownFactor;
qreal slowDownFactor;
ds >> slowDownFactor;
log(LogReceive, type, QString::number(slowdownFactor));
log(LogReceive, type, QString::number(slowDownFactor));
emit animationSpeedChanged(slowdownFactor);
emit animationSpeedChanged(slowDownFactor);
break;
}
case ObserverProtocol::ExecutionPausedChanged: {
bool paused;
ds >> paused;
log(LogReceive, type, paused ? QLatin1String("true") : QLatin1String("false"));
emit executionPausedChanged(paused);
break;
}
case ObserverProtocol::SetDesignMode: {
......@@ -291,7 +300,7 @@ void QmlJSObserverClient::setDesignModeBehavior(bool inDesignMode)
sendMessage(message);
}
void QmlJSObserverClient::setAnimationSpeed(qreal slowdownFactor)
void QmlJSObserverClient::setAnimationSpeed(qreal slowDownFactor)
{
if (!m_connection || !m_connection->isConnected())
return;
......@@ -301,10 +310,27 @@ void QmlJSObserverClient::setAnimationSpeed(qreal slowdownFactor)