#include "textmodels.h" using namespace wstext_model; using namespace wstext_present; void WsDocument::bindFormat(std::shared_ptr f) { this->document_format = f; } std::shared_ptr WsDocument::getFormat() const { return this->document_format; } uint32_t WsDocument::blockCount() const { return this->block_store.size(); } QList > WsDocument::getBlocks(uint32_t offset, uint32_t count) const { QList> retvs; for (auto idx = offset; idx < this->block_store.size(); ++idx) retvs << this->block_store.at(idx); return retvs; } void WsDocument::delBlocks(uint32_t offset, uint32_t count) { for (auto idx = offset; idx < this->block_store.size();) { this->block_store.removeAt(idx); } } void WsDocument::addBlocks(const QList> &blks, uint32_t offset) { if (offset < this->block_store.size()) { for (auto ptr : blks) this->block_store.insert(offset, ptr); } else { this->block_store.append(blks); } } QString WsDocument::toPlainText() const { QString text_content; for (auto idx = 0; idx < blockCount(); ++idx) { text_content += getBlocks(idx).at(0)->toText(); } return text_content; } void WsDocument::setPlainText(const QString &text) { auto sections = text.split("\n"); QList> blocks; for (auto &t : sections) { auto binst = std::make_shared(); blocks.append(binst); // 转换文本为内存实例 auto codes = CharSetU16::combineToU(t); auto chars = WsChar::convertFrom(codes); // 填充段落实例 QList buffers; for (auto &ptr : chars) { ptr->bindFormat(this->getFormat()->defaultCharFormat()); ptr->refer(binst); buffers.append(ptr); } binst->addElements(buffers); } addBlocks(blocks); } void WsBlock::addElements(const QList &text, uint32_t offset) { if (offset < this->element_store.size()) { for (auto *ptr : text) this->element_store.insert(offset, ptr); } else { this->element_store.append(text); } } QList WsBlock::getElements(uint32_t offset, uint32_t count) const { QList retvs; for (auto idx = offset; idx < this->element_store.size(); ++idx) retvs << this->element_store.at(idx); return retvs; } void WsBlock::delElements(uint32_t offset, uint32_t count) { for (auto idx = offset; idx < this->element_store.size();) { delete element_store.at(idx); this->element_store.removeAt(idx); } } QString WsBlock::toText() const { QString text_content; for (auto &it : element_store) text_content += it->toText(); return text_content; } wstext_model::WsChar::WsChar(uint32_t code) { this->code_store = code; } uint32_t wstext_model::WsChar::code() const { return code_store; } QList> WsChar::convertFrom(const QString &text) { QList> buffers; for(auto &code : CharSetU16::combineToU(text)) buffers << std::make_shared(code); return buffers; } Element::Element(AssembleType t) : type_store(t) {} Element::Element(const Element &other) : Element(other.baseType()) { } AssembleType Element::baseType() const { return type_store; } qulonglong Element::hashCode() const { return (qulonglong)this; } std::shared_ptr Element::clone() const { return std::make_shared(this->type_store); } qulonglong wstext_model::Element::pos() const { return 0; }