2018-08-09 18:20:17 +03:00
|
|
|
#include <QDebug>
|
2017-10-04 11:33:34 +03:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPoint>
|
2018-08-09 18:20:17 +03:00
|
|
|
#include <QShowEvent>
|
2017-10-04 11:33:34 +03:00
|
|
|
|
|
|
|
#include "Config.h"
|
|
|
|
#include "TypingDisplay.h"
|
2018-08-08 12:51:40 +03:00
|
|
|
#include "ui/Painter.h"
|
|
|
|
|
|
|
|
constexpr int LEFT_PADDING = 24;
|
2018-08-10 10:58:46 +03:00
|
|
|
constexpr int RECT_PADDING = 2;
|
2017-10-04 11:33:34 +03:00
|
|
|
|
|
|
|
TypingDisplay::TypingDisplay(QWidget *parent)
|
2018-08-09 18:20:17 +03:00
|
|
|
: OverlayWidget(parent)
|
|
|
|
, offset_{conf::textInput::height}
|
2017-10-04 11:33:34 +03:00
|
|
|
{
|
2018-08-10 10:58:46 +03:00
|
|
|
setFixedHeight(QFontMetrics(font()).height() + RECT_PADDING);
|
2018-08-09 18:20:17 +03:00
|
|
|
setAttribute(Qt::WA_TransparentForMouseEvents);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TypingDisplay::setOffset(int margin)
|
|
|
|
{
|
|
|
|
offset_ = margin;
|
|
|
|
move(0, parentWidget()->height() - offset_ - height());
|
2017-10-04 11:33:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TypingDisplay::setUsers(const QStringList &uid)
|
|
|
|
{
|
2018-08-09 18:20:17 +03:00
|
|
|
move(0, parentWidget()->height() - offset_ - height());
|
|
|
|
|
|
|
|
text_.clear();
|
|
|
|
|
2018-08-08 12:51:40 +03:00
|
|
|
if (uid.isEmpty()) {
|
2018-08-09 18:20:17 +03:00
|
|
|
hide();
|
2018-08-09 13:39:39 +03:00
|
|
|
update();
|
|
|
|
|
2018-08-08 12:51:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
text_ = uid.join(", ");
|
2017-10-04 11:33:34 +03:00
|
|
|
|
|
|
|
if (uid.size() == 1)
|
2017-10-04 22:00:26 +03:00
|
|
|
text_ += tr(" is typing");
|
2017-10-04 11:33:34 +03:00
|
|
|
else if (uid.size() > 1)
|
2017-10-04 22:00:26 +03:00
|
|
|
text_ += tr(" are typing");
|
2017-10-04 11:33:34 +03:00
|
|
|
|
2018-08-09 18:20:17 +03:00
|
|
|
show();
|
2017-10-04 11:33:34 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TypingDisplay::paintEvent(QPaintEvent *)
|
|
|
|
{
|
2018-08-08 12:51:40 +03:00
|
|
|
Painter p(this);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
2017-10-04 11:33:34 +03:00
|
|
|
|
2018-08-08 12:51:40 +03:00
|
|
|
p.setFont(font());
|
|
|
|
p.setPen(QPen(textColor()));
|
2017-10-04 11:33:34 +03:00
|
|
|
|
|
|
|
QRect region = rect();
|
2018-08-08 12:51:40 +03:00
|
|
|
region.translate(LEFT_PADDING, 0);
|
2017-10-04 11:33:34 +03:00
|
|
|
|
2018-08-08 12:51:40 +03:00
|
|
|
QFontMetrics fm(font());
|
2018-08-10 10:58:46 +03:00
|
|
|
text_ = fm.elidedText(text_, Qt::ElideRight, (double)(width() * 0.75));
|
2017-10-04 11:33:34 +03:00
|
|
|
|
2018-08-10 10:58:46 +03:00
|
|
|
QPainterPath path;
|
|
|
|
path.addRoundedRect(QRectF(0, 0, fm.width(text_) + 2 * LEFT_PADDING, height()), 3, 3);
|
|
|
|
|
|
|
|
p.fillPath(path, backgroundColor());
|
2017-10-04 22:00:26 +03:00
|
|
|
p.drawText(region, Qt::AlignVCenter, text_);
|
2017-10-04 11:33:34 +03:00
|
|
|
}
|