removed TOP_LEVEL type for model indices

This commit is contained in:
csoler 2019-08-02 15:40:11 +02:00
parent 3ed7cf4c45
commit 47bee5198c
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
2 changed files with 46 additions and 71 deletions

View File

@ -135,22 +135,38 @@ bool RsFriendListModel::hasChildren(const QModelIndex &parent) const
return false; return false;
} }
RsFriendListModel::EntryIndex RsFriendListModel::EntryIndex::topLevelIndex(uint32_t row,uint32_t nb_groups)
{
EntryIndex e;
if(row < nb_groups)
{
e.type=ENTRY_TYPE_GROUP;
e.group_index=row;
return e;
}
else
{
e.type = ENTRY_TYPE_PROFILE;
e.profile_index = row - nb_groups;
e.group_index = 0xff;
return e;
}
}
RsFriendListModel::EntryIndex RsFriendListModel::EntryIndex::parent() const RsFriendListModel::EntryIndex RsFriendListModel::EntryIndex::parent() const
{ {
EntryIndex i(*this); EntryIndex i(*this);
switch(type) switch(type)
{ {
case ENTRY_TYPE_GROUP: i.type = ENTRY_TYPE_TOP_LEVEL; case ENTRY_TYPE_GROUP: return EntryIndex();
i.group_index = 0;
break;
case ENTRY_TYPE_TOP_LEVEL: i.type = ENTRY_TYPE_UNKNOWN; case ENTRY_TYPE_PROFILE:
i.top_level_index = 0; if(i.group_index==0xff)
break; return EntryIndex();
else
case ENTRY_TYPE_PROFILE: i.type = (i.group_index==0xff)?ENTRY_TYPE_TOP_LEVEL:ENTRY_TYPE_GROUP; i.type = ENTRY_TYPE_GROUP;
i.top_level_index = 0;
break; break;
case ENTRY_TYPE_NODE: i.type = ENTRY_TYPE_PROFILE; case ENTRY_TYPE_NODE: i.type = ENTRY_TYPE_PROFILE;
@ -167,12 +183,8 @@ RsFriendListModel::EntryIndex RsFriendListModel::EntryIndex::child(int row,const
switch(type) switch(type)
{ {
case ENTRY_TYPE_UNKNOWN: i.type = ENTRY_TYPE_TOP_LEVEL; case ENTRY_TYPE_UNKNOWN:
i.top_level_index = row; i = top_level[row];
break;
case ENTRY_TYPE_TOP_LEVEL:
i = top_level[row];
break; break;
case ENTRY_TYPE_GROUP: i.type = ENTRY_TYPE_PROFILE; case ENTRY_TYPE_GROUP: i.type = ENTRY_TYPE_PROFILE;
@ -195,8 +207,7 @@ uint32_t RsFriendListModel::EntryIndex::parentRow(uint32_t nb_groups) const
switch(type) switch(type)
{ {
default: default:
case ENTRY_TYPE_UNKNOWN: return 0; case ENTRY_TYPE_UNKNOWN : return 0;
case ENTRY_TYPE_TOP_LEVEL: return top_level_index;
case ENTRY_TYPE_GROUP : return group_index; case ENTRY_TYPE_GROUP : return group_index;
case ENTRY_TYPE_PROFILE : return (group_index==0xff)?(profile_index+nb_groups):profile_index; case ENTRY_TYPE_PROFILE : return (group_index==0xff)?(profile_index+nb_groups):profile_index;
case ENTRY_TYPE_NODE : return node_index; case ENTRY_TYPE_NODE : return node_index;
@ -212,7 +223,7 @@ uint32_t RsFriendListModel::EntryIndex::parentRow(uint32_t nb_groups) const
// | | | +---- location/node index // | | | +---- location/node index
// | | +------- profile index // | | +------- profile index
// | +---------- group index // | +---------- group index
// +------------- top level index // +------------- type
// //
// Only valid indexes a 0x01->0xff. 0x00 means "no index" // Only valid indexes a 0x01->0xff. 0x00 means "no index"
// //
@ -221,13 +232,7 @@ bool RsFriendListModel::convertIndexToInternalId(const EntryIndex& e,quintptr& i
{ {
// the internal id is set to the place in the table of items. We simply shift to allow 0 to mean something special. // the internal id is set to the place in the table of items. We simply shift to allow 0 to mean something special.
if(e.top_level_index > 254 || e.group_index > 255 || e.profile_index > 255 || e.node_index > 255) id = (((uint32_t)e.type) << 24) + ((uint32_t)e.group_index << 16) + (uint32_t)(e.profile_index << 8) + (uint32_t)e.node_index;
{
RsErr() << "Index out of scope in " << __PRETTY_FUNCTION__ << std::endl;
return false;
}
id = ( (e.top_level_index + 1) << 24) + (e.group_index << 16) + (e.profile_index << 8) + e.node_index;
return true; return true;
} }
@ -236,28 +241,11 @@ bool RsFriendListModel::convertInternalIdToIndex(quintptr ref,EntryIndex& e)
if(ref == 0) if(ref == 0)
return false ; return false ;
e.profile_index = e.node_index = e.group_index = 0xff;
e.top_level_index = uint8_t((ref >> 24) & 0xff)-1;
if(e.top_level_index == 0xff)
{
e.type = ENTRY_TYPE_UNKNOWN ;
return true;
}
e.group_index = (ref >> 16) & 0xff; e.group_index = (ref >> 16) & 0xff;
e.profile_index = (ref >> 8) & 0xff; e.profile_index = (ref >> 8) & 0xff;
e.node_index = (ref >> 0) & 0xff; e.node_index = (ref >> 0) & 0xff;
if(e.node_index < 0xff) e.type = static_cast<EntryType>((ref >> 24) & 0xff);
e.type = ENTRY_TYPE_NODE;
else if(e.profile_index < 0xff)
e.type = ENTRY_TYPE_PROFILE;
else if(e.group_index < 0xff)
e.type = ENTRY_TYPE_GROUP;
else
e.type = ENTRY_TYPE_TOP_LEVEL;
return true; return true;
} }
@ -271,7 +259,7 @@ QModelIndex RsFriendListModel::index(int row, int column, const QModelIndex& par
{ {
quintptr ref ; quintptr ref ;
convertIndexToInternalId(EntryIndex::topLevelIndex(row),ref); convertIndexToInternalId(EntryIndex::topLevelIndex(row,mGroups.size()),ref);
return createIndex(row,column,ref) ; return createIndex(row,column,ref) ;
} }
@ -545,7 +533,7 @@ QVariant RsFriendListModel::sortRole(const EntryIndex& fmpe,int column) const
QVariant RsFriendListModel::displayRole(const EntryIndex& e, int col) const QVariant RsFriendListModel::displayRole(const EntryIndex& e, int col) const
{ {
std::cerr << " Display role " << e.type << ", (" << (int)e.top_level_index << "," << (int)e.group_index << ","<< (int)e.profile_index << ","<< (int)e.node_index << ") col="<< col<<": " << std::endl; std::cerr << " Display role " << e.type << ", (" << (int)e.group_index << ","<< (int)e.profile_index << ","<< (int)e.node_index << ") col="<< col<<": " << std::endl;
switch(e.type) switch(e.type)
{ {
@ -594,7 +582,7 @@ QVariant RsFriendListModel::displayRole(const EntryIndex& e, int col) const
switch(col) switch(col)
{ {
case COLUMN_THREAD_NAME: return QVariant(QString::fromUtf8(node->location.c_str())); case COLUMN_THREAD_NAME: return (node->location.empty())?QVariant(QString::fromStdString(node->id.toStdString())):QVariant(QString::fromUtf8(node->location.c_str()));
case COLUMN_THREAD_LAST_CONTACT: return QVariant(QDateTime::fromTime_t(node->lastConnect).toString()); case COLUMN_THREAD_LAST_CONTACT: return QVariant(QDateTime::fromTime_t(node->lastConnect).toString());
case COLUMN_THREAD_IP: return QVariant( (node->state & RS_PEER_STATE_CONNECTED) ? StatusDefs::connectStateIpString(*node) : QString("---")); case COLUMN_THREAD_IP: return QVariant( (node->state & RS_PEER_STATE_CONNECTED) ? StatusDefs::connectStateIpString(*node) : QString("---"));
case COLUMN_THREAD_ID: return QVariant( QString::fromStdString(node->id.toStdString()) ); case COLUMN_THREAD_ID: return QVariant( QString::fromStdString(node->id.toStdString()) );
@ -622,25 +610,17 @@ const RsFriendListModel::RsProfileDetails *RsFriendListModel::getProfileInfo(con
if(e.type != ENTRY_TYPE_PROFILE) if(e.type != ENTRY_TYPE_PROFILE)
return NULL ; return NULL ;
if(e.top_level_index >= mTopLevel.size())
return NULL ;
if(e.group_index < 0xff) if(e.group_index < 0xff)
{ {
const HierarchicalGroupInformation& group(mGroups[e.group_index]); const HierarchicalGroupInformation& group(mGroups[e.group_index]);
if(e.group_index >= group.child_profile_indices.size()) if(e.profile_index >= group.child_profile_indices.size())
return NULL ; return NULL ;
return &mProfiles[group.child_profile_indices[e.group_index]].profile_info; return &mProfiles[group.child_profile_indices[e.profile_index]].profile_info;
} }
else else
{ return &mProfiles[e.profile_index].profile_info;
if(e.group_index >= mProfiles.size())
return NULL ;
return &mProfiles[e.group_index].profile_info;
}
} }
const RsFriendListModel::RsNodeDetails *RsFriendListModel::getNodeInfo(const EntryIndex& e) const const RsFriendListModel::RsNodeDetails *RsFriendListModel::getNodeInfo(const EntryIndex& e) const
@ -648,26 +628,23 @@ const RsFriendListModel::RsNodeDetails *RsFriendListModel::getNodeInfo(const Ent
if(e.type != ENTRY_TYPE_NODE) if(e.type != ENTRY_TYPE_NODE)
return NULL ; return NULL ;
if(e.top_level_index >= mTopLevel.size())
return NULL ;
uint32_t pindex = 0; uint32_t pindex = 0;
if(e.group_index < 0xff) if(e.group_index < 0xff)
{ {
const HierarchicalGroupInformation& group(mGroups[e.group_index]); const HierarchicalGroupInformation& group(mGroups[e.group_index]);
if(e.group_index >= group.child_profile_indices.size()) if(e.profile_index >= group.child_profile_indices.size())
return NULL ; return NULL ;
pindex = group.child_profile_indices[e.group_index]; pindex = group.child_profile_indices[e.profile_index];
} }
else else
{ {
if(e.group_index >= mProfiles.size()) if(e.profile_index >= mProfiles.size())
return NULL ; return NULL ;
pindex = e.group_index; pindex = e.profile_index;
} }
if(e.node_index >= mProfiles[pindex].child_node_indices.size()) if(e.node_index >= mProfiles[pindex].child_node_indices.size())
@ -974,7 +951,6 @@ void RsFriendListModel::updateInternalData()
EntryIndex e; EntryIndex e;
e.type = ENTRY_TYPE_GROUP; e.type = ENTRY_TYPE_GROUP;
e.group_index = i; e.group_index = i;
e.top_level_index = mTopLevel.size();
mTopLevel.push_back(e); mTopLevel.push_back(e);
} }
@ -985,8 +961,9 @@ void RsFriendListModel::updateInternalData()
{ {
EntryIndex e; EntryIndex e;
e.type = ENTRY_TYPE_PROFILE; e.type = ENTRY_TYPE_PROFILE;
e.top_level_index = mTopLevel.size();
e.profile_index = i; e.profile_index = i;
e.group_index = 0xff;
mTopLevel.push_back(e); mTopLevel.push_back(e);
} }

View File

@ -87,8 +87,7 @@ public:
enum EntryType{ ENTRY_TYPE_UNKNOWN = 0x00, enum EntryType{ ENTRY_TYPE_UNKNOWN = 0x00,
ENTRY_TYPE_GROUP = 0x01, ENTRY_TYPE_GROUP = 0x01,
ENTRY_TYPE_PROFILE = 0x02, ENTRY_TYPE_PROFILE = 0x02,
ENTRY_TYPE_NODE = 0x03, ENTRY_TYPE_NODE = 0x03
ENTRY_TYPE_TOP_LEVEL = 0x04
}; };
// This structure encodes the position of a node in the hierarchy. The type tells which of the index fields are valid. // This structure encodes the position of a node in the hierarchy. The type tells which of the index fields are valid.
@ -96,14 +95,13 @@ public:
struct EntryIndex struct EntryIndex
{ {
public: public:
EntryIndex() : type(ENTRY_TYPE_UNKNOWN),top_level_index(0xff),group_index(0xff),profile_index(0xff),node_index(0xff) {} EntryIndex() : type(ENTRY_TYPE_UNKNOWN),group_index(0xff),profile_index(0xff),node_index(0xff) {}
EntryType type; // type of the entry (group,profile,location) EntryType type; // type of the entry (group,profile,location)
// Indices w.r.t. parent. The set of indices entirely determines the position of the entry in the hierarchy. // Indices w.r.t. parent. The set of indices entirely determines the position of the entry in the hierarchy.
// An index of 0xff means "undefined" // An index of 0xff means "undefined"
uint8_t top_level_index; // index in the mTopLevel tab
uint8_t group_index; // index of the group in mGroups tab uint8_t group_index; // index of the group in mGroups tab
uint8_t profile_index; // index of the child profile in its own group if group_index < 0xff, or in the mProfiles tab otherwise. uint8_t profile_index; // index of the child profile in its own group if group_index < 0xff, or in the mProfiles tab otherwise.
uint8_t node_index; // index of the child node in its own profile uint8_t node_index; // index of the child node in its own profile
@ -112,7 +110,7 @@ public:
EntryIndex child(int row,const std::vector<EntryIndex>& top_level) const; EntryIndex child(int row,const std::vector<EntryIndex>& top_level) const;
uint32_t parentRow(uint32_t nb_groups) const; uint32_t parentRow(uint32_t nb_groups) const;
static EntryIndex topLevelIndex(uint32_t row) { EntryIndex e; e.type=ENTRY_TYPE_TOP_LEVEL; e.top_level_index=row; return e; } static EntryIndex topLevelIndex(uint32_t row, uint32_t nb_groups) ;
}; };
QModelIndex root() const{ return createIndex(0,0,(void*)NULL) ;} QModelIndex root() const{ return createIndex(0,0,(void*)NULL) ;}