kdeui Library API Documentation

kfontcombo.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (c) 2001 Malte Starostik <malte@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 // $Id: kfontcombo.cpp,v 1.14.4.1 2003/07/15 10:42:17 mueller Exp $
00020 
00021 #include <qfontdatabase.h>
00022 #include <qlistbox.h>
00023 #include <qpainter.h>
00024 
00025 #include <kcharsets.h>
00026 #include <kconfig.h>
00027 #include <kglobal.h>
00028 #include <kfontdialog.h>
00029 
00030 #include "kfontcombo.h"
00031 #include "kfontcombo.moc"
00032 
00033 struct KFontComboPrivate
00034 {
00035     KFontComboPrivate()
00036         : bold(false),
00037           italic(false),
00038           underline(false),
00039           strikeOut(false),
00040           size(0),
00041           lineSpacing(0)
00042     {
00043     };
00044 
00045     bool bold : 1;
00046     bool italic : 1;
00047     bool underline : 1;
00048     bool strikeOut : 1;
00049     bool displayFonts : 1;
00050     bool modified : 1;
00051     int size;
00052     int lineSpacing;
00053     QString defaultFamily;
00054 };
00055 
00056 class KFontListItem : public QListBoxItem
00057 {
00058 public:
00059     KFontListItem(const QString &fontName, KFontCombo *combo);
00060     virtual ~KFontListItem();
00061 
00062     virtual int width(const QListBox *) const;
00063     virtual int height(const QListBox *) const;
00064 
00065     void updateFont();
00066 
00067 protected:
00068     virtual void paint(QPainter *p);
00069 
00070 private:
00071     void createFont();
00072 
00073 private:
00074     KFontCombo *m_combo;
00075     QString m_fontName;
00076     QFont *m_font;
00077     bool m_canPaintName;
00078 };
00079 
00080 KFontListItem::KFontListItem(const QString &fontName, KFontCombo *combo)
00081     : QListBoxItem(combo->listBox()),
00082       m_combo(combo),
00083       m_fontName(fontName),
00084       m_font(0),
00085       m_canPaintName(true)
00086 {
00087     setText(fontName);
00088 }
00089 
00090 KFontListItem::~KFontListItem()
00091 {
00092     delete m_font;
00093 }
00094 
00095 int KFontListItem::width(const QListBox *lb) const
00096 {
00097     if (m_font)
00098        return QFontMetrics(*m_font).width(text()) + 6;
00099     return lb->fontMetrics().width(text()) + 6;
00100 }
00101 
00102 int KFontListItem::height(const QListBox *lb) const
00103 {
00104     if (m_combo->d->displayFonts)
00105         return m_combo->d->lineSpacing + 2;
00106     QFontMetrics fm(lb->fontMetrics());
00107     return fm.lineSpacing() + 2;
00108 }
00109 
00110 void KFontListItem::paint(QPainter *p)
00111 {
00112     if (m_combo->d->displayFonts)
00113     {
00114         if (!m_font)
00115             createFont();
00116 
00117         QString t = m_fontName;
00118         if (p->device() != m_combo)
00119         {
00120             if (m_canPaintName)
00121                 p->setFont(*m_font);
00122             else
00123                 t = QString::fromLatin1("(%1)").arg(m_fontName);
00124         }
00125         QFontMetrics fm(p->fontMetrics());
00126         p->drawText(3, (m_combo->d->lineSpacing + fm.ascent() + fm.leading() / 2) / 2, t);
00127     }
00128     else
00129     {
00130         QFontMetrics fm(p->fontMetrics());
00131         p->drawText(3, fm.ascent() + fm.leading() / 2, m_fontName);
00132     }
00133 }
00134 
00135 void KFontListItem::updateFont()
00136 {
00137     if (!m_font)
00138         return;
00139 
00140     m_font->setBold(m_combo->d->bold);
00141     m_font->setItalic(m_combo->d->italic);
00142     m_font->setUnderline(m_combo->d->underline);
00143     m_font->setStrikeOut(m_combo->d->strikeOut);
00144     m_font->setPointSize(m_combo->d->size);
00145 }
00146 
00147 void KFontListItem::createFont()
00148 {
00149     if (m_font)
00150         return;
00151 
00152     m_font = new QFont(m_fontName);
00153     QFontMetrics fm(*m_font);
00154     for (unsigned int i = 0; i < m_fontName.length(); ++i)
00155         if (!fm.inFont(m_fontName[i]))
00156         {
00157             m_canPaintName = false;
00158             break;
00159         }
00160     updateFont();
00161 }
00162 
00163 KFontCombo::KFontCombo(QWidget *parent, const char *name)
00164     : KComboBox(true, parent, name)
00165 {
00166     init();
00167     QStringList families;
00168     KFontChooser::getFontList(families, 0);
00169     setFonts(families);
00170 }
00171 
00172 KFontCombo::KFontCombo(const QStringList &fonts, QWidget *parent, const char *name)
00173     : KComboBox(true, parent, name)
00174 {
00175     init();
00176     setFonts(fonts);
00177 }
00178 
00179 void KFontCombo::setFonts(const QStringList &fonts)
00180 {
00181     clear();
00182     for (QStringList::ConstIterator it = fonts.begin(); it != fonts.end(); ++it)
00183         new KFontListItem(*it, this);
00184 }
00185 
00186 /*
00187  * Maintenance note: Keep in sync with KFontAction::setFont()
00188  */
00189 void KFontCombo::setCurrentFont(const QString &family)
00190 {
00191     d->defaultFamily = family;
00192     QString lowerName = family.lower();
00193     int c = count();
00194     for(int i = 0; i < c; i++)
00195     {
00196        if (text(i).lower() == lowerName)
00197        {
00198           setCurrentItem(i);
00199           d->modified = false;
00200           return;
00201        }
00202     }
00203     int x = lowerName.find(" [");
00204     if (x>-1)
00205     {
00206        lowerName = lowerName.left(x);
00207        for(int i = 0; i < c; i++)
00208        {
00209           if (text(i).lower() == lowerName)
00210           {
00211              setCurrentItem(i);
00212              d->modified = false;
00213              return;
00214           }
00215        }
00216     }
00217 
00218     lowerName += " [";
00219     for(int i = 0; i < c; i++)
00220     {
00221        if (text(i).lower().startsWith(lowerName))
00222        {
00223           setCurrentItem(i);
00224           d->modified = false;
00225           return;
00226        }
00227     }
00228 }
00229 
00230 QString KFontCombo::currentFont() const
00231 {
00232    if (d->modified)
00233       return currentText();
00234    return d->defaultFamily;
00235 }
00236 
00237 void KFontCombo::setCurrentItem(int i)
00238 {
00239     d->modified = true;
00240     QComboBox::setCurrentItem(i);
00241 }
00242 
00243 void KFontCombo::init()
00244 {
00245     d = new KFontComboPrivate;
00246     d->displayFonts = displayFonts();
00247     setInsertionPolicy(NoInsertion);
00248     setAutoCompletion(true);
00249     setSize(12);
00250 }
00251 
00252 KFontCombo::~KFontCombo()
00253 {
00254     delete d;
00255 }
00256 
00257 void KFontCombo::setBold(bool bold)
00258 {
00259     if (d->bold == bold)
00260         return;
00261     d->bold = bold;
00262     updateFonts();
00263 }
00264 
00265 bool KFontCombo::bold() const
00266 {
00267     return d->bold;
00268 }
00269 
00270 void KFontCombo::setItalic(bool italic)
00271 {
00272     if (d->italic == italic)
00273         return;
00274     d->italic = italic;
00275     updateFonts();
00276 }
00277 
00278 bool KFontCombo::italic() const
00279 {
00280     return d->italic;
00281 }
00282 
00283 void KFontCombo::setUnderline(bool underline)
00284 {
00285     if (d->underline == underline)
00286         return;
00287     d->underline = underline;
00288     updateFonts();
00289 }
00290 
00291 bool KFontCombo::underline() const
00292 {
00293     return d->underline;
00294 }
00295 
00296 void KFontCombo::setStrikeOut(bool strikeOut)
00297 {
00298     if (d->strikeOut == strikeOut)
00299         return;
00300     d->strikeOut = strikeOut;
00301     updateFonts();
00302 }
00303 
00304 bool KFontCombo::strikeOut() const
00305 {
00306     return d->strikeOut;
00307 }
00308 
00309 void KFontCombo::setSize(int size)
00310 {
00311     if (d->size == size)
00312         return;
00313     d->size = size;
00314     QFont f;
00315     f.setPointSize(size);
00316     QFontMetrics fm(f);
00317     d->lineSpacing = fm.lineSpacing();
00318     updateFonts();
00319 }
00320 
00321 int KFontCombo::size() const
00322 {
00323     return d->size;
00324 }
00325 
00326 void KFontCombo::updateFonts()
00327 {
00328     if (!d->displayFonts)
00329         return;
00330 
00331     for (unsigned int i = 0; i < listBox()->count(); ++i)
00332     {
00333         KFontListItem *item = static_cast<KFontListItem *>(listBox()->item(i));
00334         item->updateFont();
00335     }
00336 }
00337 
00338 bool KFontCombo::displayFonts()
00339 {
00340     KConfigGroupSaver saver(KGlobal::config(), "KDE");
00341     return KGlobal::config()->readBoolEntry("DisplayFontItems", true);
00342 }
00343 
00344 void KFontCombo::virtual_hook( int id, void* data )
00345 { KComboBox::virtual_hook( id, data ); }
00346 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:56:48 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001