class CServerMessageModel : public QAbstractTableModel{ Q_OBJECT typedef struct { CGenericRailObjectStorage * storage; //указатель на данные CDecodedMessage messageData; //по сути набор из строки+времени в qdatetime } InternalNode; QList<InternalNode> fnodes;public:explicit CServerMessageModel(QObject *parent = 0); enum {columnStorage,columnTime,columnMessageText,maxColumn}; int columnCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; bool insertRows(int row, int count, const QModelIndex &parent);...public slots: void acceptMessagesFromParser(CGenericRailObjectStorage * storage, const QList<CDecodedMessage> & messagesList); void clear();};
void CServerMessageModel::acceptMessagesFromParser(CGenericRailObjectStorage *storage, const QList<CDecodedMessage> &messagesList){ if(messagesList.isEmpty()) return; beginInsertRows(QModelIndex(),fnodes.size(),messagesList.size()); foreach (const CDecodedMessage & decodedMessage, messagesList) { InternalNode node; node.storage = storage; node.messageData = decodedMessage; fnodes.append(node); } endInsertRows(); emit messagesHasBeenAdded();}
bool CServerMessageModel::insertRows(int row, int count, const QModelIndex &parent){ beginInsertRows(parent, row, row+count); for(int i=0;i<count;i++) { InternalNode node; node.storage = NULL; fnodes.insert(row,node); } endInsertRows(); return true;}bool CServerMessageModel::setData(const QModelIndex &index, const QVariant &value, int role){ if(index.isValid()) { InternalNode & node = fnodes[index.row()]; switch (role) { case StorageRole: node.storage = value.value<CGenericRailObjectStorage*>(); emit dataChanged(index,index); return true; case MessageRole: node.messageData = value.value<CDecodedMessage>(); emit dataChanged(index,index); return true; default: break; } } return false;}void CServerMessageModel::acceptMessagesFromParser(CGenericRailObjectStorage *storage, const QList<CDecodedMessage> &messagesList){ if(messagesList.isEmpty()) return; if(insertRows(0,messagesList.size(),QModelIndex())) { int row = 0; foreach (const CDecodedMessage & currentMessage, messagesList) { QModelIndex currentIndex = index(row,0); setData(currentIndex,QVariant::fromValue(storage),StorageRole); setData(currentIndex,QVariant::fromValue(currentMessage),MessageRole); row++; } emit messagesHasBeenAdded(); } else qDebug("Couldn't add messages");}
beginInsertRows(QModelIndex(),fnodes.size(),messagesList.size());