summary refs log tree commit diff
path: root/src/TypingDisplay.cpp
blob: cd405a7a7da85b1d063864d231fb861bf5fa2e86 (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
#include <QDebug>
#include <QPainter>
#include <QPoint>
#include <QShowEvent>

#include "Config.h"
#include "TypingDisplay.h"
#include "ui/Painter.h"

constexpr int LEFT_PADDING = 24;

TypingDisplay::TypingDisplay(QWidget *parent)
  : OverlayWidget(parent)
  , offset_{conf::textInput::height}
{
        QFont f;
        f.setPixelSize(conf::typingNotificationFontSize);
        setFont(f);

        setFixedHeight(QFontMetrics(font()).height() + 2);
        setAttribute(Qt::WA_TransparentForMouseEvents);
}

void
TypingDisplay::setOffset(int margin)
{
        offset_ = margin;
        move(0, parentWidget()->height() - offset_ - height());
}

void
TypingDisplay::setUsers(const QStringList &uid)
{
        move(0, parentWidget()->height() - offset_ - height());

        text_.clear();

        if (uid.isEmpty()) {
                hide();
                update();

                return;
        }

        text_ = uid.join(", ");

        if (uid.size() == 1)
                text_ += tr(" is typing");
        else if (uid.size() > 1)
                text_ += tr(" are typing");

        show();
        update();
}

void
TypingDisplay::paintEvent(QPaintEvent *)
{
        Painter p(this);
        PainterHighQualityEnabler hq(p);

        p.setFont(font());
        p.setPen(QPen(textColor()));

        QRect region = rect();
        region.translate(LEFT_PADDING, 0);

        QFontMetrics fm(font());
        text_ = fm.elidedText(text_, Qt::ElideRight, width() - 3 * LEFT_PADDING);

        p.drawText(region, Qt::AlignVCenter, text_);
}