NOX: Payload - Separated protocol and widget as per new framework

This commit is contained in:
Srivats P. 2014-03-13 07:27:29 +05:30
parent c0f009c44a
commit f39031d663
4 changed files with 170 additions and 82 deletions

View File

@ -1,5 +1,5 @@
/*
Copyright (C) 2010 Srivats P.
Copyright (C) 2010, 2013-2014 Srivats P.
This file is part of "Ostinato"
@ -17,46 +17,16 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <qendian.h>
#include <QHostAddress>
//#include "../client/stream.h"
#include "payload.h"
#include "streambase.h"
PayloadConfigForm::PayloadConfigForm(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
}
void PayloadConfigForm::on_cmbPatternMode_currentIndexChanged(int index)
{
switch(index)
{
case OstProto::Payload::e_dp_fixed_word:
lePattern->setEnabled(true);
break;
case OstProto::Payload::e_dp_inc_byte:
case OstProto::Payload::e_dp_dec_byte:
case OstProto::Payload::e_dp_random:
lePattern->setDisabled(true);
break;
default:
qWarning("Unhandled/Unknown PatternMode = %d",index);
}
}
PayloadProtocol::PayloadProtocol(StreamBase *stream, AbstractProtocol *parent)
: AbstractProtocol(stream, parent)
{
configForm = NULL;
}
PayloadProtocol::~PayloadProtocol()
{
delete configForm;
}
AbstractProtocol* PayloadProtocol::createInstance(StreamBase *stream,
@ -196,6 +166,12 @@ QVariant PayloadProtocol::fieldData(int index, FieldAttrib attrib,
// Meta fields
case payload_dataPatternMode:
switch(attrib)
{
case FieldValue: return data.pattern_mode();
default: break;
}
break;
default:
break;
}
@ -203,10 +179,38 @@ QVariant PayloadProtocol::fieldData(int index, FieldAttrib attrib,
return AbstractProtocol::fieldData(index, attrib, streamIndex);
}
bool PayloadProtocol::setFieldData(int /*index*/, const QVariant &/*value*/,
FieldAttrib /*attrib*/)
bool PayloadProtocol::setFieldData(int index, const QVariant &value,
FieldAttrib attrib)
{
return false;
bool isOk = false;
if (attrib != FieldValue)
return false;
switch (index)
{
case payload_dataPattern:
{
uint pattern = value.toUInt(&isOk);
if (isOk)
data.set_pattern(pattern);
break;
}
case payload_dataPatternMode:
{
uint mode = value.toUInt(&isOk);
if (isOk && data.DataPatternMode_IsValid(mode))
data.set_pattern_mode(OstProto::Payload::DataPatternMode(mode));
else
isOk = false;
break;
}
default:
qFatal("%s: unimplemented case %d in switch", __PRETTY_FUNCTION__,
index);
break;
}
return isOk;
}
bool PayloadProtocol::isProtocolFrameValueVariable() const
@ -252,33 +256,3 @@ int PayloadProtocol::protocolFrameVariableCount() const
return count;
}
QWidget* PayloadProtocol::configWidget()
{
if (configForm == NULL)
{
configForm = new PayloadConfigForm;
loadConfigWidget();
}
return configForm;
}
void PayloadProtocol::loadConfigWidget()
{
configWidget();
configForm->cmbPatternMode->setCurrentIndex(data.pattern_mode());
configForm->lePattern->setText(uintToHexStr(data.pattern(), 4));
}
void PayloadProtocol::storeConfigWidget()
{
bool isOk;
configWidget();
data.set_pattern_mode((OstProto::Payload::DataPatternMode)
configForm->cmbPatternMode->currentIndex());
data.set_pattern(configForm->lePattern->text().remove(QChar(' ')).toULong(&isOk, 16));
}

View File

@ -1,5 +1,5 @@
/*
Copyright (C) 2010 Srivats P.
Copyright (C) 2010-2014 Srivats P.
This file is part of "Ostinato"
@ -23,22 +23,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "abstractprotocol.h"
#include "payload.pb.h"
#include "ui_payload.h"
class PayloadConfigForm : public QWidget, public Ui::payload
{
Q_OBJECT
public:
PayloadConfigForm(QWidget *parent = 0);
private slots:
void on_cmbPatternMode_currentIndexChanged(int index);
};
class PayloadProtocol : public AbstractProtocol
{
private:
OstProto::Payload data;
PayloadConfigForm *configForm;
public:
enum payloadfield
{
payload_dataPattern,
@ -49,7 +37,6 @@ private:
payload_fieldCount
};
public:
PayloadProtocol(StreamBase *stream, AbstractProtocol *parent = 0);
virtual ~PayloadProtocol();
@ -77,9 +64,8 @@ public:
virtual bool isProtocolFrameSizeVariable() const;
virtual int protocolFrameVariableCount() const;
virtual QWidget* configWidget();
virtual void loadConfigWidget();
virtual void storeConfigWidget();
private:
OstProto::Payload data;
};
#endif

84
common/payloadconfig.cpp Normal file
View File

@ -0,0 +1,84 @@
/*
Copyright (C) 2010-2014 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "payloadconfig.h"
#include "payload.h"
PayloadConfigForm::PayloadConfigForm(QWidget *parent)
: AbstractProtocolConfigForm(parent)
{
setupUi(this);
}
PayloadConfigForm::~PayloadConfigForm()
{
}
AbstractProtocolConfigForm* PayloadConfigForm::createInstance()
{
return new PayloadConfigForm;
}
void PayloadConfigForm::loadWidget(AbstractProtocol *proto)
{
cmbPatternMode->setCurrentIndex(
proto->fieldData(
PayloadProtocol::payload_dataPatternMode,
AbstractProtocol::FieldValue
).toUInt());
lePattern->setText(uintToHexStr(
proto->fieldData(
PayloadProtocol::payload_dataPattern,
AbstractProtocol::FieldValue
).toUInt(), 4));
}
void PayloadConfigForm::storeWidget(AbstractProtocol *proto)
{
bool isOk;
proto->setFieldData(
PayloadProtocol::payload_dataPatternMode,
cmbPatternMode->currentIndex());
proto->setFieldData(
PayloadProtocol::payload_dataPattern,
lePattern->text().remove(QChar(' ')).toUInt(&isOk, BASE_HEX));
}
void PayloadConfigForm::on_cmbPatternMode_currentIndexChanged(int index)
{
switch(index)
{
case OstProto::Payload::e_dp_fixed_word:
lePattern->setEnabled(true);
break;
case OstProto::Payload::e_dp_inc_byte:
case OstProto::Payload::e_dp_dec_byte:
case OstProto::Payload::e_dp_random:
lePattern->setDisabled(true);
break;
default:
qWarning("Unhandled/Unknown PatternMode = %d",index);
}
}

44
common/payloadconfig.h Normal file
View File

@ -0,0 +1,44 @@
/*
Copyright (C) 2010-2014 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _PAYLOAD_CONFIG_H
#define _PAYLOAD_CONFIG_H
#include "abstractprotocolconfig.h"
#include "ui_payload.h"
class PayloadConfigForm :
public AbstractProtocolConfigForm,
private Ui::payload
{
Q_OBJECT
public:
PayloadConfigForm(QWidget *parent = 0);
virtual ~PayloadConfigForm();
static AbstractProtocolConfigForm* createInstance();
virtual void loadWidget(AbstractProtocol *proto);
virtual void storeWidget(AbstractProtocol *proto);
private slots:
void on_cmbPatternMode_currentIndexChanged(int index);
};
#endif