Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix: iterate all blocks
Browse files Browse the repository at this point in the history
refactor: block iteration loop

refactor: replaced checks with chop()
  • Loading branch information
ezavod committed Mar 12, 2017
1 parent aabf34e commit 7a5c5a8
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/chatlog/content/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,32 +337,39 @@ QString Text::extractSanitizedText(int from, int to) const
return "";

QString txt;
QTextBlock block = doc->firstBlock();

for (QTextBlock::Iterator itr = block.begin(); itr != block.end(); ++itr) {
int pos =
itr.fragment()
.position(); // fragment position -> position of the first character in the fragment

if (itr.fragment().charFormat().isImageFormat()) {
QTextImageFormat imgFmt = itr.fragment().charFormat().toImageFormat();
QString key = imgFmt.name(); // img key (eg. key::D for :D)
QString rune = key.mid(4);

if (pos >= from && pos < to) {
txt += rune;
++pos;
}
} else {
for (QChar c : itr.fragment().text()) {
if (pos >= from && pos < to)
txt += c;

++pos;
QTextBlock begin = doc->findBlock(from);
QTextBlock end = doc->findBlock(to);
for (QTextBlock block = begin; block != end.next() && block.isValid(); block = block.next()) {
for (QTextBlock::Iterator itr = block.begin(); itr != block.end(); ++itr) {
int pos =
itr.fragment()
.position(); // fragment position -> position of the first character in the fragment

if (itr.fragment().charFormat().isImageFormat()) {
QTextImageFormat imgFmt = itr.fragment().charFormat().toImageFormat();
QString key = imgFmt.name(); // img key (eg. key::D for :D)
QString rune = key.mid(4);

if (pos >= from && pos < to) {
txt += rune;
++pos;
}
} else {
for (QChar c : itr.fragment().text()) {
if (pos >= from && pos < to)
txt += c;

++pos;
}
}
}

txt += '\n';
}

txt.chop(1);

return txt;
}

Expand Down

0 comments on commit 7a5c5a8

Please sign in to comment.