2017-07-02 14:28:37 +02:00
|
|
|
#include "widgets/basewidget.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
|
|
|
#include "application.hpp"
|
2018-04-03 02:55:32 +02:00
|
|
|
#include "debug/log.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/settingsmanager.hpp"
|
|
|
|
#include "singletons/thememanager.hpp"
|
2017-07-02 14:28:37 +02:00
|
|
|
|
2018-01-25 20:49:49 +01:00
|
|
|
#include <QChildEvent>
|
2017-07-02 14:28:37 +02:00
|
|
|
#include <QDebug>
|
2018-01-05 03:30:43 +01:00
|
|
|
#include <QIcon>
|
2017-12-18 02:47:01 +01:00
|
|
|
#include <QLayout>
|
|
|
|
#include <QtGlobal>
|
2017-07-02 14:28:37 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
BaseWidget::BaseWidget(QWidget *parent, Qt::WindowFlags f)
|
2018-01-15 01:35:35 +01:00
|
|
|
: QWidget(parent, f)
|
2017-07-02 14:28:37 +02:00
|
|
|
{
|
|
|
|
this->init();
|
|
|
|
}
|
|
|
|
|
2018-04-13 23:17:10 +02:00
|
|
|
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
|
|
|
{
|
|
|
|
BaseWidget *baseWidget = dynamic_cast<BaseWidget *>(this->window());
|
|
|
|
|
|
|
|
if (baseWidget == nullptr) {
|
|
|
|
return 1.f;
|
|
|
|
}
|
2018-03-31 13:44:15 +02:00
|
|
|
|
|
|
|
return baseWidget->scale;
|
2017-09-22 00:50:43 +02:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2017-07-02 14:28:37 +02:00
|
|
|
void BaseWidget::init()
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
|
|
|
this->themeManager = app->themes;
|
|
|
|
|
|
|
|
this->themeConnection = this->themeManager->updated.connect([this]() {
|
2018-01-25 20:49:49 +01:00
|
|
|
this->themeRefreshEvent();
|
2017-07-02 14:28:37 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
|
2018-03-31 13:44:15 +02:00
|
|
|
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 *)
|
|
|
|
{
|
2018-05-08 15:12:04 +02:00
|
|
|
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(value);
|
|
|
|
this->scaleChanged.invoke(value);
|
|
|
|
|
2018-01-25 21:11:14 +01:00
|
|
|
this->setScaleIndependantSize(this->getScaleIndependantSize());
|
|
|
|
|
2018-01-25 20:49:49 +01:00
|
|
|
// 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()
|
2017-07-02 14:28:37 +02:00
|
|
|
{
|
|
|
|
// Do any color scheme updates here
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|