[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfrelativefont.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * file klfrelativefont.cpp
3 * This file is part of the KLatexFormula Project.
4 * Copyright (C) 2011 by Philippe Faist
5 * philippe.faist at bluewin.ch
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22/* $Id$ */
23
24#include <QEvent>
25
26#include "klfrelativefont.h"
27
28
30 : QObject(parent), pReference(parent), pTarget(parent), pInhibitFontChangeRecursion(false),
31 pHasAppliedFont(false), pThorough(false)
32{
33 klfDbg("constructor. Parent="<<parent) ;
35}
36
37KLFRelativeFontBase::KLFRelativeFontBase(QWidget *ref, QWidget *target)
38 : QObject(target), pReference(ref), pTarget(target), pInhibitFontChangeRecursion(false),
39 pHasAppliedFont(false), pThorough(false)
40{
41 klfDbg("constructor. Ref="<<ref<<", tgt="<<target) ;
42 ref->installEventFilter(this);
43 if (ref != target)
44 target->installEventFilter(this);
45}
46
48{
49}
50
52{
53 pThorough = thorough;
54}
55
56
58{
60
61 QWidget *ref = referenceWidget();
62 QWidget *target = targetWidget();
63
64 klfDbg("eventFilter("<<object<<",ev.type="<<event->type()<<"), ref="<<ref<<", tgt="<<target) ;
65
66 // // Don't care about ApplicationFontChange, a FontChange event is generated anyway.
67 // if (event->type() == QEvent::ApplicationFontChange) {
68 // klfDbg("FYI: application font change!!") ;
69 // return false; // a FontChange event is generated anyway
70 // }
71
72 if (object == ref) {
73 if (event->type() == QEvent::FontChange) {
74 klfDbg("event filter, font change event! object="<<object<<", event/type="<<event->type()
75 <<", refWidget="<<ref<<", targetWidget="<<target) ;
76 if (pInhibitFontChangeRecursion) {
77 klfDbg("inhibited `font change' event recursion.");
78 pInhibitFontChangeRecursion = false;
79 return false;
80 }
81
82 calculateAndApplyNewFont();
83 return false;
84 }
85 }
86 if (object == target) {
87 if (event->type() == QEvent::Show) {
88 if (!pHasAppliedFont)
89 calculateAndApplyNewFont();
90 return false;
91 }
92 }
93 return false; // never eat an event
94}
95
96
97static void set_property_children(QObject *object, const char *inherits, const char *propName,
98 const QVariant& value)
99{
100 bool wantinherits = (inherits != NULL && *inherits != '\0') ;
101 QObjectList children = object->children();
102 Q_FOREACH(QObject *obj, children) {
103 if (!wantinherits || obj->inherits(inherits)) {
104 QString dontchangepropname = QString("klfDontChange_") + propName;
105 QVariant v = obj->property(dontchangepropname.toLatin1().constData());
106 if (!v.isValid() || !v.toBool()) {
107 obj->setProperty(propName, value);
108 set_property_children(obj, inherits, propName, value);
109 }
110 }
111 }
112}
113
114
115void KLFRelativeFontBase::calculateAndApplyNewFont()
116{
117 QWidget *ref = referenceWidget();
118 QWidget *target = targetWidget();
119 QFont f = calculateRelativeFont(ref->font());
120 klfDbg("Applying font "<<f<<" calculated from base font "<<ref->font()) ;
121 if (ref == target) {
122 // Set this flag to `true' so that the generated font change event is not picked up.
123 pInhibitFontChangeRecursion = true;
124 }
125 target->setFont(f);
126 if (pThorough)
127 set_property_children(target, "QWidget", "font", QVariant(f));
128
129 pHasAppliedFont = true;
130}
131
132
133
134// -----------
135
136
138 : KLFRelativeFontBase(parent)
139{
140 rfinit();
141}
142KLFRelativeFont::KLFRelativeFont(QWidget *ref, QWidget *t)
143 : KLFRelativeFontBase(ref, t)
144{
145 rfinit();
146}
147
148void KLFRelativeFont::rfinit()
149{
151 pRelPointSize = 0;
152 klfDbg("...");
153 pForceFamily = QString();
154 klfDbg("...");
155 pForceWeight = -1;
156 klfDbg("...");
157 pForceStyle = -1;
158}
159
161{
162}
163
165{
166 pRelPointSize = relps;
167}
169{
170 pForceFamily = family;
171}
173{
174 pForceWeight = w;
175}
177{
178 pForceStyle = style;
179}
180
182{
184
185 QFont f = baseFont;
186 if (!pForceFamily.isEmpty())
187 f.setFamily(pForceFamily);
188 if (pForceWeight >= 0)
189 f.setWeight(pForceWeight);
190 if (pForceStyle >= 0)
191 f.setStyle((QFont::Style)pForceStyle);
192 if (pRelPointSize != 0)
193 f.setPointSize(QFontInfo(f).pointSize() + pRelPointSize);
194
195 return f;
196}
197
KLFRelativeFontBase(QWidget *parent)
virtual QFont calculateRelativeFont(const QFont &baseFont)=0
bool eventFilter(QObject *object, QEvent *event)
void setThorough(bool thorough)
QWidget * referenceWidget()
void setForceWeight(int weight)
void setRelPointSize(int relps)
KLFRelativeFont(QWidget *parent)
virtual QFont calculateRelativeFont(const QFont &baseFont)
void setForceStyle(int style)
void setForceFamily(const QString &family)
const char * style
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
Definition klfdebug.h:152
#define KLF_FUNC_NAME
Definition klfdebug.h:194
#define klfDbg(streamableItems)
print debug stream items
Definition klfdebug.h:158
const char * constData() const
void setFamily(const QString &family)
void setPointSize(int pointSize)
void setStyle(Style style)
void setWeight(int weight)
virtual bool event(QEvent *e)
bool inherits(const char *className) const
void installEventFilter(QObject *filterObj)
QObject * parent() const
QVariant property(const char *name) const
bool setProperty(const char *name, const QVariant &value)
bool isEmpty() const
QByteArray toLatin1() const
bool isValid() const
bool toBool() const

Generated by doxygen 1.9.7