diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp index 33dcc2efb..210d4f27f 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumModel.cpp @@ -42,32 +42,25 @@ RsGxsForumModel::RsGxsForumModel(QObject *parent) mPosts[N].children.push_back(ForumModelIndex(N+1)); mPosts[N+1].parent = ForumModelIndex(N); 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 { - void *ref = (parent.isValid())?parent.internalPointer():NULL ; + if(parent.column() > 0) + return 0; if(mPosts.empty()) // security. Should never happen. return 0; - uint32_t entry = 0 ; - int source_id ; - - if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size()) - { -#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(); + if(!parent.isValid()) + return getChildrenCount(NULL); + else + return getChildrenCount(parent.internalPointer()); } - int RsGxsForumModel::columnCount(const QModelIndex &parent) const { return COLUMN_COUNT ; @@ -75,7 +68,10 @@ int RsGxsForumModel::columnCount(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; if(!convertRefPointerToTabEntry(ref,entry) || entry >= mPosts.size()) @@ -86,8 +82,8 @@ bool RsGxsForumModel::hasChildren(const QModelIndex &parent) const return false ; } -#ifdef DEBUG_DOWNLOADLIST - std::cerr << "hasChildren-3(" << parent << ") : " << !mDownloads[entry].peers.empty() << std::endl; +#ifdef DEBUG_FORUMMODEL + std::cerr << "hasChildren-3(" << parent << ") : " << !mPosts[entry].children.empty() << std::endl; #endif 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 { - 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 ; } 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 { - if(row < 0 || column < 0 || column >= COLUMN_COUNT) +// if(!hasIndex(row,column,parent)) + if(row < 0 || column < 0 || column >= COLUMN_COUNT) return QModelIndex(); - void *parent_ref = (parent.isValid())?parent.internalPointer():NULL ; - 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() ; - } - + void *ref = getChildRef(parent.internalPointer(),row); #ifdef DEBUG_FORUMMODEL std::cerr << "index-3(" << row << "," << column << " parent=" << parent << ") : " << createIndex(row,column,ref) << std::endl; #endif 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()) + return QModelIndex(); - if(!child_ref) - return QModelIndex() ; + void *child_ref = index.internalPointer(); + int row=0; - ForumModelIndex child_entry ; + void *parent_ref = getParentRef(child_ref,row) ; - if(!convertRefPointerToTabEntry(child_ref,child_entry) || child_entry >= mPosts.size()) - return QModelIndex() ; + if(parent_ref == NULL) // root + return QModelIndex() ; - void *parent_ref =NULL; - ForumModelIndex parent_entry = mPosts[child_entry].parent; - QModelIndex indx; + return createIndex(row,0,parent_ref); +} + +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 - indx = QModelIndex() ; + { + row = 0; + return NULL ; + } else { - if(!convertTabEntryToRefPointer(parent_entry,parent_ref)) - return QModelIndex() ; + void *parent_ref; + 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 { 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 { +#ifdef DEBUG_FORUMMODEL + std::cerr << "calling data(" << index << ") role=" << role << std::endl; +#endif + if(!index.isValid()) return QVariant(); - int coln = index.column() ; - switch(role) { 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::EditRole: case Qt::ToolTipRole: - case Qt::StatusTipRole: - return QVariant(); + case Qt::StatusTipRole: return QVariant(); + default: break; } void *ref = (index.isValid())?index.internalPointer():NULL ; @@ -266,10 +311,10 @@ QVariant RsGxsForumModel::sizeHintRole(int col) const switch(col) { default: - case COLUMN_TITLE: return QVariant( factor * 170 ); - case COLUMN_READ_STATUS: return QVariant( factor * 10 ); - case COLUMN_DATE: return QVariant( factor * 75 ); - case COLUMN_AUTHOR: return QVariant( factor * 75 ); + case COLUMN_TITLE: return QVariant( QSize(factor * 170, factor*14 )); + case COLUMN_READ_STATUS: return QVariant( QSize(factor * 10 , factor*14 )); + case COLUMN_DATE: return QVariant( QSize(factor * 75 , factor*14 )); + case COLUMN_AUTHOR: return QVariant( QSize(factor * 75 , factor*14 )); } } diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumModel.h b/retroshare-gui/src/gui/gxsforums/GxsForumModel.h index e5fdbf976..a951116a8 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumModel.h +++ b/retroshare-gui/src/gui/gxsforums/GxsForumModel.h @@ -1,4 +1,20 @@ +#ifdef SUSPENDED_CODE #include "retroshare/rsgxsifacetypes.h" +#else +#include +#include + +struct RsMsgMetaData +{ + std::string mMsgName ; + time_t mPublishTs; + uint32_t mMsgStatus; + QString mAuthorId; +}; + +#endif + +#include // 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 @@ -29,17 +45,18 @@ public: enum Roles{ SortRole = Qt::UserRole+1 }; - int rowCount(const QModelIndex& parent = QModelIndex()) const; - int columnCount(const QModelIndex &parent = QModelIndex()) const; - bool hasChildren(const QModelIndex &parent = QModelIndex()) const; + int rowCount(const QModelIndex& parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; - QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const; - QModelIndex parent(const QModelIndex& child) const; + QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const override; + 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 data(const QModelIndex &index, 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 override; - QVariant sizeHintRole(int col) const; + QVariant sizeHintRole(int col) const; QVariant displayRole(const RsMsgMetaData& meta, int col) const; QVariant userRole(const RsMsgMetaData& meta, int col) const; QVariant decorationRole(const RsMsgMetaData &meta, int col) const; @@ -47,7 +64,12 @@ public: void update_posts(); private: - static bool convertTabEntryToRefPointer(uint32_t entry,void *& ref); + 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 convertRefPointerToTabEntry(void *ref,uint32_t& entry); std::vector mPosts ; // store the list of posts updated from rsForums.