Skip to content

Commit

Permalink
qmlui: more actions tracked via Tardis
Browse files Browse the repository at this point in the history
Moved some more engine specific code away from NetworkManager
Prepared ground for deletion actions
  • Loading branch information
mcallegari committed Nov 4, 2017
1 parent f2364d7 commit f05989e
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 86 deletions.
2 changes: 2 additions & 0 deletions qmlui/chasereditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bool ChaserEditor::addFunctions(QVariantList idsList, int insertIndex)
step.duration = 1000;
step.hold = step.duration;
}
Tardis::instance()->enqueueAction(ChaserAddStep, m_chaser, QVariant(), insertIndex);
m_chaser->addStep(step, insertIndex++);
}
updateStepsList();
Expand Down Expand Up @@ -130,6 +131,7 @@ bool ChaserEditor::addStep(int insertIndex)

qDebug() << "Values added: " << step.values.count();

Tardis::instance()->enqueueAction(ChaserAddStep, m_chaser, QVariant(), insertIndex);
m_chaser->addStep(step, insertIndex);
updateStepsList();
return true;
Expand Down
20 changes: 20 additions & 0 deletions qmlui/efxeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ void EFXEditor::setFunctionID(quint32 id)
{
if (id == Function::invalidId())
{
disconnect(m_efx, &EFX::attributeChanged, this, &EFXEditor::slotAttributeChanged);
m_efx = NULL;
return;
}

m_efx = qobject_cast<EFX *>(m_doc->function(id));
if (m_efx != NULL)
{
connect(m_efx, &EFX::attributeChanged, this, &EFXEditor::slotAttributeChanged);
updateAlgorithmData();
emit algorithmIndexChanged();
}
Expand All @@ -68,6 +70,24 @@ void EFXEditor::setFunctionID(quint32 id)
updateFixtureList();
}

void EFXEditor::slotAttributeChanged(int attrIndex, qreal fraction)
{
Q_UNUSED(fraction)

switch (attrIndex)
{
case EFX::Width: emit algorithmWidthChanged(); break;
case EFX::Height: emit algorithmHeightChanged(); break;
case EFX::Rotation: emit algorithmRotationChanged(); break;
case EFX::XOffset: emit algorithmXOffsetChanged(); break;
case EFX::YOffset: emit algorithmYOffsetChanged(); break;
case EFX::StartOffset: emit algorithmStartOffsetChanged(); break;
default: break;
}

updateAlgorithmData();
}

/************************************************************************
* Algorithm
************************************************************************/
Expand Down
3 changes: 3 additions & 0 deletions qmlui/efxeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class EFXEditor : public FunctionEditor
/** Set the ID of the EFX to edit */
void setFunctionID(quint32 id);

protected slots:
void slotAttributeChanged(int attrIndex, qreal fraction);

private:
/** Reference of the EFX currently being edited */
EFX *m_efx;
Expand Down
44 changes: 4 additions & 40 deletions qmlui/tardis/networkmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
#include "networkmanager.h"
#include "networkpacketizer.h"
#include "simplecrypt.h"

#include "function.h"
#include "vcwidget.h"
#include "fixture.h"
#include "efx.h"
#include "tardis.h"
#include "doc.h"

#define DEFAULT_UDP_PORT 9997
Expand Down Expand Up @@ -96,40 +92,6 @@ int NetworkManager::connectionsCount()
return 0;
}

void NetworkManager::addBufferSection(QByteArray &packet, TardisAction *action)
{
if (action == NULL || action->m_object == NULL)
return;

QBuffer buffer;
buffer.open(QIODevice::WriteOnly | QIODevice::Text);
QXmlStreamWriter xmlWriter(&buffer);

switch(action->m_action)
{
case FixtureCreate:
{
Fixture *fixture = qobject_cast<Fixture *>(action->m_object);
if (fixture)
fixture->saveXML(&xmlWriter);
}
break;
case EFXAddFixture:
{
// EFXFixture is not a QObject, so a simple C cast is enough
EFXFixture *fixture = (EFXFixture *)action->m_object;
fixture->saveXML(&xmlWriter);
}
break;
default:
qWarning() << "Buffered action" << action->m_action << "not implemented !";
break;
}

if (buffer.bytesAvailable())
m_packetizer->addSection(packet, buffer.buffer());
}

