From 93154fddc4b0a10fd29fde38824066dd64d6b5f5 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Wed, 26 Mar 2025 01:20:54 +0100 Subject: [PATCH] Added new class FontSizeHandler to set font size from settings --- retroshare-gui/src/retroshare-gui.pro | 2 + retroshare-gui/src/util/FontSizeHandler.cpp | 210 ++++++++++++++++++++ retroshare-gui/src/util/FontSizeHandler.h | 71 +++++++ 3 files changed, 283 insertions(+) create mode 100644 retroshare-gui/src/util/FontSizeHandler.cpp create mode 100644 retroshare-gui/src/util/FontSizeHandler.h diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index d73117b84..e88d931f6 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -451,6 +451,7 @@ HEADERS += rshare.h \ util/qtthreadsutils.h \ util/ClickableLabel.h \ util/AspectRatioPixmapLabel.h \ + util/FontSizeHandler.h \ gui/profile/ProfileWidget.h \ gui/profile/ProfileManager.h \ gui/profile/StatusMessage.h \ @@ -817,6 +818,7 @@ SOURCES += main.cpp \ util/RichTextEdit.cpp \ util/ClickableLabel.cpp \ util/AspectRatioPixmapLabel.cpp \ + util/FontSizeHandler.cpp \ gui/profile/ProfileWidget.cpp \ gui/profile/StatusMessage.cpp \ gui/profile/ProfileManager.cpp \ diff --git a/retroshare-gui/src/util/FontSizeHandler.cpp b/retroshare-gui/src/util/FontSizeHandler.cpp new file mode 100644 index 000000000..c184da0de --- /dev/null +++ b/retroshare-gui/src/util/FontSizeHandler.cpp @@ -0,0 +1,210 @@ +/******************************************************************************* + * 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(NotifyQt::getInstance(), &NotifyQt::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(NotifyQt::getInstance(), &NotifyQt::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); + } + } +} diff --git a/retroshare-gui/src/util/FontSizeHandler.h b/retroshare-gui/src/util/FontSizeHandler.h new file mode 100644 index 000000000..19859227d --- /dev/null +++ b/retroshare-gui/src/util/FontSizeHandler.h @@ -0,0 +1,71 @@ +/******************************************************************************* + * util/FontSizeHandler.h * + * * + * 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 . * + * * + *******************************************************************************/ + +#ifndef _FONTSIZEHANDLER_H +#define _FONTSIZEHANDLER_H + +class QWidget; +class QAbstractItemView; +class FontSizeHandlerObject; + +// Class to handle font size and message font size setting +class FontSizeHandlerBase +{ + friend class FontSizeHandlerObject; + +public: + virtual ~FontSizeHandlerBase(); + + int getFontSize(); + + void registerFontSize(QWidget *widget, std::function callback = nullptr); + void registerFontSize(QAbstractItemView *view, float iconHeightFactor = 0.0f, std::function callback = nullptr); + + void updateFontSize(QWidget *widget, std::function callback = nullptr, bool force = false); + void updateFontSize(QAbstractItemView *view, float iconHeightFactor, std::function callback, bool force = false); + +protected: + enum Type { + FONT_SIZE, + MESSAGE_FONT_SIZE + }; + + FontSizeHandlerBase(Type type); + +private: + Type mType; + FontSizeHandlerObject *mObject; +}; + +// Class to handle font size setting +class FontSizeHandler : public FontSizeHandlerBase +{ +public: + FontSizeHandler() : FontSizeHandlerBase(FONT_SIZE) {} +}; + +// Class to handle message font size setting +class MessageFontSizeHandler : public FontSizeHandlerBase +{ +public: + MessageFontSizeHandler() : FontSizeHandlerBase(MESSAGE_FONT_SIZE) {} +}; + +#endif // FONTSIZEHANDLER