2017-07-02 14:28:37 +02:00
|
|
|
#include "widgets/basewidget.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "singletons/settingsmanager.hpp"
|
|
|
|
#include "singletons/thememanager.hpp"
|
2017-12-24 16:01:01 +01:00
|
|
|
#include "widgets/tooltipwidget.hpp"
|
2017-07-02 14:28:37 +02:00
|
|
|
|
|
|
|
#include <QDebug>
|
2017-12-18 02:47:01 +01:00
|
|
|
#include <QLayout>
|
|
|
|
#include <QtGlobal>
|
2017-07-02 14:28:37 +02:00
|
|
|
#include <boost/signals2.hpp>
|
2017-09-22 00:50:43 +02:00
|
|
|
#include "util/nativeeventhelper.hpp"
|
2017-07-02 14:28:37 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
BaseWidget::BaseWidget(singletons::ThemeManager &_themeManager, QWidget *parent)
|
2017-07-02 14:28:37 +02:00
|
|
|
: QWidget(parent)
|
2017-12-31 00:50:07 +01:00
|
|
|
, themeManager(_themeManager)
|
2017-07-02 14:28:37 +02:00
|
|
|
{
|
|
|
|
this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseWidget::BaseWidget(BaseWidget *parent)
|
|
|
|
: QWidget(parent)
|
2017-12-31 22:58:35 +01:00
|
|
|
, themeManager(singletons::ThemeManager::getInstance())
|
2017-07-02 14:28:37 +02:00
|
|
|
{
|
|
|
|
this->init();
|
|
|
|
}
|
|
|
|
|
2017-12-17 16:45:15 +01:00
|
|
|
BaseWidget::BaseWidget(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
2017-12-31 22:58:35 +01:00
|
|
|
, themeManager(singletons::ThemeManager::getInstance())
|
2017-12-17 16:45:15 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:50:43 +02:00
|
|
|
float BaseWidget::getDpiMultiplier()
|
|
|
|
{
|
2017-12-18 00:54:17 +01: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 {
|
|
|
|
return baseWidget->dpiMultiplier;
|
|
|
|
// int screenNr = QApplication::desktop()->screenNumber(this);
|
|
|
|
// QScreen *screen = QApplication::screens().at(screenNr);
|
|
|
|
// return screen->logicalDotsPerInch() / 96.f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 14:28:37 +02:00
|
|
|
void BaseWidget::init()
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
auto connection = this->themeManager.updated.connect([this]() {
|
2017-07-02 14:28:37 +02:00
|
|
|
this->refreshTheme();
|
|
|
|
|
|
|
|
this->update();
|
|
|
|
});
|
2017-12-19 01:54:51 +01:00
|
|
|
|
|
|
|
QObject::connect(this, &QObject::destroyed, [connection] {
|
|
|
|
connection.disconnect(); //
|
|
|
|
});
|
2017-07-02 14:28:37 +02:00
|
|
|
}
|
|
|
|
|
2017-09-23 18:37:51 +02:00
|
|
|
void BaseWidget::initAsWindow()
|
|
|
|
{
|
2017-12-24 16:01:01 +01:00
|
|
|
this->isWindow = true;
|
|
|
|
|
2017-09-23 18:37:51 +02:00
|
|
|
#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
|
|
|
|
2017-12-31 22:58:35 +01:00
|
|
|
if (singletons::SettingManager::getInstance().windowTopMost.getValue()) {
|
2017-12-17 16:45:15 +01:00
|
|
|
this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
|
|
|
|
}
|
2017-09-23 18:37:51 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 14:28:37 +02:00
|
|
|
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;
|
|
|
|
|
2017-12-18 02:47:01 +01:00
|
|
|
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
|
|
|
|
|
2017-12-24 23:56:08 +01:00
|
|
|
void BaseWidget::changeEvent(QEvent *)
|
|
|
|
{
|
|
|
|
if (this->isWindow) {
|
|
|
|
TooltipWidget::getInstance()->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseWidget::leaveEvent(QEvent *)
|
2017-12-24 16:01:01 +01:00
|
|
|
{
|
|
|
|
if (this->isWindow) {
|
|
|
|
TooltipWidget::getInstance()->hide();
|
|
|
|
}
|
|
|
|
}
|
2017-07-02 14:28:37 +02:00
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|