[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfrelativefont.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfrelativefont.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
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   //  // Don't care about ApplicationFontChange, a FontChange event is generated anyway.
00067   //  if (event->type() == QEvent::ApplicationFontChange) {
00068   //    klfDbg("FYI: application font change!!") ;
00069   //    return false; // a FontChange event is generated anyway
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; // never eat an event
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     // Set this flag to `true' so that the generated font change event is not picked up.
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 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net