2018-06-26 14:09:39 +02:00
|
|
|
#include "widgets/BaseWidget.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
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"
|
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 {
|
|
|
|
|
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
|
|
|
{
|
2018-06-25 22:06:17 +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-03-31 13:44:15 +02:00
|
|
|
|
|
|
|
return baseWidget->scale;
|
2017-09-22 00:50:43 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 22:06:17 +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));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2018-06-25 22:06:17 +02:00
|
|
|
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()
|
2017-07-02 14:28:37 +02:00
|
|
|
{
|
|
|
|
// Do any color scheme updates here
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|