summaryrefslogtreecommitdiff
path: root/src/main-view/FieldContentCodec.cpp
blob: ac9e6c54176e5b7daf5bbd9e15e5ec2ec1353b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
}