void NetworkManager::sendAction(quint32 objID, TardisAction action)
{
QByteArray packet;
Expand All @@ -139,8 +101,10 @@ void NetworkManager::sendAction(quint32 objID, TardisAction action)
switch (action.m_action)
{
case FixtureCreate:
case FunctionCreate:
case ChaserAddStep:
case EFXAddFixture:
addBufferSection(packet, &action);
m_packetizer->addSection(packet, Tardis::actionToByteArray(action.m_action, action.m_object, action.m_newValue));
break;

default:
Expand Down
4 changes: 0 additions & 4 deletions qmlui/tardis/networkmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ public slots:
protected:
QString defaultName();

/** From the given $object, generates an XML buffer and
* add it to the provided $packet */
void addBufferSection(QByteArray &packet, TardisAction *action);

/** Send the content of $packet using the provided $socket */
bool sendTCPPacket(QTcpSocket *socket, QByteArray &packet, bool encrypt);

Expand Down
157 changes: 117 additions & 40 deletions qmlui/tardis/tardis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,107 @@ void Tardis::run()
}
}

QByteArray Tardis::actionToByteArray(int code, QObject *object, QVariant data)
{
if (object == NULL)
return QByteArray();

QBuffer buffer;
buffer.open(QIODevice::WriteOnly | QIODevice::Text);
QXmlStreamWriter xmlWriter(&buffer);

switch(code)
{
case FixtureCreate:
case FixtureDelete:
{
Fixture *fixture = qobject_cast<Fixture *>(object);
if (fixture)
fixture->saveXML(&xmlWriter);
}
break;
case FunctionCreate:
case FunctionDelete:
{
Function *function = qobject_cast<Function *>(object);
if (function)
function->saveXML(&xmlWriter);
}
break;
case ChaserAddStep:
{
Chaser *chaser = qobject_cast<Chaser *>(object);
ChaserStep *step = chaser->stepAt(data.toInt());
step->saveXML(&xmlWriter, data.toInt(), chaser->type() == Function::SequenceType ? true : false);
}
break;
case EFXAddFixture:
{
// EFXFixture is not a QObject, so a simple C cast is enough
EFXFixture *fixture = (EFXFixture *)object;
fixture->saveXML(&xmlWriter);
}
break;
default:
qWarning() << "Buffered action" << code << "not implemented !";
break;
}

return buffer.buffer();
}

bool Tardis::processBufferedAction(TardisAction &action, QVariant &value)
{
if (value.type() != QVariant::ByteArray)
return false;

QBuffer buffer;
buffer.setData(value.toByteArray());
buffer.open(QIODevice::ReadOnly | QIODevice::Text);
QXmlStreamReader xmlReader(&buffer);
xmlReader.readNextStartElement();

switch(action.m_action)
{
case FixtureCreate:
{
Fixture::loader(xmlReader, m_doc);
}
break;
case FunctionCreate:
{
Function::loader(xmlReader, m_doc);
}
break;
case ChaserAddStep:
{
Chaser *chaser = qobject_cast<Chaser *>(action.m_object);
ChaserStep step;
int stepNumber = -1;

if (step.loadXML(xmlReader, stepNumber) == true)
chaser->addStep(step, stepNumber);
}
break;
case EFXAddFixture:
{
EFX *efx = qobject_cast<EFX *>(action.m_object);
EFXFixture *ef = new EFXFixture(efx);

ef->loadXML(xmlReader);
efx->addFixture(ef);
}
break;

default:
// This action was either not buffered or not implemented
return false;
break;
}

return true;
}

void Tardis::slotProcessNetworkAction(int code, quint32 id, QVariant value)
{
TardisAction action;
Expand Down Expand Up @@ -259,39 +360,8 @@ void Tardis::slotProcessNetworkAction(int code, quint32 id, QVariant value)
}

