fixed bug in sizeHintRole()

This commit is contained in:
csoler 2018-11-19 21:45:42 +01:00
parent a532b68b8e
commit dbeb97c0e9
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
2 changed files with 148 additions and 81 deletions

View File

@ -42,32 +42,25 @@ RsGxsForumModel::RsGxsForumModel(QObject *parent)
mPosts[N].children.push_back(ForumModelIndex(N+1)); mPosts[N].children.push_back(ForumModelIndex(N+1));
mPosts[N+1].parent = ForumModelIndex(N); mPosts[N+1].parent = ForumModelIndex(N);
mPosts[N+1].prow = 0; mPosts[N+1].prow = 0;
RsMsgMetaData meta;
meta.mMsgName = "message " + (QString::number(N+1).toStdString()) ;
mPosts[N+1].meta_versions.push_back(meta);
} }
int RsGxsForumModel::rowCount(const QModelIndex& parent) const int RsGxsForumModel::rowCount(const QModelIndex& parent) const
{ {
void *ref = (parent.isValid())?parent.internalPointer():NULL ; if(parent.column() > 0)
return 0;
if(mPosts.empty()) // security. Should never happen. if(mPosts.empty()) // security. Should never happen.
return 0; return 0;
uint32_t entry = 0 ; if(!parent.isValid())
int source_id ; return getChildrenCount(NULL);
else
if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size()) return getChildrenCount(parent.internalPointer());
{
#ifdef DEBUG_FORUMMODEL
std::cerr << "rowCount-2(" << parent << ") : " << 0 << std::endl;
#endif
return 0 ;
} }
#ifdef DEBUG_FORUMMODEL
std::cerr << "rowCount-3(" << parent << ") : " << mPosts[entry].children.size() << std::endl;
#endif
return mPosts[entry].children.size();
}
int RsGxsForumModel::columnCount(const QModelIndex &parent) const int RsGxsForumModel::columnCount(const QModelIndex &parent) const
{ {
return COLUMN_COUNT ; return COLUMN_COUNT ;
@ -75,7 +68,10 @@ int RsGxsForumModel::columnCount(const QModelIndex &parent) const
bool RsGxsForumModel::hasChildren(const QModelIndex &parent) const bool RsGxsForumModel::hasChildren(const QModelIndex &parent) const
{ {
void *ref = (parent.isValid())?parent.internalPointer():NULL ; if(!parent.isValid())
return true;
void *ref = parent.internalPointer();
uint32_t entry = 0; uint32_t entry = 0;
if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size()) if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size())
@ -86,8 +82,8 @@ bool RsGxsForumModel::hasChildren(const QModelIndex &parent) const
return false ; return false ;
} }
#ifdef DEBUG_DOWNLOADLIST #ifdef DEBUG_FORUMMODEL
std::cerr << "hasChildren-3(" << parent << ") : " << !mDownloads[entry].peers.empty() << std::endl; std::cerr << "hasChildren-3(" << parent << ") : " << !mPosts[entry].children.empty() << std::endl;
#endif #endif
return !mPosts[entry].children.empty(); return !mPosts[entry].children.empty();
} }
@ -112,7 +108,7 @@ bool RsGxsForumModel::convertRefPointerToTabEntry(void *ref,uint32_t& entry)
if(val > (intptr_t)(~(uint32_t(0)))) // make sure the pointer is an int that fits in 32bits if(val > (intptr_t)(~(uint32_t(0)))) // make sure the pointer is an int that fits in 32bits
{ {
std::cerr << "(EE) trying to make a ForumModelIndex out of a number that is larger than 2^32 !" << std::endl; std::cerr << "(EE) trying to make a ForumModelIndex out of a number that is larger than 2^32-1 !" << std::endl;
return false ; return false ;
} }
entry = uint32_t(val); entry = uint32_t(val);
@ -122,70 +118,117 @@ bool RsGxsForumModel::convertRefPointerToTabEntry(void *ref,uint32_t& entry)
QModelIndex RsGxsForumModel::index(int row, int column, const QModelIndex & parent) const QModelIndex RsGxsForumModel::index(int row, int column, const QModelIndex & parent) const
{ {
// if(!hasIndex(row,column,parent))
if(row < 0 || column < 0 || column >= COLUMN_COUNT) if(row < 0 || column < 0 || column >= COLUMN_COUNT)
return QModelIndex(); return QModelIndex();
void *parent_ref = (parent.isValid())?parent.internalPointer():NULL ; void *ref = getChildRef(parent.internalPointer(),row);
uint32_t parent_entry = 0;
int source_id=0 ;
// We dont need to handle parent==NULL because the conversion will return entry=0 which is what we need.
if(!convertRefPointerToTabEntry(parent_ref,parent_entry) || parent_entry >= mPosts.size())
{
#ifdef DEBUG_FORUMMODEL
std::cerr << "index-5(" << row << "," << column << " parent=" << parent << ") : " << "NULL"<< std::endl ;
#endif
return QModelIndex() ;
}
void *ref = NULL ;
if(row >= mPosts[parent_entry].children.size() || !convertTabEntryToRefPointer(mPosts[parent_entry].children[row],ref))
{
#ifdef DEBUG_FORUMMODEL
std::cerr << "index-4(" << row << "," << column << " parent=" << parent << ") : " << "NULL" << std::endl;
#endif
return QModelIndex() ;
}
#ifdef DEBUG_FORUMMODEL #ifdef DEBUG_FORUMMODEL
std::cerr << "index-3(" << row << "," << column << " parent=" << parent << ") : " << createIndex(row,column,ref) << std::endl; std::cerr << "index-3(" << row << "," << column << " parent=" << parent << ") : " << createIndex(row,column,ref) << std::endl;
#endif #endif
return createIndex(row,column,ref) ; return createIndex(row,column,ref) ;
} }
QModelIndex RsGxsForumModel::parent(const QModelIndex& child) const QModelIndex RsGxsForumModel::parent(const QModelIndex& index) const
{ {
void *child_ref = (child.isValid())?child.internalPointer():NULL ; if(!index.isValid())
if(!child_ref)
return QModelIndex(); return QModelIndex();
ForumModelIndex child_entry ; void *child_ref = index.internalPointer();
int row=0;
if(!convertRefPointerToTabEntry(child_ref,child_entry) || child_entry >= mPosts.size()) void *parent_ref = getParentRef(child_ref,row) ;
if(parent_ref == NULL) // root
return QModelIndex() ; return QModelIndex() ;
void *parent_ref =NULL; return createIndex(row,0,parent_ref);
ForumModelIndex parent_entry = mPosts[child_entry].parent; }
QModelIndex indx;
Qt::ItemFlags RsGxsForumModel::flags(const QModelIndex& index) const
{
if (!index.isValid())
return 0;
return QAbstractItemModel::flags(index);
}
void *RsGxsForumModel::getChildRef(void *ref,int row) const
{
ForumModelIndex entry ;
if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size())
return NULL ;
void *new_ref;
if(row >= mPosts[entry].children.size())
return NULL;
convertTabEntryToRefPointer(mPosts[entry].children[row],new_ref);
return new_ref;
}
void *RsGxsForumModel::getParentRef(void *ref,int& row) const
{
ForumModelIndex ref_entry;
if(!convertRefPointerToTabEntry(ref,ref_entry) || ref_entry >= mPosts.size())
return NULL ;
ForumModelIndex parent_entry = mPosts[ref_entry].parent;
if(parent_entry == 0) // top level index if(parent_entry == 0) // top level index
indx = QModelIndex() ; {
row = 0;
return NULL ;
}
else else
{ {
if(!convertTabEntryToRefPointer(parent_entry,parent_ref)) void *parent_ref;
return QModelIndex() ; convertTabEntryToRefPointer(parent_entry,parent_ref);
row = mPosts[parent_entry].prow;
indx = createIndex(mPosts[parent_entry].prow,child.column(),parent_ref) ; return parent_ref;
} }
#ifdef DEBUG_FORUMMODEL
std::cerr << "parent-1(" << child << ") : " << indx << std::endl;
#endif
return indx;
} }
int RsGxsForumModel::getChildrenCount(void *ref) const
{
uint32_t entry = 0 ;
if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size())
return 0 ;
return mPosts[entry].children.size();
}
//bool RsGxsForumModel::hasIndex(int row,int column,const QModelIndex& parent) const
//{
// if(row < 0 || column < 0 || column >= COLUMN_COUNT)
// return false;
//
// if(!parent.isValid())
// return false;
//
// ForumModelIndex entry;
//
// convertRefPointerToTabEntry(parent.internalPointer(),entry);
//
// if(entry >= mPosts.size())
// return false;
//
// if(row >= mPosts[entry].children.size())
// return false;
//
// if(mPosts[entry].children[row] >= mPosts.size())
// return false;
//
// return true;
//}
QVariant RsGxsForumModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant RsGxsForumModel::headerData(int section, Qt::Orientation orientation, int role) const
{ {
if(role != Qt::DisplayRole) if(role != Qt::DisplayRole)
@ -203,11 +246,13 @@ QVariant RsGxsForumModel::headerData(int section, Qt::Orientation orientation, i
QVariant RsGxsForumModel::data(const QModelIndex &index, int role) const QVariant RsGxsForumModel::data(const QModelIndex &index, int role) const
{ {
#ifdef DEBUG_FORUMMODEL
std::cerr << "calling data(" << index << ") role=" << role << std::endl;
#endif
if(!index.isValid()) if(!index.isValid())
return QVariant(); return QVariant();
int coln = index.column() ;
switch(role) switch(role)
{ {
case Qt::SizeHintRole: return sizeHintRole(index.column()) ; case Qt::SizeHintRole: return sizeHintRole(index.column()) ;
@ -216,8 +261,8 @@ QVariant RsGxsForumModel::data(const QModelIndex &index, int role) const
case Qt::WhatsThisRole: case Qt::WhatsThisRole:
case Qt::EditRole: case Qt::EditRole:
case Qt::ToolTipRole: case Qt::ToolTipRole:
case Qt::StatusTipRole: case Qt::StatusTipRole: return QVariant();
return QVariant(); default: break;
} }
void *ref = (index.isValid())?index.internalPointer():NULL ; void *ref = (index.isValid())?index.internalPointer():NULL ;
@ -266,10 +311,10 @@ QVariant RsGxsForumModel::sizeHintRole(int col) const
switch(col) switch(col)
{ {
default: default:
case COLUMN_TITLE: return QVariant( factor * 170 ); case COLUMN_TITLE: return QVariant( QSize(factor * 170, factor*14 ));
case COLUMN_READ_STATUS: return QVariant( factor * 10 ); case COLUMN_READ_STATUS: return QVariant( QSize(factor * 10 , factor*14 ));
case COLUMN_DATE: return QVariant( factor * 75 ); case COLUMN_DATE: return QVariant( QSize(factor * 75 , factor*14 ));
case COLUMN_AUTHOR: return QVariant( factor * 75 ); case COLUMN_AUTHOR: return QVariant( QSize(factor * 75 , factor*14 ));
} }
} }

View File

@ -1,4 +1,20 @@
#ifdef SUSPENDED_CODE
#include "retroshare/rsgxsifacetypes.h" #include "retroshare/rsgxsifacetypes.h"
#else
#include <string>
#include <iostream>
struct RsMsgMetaData
{
std::string mMsgName ;
time_t mPublishTs;
uint32_t mMsgStatus;
QString mAuthorId;
};
#endif
#include <QModelIndex>
// This class holds the actual hierarchy of posts, represented by identifiers // This class holds the actual hierarchy of posts, represented by identifiers
// It is responsible for auto-updating when necessary and holds a mutex to allow the Model to // It is responsible for auto-updating when necessary and holds a mutex to allow the Model to
@ -29,15 +45,16 @@ public:
enum Roles{ SortRole = Qt::UserRole+1 }; enum Roles{ SortRole = Qt::UserRole+1 };
int rowCount(const QModelIndex& parent = QModelIndex()) const; int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const; QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& child) const; QModelIndex parent(const QModelIndex& child) const override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant sizeHintRole(int col) const; QVariant sizeHintRole(int col) const;
QVariant displayRole(const RsMsgMetaData& meta, int col) const; QVariant displayRole(const RsMsgMetaData& meta, int col) const;
@ -47,6 +64,11 @@ public:
void update_posts(); void update_posts();
private: private:
void *getParentRef(void *ref,int& row) const;
void *getChildRef(void *ref,int row) const;
//bool hasIndex(int row,int column,const QModelIndex& parent)const;
int getChildrenCount(void *ref) const;
static bool convertTabEntryToRefPointer(uint32_t entry,void *& ref); static bool convertTabEntryToRefPointer(uint32_t entry,void *& ref);
static bool convertRefPointerToTabEntry(void *ref,uint32_t& entry); static bool convertRefPointerToTabEntry(void *ref,uint32_t& entry);