TextPresents/libWsTextEdit/textmodels.cpp

148 lines
3.3 KiB
C++

#include "textmodels.h"
using namespace wstext_model;
using namespace wstext_present;
void WsDocument::bindFormat(std::shared_ptr<DocFormat> f) { this->document_format = f; }
std::shared_ptr<DocFormat> WsDocument::getFormat() const { return this->document_format; }
uint32_t WsDocument::blockCount() const
{
return this->block_store.size();
}
QList<std::shared_ptr<WsBlock> > WsDocument::getBlocks(uint32_t offset, uint32_t count) const
{
QList<std::shared_ptr<WsBlock>> 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<std::shared_ptr<WsBlock>> &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<std::shared_ptr<WsBlock>> blocks;
for (auto &t : sections) {
auto binst = std::make_shared<WsBlock>();
blocks.append(binst);
// 转换文本为内存实例
auto codes = CharSetU16::combineToU(t);
auto chars = WsChar::convertFrom(codes);
// 填充段落实例
QList<Element*> 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<Element*> &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<Element *> WsBlock::getElements(uint32_t offset, uint32_t count) const
{
QList<Element*> 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<std::shared_ptr<const WsChar>> WsChar::convertFrom(const QString &text)
{
QList<std::shared_ptr<const WsChar>> buffers;
for(auto &code : CharSetU16::combineToU(text))
buffers << std::make_shared<WsChar>(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<const Element> Element::clone() const {
return std::make_shared<Element>(this->type_store);
}
qulonglong wstext_model::Element::pos() const
{
return 0;
}