void MessageModel::insertSubtree(const QModelIndex &parent, std::vector<Cryptography::MessagePart::Ptr> &&parts) { Q_ASSERT(!parent.isValid() || parent.model() == this); auto *part = translatePtr(parent); Q_ASSERT(part); #ifdef MIME_TREE_DEBUG qDebug() << "Whole tree:\n" << *m_rootPart; qDebug() << "Inserting at:\n" << (void*)(part); #endif beginInsertRows(parent, 0, parts.size()); Q_ASSERT(part->m_children.empty()); for (size_t i = 0; i < parts.size(); ++i) { #ifdef MIME_TREE_DEBUG qDebug() << "New item[" << i << "]:\n" << *(parts[i].get()); #endif parts[i]->m_parent = part; part->m_children[i] = std::move(parts[i]); } bool needsDataChanged = false; if (LocalMessagePart *localPart = dynamic_cast<LocalMessagePart*>(part)) { localPart->m_localState = MessagePart::FetchingState::DONE; needsDataChanged = true; } endInsertRows(); if (needsDataChanged) { emit dataChanged(parent, parent); } }
QModelIndex MessageModel::parent(const QModelIndex &child) const { if (!child.isValid()) return QModelIndex(); Q_ASSERT(child.model() == this); auto childPart = translatePtr(child); Q_ASSERT(childPart); auto parentPart = childPart->parent(); return (parentPart && parentPart != m_rootPart.get()) ? createIndex(parentPart->row(), 0, parentPart) : QModelIndex(); }
QVariant MessageModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } Q_ASSERT(index.model() == this); return translatePtr(index)->data(role); }
QModelIndex MessageModel::index(int row, int column, const QModelIndex &parent) const { Q_ASSERT(!parent.isValid() || parent.model() == this); auto parentPart = translatePtr(parent); Q_ASSERT(parentPart); auto childPtr = parentPart->child(const_cast<MessageModel *>(this), row, column); if (!childPtr) { return QModelIndex(); } else { return createIndex(row, column, childPtr); } }
QVariant MessageModel::data(const QModelIndex &index, int role) const { if (role == Imap::Mailbox::RoleIsNetworkOffline) return m_message.data(Imap::Mailbox::RoleIsNetworkOffline); if (!index.isValid()) { return QVariant(); } Q_ASSERT(index.model() == this); return translatePtr(index)->data(role); }
void MessageModel::replaceMeWithSubtree(const QModelIndex &parent, MessagePart *partToReplace, MessagePart::Ptr tree) { Q_ASSERT(!parent.isValid() || parent.model() == this); auto *part = translatePtr(parent); Q_ASSERT(part); #ifdef MIME_TREE_DEBUG qDebug() << "Whole tree:\n" << *m_rootPart; qDebug() << "Replacing:\n" << (void*)(partToReplace); qDebug() << "New items:\n" << *(tree.get()); #endif Q_ASSERT(partToReplace); Q_ASSERT(tree->row() == partToReplace->row()); auto it = part->m_children.find(partToReplace->row()); Q_ASSERT(it != part->m_children.end()); Q_ASSERT(it->second->row() == partToReplace->row()); Q_ASSERT(partToReplace->row() == tree->row()); emit layoutAboutToBeChanged(QList<QPersistentModelIndex>() << parent); auto oldIndexes = persistentIndexList(); auto newIndexes = oldIndexes; for (int i = 0; i < oldIndexes.size(); ++i) { const auto &index = oldIndexes[i]; if (index.parent() == parent && index.column() == 0 && index.row() == tree->row()) { newIndexes[i] = createIndex(tree->row(), 0, tree.get()); } } changePersistentIndexList(oldIndexes, newIndexes); tree->m_parent = part; MessagePart::Ptr partGoingAway = std::move(it->second); it->second = std::move(tree); emit layoutChanged(QList<QPersistentModelIndex>() << parent); emit dataChanged(parent, parent); #ifdef MIME_TREE_DEBUG qDebug() << "After replacement:\n" << *m_rootPart; #endif }
int MessageModel::columnCount(const QModelIndex &parent) const { return translatePtr(parent)->columnCount(const_cast<MessageModel *>(this)); }