Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QEvent>
00025
00026 #include "klfrelativefont.h"
00027
00028
00029 KLFRelativeFontBase::KLFRelativeFontBase(QWidget *parent)
00030 : QObject(parent), pReference(parent), pTarget(parent), pInhibitFontChangeRecursion(false),
00031 pHasAppliedFont(false), pThorough(false)
00032 {
00033 klfDbg("constructor. Parent="<<parent) ;
00034 parent->installEventFilter(this);
00035 }
00036
00037 KLFRelativeFontBase::KLFRelativeFontBase(QWidget *ref, QWidget *target)
00038 : QObject(target), pReference(ref), pTarget(target), pInhibitFontChangeRecursion(false),
00039 pHasAppliedFont(false), pThorough(false)
00040 {
00041 klfDbg("constructor. Ref="<<ref<<", tgt="<<target) ;
00042 ref->installEventFilter(this);
00043 if (ref != target)
00044 target->installEventFilter(this);
00045 }
00046
00047 KLFRelativeFontBase::~KLFRelativeFontBase()
00048 {
00049 }
00050
00051 void KLFRelativeFontBase::setThorough(bool thorough)
00052 {
00053 pThorough = thorough;
00054 }
00055
00056
00057 bool KLFRelativeFontBase::eventFilter(QObject *object, QEvent *event)
00058 {
00059 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00060
00061 QWidget *ref = referenceWidget();
00062 QWidget *target = targetWidget();
00063
00064 klfDbg("eventFilter("<<object<<",ev.type="<<event->type()<<"), ref="<<ref<<", tgt="<<target) ;
00065
00066
00067
00068
00069
00070
00071
00072 if (object == ref) {
00073 if (event->type() == QEvent::FontChange) {
00074 klfDbg("event filter, font change event! object="<<object<<", event/type="<<event->type()
00075 <<", refWidget="<<ref<<", targetWidget="<<target) ;
00076 if (pInhibitFontChangeRecursion) {
00077 klfDbg("inhibited `font change' event recursion.");
00078 pInhibitFontChangeRecursion = false;
00079 return false;
00080 }
00081
00082 calculateAndApplyNewFont();
00083 return false;
00084 }
00085 }
00086 if (object == target) {
00087 if (event->type() == QEvent::Show) {
00088 if (!pHasAppliedFont)
00089 calculateAndApplyNewFont();
00090 return false;
00091 }
00092 }
00093 return false;
00094 }
00095
00096
00097 static void set_property_children(QObject *object, const char *inherits, const char *propName,
00098 const QVariant& value)
00099 {
00100 bool wantinherits = (inherits != NULL && *inherits != '\0') ;
00101 QObjectList children = object->children();
00102 Q_FOREACH(QObject *obj, children) {
00103 if (!wantinherits || obj->inherits(inherits)) {
00104 QString dontchangepropname = QString("klfDontChange_") + propName;
00105 QVariant v = obj->property(dontchangepropname.toLatin1().constData());
00106 if (!v.isValid() || !v.toBool()) {
00107 obj->setProperty(propName, value);
00108 set_property_children(obj, inherits, propName, value);
00109 }
00110 }
00111 }
00112 }
00113
00114
00115 void KLFRelativeFontBase::calculateAndApplyNewFont()
00116 {
00117 QWidget *ref = referenceWidget();
00118 QWidget *target = targetWidget();
00119 QFont f = calculateRelativeFont(ref->font());
00120 klfDbg("Applying font "<<f<<" calculated from base font "<<ref->font()) ;
00121 if (ref == target) {
00122
00123 pInhibitFontChangeRecursion = true;
00124 }
00125 target->setFont(f);
00126 if (pThorough)
00127 set_property_children(target, "QWidget", "font", QVariant(f));
00128
00129 pHasAppliedFont = true;
00130 }
00131
00132
00133
00134
00135
00136
00137 KLFRelativeFont::KLFRelativeFont(QWidget *parent)
00138 : KLFRelativeFontBase(parent)
00139 {
00140 rfinit();
00141 }
00142 KLFRelativeFont::KLFRelativeFont(QWidget *ref, QWidget *t)
00143 : KLFRelativeFontBase(ref, t)
00144 {
00145 rfinit();
00146 }
00147
00148 void KLFRelativeFont::rfinit()
00149 {
00150 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00151 pRelPointSize = 0;
00152 klfDbg("...");
00153 pForceFamily = QString();
00154 klfDbg("...");
00155 pForceWeight = -1;
00156 klfDbg("...");
00157 pForceStyle = -1;
00158 }
00159
00160 KLFRelativeFont::~KLFRelativeFont()
00161 {
00162 }
00163
00164 void KLFRelativeFont::setRelPointSize(int relps)
00165 {
00166 pRelPointSize = relps;
00167 }
00168 void KLFRelativeFont::setForceFamily(const QString& family)
00169 {
00170 pForceFamily = family;
00171 }
00172 void KLFRelativeFont::setForceWeight(int w)
00173 {
00174 pForceWeight = w;
00175 }
00176 void KLFRelativeFont::setForceStyle(int style)
00177 {
00178 pForceStyle = style;
00179 }
00180
00181 QFont KLFRelativeFont::calculateRelativeFont(const QFont& baseFont)
00182 {
00183 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00184
00185 QFont f = baseFont;
00186 if (!pForceFamily.isEmpty())
00187 f.setFamily(pForceFamily);
00188 if (pForceWeight >= 0)
00189 f.setWeight(pForceWeight);
00190 if (pForceStyle >= 0)
00191 f.setStyle((QFont::Style)pForceStyle);
00192 if (pRelPointSize != 0)
00193 f.setPointSize(QFontInfo(f).pointSize() + pRelPointSize);
00194
00195 return f;
00196 }
00197