summaryrefslogtreecommitdiff
path: root/src/main-view/FieldContentCodec.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/main-view/FieldContentCodec.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/main-view/FieldContentCodec.cpp')
-rw-r--r--src/main-view/FieldContentCodec.cpp52
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);
+}