diff options
Diffstat (limited to 'src/main-view/FieldContentCodec.cpp')
-rw-r--r-- | src/main-view/FieldContentCodec.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/main-view/FieldContentCodec.cpp b/src/main-view/FieldContentCodec.cpp new file mode 100644 index 0000000..ac9e6c5 --- /dev/null +++ b/src/main-view/FieldContentCodec.cpp @@ -0,0 +1,52 @@ +#include "FieldContentCodec.h" + +#include "FieldContentPainter.h" + +FieldContentCodec::FieldContentCodec(FieldContentPainter* painter): + painter(painter), + imageRx("<img\\s+src=\"([^\"]+)\"\\s*/?>") +{ + initLoopParams(); +} + +void FieldContentCodec::initLoopParams() +{ + textPos = 0; + prevTextPos = 0; +} + +void FieldContentCodec::parse(const QString& text) +{ + painter->startDrawing(); + initLoopParams(); + this->text = text; + + while(findNextImage() >= 0) + { + drawTextChunk(textPos - prevTextPos); + drawImage(); + textPos += imageRx.matchedLength(); + prevTextPos = textPos; + } + drawTextChunk(-1); + painter->endDrawing(); +} + +int FieldContentCodec::findNextImage() +{ + textPos = imageRx.indexIn(text, textPos); + return textPos; +} + +void FieldContentCodec::drawTextChunk(int len) +{ + painter->drawText(text.mid(prevTextPos, len)); +} + +void FieldContentCodec::drawImage() +{ + QString imagePath = imageRx.cap(1); + if(!QFileInfo(imagePath).exists()) + imagePath = ":/images/broken-image.png"; + painter->drawImage(imagePath); +} |