diff --git a/common/payload.cpp b/common/payload.cpp index 7fd0dd5..2a7756d 100644 --- a/common/payload.cpp +++ b/common/payload.cpp @@ -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 */ -#include -#include - -//#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)); -} - diff --git a/common/payload.h b/common/payload.h index 822ee37..4c95da5 100644 --- a/common/payload.h +++ b/common/payload.h @@ -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 #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 diff --git a/common/payloadconfig.cpp b/common/payloadconfig.cpp new file mode 100644 index 0000000..d06d672 --- /dev/null +++ b/common/payloadconfig.cpp @@ -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 +*/ + +#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); + } +} + + + diff --git a/common/payloadconfig.h b/common/payloadconfig.h new file mode 100644 index 0000000..8ebfeb0 --- /dev/null +++ b/common/payloadconfig.h @@ -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 +*/ + +#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