mirror-chatterino2/src/widgets/BaseWidget.cpp

153 lines
3.2 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "widgets/BaseWidget.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "debug/Log.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
2018-06-26 14:09:39 +02:00
#include "widgets/BaseWindow.hpp"
2018-01-25 20:49:49 +01:00
#include <QChildEvent>
#include <QDebug>
2018-01-05 03:30:43 +01:00
#include <QIcon>
#include <QLayout>
#include <QtGlobal>
namespace chatterino {
BaseWidget::BaseWidget(QWidget *parent, Qt::WindowFlags f)
: QWidget(parent, f)
{
this->init();
}
BaseWidget::~BaseWidget()
{
this->themeConnection.disconnect();
}
2018-01-25 20:49:49 +01:00
float BaseWidget::getScale() const
2017-09-22 00:50:43 +02:00
{
if (this->overrideScale) {
return this->overrideScale.get();
}
2017-09-22 00:50:43 +02:00
BaseWidget *baseWidget = dynamic_cast<BaseWidget *>(this->window());
if (baseWidget == nullptr) {
return 1.f;
}
return baseWidget->scale;
2017-09-22 00:50:43 +02:00
}
void BaseWidget::setOverrideScale(boost::optional<float> value)
{
this->overrideScale = value;
this->setScale(this->getScale());
}
boost::optional<float> BaseWidget::getOverrideScale() const
{
return this->overrideScale;
}
2018-01-25 21:11:14 +01:00
QSize BaseWidget::getScaleIndependantSize() const
{
return this->scaleIndependantSize;
}
int BaseWidget::getScaleIndependantWidth() const
{
return this->scaleIndependantSize.width();
}
int BaseWidget::getScaleIndependantHeight() const
{
return this->scaleIndependantSize.height();
}
void BaseWidget::setScaleIndependantSize(int width, int height)
{
this->setScaleIndependantSize(QSize(width, height));
}
void BaseWidget::setScaleIndependantSize(QSize size)
{
this->scaleIndependantSize = size;
if (size.width() > 0) {
this->setFixedWidth((int)(size.width() * this->getScale()));
}
if (size.height() > 0) {
this->setFixedHeight((int)(size.height() * this->getScale()));
}
}
void BaseWidget::setScaleIndependantWidth(int value)
{
this->setScaleIndependantSize(QSize(value, this->scaleIndependantSize.height()));
}
void BaseWidget::setScaleIndependantHeight(int value)
{
this->setScaleIndependantSize(QSize(this->scaleIndependantSize.height(), value));
}
void BaseWidget::init()
{
auto app = getApp();
this->themeManager = app->themes;
this->themeConnection = this->themeManager->updated.connect([this]() {
2018-01-25 20:49:49 +01:00
this->themeRefreshEvent();
this->update();
});
}
2018-01-25 20:49:49 +01:00
void BaseWidget::childEvent(QChildEvent *event)
{
if (event->added()) {
BaseWidget *widget = dynamic_cast<BaseWidget *>(event->child());
if (widget != nullptr) {
2018-01-25 20:49:49 +01:00
this->widgets.push_back(widget);
}
} else if (event->removed()) {
for (auto it = this->widgets.begin(); it != this->widgets.end(); it++) {
if (*it == event->child()) {
this->widgets.erase(it);
break;
}
}
}
}
2018-01-25 21:11:14 +01:00
2018-04-18 09:33:05 +02:00
void BaseWidget::showEvent(QShowEvent *)
{
this->setScale(this->getScale());
2018-04-18 09:33:05 +02:00
this->themeRefreshEvent();
}
2018-01-25 20:49:49 +01:00
void BaseWidget::setScale(float value)
{
// update scale value
this->scale = value;
this->scaleChangedEvent(this->getScale());
this->scaleChanged.invoke(this->getScale());
2018-01-25 20:49:49 +01:00
2018-01-25 21:11:14 +01:00
this->setScaleIndependantSize(this->getScaleIndependantSize());
2018-01-25 20:49:49 +01:00
}
void BaseWidget::scaleChangedEvent(float newDpi)
{
}
void BaseWidget::themeRefreshEvent()
{
// Do any color scheme updates here
}
} // namespace chatterino