# kdeui Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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
    00020 #include <qfontdatabase.h>
    00021 #include <qlistbox.h>
    00022 #include <qpainter.h>
    00023
    00024 #include <kcharsets.h>
    00025 #include <kconfig.h>
    00026 #include <kglobal.h>
    00027 #include <kfontdialog.h>
    00028
    00029 #include "kfontcombo.h"
    00030 #include "kfontcombo.moc"
    00031
    00032 struct KFontComboPrivate
    00033 {
    00034     KFontComboPrivate()
    00035         : bold(false),
    00036           italic(false),
    00037           underline(false),
    00038           strikeOut(false),
    00039       modified(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     QString lowerName = family.lower();
    00192     int c = count();
    00193     for(int i = 0; i < c; i++)
    00194     {
    00195        if (text(i).lower() == lowerName)
    00196        {
    00197           setCurrentItem(i);
    00198           d->defaultFamily = text(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->defaultFamily = text(i);
    00213          d->modified = false;
    00214              return;
    00215           }
    00216        }
    00217     }
    00218
    00219     lowerName += " [";
    00220     for(int i = 0; i < c; i++)
    00221     {
    00222        if (text(i).lower().startsWith(lowerName))
    00223        {
    00224           setCurrentItem(i);
    00225           d->defaultFamily = text(i);
    00226       d->modified = false;
    00227           return;
    00228        }
    00229     }
    00230 }
    00231
    00232 void KFontCombo::slotModified( int )
    00233 {
    00234    d->modified = 1;
    00235 }
    00236
    00237 QString KFontCombo::currentFont() const
    00238 {
    00239    if (d->modified)
    00240       return currentText();
    00241    return d->defaultFamily;
    00242 }
    00243
    00244 void KFontCombo::setCurrentItem(int i)
    00245 {
    00246     d->modified = true;
    00247     QComboBox::setCurrentItem(i);
    00248 }
    00249
    00250 void KFontCombo::init()
    00251 {
    00252     d = new KFontComboPrivate;
    00253     d->displayFonts = displayFonts();
    00254     setInsertionPolicy(NoInsertion);
    00255     setAutoCompletion(true);
    00256     setSize(12);
    00257     connect( this, SIGNAL(highlighted(int)), SLOT(slotModified(int)));
    00258 }
    00259
    00260 KFontCombo::~KFontCombo()
    00261 {
    00262     delete d;
    00263 }
    00264
    00265 void KFontCombo::setBold(bool bold)
    00266 {
    00267     if (d->bold == bold)
    00268         return;
    00269     d->bold = bold;
    00270     updateFonts();
    00271 }
    00272
    00273 bool KFontCombo::bold() const
    00274 {
    00275     return d->bold;
    00276 }
    00277
    00278 void KFontCombo::setItalic(bool italic)
    00279 {
    00280     if (d->italic == italic)
    00281         return;
    00282     d->italic = italic;
    00283     updateFonts();
    00284 }
    00285
    00286 bool KFontCombo::italic() const
    00287 {
    00288     return d->italic;
    00289 }
    00290
    00291 void KFontCombo::setUnderline(bool underline)
    00292 {
    00293     if (d->underline == underline)
    00294         return;
    00295     d->underline = underline;
    00296     updateFonts();
    00297 }
    00298
    00299 bool KFontCombo::underline() const
    00300 {
    00301     return d->underline;
    00302 }
    00303
    00304 void KFontCombo::setStrikeOut(bool strikeOut)
    00305 {
    00306     if (d->strikeOut == strikeOut)
    00307         return;
    00308     d->strikeOut = strikeOut;
    00309     updateFonts();
    00310 }
    00311
    00312 bool KFontCombo::strikeOut() const
    00313 {
    00314     return d->strikeOut;
    00315 }
    00316
    00317 void KFontCombo::setSize(int size)
    00318 {
    00319     if (d->size == size)
    00320         return;
    00321     d->size = size;
    00322     QFont f;
    00323     f.setPointSize(size);
    00324     QFontMetrics fm(f);
    00325     d->lineSpacing = fm.lineSpacing();
    00326     updateFonts();
    00327 }
    00328
    00329 int KFontCombo::size() const
    00330 {
    00331     return d->size;
    00332 }
    00333
    00334 void KFontCombo::updateFonts()
    00335 {
    00336     if (!d->displayFonts)
    00337         return;
    00338
    00339     for (unsigned int i = 0; i < listBox()->count(); ++i)
    00340     {
    00341         KFontListItem *item = static_cast<KFontListItem *>(listBox()->item(i));
    00342         item->updateFont();
    00343     }
    00344 }
    00345
    00346 bool KFontCombo::displayFonts()
    00347 {
    00348     KConfigGroupSaver saver(KGlobal::config(), "KDE");
    00349     return KGlobal::config()->readBoolEntry("DisplayFontItems", true);
    00350 }
    00351
    00352 void KFontCombo::virtual_hook( int id, void* data )
    00353 { KComboBox::virtual_hook( id, data ); }
    00354

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:20:17 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003