mirror-chatterino2/src/widgets/BaseWidget.cpp

142 lines
3.1 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)
{
2018-07-06 17:11:37 +02:00
this->theme = getApp()->themes;
2018-07-06 17:11:37 +02:00
this->signalHolder_.managedConnect(this->theme->updated, [this]() {
this->themeChangedEvent();
this->update();
});
}
2018-01-25 20:49:49 +01:00
float BaseWidget::getScale() const
2017-09-22 00:50:43 +02:00
{
2018-07-06 17:11:37 +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;
}
2018-07-06 17:11:37 +02:00
return baseWidget->scale_;
}
void BaseWidget::setScale(float value)
{
// update scale value
this->scale_ = value;
this->scaleChangedEvent(this->getScale());
this->scaleChanged.invoke(this->getScale());
this->setScaleIndependantSize(this->getScaleIndependantSize());
2017-09-22 00:50:43 +02:00
}
void BaseWidget::setOverrideScale(boost::optional<float> value)
{
2018-07-06 17:11:37 +02:00
this->overrideScale_ = value;
this->setScale(this->getScale());
}
boost::optional<float> BaseWidget::getOverrideScale() const
{
2018-07-06 17:11:37 +02:00
return this->overrideScale_;
}
2018-01-25 21:11:14 +01:00
QSize BaseWidget::getScaleIndependantSize() const
{
2018-07-06 17:11:37 +02:00
return this->scaleIndependantSize_;
2018-01-25 21:11:14 +01:00
}
int BaseWidget::getScaleIndependantWidth() const
{
2018-07-06 17:11:37 +02:00
return this->scaleIndependantSize_.width();
2018-01-25 21:11:14 +01:00
}
int BaseWidget::getScaleIndependantHeight() const
{
2018-07-06 17:11:37 +02:00
return this->scaleIndependantSize_.height();
2018-01-25 21:11:14 +01:00
}
void BaseWidget::setScaleIndependantSize(int width, int height)
{
this->setScaleIndependantSize(QSize(width, height));
}
void BaseWidget::setScaleIndependantSize(QSize size)
{
2018-07-06 17:11:37 +02:00
this->scaleIndependantSize_ = size;
2018-01-25 21:11:14 +01:00
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)
{
2018-07-06 17:11:37 +02:00
this->setScaleIndependantSize(QSize(value, this->scaleIndependantSize_.height()));
2018-01-25 21:11:14 +01:00
}
void BaseWidget::setScaleIndependantHeight(int value)
{
2018-07-06 17:11:37 +02:00
this->setScaleIndependantSize(QSize(this->scaleIndependantSize_.height(), value));
}
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-07-06 17:11:37 +02:00
this->widgets_.push_back(widget);
2018-01-25 20:49:49 +01:00
}
} else if (event->removed()) {
2018-07-06 17:11:37 +02:00
for (auto it = this->widgets_.begin(); it != this->widgets_.end(); it++) {
2018-01-25 20:49:49 +01:00
if (*it == event->child()) {
2018-07-06 17:11:37 +02:00
this->widgets_.erase(it);
2018-01-25 20:49:49 +01:00
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-07-06 17:11:37 +02:00
this->themeChangedEvent();
2018-01-25 20:49:49 +01:00
}
void BaseWidget::scaleChangedEvent(float newDpi)
{
}
2018-07-06 17:11:37 +02:00
void BaseWidget::themeChangedEvent()
{
// Do any color scheme updates here
}
} // namespace chatterino