summaryrefslogtreecommitdiff
path: root/src/main-view/DictTableDelegatePainter.cpp
blob: 2714bdc3faa63cd69495cb84dcc077f47ccb6c12 (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
53
54
55
56
57
58
59
60
61
#include "DictTableDelegatePainter.h"

DictTableDelegatePainter::DictTableDelegatePainter(QPainter* painter,
    const QRect& contentRect, const QFontMetrics& fontMetrics):
        painter(painter), contentRect(contentRect), fontMetrics(fontMetrics)
{
    initLoopParams();
}

void DictTableDelegatePainter::initLoopParams()
{
    offset = 0;
    textFitstoRect = true;
}

void DictTableDelegatePainter::startDrawing()
{
    painter->save();
    painter->setClipRect(contentRect);
    initLoopParams();
}

void DictTableDelegatePainter::endDrawing()
{
    painter->restore();
}

void DictTableDelegatePainter::drawText(const QString& text)
{
    if(!textFitstoRect)
        return;
    QString elidedText = getElidedText(text);
    textFitstoRect = elidedText == text;
    painter->drawText(contentRect.translated(offset, 0),
        elidedText, getTextOption());
    offset += fontMetrics.width(elidedText);
}

void DictTableDelegatePainter::drawImage(const QString& filePath)
{
    if(!textFitstoRect)
        return;
    QPixmap image(filePath);
    if(image.isNull())
        image = QPixmap(":/images/broken-image.png");
    image = image.scaledToHeight(ThumbnailSize, Qt::SmoothTransformation);
    painter->drawPixmap(contentRect.topLeft() + QPoint(offset, 0), image);
    offset += image.width();
}

QString DictTableDelegatePainter::getElidedText(const QString& text)
{
    return fontMetrics.elidedText(text, Qt::ElideRight, contentRect.width() - offset);
}

QTextOption DictTableDelegatePainter::getTextOption()
{
    QTextOption textOption(Qt::AlignVCenter);
    textOption.setWrapMode(QTextOption::NoWrap);
    return textOption;
}