Wireshark 5.1.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
font_manager.h
Go to the documentation of this file.
1
9
10#ifndef FONT_MANAGER_H
11#define FONT_MANAGER_H
12
13#include <QByteArray>
14#include <QFont>
15#include <QList>
16#include <QMutex>
17#include <QObject>
18#include <QPair>
19
59class FontManager : public QObject
60{
61 Q_OBJECT
62public:
74 enum class ZoomScope {
75 AppWide,
76 ContentOnly
77 };
78
80 static FontManager *instance();
81
82 // --- Value accessors: static only (single instance, no instance
83 // duplicates, so there is no static/instance name clash) ---
84
86 static QFont font();
88 static QFont monospaceFont();
90 static QFont zoomedFont();
92 static QFont zoomedMonospaceFont();
93
98 static qreal zoomFactor();
99
102 static QFont scaledFont(const QFont &base);
103
106 static QFont fontFromName(const QString &name);
108 static QString displayName(const QFont &font);
111 static QFont guaranteeMonospace(const QFont &font);
118 static QFont themeFont(const QString &fontName, bool monospace);
125 static QFont preferenceMonospaceFont(const QFont &font);
126
127 // --- Mutators & queries ---
128
139 void setRegularFont(const QString &fontName);
140
150 void setMonospaceFont(const QString &fontName);
151
152 void zoomIn();
153 void zoomOut();
154 void resetZoom();
159 void setZoomLevel(int level);
160
167 void restoreZoomLevel();
168 int zoomLevel() const { return zoom_level_; }
169
171 static constexpr int MinZoomLevel = -10;
172 static constexpr int MaxZoomLevel = 10;
174 bool canZoomIn() const { return zoom_level_ < MaxZoomLevel; }
176 bool canZoomOut() const { return zoom_level_ > MinZoomLevel; }
177
185
186signals:
194
195protected:
196 explicit FontManager(QObject *parent = nullptr);
197 ~FontManager();
198
199 // Catches two events on qApp.
200 //
201 // QEvent::ThemeChange: Qt sends it after a platform theme change (an OS
202 // font change, but also a light/dark switch). The system general font is
203 // re-read and, when it differs from the last value seen and no theme/pref
204 // overrides regular, adopted as the new base regular font.
205 //
206 // QEvent::ApplicationFontChange: a third-party QApplication::setFont()
207 // call. Distinguishes our own push from it by comparing the now-current
208 // qApp->font() against applicationFont() (the value we would push): when
209 // they match the change is ours and is ignored; when they differ the
210 // font is adopted (unless a theme/pref overrides regular).
211 bool eventFilter(QObject *watched, QEvent *event) override;
212
213private:
214 static FontManager *instance_;
215 static QMutex mutex_;
216
217 QFont base_regular_;
218 QFont base_monospace_;
219 int zoom_level_ = 0;
220 ZoomScope zoom_scope_;
221
222 // True when a theme or pref explicitly set the regular font; while set,
223 // an external OS font change is NOT adopted (our override wins).
224 bool regular_overridden_ = false;
225
226 // System general font at construction. While base_regular_ equals it,
227 // the captured class fonts are used as they are (see classFont()).
228 QFont base_platform_general_;
229 // System general font as last seen by the QEvent::ThemeChange handler.
230 QFont last_system_general_;
231 // Per-class fonts the platform defined (hash key, font), captured in the
232 // constructor before the first QApplication::setFont(), which discards
233 // them. Only keys the platform actually set are listed.
234 QList<QPair<QByteArray, QFont>> base_class_fonts_;
235
237 void adoptRegularFont(const QFont &font);
238
239 QFont applicationFont() const;
240 QFont zoomed(const QFont &base) const;
242 QFont classFont(const QFont &platformClass) const;
245 void applyApplicationFont(bool force = false);
246 void applyZoom();
247 void syncMonospacePref();
248};
249
250#endif /* FONT_MANAGER_H */
Definition font_manager.h:60
void setZoomLevel(int level)
Definition font_manager.cpp:331
static QString displayName(const QFont &font)
Definition font_manager.cpp:489
static constexpr int MinZoomLevel
Definition font_manager.h:171
static QFont zoomedFont()
Definition font_manager.cpp:238
void restoreZoomLevel()
Definition font_manager.cpp:344
void monospaceFontChanged(QFont font)
bool canZoomOut() const
Definition font_manager.h:176
void reapplyApplicationFonts()
Definition font_manager.cpp:447
static QFont monospaceFont()
Definition font_manager.cpp:233
static QFont themeFont(const QString &fontName, bool monospace)
Definition font_manager.cpp:301
static QFont zoomedMonospaceFont()
Definition font_manager.cpp:244
void setRegularFont(const QString &fontName)
Definition font_manager.cpp:268
ZoomScope
Definition font_manager.h:74
static qreal zoomFactor()
Definition font_manager.cpp:250
bool canZoomIn() const
Definition font_manager.h:174
static QFont guaranteeMonospace(const QFont &font)
Definition font_manager.cpp:495
static QFont fontFromName(const QString &name)
Definition font_manager.cpp:459
void applicationFontChanged(QFont font)
void setMonospaceFont(const QString &fontName)
Definition font_manager.cpp:283
static QFont font()
Definition font_manager.cpp:228
static QFont scaledFont(const QFont &base)
Definition font_manager.cpp:259
static FontManager * instance()
Definition font_manager.cpp:154
void zoomChanged()
static QFont preferenceMonospaceFont(const QFont &font)
Definition font_manager.cpp:310