ostinato/client/packetmodel.cpp
Srivats P. 1357f495ac Major rewrite of the protocol framework - changes not yet complete
Common
------
- Change in OstProto
	- Individual protocols are now extensions of (new) message 'Protocol' instead of 'Stream'
	- Stream now contains a repeated Protocol which also determines the ordered set of currently selected protocols; StreamCore.frame_proto which was doing this earlier has been removed

- Change in AbstractProtocol Interface
	- Parent changed to StreamBase
	- Corresponding change in constructor and factory func - createInstance()
	- new method protocolNumber() - returns unique id for each protocol
	- protoDataCopyInto/From() now copies into OstProto::Protocol instead of OstProto::Stream
- Change in all subclasses of AbstractProtocol to match new interface
- For all protocols, configFrom is no longer static, but each protocol has its own configForm
	- configForm creation is lazy
	- configForm is still a child of the protocol i.e. it will be destroyed alongwith the protocol
	- TODO: convert configWidget() to a pure factory function i.e. the protocol does not own the configForm - this requires us to pass the widget into load/storeConfigWidget() methods

- ProtocolCollection class removed alongwith its .h and .cpp
- ProtocolList class redefined to serve the purpose of ProtocolCollection
- New class ProtocolListIterator defined to iterate ProtocolList
- AbstractProtocol methods now use the ProtocolListIterator
- Factory function createProtocol() added to ProtocolManager
- OstProto::StreamCore accessor functions moved from Stream to StreamBase

Server
------
- MyService uses the newly moved accessors to StreamBase for OstProto::StreamCore members
- StreamInfo now uses the protocols to create the packet

Client
------
- StreamConfigDialog now uses ProtocolListIterator
- So does PacketModel
2009-08-02 14:52:34 +00:00

204 lines
4.4 KiB
C++

#include <QHostAddress>
#include "packetmodel.h"
#include "../common/protocollistiterator.h"
#include "../common/abstractprotocol.h"
PacketModel::PacketModel(QObject *parent)
{
}
void PacketModel::setSelectedProtocols(ProtocolListIterator &iter)
{
QList<const AbstractProtocol*> currentProtocols;
iter.toFront();
while (iter.hasNext())
currentProtocols.append(iter.next());
if (mSelectedProtocols != currentProtocols)
{
mSelectedProtocols = currentProtocols;
reset();
}
}
int PacketModel::rowCount(const QModelIndex &parent) const
{
IndexId parentId;
// qDebug("in %s", __FUNCTION__);
// Parent == Invalid i.e. Invisible Root.
// ==> Children are Protocol (Top Level) Items
if (!parent.isValid())
return mSelectedProtocols.size();
// Parent - Valid Item
parentId.w = parent.internalId();
switch(parentId.ws.type)
{
case ITYP_PROTOCOL:
return mSelectedProtocols.at(parentId.ws.protocol)->frameFieldCount();
case ITYP_FIELD:
return 0;
default:
qWarning("%s: Unhandled ItemType", __FUNCTION__);
}
Q_ASSERT(1 == 1); // Unreachable code
qWarning("%s: Catch all - need to investigate", __FUNCTION__);
return 0; // catch all
}
int PacketModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
QModelIndex PacketModel::index(int row, int col, const QModelIndex &parent) const
{
QModelIndex index;
IndexId id, parentId;
if (!hasIndex(row, col, parent))
goto _exit;
// Parent is Invisible Root
// Request for a Protocol Item
if (!parent.isValid())
{
id.w = 0;
id.ws.type = ITYP_PROTOCOL;
id.ws.protocol = row;
index = createIndex(row, col, id.w);
goto _exit;
}
// Parent is a Valid Item
parentId.w = parent.internalId();
id.w = parentId.w;
switch(parentId.ws.type)
{
case ITYP_PROTOCOL:
id.ws.type = ITYP_FIELD;
index = createIndex(row, col, id.w);
goto _exit;
case ITYP_FIELD:
goto _exit;
default:
qWarning("%s: Unhandled ItemType", __FUNCTION__);
}
Q_ASSERT(1 == 1); // Unreachable code
_exit:
return index;
}
QModelIndex PacketModel::parent(const QModelIndex &index) const
{
QModelIndex parentIndex;
IndexId id, parentId;
if (!index.isValid())
return QModelIndex();
id.w = index.internalId();
parentId.w = id.w;
switch(id.ws.type)
{
case ITYP_PROTOCOL:
// return invalid index for invisible root
goto _exit;
case ITYP_FIELD:
parentId.ws.type = ITYP_PROTOCOL;
parentIndex = createIndex(id.ws.protocol, 0, parentId.w);
goto _exit;
default:
qWarning("%s: Unhandled ItemType", __FUNCTION__);
}
Q_ASSERT(1 == 1); // Unreachable code
_exit:
return parentIndex;
}
QVariant PacketModel::data(const QModelIndex &index, int role) const
{
IndexId id;
if (!index.isValid())
return QVariant();
id.w = index.internalId();
// FIXME(HI): Relook at this completely
if (role == Qt::UserRole)
{
switch(id.ws.type)
{
case ITYP_PROTOCOL:
qDebug("*** %d/%d", id.ws.protocol, mSelectedProtocols.size());
return mSelectedProtocols.at(id.ws.protocol)->
protocolFrameValue();
case ITYP_FIELD:
return mSelectedProtocols.at(id.ws.protocol)->fieldData(
index.row(), AbstractProtocol::FieldFrameValue);
default:
qWarning("%s: Unhandled ItemType", __FUNCTION__);
}
return QByteArray();
}
// FIXME: Use a new enum here instead of UserRole
if (role == (Qt::UserRole+1))
{
switch(id.ws.type)
{
case ITYP_PROTOCOL:
return mSelectedProtocols.at(id.ws.protocol)->
protocolFrameValue().size();
case ITYP_FIELD:
return mSelectedProtocols.at(id.ws.protocol)->fieldData(
index.row(), AbstractProtocol::FieldBitSize);
default:
qWarning("%s: Unhandled ItemType", __FUNCTION__);
}
return QVariant();
}
if (role != Qt::DisplayRole)
return QVariant();
switch(id.ws.type)
{
case ITYP_PROTOCOL:
return QString("%1 (%2)")
.arg(mSelectedProtocols.at(id.ws.protocol)->shortName())
.arg(mSelectedProtocols.at(id.ws.protocol)->name());
case ITYP_FIELD:
return mSelectedProtocols.at(id.ws.protocol)->fieldData(index.row(),
AbstractProtocol::FieldName).toString() + QString(" : ") +
mSelectedProtocols.at(id.ws.protocol)->fieldData(index.row(),
AbstractProtocol::FieldTextValue).toString();
default:
qWarning("%s: Unhandled ItemType", __FUNCTION__);
}
Q_ASSERT(1 == 1); // Unreachable code
return QVariant();
}