mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-04-14 04:23:10 -04:00
Added new class FontSizeHandler to set font size from settings
This commit is contained in:
parent
cab91819d6
commit
93154fddc4
@ -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 \
|
||||
|
210
retroshare-gui/src/util/FontSizeHandler.cpp
Normal file
210
retroshare-gui/src/util/FontSizeHandler.cpp
Normal file
@ -0,0 +1,210 @@
|
||||
/*******************************************************************************
|
||||
* util/FontSizeHandler.cpp *
|
||||
* *
|
||||
* Copyright (C) 2025, Retroshare Team <retroshare.project@gmail.com> *
|
||||
* *
|
||||
* 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 <https://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
#include <QMap>
|
||||
#include <QWidget>
|
||||
#include <QAbstractItemView>
|
||||
|
||||
#include "rshare.h"
|
||||
#include "FontSizeHandler.h"
|
||||
#include "gui/settings/rsharesettings.h"
|
||||
#include "gui/notifyqt.h"
|
||||
|
||||
// Data for QAbstractItemView
|
||||
struct FontSizeHandlerWidgetData
|
||||
{
|
||||
std::function<void(QWidget*, int)> callback;
|
||||
};
|
||||
|
||||
// Data for QWidget
|
||||
struct FontSizeHandlerViewData
|
||||
{
|
||||
float iconHeightFactor;
|
||||
std::function<void(QAbstractItemView*, int)> callback;
|
||||
};
|
||||
|
||||
class FontSizeHandlerObject : public QObject
|
||||
{
|
||||
public:
|
||||
FontSizeHandlerObject(FontSizeHandlerBase *fontSizeHandler): QObject()
|
||||
{
|
||||
mFontSizeHandlerBase = fontSizeHandler;
|
||||
}
|
||||
|
||||
bool eventFilter(QObject* object, QEvent* event)
|
||||
{
|
||||
if (event->type() == QEvent::StyleChange) {
|
||||
QMap<QAbstractItemView*, FontSizeHandlerViewData>::iterator itView = mView.find((QAbstractItemView*) object);
|
||||
if (itView != mView.end()) {
|
||||
mFontSizeHandlerBase->updateFontSize(itView.key(), itView.value().iconHeightFactor, itView.value().callback);
|
||||
}
|
||||
|
||||
QMap<QWidget*, FontSizeHandlerWidgetData>::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<void(QWidget*, int)> 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<void(QAbstractItemView*, int)> 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<QAbstractItemView*, FontSizeHandlerViewData> mView;
|
||||
QMap<QWidget*, FontSizeHandlerWidgetData> 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<void(QWidget*, int)> callback)
|
||||
{
|
||||
if (!widget) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mObject) {
|
||||
mObject = new FontSizeHandlerObject(this);
|
||||
}
|
||||
|
||||
mObject->registerFontSize(widget, callback);
|
||||
}
|
||||
|
||||
void FontSizeHandlerBase::registerFontSize(QAbstractItemView *view, float iconHeightFactor, std::function<void(QAbstractItemView*, int)> callback)
|
||||
{
|
||||
if (!view) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mObject) {
|
||||
mObject = new FontSizeHandlerObject(this);
|
||||
}
|
||||
|
||||
mObject->registerFontSize(view, iconHeightFactor, callback);
|
||||
}
|
||||
|
||||
void FontSizeHandlerBase::updateFontSize(QWidget *widget, std::function<void (QWidget *, int)> 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<void (QAbstractItemView *, int)> 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);
|
||||
}
|
||||
}
|
||||
}
|
71
retroshare-gui/src/util/FontSizeHandler.h
Normal file
71
retroshare-gui/src/util/FontSizeHandler.h
Normal file
@ -0,0 +1,71 @@
|
||||
/*******************************************************************************
|
||||
* util/FontSizeHandler.h *
|
||||
* *
|
||||
* Copyright (C) 2025, Retroshare Team <retroshare.project@gmail.com> *
|
||||
* *
|
||||
* 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 <https://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
*******************************************************************************/
|
||||
|
||||
#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<void(QWidget*, int)> callback = nullptr);
|
||||
void registerFontSize(QAbstractItemView *view, float iconHeightFactor = 0.0f, std::function<void(QAbstractItemView*, int)> callback = nullptr);
|
||||
|
||||
void updateFontSize(QWidget *widget, std::function<void(QWidget*, int)> callback = nullptr, bool force = false);
|
||||
void updateFontSize(QAbstractItemView *view, float iconHeightFactor, std::function<void (QAbstractItemView *, int)> 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
|
Loading…
x
Reference in New Issue
Block a user