1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
/*
Copyright (C) 2015 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 "variablefieldswidget.h"
#include "abstractprotocol.h"
#include "protocollistiterator.h"
#include "stream.h"
#include <QListWidgetItem>
#include <QMetaType>
#include <QStringList>
Q_DECLARE_METATYPE(AbstractProtocol*);
Q_DECLARE_METATYPE(OstProto::VariableField);
QStringList typeNames = QStringList()
<< "Counter8"
<< "Counter16"
<< "Counter32";
QStringList modeNames = QStringList()
<< "Increment"
<< "Decrement"
<< "Random";
#define uintToHexStr(num, bytes) \
QString("%1").arg(num, bytes*2, BASE_HEX, QChar('0')).toUpper()
#define hexStrToUInt(str) \
str.toUInt(NULL, BASE_HEX)
/*
* NOTES:
* 1. We use a QSpinBox for all numeric values except for 'value' because
* QSpinBox value is of type 'int' - we would like the ability to store
* quint32
* 2. This widget will keep the stream always updated - every editing change
* of a attribute is immediately updated in the stream; the consequence
* of this design is that an explicit 'store' of widget contents to the
* stream is no longer required - we still define a store() method in
* case we need to change the design later
*/
VariableFieldsWidget::VariableFieldsWidget(QWidget *parent)
: QWidget(parent)
{
stream_ = NULL;
isProgLoad_ = false;
lastSelectedProtocolIndex_ = 0;
setupUi(this);
attribGroup->setHidden(true);
type->addItems(typeNames);
mode->addItems(modeNames);
valueRange_ = new QIntValidator(this);
// FIXME: we can't use QIntValidator - since we want value to be able
// to enter a quint32
//value->setValidator(valueRange_);
connect(type, SIGNAL(currentIndexChanged(int)),
SLOT(updateCurrentVariableField()));
connect(offset, SIGNAL(valueChanged(int)),
SLOT(updateCurrentVariableField()));
connect(bitmask, SIGNAL(textChanged(QString)),
SLOT(updateCurrentVariableField()));
connect(mode, SIGNAL(currentIndexChanged(int)),
SLOT(updateCurrentVariableField()));
connect(value, SIGNAL(textChanged(QString)),
SLOT(updateCurrentVariableField()));
connect(count, SIGNAL(valueChanged(int)),
SLOT(updateCurrentVariableField()));
connect(step, SIGNAL(valueChanged(int)),
SLOT(updateCurrentVariableField()));
}
void VariableFieldsWidget::setStream(Stream *stream)
{
stream_ = stream;
}
void VariableFieldsWidget::load()
{
Q_ASSERT(stream_);
Q_ASSERT(protocolList->count() == 0);
Q_ASSERT(variableFieldList->count() == 0);
ProtocolListIterator *iter = stream_->createProtocolListIterator();
while (iter->hasNext()) {
AbstractProtocol *proto = iter->next();
QListWidgetItem *protoItem = new QListWidgetItem;
protoItem->setData(kProtocolPtrRole, QVariant::fromValue(proto));
protoItem->setData(kCurrentVarFieldRole,
QVariant::fromValue(proto->variableFieldCount() ? 0 : -1));
protoItem->setText(proto->shortName());
decorateProtocolItem(protoItem);
protocolList->addItem(protoItem);
}
delete iter;
if (lastSelectedProtocolIndex_ < protocolList->count())
protocolList->setCurrentRow(lastSelectedProtocolIndex_);
// XXX: protocolList->setCurrentRow() above will emit currentItemChanged
// which will load variableFieldsList - no need to load it explicitly
}
void VariableFieldsWidget::store()
{
/* Do Nothing - see Notes at the top of the file */
}
void VariableFieldsWidget::clear()
{
protocolList->clear();
variableFieldList->clear();
}
void VariableFieldsWidget::on_protocolList_currentItemChanged(
QListWidgetItem *current,
QListWidgetItem *previous)
{
AbstractProtocol *proto;
qDebug("%s: curr = %p, prev = %p", __FUNCTION__, current, previous);
if (current == NULL)
goto _exit;
proto = current->data(kProtocolPtrRole).value<AbstractProtocol*>();
loadProtocolFields(proto);
variableFieldList->clear();
for (int i = 0; i < proto->variableFieldCount(); i++) {
OstProto::VariableField vf = proto->variableField(i);
QListWidgetItem *vfItem = new QListWidgetItem;
setVariableFieldItem(vfItem, proto, vf);
variableFieldList->addItem(vfItem);
}
// While switching protocols, we want to setup the attrib group
// validation/ranges/masks for the current protocol, which is done
// by the field/type signal handlers - so clear field/type index
// now so that signals are emitted when we add/select a VF
field->setCurrentIndex(-1);
type->setCurrentIndex(-1);
variableFieldList->setCurrentRow(
current->data(kCurrentVarFieldRole).value<int>());
lastSelectedProtocolIndex_ = protocolList->currentRow();
_exit:
addButton->setEnabled(current != NULL);
}
void VariableFieldsWidget::on_variableFieldList_currentItemChanged(
QListWidgetItem *current,
QListWidgetItem *previous)
{
OstProto::VariableField vf;
qDebug("%s: curr = %p, prev = %p",
__FUNCTION__, current, previous);
if (current == NULL)
goto _exit;
vf = current->data(kVarFieldRole).value<OstProto::VariableField>();
isProgLoad_ = true;
field->setCurrentIndex(fieldIndex(vf));
type->setCurrentIndex(vf.type());
offset->setValue(vf.offset());
bitmask->setText(uintToHexStr(vf.mask(), typeSize(vf.type())));
value->setText(QString().setNum(vf.value()));
mode->setCurrentIndex(vf.mode());
count->setValue(vf.count());
step->setValue(vf.step());
isProgLoad_ = false;
protocolList->currentItem()->setData(
kCurrentVarFieldRole,
QVariant::fromValue(variableFieldList->currentRow()));
_exit:
attribGroup->setHidden(current == NULL);
deleteButton->setEnabled(current != NULL);
}
void VariableFieldsWidget::on_addButton_clicked()
{
QListWidgetItem *protoItem = protocolList->currentItem();
if (!protoItem)
return;
AbstractProtocol *proto = protoItem->data(kProtocolPtrRole)
.value<AbstractProtocol*>();
OstProto::VariableField vf;
QListWidgetItem *vfItem = new QListWidgetItem;
proto->appendVariableField(vf);
setVariableFieldItem(vfItem, proto, vf);
variableFieldList->addItem(vfItem);
variableFieldList->setCurrentItem(vfItem);
decorateProtocolItem(protoItem);
}
void VariableFieldsWidget::on_deleteButton_clicked()
{
QListWidgetItem *protoItem = protocolList->currentItem();
int vfIdx = variableFieldList->currentRow();
if (!protoItem || (vfIdx < 0))
return;
AbstractProtocol *proto = protoItem->data(kProtocolPtrRole)
.value<AbstractProtocol*>();
proto->removeVariableField(vfIdx);
delete variableFieldList->takeItem(vfIdx);
// XXX: takeItem() above triggers a currentChanged, but the signal
// is emitted after the "current" is changed to an item after
// or before the item(s) to be deleted but before the item(s)
// are actually deleted - so the current inside that slot is not
// correct and we need to re-save it again
protocolList->currentItem()->setData(
kCurrentVarFieldRole,
QVariant::fromValue(variableFieldList->currentRow()));
decorateProtocolItem(protoItem);
}
void VariableFieldsWidget::on_field_currentIndexChanged(int index)
{
if (index < 0)
return;
QVariantMap vm = field->itemData(index).toMap();
if (index) { // standard frame fields
offset->setValue(vm["offset"].toUInt());
offset->setDisabled(true);
type->setCurrentIndex(vm["type"].toUInt());
type->setDisabled(true);
bitmask->setText(uintToHexStr(
vm["mask"].toUInt(),
typeSize(OstProto::VariableField::Type(
vm["type"].toUInt()))));
bitmask->setDisabled(true);
}
else { // custom field
offset->setEnabled(true);
type->setEnabled(true);
bitmask->setEnabled(true);
}
}
void VariableFieldsWidget::on_type_currentIndexChanged(int index)
{
if ((index < 0) || !protocolList->currentItem())
return;
AbstractProtocol *proto = protocolList->currentItem()
->data(kProtocolPtrRole)
.value<AbstractProtocol*>();
int protoSize = proto->protocolFrameSize();
switch (index)
{
case OstProto::VariableField::kCounter8:
offset->setRange(0, protoSize - 1);
bitmask->setInputMask("HH");
bitmask->setText("FF");
valueRange_->setRange(0, 0xFF);
count->setRange(1, 0x100);
step->setRange(0, 0xFF);
break;
case OstProto::VariableField::kCounter16:
offset->setRange(0, protoSize - 2);
bitmask->setInputMask("HHHH");
bitmask->setText("FFFF");
valueRange_->setRange(0, 0xFFFF);
count->setRange(1, 0x10000);
step->setRange(0, 0xFFFF);
break;
case OstProto::VariableField::kCounter32:
offset->setRange(0, protoSize - 4);
bitmask->setInputMask("HHHHHHHH");
bitmask->setText("FFFFFFFF");
valueRange_->setRange(0, 0xFFFFFFFF);
count->setRange(1, 0x7FFFFFFF); // XXX: QSpinBox max limited to int32
step->setRange(0, 0x7FFFFFFF);
break;
default:
Q_ASSERT(false); // unreachable
break;
}
}
void VariableFieldsWidget::updateCurrentVariableField()
{
// Prevent recursion
if (isProgLoad_)
return;
if (!protocolList->currentItem())
return;
if (!variableFieldList->currentItem())
return;
OstProto::VariableField vf;
vf.set_type(OstProto::VariableField::Type(type->currentIndex()));
vf.set_offset(offset->value());
vf.set_mask(hexStrToUInt(bitmask->text()));
vf.set_value(value->text().toUInt());
vf.set_mode(OstProto::VariableField::Mode(mode->currentIndex()));
vf.set_count(count->value());
vf.set_step(step->value());
QListWidgetItem *protoItem = protocolList->currentItem();
AbstractProtocol *proto = protoItem->data(kProtocolPtrRole)
.value<AbstractProtocol*>();
proto->mutableVariableField(variableFieldList->currentRow())->CopyFrom(vf);
setVariableFieldItem(variableFieldList->currentItem(), proto, vf);
}
void VariableFieldsWidget::decorateProtocolItem(QListWidgetItem *item)
{
AbstractProtocol *proto = item->data(kProtocolPtrRole)
.value<AbstractProtocol*>();
QFont font = item->font();
font.setBold(proto->variableFieldCount() > 0);
item->setFont(font);
}
void VariableFieldsWidget::loadProtocolFields(
const AbstractProtocol *protocol)
{
QVariantMap vm;
field->clear();
field->addItem("Custom");
for (int i = 0; i < protocol->fieldCount(); i++) {
if (!protocol->fieldFlags(i).testFlag(AbstractProtocol::FrameField))
continue;
QString name = protocol->fieldData(i, AbstractProtocol::FieldName)
.toString();
int bitOfs = protocol->fieldFrameBitOffset(i);
int byteOfs = bitOfs >> 3;
uint bitSize = protocol->fieldData(i, AbstractProtocol::FieldBitSize)
.toInt();
if (bitSize == 0)
continue;
vm["offset"] = byteOfs;
if (bitSize <= 8) {
vm["type"] = int(OstProto::VariableField::kCounter8);
vm["mask"] = ((0xFF << (8 - bitSize)) & 0xFF)
>> (bitOfs & 0x7);
}
else if (bitSize <= 16) {
vm["type"] = int(OstProto::VariableField::kCounter16);
vm["mask"] = ((0xFFFF << (16 - bitSize)) & 0xFFFF)
>> (bitOfs & 0x7);
}
else if (bitSize <= 32) {
vm["type"] = int(OstProto::VariableField::kCounter32);
vm["mask"] = ((0xFFFFFFFF << (32 - bitSize)) & 0xFFFFFFFF)
>> (bitOfs & 0x7);
}
else {
vm["type"] = int(OstProto::VariableField::kCounter32);
vm["mask"] = 0xFFFFFFFF;
}
field->addItem(name, vm);
}
}
/*! Given a VariableField::Type, return corresponding size in bytes */
int VariableFieldsWidget::typeSize(OstProto::VariableField::Type type)
{
switch(type) {
case OstProto::VariableField::kCounter8 : return 1;
case OstProto::VariableField::kCounter16: return 2;
case OstProto::VariableField::kCounter32: return 4;
default: break;
}
return 4;
}
/*! Given a variableField, return corresponding index in the field ComboBox */
int VariableFieldsWidget::fieldIndex(const OstProto::VariableField &vf)
{
QVariantMap vm;
vm["type"] = int(vf.type());
vm["offset"] = vf.offset();
vm["mask"] = vf.mask();
int index = field->findData(vm);
qDebug("vm %d %d 0x%x => index %d", vf.type(), vf.offset(), vf.mask(), index);
// Not found? Use 'Custom'
if (index < 0)
index = 0;
return index;
}
void VariableFieldsWidget::setVariableFieldItem(
QListWidgetItem *item,
const AbstractProtocol *protocol,
const OstProto::VariableField &vf)
{
uint from = vf.value() & vf.mask();
uint to;
QString fieldName = field->itemText(fieldIndex(vf));
QString itemText;
if (vf.mode() == OstProto::VariableField::kDecrement)
to = (vf.value() - (vf.count()-1)*vf.step()) & vf.mask();
else
to = (vf.value() + (vf.count()-1)*vf.step()) & vf.mask();
item->setData(kVarFieldRole, QVariant::fromValue(vf));
itemText = QString("%1 %2 %3 from %4 to %5")
.arg(protocol->shortName())
.arg(fieldName)
.arg(modeNames.at(vf.mode()))
.arg(from)
.arg(to);
if (vf.step() != 1)
itemText.append(QString(" step %1").arg(vf.step()));
item->setText(itemText);
}
|