mirror-chatterino2/src/widgets/basewidget.cpp

120 lines
2.7 KiB
C++
Raw Normal View History

#include "widgets/basewidget.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/settingsmanager.hpp"
#include "singletons/thememanager.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>
#include <boost/signals2.hpp>
namespace chatterino {
namespace widgets {
BaseWidget::BaseWidget(singletons::ThemeManager &_themeManager, QWidget *parent, Qt::WindowFlags f)
: QWidget(parent, f)
2017-12-31 00:50:07 +01:00
, themeManager(_themeManager)
{
this->init();
}
BaseWidget::BaseWidget(BaseWidget *parent, Qt::WindowFlags f)
: QWidget(parent, f)
2017-12-31 22:58:35 +01:00
, themeManager(singletons::ThemeManager::getInstance())
{
this->init();
}
2018-01-25 20:49:49 +01:00
float BaseWidget::getScale() const
2017-09-22 00:50:43 +02:00
{
// return 1.f;
2017-09-22 00:50:43 +02:00
BaseWidget *baseWidget = dynamic_cast<BaseWidget *>(this->window());
if (baseWidget == nullptr) {
return 1.f;
} else {
2018-01-25 20:49:49 +01:00
return baseWidget->scale;
2017-09-22 00:50:43 +02:00
}
}
void BaseWidget::init()
{
2017-12-31 00:50:07 +01:00
auto connection = this->themeManager.updated.connect([this]() {
2018-01-25 20:49:49 +01:00
this->themeRefreshEvent();
this->update();
});
QObject::connect(this, &QObject::destroyed, [connection] {
connection.disconnect(); //
});
}
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) {
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;
}
}
}
}
void BaseWidget::setScale(float value)
{
// update scale value
this->scale = value;
this->scaleChangedEvent(value);
this->scaleChanged.invoke(value);
// set scale for all children
BaseWidget::setScaleRecursive(value, this);
}
void BaseWidget::setScaleRecursive(float scale, QObject *object)
{
for (QObject *child : object->children()) {
BaseWidget *widget = dynamic_cast<BaseWidget *>(child);
if (widget != nullptr) {
widget->setScale(scale);
continue;
}
// QLayout *layout = nullptr;
// QWidget *widget = dynamic_cast<QWidget *>(child);
// if (widget != nullptr) {
// layout = widget->layout();
// }
// else {
QLayout *layout = dynamic_cast<QLayout *>(object);
if (layout != nullptr) {
setScaleRecursive(scale, layout);
}
// }
}
}
void BaseWidget::scaleChangedEvent(float newDpi)
{
}
void BaseWidget::themeRefreshEvent()
{
// Do any color scheme updates here
}
} // namespace widgets
} // namespace chatterino