2018-06-26 14:09:39 +02:00
|
|
|
#include "Label.hpp"
|
2018-06-11 15:04:54 +02:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
2018-06-11 15:04:54 +02:00
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
Label::Label(QString text, FontStyle style)
|
|
|
|
: Label(nullptr, text, style)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Label::Label(BaseWidget *parent, QString text, FontStyle style)
|
|
|
|
: BaseWidget(parent)
|
|
|
|
, text_(text)
|
|
|
|
, fontStyle_(style)
|
|
|
|
{
|
|
|
|
auto app = getApp();
|
|
|
|
|
2018-06-24 13:32:56 +02:00
|
|
|
this->connections_.managedConnect(app->fonts->fontChanged, [this] { this->updateSize(); });
|
2018-06-11 15:04:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString &Label::getText() const
|
|
|
|
{
|
|
|
|
return this->text_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setText(const QString &text)
|
|
|
|
{
|
2018-06-19 20:14:13 +02:00
|
|
|
if (this->text_ != text) {
|
|
|
|
this->text_ = text;
|
|
|
|
this->updateSize();
|
|
|
|
this->update();
|
|
|
|
}
|
2018-06-11 15:04:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-06-24 17:01:45 +02:00
|
|
|
void Label::scaleChangedEvent(float scale)
|
2018-06-11 15:04:54 +02:00
|
|
|
{
|
|
|
|
this->updateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Label::sizeHint() const
|
|
|
|
{
|
|
|
|
return this->preferedSize_;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Label::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
return this->preferedSize_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
auto app = getApp();
|
|
|
|
|
|
|
|
QPainter painter(this);
|
2018-06-24 18:30:48 +02:00
|
|
|
QFontMetrics metrics = app->fonts->getFontMetrics(
|
|
|
|
this->getFontStyle(),
|
|
|
|
this->getScale() * 96.f / this->logicalDpiX() * this->devicePixelRatioF());
|
|
|
|
painter.setFont(app->fonts->getFont(
|
|
|
|
this->getFontStyle(),
|
|
|
|
this->getScale() * 96.f / this->logicalDpiX() * this->devicePixelRatioF()));
|
2018-06-11 15:04:54 +02:00
|
|
|
|
2018-06-11 21:57:17 +02:00
|
|
|
int offset = this->getOffset();
|
2018-06-11 15:04:54 +02:00
|
|
|
|
|
|
|
// draw text
|
|
|
|
QRect textRect(offset, 0, this->width() - offset - offset, this->height());
|
|
|
|
|
|
|
|
int width = metrics.width(this->text_);
|
|
|
|
Qt::Alignment alignment = !this->centered_ || width > textRect.width()
|
|
|
|
? Qt::AlignLeft | Qt::AlignVCenter
|
|
|
|
: Qt::AlignCenter;
|
|
|
|
|
|
|
|
QTextOption option(alignment);
|
|
|
|
option.setWrapMode(QTextOption::NoWrap);
|
|
|
|
painter.drawText(textRect, this->text_, option);
|
2018-06-19 18:55:45 +02:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
painter.setPen(QColor(255, 0, 0));
|
|
|
|
painter.drawRect(0, 0, this->width() - 1, this->height() - 1);
|
|
|
|
#endif
|
2018-06-11 15:04:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::updateSize()
|
|
|
|
{
|
|
|
|
auto app = getApp();
|
|
|
|
|
|
|
|
QFontMetrics metrics = app->fonts->getFontMetrics(this->fontStyle_, this->getScale());
|
|
|
|
|
2018-06-11 21:57:17 +02:00
|
|
|
int width = metrics.width(this->text_) + (2 * this->getOffset());
|
|
|
|
int height = metrics.height();
|
|
|
|
this->preferedSize_ = QSize(width, height);
|
2018-06-11 15:04:54 +02:00
|
|
|
|
|
|
|
this->updateGeometry();
|
|
|
|
}
|
|
|
|
|
2018-06-11 21:57:17 +02:00
|
|
|
int Label::getOffset()
|
|
|
|
{
|
|
|
|
return this->hasOffset_ ? int(8 * this->getScale()) : 0;
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:04:54 +02:00
|
|
|
} // namespace chatterino
|