nheko/src/TypingDisplay.cpp

87 lines
2.2 KiB
C++
Raw Normal View History

#include <QDebug>
2017-10-04 10:33:34 +02:00
#include <QPainter>
#include <QPoint>
#include <QShowEvent>
2019-07-05 04:58:06 +02:00
#include <QtGlobal>
2017-10-04 10:33:34 +02:00
#include "Config.h"
#include "TypingDisplay.h"
#include "ui/Painter.h"
constexpr int LEFT_PADDING = 24;
2018-08-10 09:58:46 +02:00
constexpr int RECT_PADDING = 2;
2017-10-04 10:33:34 +02:00
TypingDisplay::TypingDisplay(QWidget *parent)
: OverlayWidget(parent)
, offset_{conf::textInput::height}
2017-10-04 10:33:34 +02:00
{
2018-08-10 09:58:46 +02:00
setFixedHeight(QFontMetrics(font()).height() + RECT_PADDING);
setAttribute(Qt::WA_TransparentForMouseEvents);
}
void
TypingDisplay::setOffset(int margin)
{
offset_ = margin;
move(0, parentWidget()->height() - offset_ - height());
2017-10-04 10:33:34 +02:00
}
void
TypingDisplay::setUsers(const QStringList &uid)
{
move(0, parentWidget()->height() - offset_ - height());
text_.clear();
2019-07-28 18:13:16 +02:00
QString temp = text_ +=
tr("%1 and %2 are typing",
"Multiple users are typing. First argument is a comma separated list of potentially "
"multiple users. Second argument is the last user of that list. (If only one user is "
"typing, %1 is empty. You should still use it in your string though to silence Qt "
"warnings.)",
uid.size());
if (uid.isEmpty()) {
hide();
2018-08-09 12:39:39 +02:00
update();
return;
}
2019-07-28 18:13:16 +02:00
QStringList uidWithoutLast = uid;
uidWithoutLast.pop_back();
text_ = temp.arg(uidWithoutLast.join(", ")).arg(uid.back());
2017-10-04 10:33:34 +02:00
show();
2017-10-04 10:33:34 +02:00
update();
}
void
TypingDisplay::paintEvent(QPaintEvent *)
{
Painter p(this);
PainterHighQualityEnabler hq(p);
2017-10-04 10:33:34 +02:00
QFont f;
f.setPointSizeF(f.pointSizeF() * 0.9);
p.setFont(f);
p.setPen(QPen(textColor()));
2017-10-04 10:33:34 +02:00
QRect region = rect();
region.translate(LEFT_PADDING, 0);
2017-10-04 10:33:34 +02:00
QFontMetrics fm(f);
2018-08-10 09:58:46 +02:00
text_ = fm.elidedText(text_, Qt::ElideRight, (double)(width() * 0.75));
2017-10-04 10:33:34 +02:00
2018-08-10 09:58:46 +02:00
QPainterPath path;
2019-07-05 04:58:06 +02:00
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
path.addRoundedRect(QRectF(0, 0, fm.width(text_) + 2 * LEFT_PADDING, height()), 3, 3);
#else
2019-07-05 03:31:28 +02:00
path.addRoundedRect(
QRectF(0, 0, fm.horizontalAdvance(text_) + 2 * LEFT_PADDING, height()), 3, 3);
2019-07-05 04:58:06 +02:00
#endif
2018-08-10 09:58:46 +02:00
p.fillPath(path, backgroundColor());
p.drawText(region, Qt::AlignVCenter, text_);
2017-10-04 10:33:34 +02:00
}