mirror-chatterino2/src/widgets/basewidget.cpp

115 lines
2.6 KiB
C++
Raw Normal View History

#include "widgets/basewidget.hpp"
#include "colorscheme.hpp"
2017-12-17 16:45:15 +01:00
#include "settingsmanager.hpp"
#include "widgets/tooltipwidget.hpp"
#include <QDebug>
#include <QLayout>
#include <QtGlobal>
#include <boost/signals2.hpp>
2017-09-22 00:50:43 +02:00
#include "util/nativeeventhelper.hpp"
namespace chatterino {
namespace widgets {
BaseWidget::BaseWidget(ColorScheme &_colorScheme, QWidget *parent)
: QWidget(parent)
, colorScheme(_colorScheme)
{
this->init();
}
BaseWidget::BaseWidget(BaseWidget *parent)
: QWidget(parent)
2017-12-19 03:18:00 +01:00
, colorScheme(*ColorScheme::instance)
{
this->init();
}
2017-12-17 16:45:15 +01:00
BaseWidget::BaseWidget(QWidget *parent)
: QWidget(parent)
, colorScheme(*ColorScheme::instance)
{
}
2017-09-22 00:50:43 +02:00
float BaseWidget::getDpiMultiplier()
{
// 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 {
return baseWidget->dpiMultiplier;
// int screenNr = QApplication::desktop()->screenNumber(this);
// QScreen *screen = QApplication::screens().at(screenNr);
// return screen->logicalDotsPerInch() / 96.f;
}
}
void BaseWidget::init()
{
auto connection = this->colorScheme.updated.connect([this]() {
this->refreshTheme();
this->update();
});
QObject::connect(this, &QObject::destroyed, [connection] {
connection.disconnect(); //
});
}
void BaseWidget::initAsWindow()
{
this->isWindow = true;
#ifdef USEWINSDK
auto dpi = util::getWindowDpi(this->winId());
if (dpi) {
this->dpiMultiplier = dpi.value() / 96.f;
}
#endif
2017-12-17 16:45:15 +01:00
if (SettingsManager::getInstance().windowTopMost.getValue()) {
this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
}
}
void BaseWidget::refreshTheme()
{
// Do any color scheme updates here
}
2017-09-22 00:50:43 +02:00
#ifdef USEWINSDK
bool BaseWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
int dpi;
if (util::tryHandleDpiChangedMessage(message, dpi)) {
qDebug() << "dpi changed";
float oldDpiMultiplier = this->dpiMultiplier;
this->dpiMultiplier = dpi / 96.f;
float scale = this->dpiMultiplier / oldDpiMultiplier;
this->dpiMultiplierChanged(oldDpiMultiplier, this->dpiMultiplier);
2017-09-22 00:50:43 +02:00
this->resize(static_cast<int>(this->width() * scale),
static_cast<int>(this->height() * scale));
}
return QWidget::nativeEvent(eventType, message, result);
}
#endif
void BaseWidget::focusOutEvent(QFocusEvent *)
{
if (this->isWindow) {
TooltipWidget::getInstance()->hide();
}
}
} // namespace widgets
} // namespace chatterino