2019-09-08 22:27:57 +02:00
|
|
|
#include "Label.hpp"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
2019-10-07 15:46:08 +02:00
|
|
|
namespace chatterino {
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
Label::Label(QString text, FontStyle style)
|
2021-04-10 14:34:40 +02:00
|
|
|
: Label(nullptr, std::move(text), style)
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Label::Label(BaseWidget *parent, QString text, FontStyle style)
|
|
|
|
: BaseWidget(parent)
|
2021-04-10 14:34:40 +02:00
|
|
|
, text_(std::move(text))
|
2019-09-08 22:27:57 +02:00
|
|
|
, fontStyle_(style)
|
|
|
|
{
|
2020-11-08 12:02:19 +01:00
|
|
|
this->connections_.managedConnect(getFonts()->fontChanged, [this] {
|
|
|
|
this->updateSize();
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString &Label::getText() const
|
|
|
|
{
|
|
|
|
return this->text_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setText(const QString &text)
|
|
|
|
{
|
|
|
|
if (this->text_ != text)
|
|
|
|
{
|
|
|
|
this->text_ = text;
|
|
|
|
this->updateSize();
|
|
|
|
this->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FontStyle Label::getFontStyle() const
|
|
|
|
{
|
|
|
|
return this->fontStyle_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::getCentered() const
|
|
|
|
{
|
|
|
|
return this->centered_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setCentered(bool centered)
|
|
|
|
{
|
|
|
|
this->centered_ = centered;
|
|
|
|
this->updateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::getHasOffset() const
|
|
|
|
{
|
|
|
|
return this->hasOffset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setHasOffset(bool hasOffset)
|
|
|
|
{
|
|
|
|
this->hasOffset_ = hasOffset;
|
|
|
|
this->updateSize();
|
|
|
|
}
|
|
|
|
void Label::setFontStyle(FontStyle style)
|
|
|
|
{
|
|
|
|
this->fontStyle_ = style;
|
|
|
|
this->updateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::scaleChangedEvent(float scale)
|
|
|
|
{
|
|
|
|
this->updateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Label::sizeHint() const
|
|
|
|
{
|
|
|
|
return this->preferedSize_;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Label::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
return this->preferedSize_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
2019-10-06 21:31:12 +02:00
|
|
|
|
|
|
|
qreal deviceDpi =
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
this->devicePixelRatioF();
|
|
|
|
#else
|
|
|
|
1.0;
|
|
|
|
#endif
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
QFontMetrics metrics = getFonts()->getFontMetrics(
|
|
|
|
this->getFontStyle(),
|
2020-04-19 21:05:40 +02:00
|
|
|
this->scale() * 96.f /
|
|
|
|
std::max<float>(0.01, this->logicalDpiX() * deviceDpi));
|
2019-09-08 22:27:57 +02:00
|
|
|
painter.setFont(getFonts()->getFont(
|
2019-10-06 21:31:12 +02:00
|
|
|
this->getFontStyle(),
|
2020-04-19 21:05:40 +02:00
|
|
|
this->scale() * 96.f /
|
|
|
|
std::max<float>(0.02, this->logicalDpiX() * deviceDpi)));
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
int offset = this->getOffset();
|
|
|
|
|
|
|
|
// draw text
|
|
|
|
QRect textRect(offset, 0, this->width() - offset - offset, this->height());
|
|
|
|
|
2021-03-13 14:47:02 +01:00
|
|
|
int width = metrics.horizontalAdvance(this->text_);
|
2019-09-08 22:27:57 +02:00
|
|
|
Qt::Alignment alignment = !this->centered_ || width > textRect.width()
|
|
|
|
? Qt::AlignLeft | Qt::AlignVCenter
|
|
|
|
: Qt::AlignCenter;
|
|
|
|
|
|
|
|
painter.setBrush(this->palette().windowText());
|
|
|
|
|
|
|
|
QTextOption option(alignment);
|
|
|
|
option.setWrapMode(QTextOption::NoWrap);
|
|
|
|
painter.drawText(textRect, this->text_, option);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
painter.setPen(QColor(255, 0, 0));
|
|
|
|
painter.drawRect(0, 0, this->width() - 1, this->height() - 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::updateSize()
|
|
|
|
{
|
|
|
|
QFontMetrics metrics =
|
|
|
|
getFonts()->getFontMetrics(this->fontStyle_, this->scale());
|
|
|
|
|
2021-03-13 14:47:02 +01:00
|
|
|
int width =
|
|
|
|
metrics.horizontalAdvance(this->text_) + (2 * this->getOffset());
|
2019-09-08 22:27:57 +02:00
|
|
|
int height = metrics.height();
|
|
|
|
this->preferedSize_ = QSize(width, height);
|
|
|
|
|
|
|
|
this->updateGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Label::getOffset()
|
|
|
|
{
|
|
|
|
return this->hasOffset_ ? int(8 * this->scale()) : 0;
|
|
|
|
}
|
|
|
|
|
2019-10-07 15:46:08 +02:00
|
|
|
} // namespace chatterino
|