From d24f813f3f2a05c112e803e4256b53535895fc98 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 14 Jul 2021 11:49:10 +1200 Subject: Initial mirror commit --- src/main-view/FieldContentCodec.cpp | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main-view/FieldContentCodec.cpp (limited to 'src/main-view/FieldContentCodec.cpp') 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("") +{ + 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); +} -- cgit