/******************************************************************************* * util/FontSizeHandler.cpp * * * * Copyright (C) 2025, Retroshare Team * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * * * * You should have received a copy of the GNU Affero General Public License * * along with this program. If not, see . * * * *******************************************************************************/ #include #include #include #include "rshare.h" #include "FontSizeHandler.h" #include "gui/settings/rsharesettings.h" #include "gui/notifyqt.h" // Data for QAbstractItemView struct FontSizeHandlerWidgetData { std::function callback; }; // Data for QWidget struct FontSizeHandlerViewData { float iconHeightFactor; std::function callback; }; class FontSizeHandlerObject : public QObject { public: FontSizeHandlerObject(FontSizeHandlerBase *fontSizeHandler): QObject() { mFontSizeHandlerBase = fontSizeHandler; } bool eventFilter(QObject* object, QEvent* event) { if (event->type() == QEvent::StyleChange) { QMap::iterator itView = mView.find((QAbstractItemView*) object); if (itView != mView.end()) { mFontSizeHandlerBase->updateFontSize(itView.key(), itView.value().iconHeightFactor, itView.value().callback); } QMap::iterator itWidget = mWidget.find((QWidget*) object); if (itWidget != mWidget.end()) { mFontSizeHandlerBase->updateFontSize(itWidget.key(), itWidget.value().callback); } } return false; } void registerFontSize(QWidget *widget, std::function callback) { FontSizeHandlerWidgetData data; data.callback = callback; mWidget.insert(widget, data); QObject::connect(RsGUIEventManager::getInstance(), &RsGUIEventManager::settingsChanged, widget, [this, widget, callback]() { mFontSizeHandlerBase->updateFontSize(widget, callback); }); widget->installEventFilter(this); QObject::connect(widget, &QObject::destroyed, this, [this, widget](QObject *object) { if (widget == object) { mWidget.remove(widget); widget->removeEventFilter(this); } }); mFontSizeHandlerBase->updateFontSize(widget, callback, true); } void registerFontSize(QAbstractItemView *view, float iconHeightFactor, std::function callback) { FontSizeHandlerViewData data; data.iconHeightFactor = iconHeightFactor; data.callback = callback; mView.insert(view, data); QObject::connect(RsGUIEventManager::getInstance(), &RsGUIEventManager::settingsChanged, view, [this, view, iconHeightFactor, callback]() { mFontSizeHandlerBase->updateFontSize(view, iconHeightFactor, callback); }); view->installEventFilter(this); QObject::connect(view, &QObject::destroyed, this, [this, view](QObject *object) { if (view == object) { mView.remove(view); view->removeEventFilter(this); } }); mFontSizeHandlerBase->updateFontSize(view, iconHeightFactor, callback, true); } private: FontSizeHandlerBase *mFontSizeHandlerBase; QMap mView; QMap mWidget; }; FontSizeHandlerBase::FontSizeHandlerBase(Type type) { mType = type; mObject = nullptr; } FontSizeHandlerBase::~FontSizeHandlerBase() { if (mObject) { mObject->deleteLater(); mObject = nullptr; } } int FontSizeHandlerBase::getFontSize() { switch (mType) { case FONT_SIZE: return Settings->getFontSize(); case MESSAGE_FONT_SIZE: return Settings->getMessageFontSize(); } return 0; } void FontSizeHandlerBase::registerFontSize(QWidget *widget, std::function callback) { if (!widget) { return; } if (!mObject) { mObject = new FontSizeHandlerObject(this); } mObject->registerFontSize(widget, callback); } void FontSizeHandlerBase::registerFontSize(QAbstractItemView *view, float iconHeightFactor, std::function callback) { if (!view) { return; } if (!mObject) { mObject = new FontSizeHandlerObject(this); } mObject->registerFontSize(view, iconHeightFactor, callback); } void FontSizeHandlerBase::updateFontSize(QWidget *widget, std::function callback, bool force) { if (!widget) { return; } int fontSize = getFontSize(); QFont font = widget->font(); if (force || font.pointSize() != fontSize) { font.setPointSize(fontSize); widget->setFont(font); if (callback) { callback(widget, fontSize); } } } void FontSizeHandlerBase::updateFontSize(QAbstractItemView *view, float iconHeightFactor, std::function callback, bool force) { if (!view) { return; } int fontSize = getFontSize(); QFont font = view->font(); if (force || font.pointSize() != fontSize) { font.setPointSize(fontSize); view->setFont(font); if (iconHeightFactor) { QFontMetricsF fontMetrics(font); int iconHeight = fontMetrics.height() * iconHeightFactor; view->setIconSize(QSize(iconHeight, iconHeight)); } if (callback) { callback(view, fontSize); } } }