matrixion/src/TypingDisplay.cpp

56 lines
1.1 KiB
C++
Raw Normal View History

2017-10-04 11:33:34 +03:00
#include <QPainter>
#include <QPoint>
#include "Config.h"
#include "TypingDisplay.h"
#include "ui/Painter.h"
constexpr int LEFT_PADDING = 24;
2017-10-04 11:33:34 +03:00
TypingDisplay::TypingDisplay(QWidget *parent)
: QWidget(parent)
{
QFont f;
f.setPixelSize(conf::typingNotificationFontSize);
setFont(f);
2017-10-04 11:33:34 +03:00
setFixedHeight(QFontMetrics(font()).height() + 2);
2017-10-04 11:33:34 +03:00
}
void
TypingDisplay::setUsers(const QStringList &uid)
{
if (uid.isEmpty()) {
2017-10-04 11:33:34 +03:00
text_.clear();
return;
}
text_ = uid.join(", ");
2017-10-04 11:33:34 +03:00
if (uid.size() == 1)
text_ += tr(" is typing");
2017-10-04 11:33:34 +03:00
else if (uid.size() > 1)
text_ += tr(" are typing");
2017-10-04 11:33:34 +03:00
update();
}
void
TypingDisplay::paintEvent(QPaintEvent *)
{
Painter p(this);
PainterHighQualityEnabler hq(p);
2017-10-04 11:33:34 +03:00
p.setFont(font());
p.setPen(QPen(textColor()));
2017-10-04 11:33:34 +03:00
QRect region = rect();
region.translate(LEFT_PADDING, 0);
2017-10-04 11:33:34 +03:00
QFontMetrics fm(font());
text_ = fm.elidedText(text_, Qt::ElideRight, width() - 3 * LEFT_PADDING);
2017-10-04 11:33:34 +03:00
p.drawText(region, Qt::AlignVCenter, text_);
2017-10-04 11:33:34 +03:00
}