// 2- Handle creation cases, where an XML fragment is provided
switch(code)
{
case FixtureCreate:
{
QBuffer buffer;
buffer.setData(value.toByteArray());
buffer.open(QIODevice::ReadOnly | QIODevice::Text);
QXmlStreamReader xmlReader(&buffer);
xmlReader.readNextStartElement();
Fixture::loader(xmlReader, m_doc);
return;
}
break;
case EFXAddFixture:
{
EFX *efx = qobject_cast<EFX *>(action.m_object);
EFXFixture *ef = new EFXFixture(efx);
QBuffer buffer;

buffer.setData(value.toByteArray());
buffer.open(QIODevice::ReadOnly | QIODevice::Text);
QXmlStreamReader xmlReader(&buffer);
xmlReader.readNextStartElement();

ef->loadXML(xmlReader);
efx->addFixture(ef);
return;
}
break;

default:
break;
}
if (processBufferedAction(action, value))
return;

// 3- store value on oldValue, since we're going to call the method used for undoing actions
action.m_oldValue = value;
Expand Down Expand Up @@ -388,6 +458,12 @@ void Tardis::processAction(TardisAction &action)
}
break;

case ChaserAddStep:
{
Chaser *chaser = qobject_cast<Chaser *>(action.m_object);
chaser->removeStep(action.m_newValue.toInt());
}
break;
case ChaserSetStepFadeIn:
{
Chaser *chaser = qobject_cast<Chaser *>(action.m_object);
Expand Down Expand Up @@ -425,6 +501,13 @@ void Tardis::processAction(TardisAction &action)
}
break;

case EFXAddFixture:
{
auto member = std::mem_fn(&EFX::removeFixture);
EFXFixture *ef = (EFXFixture *)action.m_oldValue.value<void *>();
member(qobject_cast<EFX *>(action.m_object), ef);
}
break;
case EFXSetAlgorithmIndex:
{
auto member = std::mem_fn(&EFX::setAlgorithm);
Expand Down Expand Up @@ -497,13 +580,6 @@ void Tardis::processAction(TardisAction &action)
member(qobject_cast<EFX *>(action.m_object), action.m_oldValue.toInt());
}
break;
case EFXAddFixture:
{
auto member = std::mem_fn(&EFX::removeFixture);
EFXFixture *ef = (EFXFixture *)action.m_oldValue.value<void *>();
member(qobject_cast<EFX *>(action.m_object), ef);
}
break;

/* ******************* Virtual console editing actions ******************** */

Expand Down Expand Up @@ -554,3 +630,4 @@ void Tardis::processAction(TardisAction &action)
}
}


5 changes: 5 additions & 0 deletions qmlui/tardis/tardis.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ class Tardis : public QThread

void processAction(TardisAction &action);

static QByteArray actionToByteArray(int code, QObject *object, QVariant data);

/** Reset the actions history */
void resetHistory();

/** @reimp */
void run(); // thread run function

protected:
bool processBufferedAction(TardisAction &action, QVariant &value);

protected slots:
void slotProcessNetworkAction(int code, quint32 id, QVariant value);

Expand Down
6 changes: 4 additions & 2 deletions qmlui/tardis/tardisactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ enum
SceneSetChannelValue,
SceneUnsetChannelValue,

ChaserAddStep,
ChaserRemoveStep,
ChaserSetStepFadeIn,
ChaserSetStepHold,
ChaserSetStepFadeOut,
ChaserSetStepDuration,

EFXAddFixture,
EFXRemoveFixture,
EFXSetAlgorithmIndex,
EFXSetRelative,
EFXSetWidth,
Expand All @@ -75,8 +79,6 @@ enum
EFXSetYFrequency,
EFXSetXPhase,
EFXSetYPhase,
EFXAddFixture,
EFXRemoveFixture,

/* Virtual console editing actions */
VCWidgetCreate = 0xE000,
Expand Down

0 comments on commit f05989e

Please sign in to comment.