2018-01-27 21:13:22 +01:00
|
|
|
#include "label.hpp"
|
2018-04-28 15:20:18 +02:00
|
|
|
|
|
|
|
#include "application.hpp"
|
2018-01-27 21:13:22 +01:00
|
|
|
#include "singletons/fontmanager.hpp"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
2018-04-03 02:55:32 +02:00
|
|
|
|
2018-01-27 21:13:22 +01:00
|
|
|
Label::Label(BaseWidget *parent)
|
|
|
|
: BaseWidget(parent)
|
|
|
|
{
|
2018-04-28 15:20:18 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
|
|
|
app->fonts->fontChanged.connect([=]() {
|
|
|
|
this->scaleChangedEvent(this->getScale()); //
|
|
|
|
});
|
2018-01-27 21:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString &Label::getText() const
|
|
|
|
{
|
|
|
|
return this->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setText(const QString &value)
|
|
|
|
{
|
|
|
|
this->text = value;
|
|
|
|
this->scaleChangedEvent(this->getScale());
|
|
|
|
}
|
|
|
|
|
|
|
|
FontStyle Label::getFontStyle() const
|
|
|
|
{
|
|
|
|
return this->fontStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setFontStyle(FontStyle style)
|
|
|
|
{
|
|
|
|
this->fontStyle = style;
|
|
|
|
this->scaleChangedEvent(this->getScale());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::scaleChangedEvent(float scale)
|
|
|
|
{
|
2018-04-28 15:20:18 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
|
|
|
QFontMetrics metrics = app->fonts->getFontMetrics(this->fontStyle, scale);
|
2018-01-27 21:13:22 +01:00
|
|
|
|
|
|
|
this->preferedSize = QSize(metrics.width(this->text), metrics.height());
|
|
|
|
|
|
|
|
this->updateGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Label::sizeHint() const
|
|
|
|
{
|
|
|
|
return this->preferedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Label::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
return this->preferedSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::paintEvent(QPaintEvent *)
|
|
|
|
{
|
2018-04-28 15:20:18 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
2018-01-27 21:13:22 +01:00
|
|
|
QPainter painter(this);
|
2018-04-28 15:20:18 +02:00
|
|
|
painter.setFont(app->fonts->getFont(this->fontStyle,
|
|
|
|
this->getScale() / painter.device()->devicePixelRatioF()));
|
2018-01-27 21:13:22 +01:00
|
|
|
|
2018-04-28 15:20:18 +02:00
|
|
|
int width = app->fonts->getFontMetrics(this->fontStyle, this->getScale()).width(this->text);
|
2018-01-27 21:13:22 +01:00
|
|
|
|
|
|
|
int flags = Qt::TextSingleLine;
|
|
|
|
|
|
|
|
if (this->width() < width) {
|
|
|
|
flags |= Qt::AlignLeft | Qt::AlignVCenter;
|
|
|
|
} else {
|
|
|
|
flags |= Qt::AlignCenter;
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.drawText(this->rect(), flags, this->text);
|
|
|
|
}
|
2018-04-03 02:55:32 +02:00
|
|
|
|
2018-01-27 21:13:22 +01:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|