2008-06-14 01:35:17 -05:00
|
|
|
#include <QHostAddress>
|
2009-08-02 09:52:34 -05:00
|
|
|
|
2008-05-25 06:30:30 -05:00
|
|
|
#include "packetmodel.h"
|
2009-08-02 09:52:34 -05:00
|
|
|
#include "../common/protocollistiterator.h"
|
|
|
|
#include "../common/abstractprotocol.h"
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2009-08-02 09:52:34 -05:00
|
|
|
PacketModel::PacketModel(QObject *parent)
|
2008-05-25 06:30:30 -05:00
|
|
|
{
|
2009-04-27 11:51:44 -05:00
|
|
|
}
|
|
|
|
|
2009-08-02 09:52:34 -05:00
|
|
|
void PacketModel::setSelectedProtocols(ProtocolListIterator &iter)
|
2009-04-27 11:51:44 -05:00
|
|
|
{
|
2009-08-02 09:52:34 -05:00
|
|
|
QList<const AbstractProtocol*> currentProtocols;
|
|
|
|
|
|
|
|
iter.toFront();
|
|
|
|
while (iter.hasNext())
|
|
|
|
currentProtocols.append(iter.next());
|
|
|
|
|
|
|
|
if (mSelectedProtocols != currentProtocols)
|
2009-05-24 09:54:11 -05:00
|
|
|
{
|
2009-08-02 09:52:34 -05:00
|
|
|
mSelectedProtocols = currentProtocols;
|
2009-05-24 09:54:11 -05:00
|
|
|
reset();
|
|
|
|
}
|
2008-05-25 06:30:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int PacketModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2008-05-25 06:53:49 -05:00
|
|
|
IndexId parentId;
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2009-04-27 11:51:44 -05:00
|
|
|
// qDebug("in %s", __FUNCTION__);
|
|
|
|
|
2008-08-23 23:39:08 -05:00
|
|
|
// Parent == Invalid i.e. Invisible Root.
|
|
|
|
// ==> Children are Protocol (Top Level) Items
|
2008-05-25 06:53:49 -05:00
|
|
|
if (!parent.isValid())
|
2009-04-27 11:51:44 -05:00
|
|
|
return mSelectedProtocols.size();
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
// Parent - Valid Item
|
|
|
|
parentId.w = parent.internalId();
|
|
|
|
switch(parentId.ws.type)
|
2008-05-25 06:30:30 -05:00
|
|
|
{
|
2008-05-25 06:53:49 -05:00
|
|
|
case ITYP_PROTOCOL:
|
2009-04-27 11:51:44 -05:00
|
|
|
return mSelectedProtocols.at(parentId.ws.protocol)->frameFieldCount();
|
2008-05-25 06:53:49 -05:00
|
|
|
case ITYP_FIELD:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
qWarning("%s: Unhandled ItemType", __FUNCTION__);
|
2008-05-25 06:30:30 -05:00
|
|
|
}
|
|
|
|
|
2009-10-14 10:16:56 -05:00
|
|
|
Q_ASSERT(1 == 0); // Unreachable code
|
2008-05-25 06:53:49 -05:00
|
|
|
qWarning("%s: Catch all - need to investigate", __FUNCTION__);
|
2008-05-25 06:30:30 -05:00
|
|
|
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;
|
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
// Parent is Invisible Root
|
|
|
|
// Request for a Protocol Item
|
|
|
|
if (!parent.isValid())
|
2008-05-25 06:30:30 -05:00
|
|
|
{
|
2008-05-25 06:53:49 -05:00
|
|
|
id.w = 0;
|
|
|
|
id.ws.type = ITYP_PROTOCOL;
|
2008-06-14 01:35:17 -05:00
|
|
|
id.ws.protocol = row;
|
2008-05-25 06:30:30 -05:00
|
|
|
index = createIndex(row, col, id.w);
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
// Parent is a Valid Item
|
|
|
|
parentId.w = parent.internalId();
|
|
|
|
id.w = parentId.w;
|
|
|
|
switch(parentId.ws.type)
|
2008-05-25 06:30:30 -05:00
|
|
|
{
|
2008-05-25 06:53:49 -05:00
|
|
|
case ITYP_PROTOCOL:
|
|
|
|
id.ws.type = ITYP_FIELD;
|
2008-05-25 06:30:30 -05:00
|
|
|
index = createIndex(row, col, id.w);
|
|
|
|
goto _exit;
|
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
case ITYP_FIELD:
|
2009-10-14 10:16:56 -05:00
|
|
|
Q_ASSERT(1 == 0); // Unreachable code
|
2008-05-25 06:53:49 -05:00
|
|
|
goto _exit;
|
|
|
|
|
|
|
|
default:
|
|
|
|
qWarning("%s: Unhandled ItemType", __FUNCTION__);
|
|
|
|
}
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2009-10-14 10:16:56 -05:00
|
|
|
Q_ASSERT(1 == 0); // Unreachable code
|
2008-06-14 01:35:17 -05:00
|
|
|
|
2008-05-25 06:30:30 -05:00
|
|
|
_exit:
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex PacketModel::parent(const QModelIndex &index) const
|
|
|
|
{
|
2008-05-25 06:53:49 -05:00
|
|
|
QModelIndex parentIndex;
|
2008-05-25 06:30:30 -05:00
|
|
|
IndexId id, parentId;
|
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
if (!index.isValid())
|
|
|
|
return QModelIndex();
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
id.w = index.internalId();
|
|
|
|
parentId.w = id.w;
|
|
|
|
switch(id.ws.type)
|
2008-05-25 06:30:30 -05:00
|
|
|
{
|
2008-05-25 06:53:49 -05:00
|
|
|
case ITYP_PROTOCOL:
|
|
|
|
// return invalid index for invisible root
|
|
|
|
goto _exit;
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
case ITYP_FIELD:
|
|
|
|
parentId.ws.type = ITYP_PROTOCOL;
|
2008-06-14 01:35:17 -05:00
|
|
|
parentIndex = createIndex(id.ws.protocol, 0, parentId.w);
|
2008-05-25 06:53:49 -05:00
|
|
|
goto _exit;
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
default:
|
|
|
|
qWarning("%s: Unhandled ItemType", __FUNCTION__);
|
2008-05-25 06:30:30 -05:00
|
|
|
}
|
|
|
|
|
2008-06-14 01:35:17 -05:00
|
|
|
Q_ASSERT(1 == 1); // Unreachable code
|
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
_exit:
|
|
|
|
return parentIndex;
|
2008-05-25 06:30:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant PacketModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2009-11-03 08:02:09 -06:00
|
|
|
IndexId id;
|
|
|
|
int fieldIdx = 0;
|
2008-05-25 06:53:49 -05:00
|
|
|
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
2008-08-30 03:49:08 -05:00
|
|
|
id.w = index.internalId();
|
|
|
|
|
2009-11-03 08:02:09 -06:00
|
|
|
if (id.ws.type == ITYP_FIELD)
|
|
|
|
{
|
|
|
|
const AbstractProtocol *p = mSelectedProtocols.at(id.ws.protocol);
|
|
|
|
int n = index.row() + 1;
|
|
|
|
|
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
if (!(p->fieldFlags(fieldIdx).testFlag(
|
|
|
|
AbstractProtocol::FieldIsMeta)))
|
|
|
|
n--;
|
|
|
|
fieldIdx++;
|
|
|
|
}
|
|
|
|
fieldIdx--;
|
|
|
|
}
|
|
|
|
|
2008-08-30 03:49:08 -05:00
|
|
|
// FIXME(HI): Relook at this completely
|
|
|
|
if (role == Qt::UserRole)
|
|
|
|
{
|
|
|
|
switch(id.ws.type)
|
|
|
|
{
|
|
|
|
case ITYP_PROTOCOL:
|
2009-04-27 11:51:44 -05:00
|
|
|
qDebug("*** %d/%d", id.ws.protocol, mSelectedProtocols.size());
|
|
|
|
return mSelectedProtocols.at(id.ws.protocol)->
|
|
|
|
protocolFrameValue();
|
2008-08-30 03:49:08 -05:00
|
|
|
|
|
|
|
case ITYP_FIELD:
|
2009-04-27 11:51:44 -05:00
|
|
|
return mSelectedProtocols.at(id.ws.protocol)->fieldData(
|
2009-11-03 08:02:09 -06:00
|
|
|
fieldIdx, AbstractProtocol::FieldFrameValue);
|
2008-08-30 03:49:08 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
qWarning("%s: Unhandled ItemType", __FUNCTION__);
|
|
|
|
}
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
2009-04-27 11:51:44 -05:00
|
|
|
// 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(
|
2009-11-03 08:02:09 -06:00
|
|
|
fieldIdx, AbstractProtocol::FieldBitSize);
|
2009-04-27 11:51:44 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
qWarning("%s: Unhandled ItemType", __FUNCTION__);
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
if (role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
switch(id.ws.type)
|
|
|
|
{
|
|
|
|
case ITYP_PROTOCOL:
|
2008-08-23 23:39:08 -05:00
|
|
|
return QString("%1 (%2)")
|
2009-04-27 11:51:44 -05:00
|
|
|
.arg(mSelectedProtocols.at(id.ws.protocol)->shortName())
|
|
|
|
.arg(mSelectedProtocols.at(id.ws.protocol)->name());
|
2008-05-25 06:53:49 -05:00
|
|
|
|
|
|
|
case ITYP_FIELD:
|
2009-11-03 08:02:09 -06:00
|
|
|
return mSelectedProtocols.at(id.ws.protocol)->fieldData(fieldIdx,
|
2009-04-27 11:51:44 -05:00
|
|
|
AbstractProtocol::FieldName).toString() + QString(" : ") +
|
2009-11-03 08:02:09 -06:00
|
|
|
mSelectedProtocols.at(id.ws.protocol)->fieldData(fieldIdx,
|
2009-04-27 11:51:44 -05:00
|
|
|
AbstractProtocol::FieldTextValue).toString();
|
2008-05-25 06:53:49 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
qWarning("%s: Unhandled ItemType", __FUNCTION__);
|
|
|
|
}
|
2008-05-25 06:30:30 -05:00
|
|
|
|
2008-06-14 01:35:17 -05:00
|
|
|
Q_ASSERT(1 == 1); // Unreachable code
|
|
|
|
|
2008-05-25 06:53:49 -05:00
|
|
|
return QVariant();
|
2008-05-25 06:30:30 -05:00
|
|
|
}
|