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.16 2003/06/05 14:02:41 bmeyer 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       modified(false),
00041           size(0),
00042           lineSpacing(0)
00043     {
00044     };
00045 
00046     bool bold : 1;
00047     bool italic : 1;
00048     bool underline : 1;
00049     bool strikeOut : 1;
00050     bool displayFonts : 1;
00051     bool modified : 1;
00052     int size;
00053     int lineSpacing;
00054     QString defaultFamily;
00055 };
00056 
00057 class KFontListItem : public QListBoxItem
00058 {
00059 public:
00060     KFontListItem(const QString &fontName, KFontCombo *combo);
00061     virtual ~KFontListItem();
00062 
00063     virtual int width(const QListBox *) const;
00064     virtual int height(const QListBox *) const;
00065 
00066     void updateFont();
00067 
00068 protected:
00069     virtual void paint(QPainter *p);
00070 
00071 private:
00072     void createFont();
00073 
00074 private:
00075     KFontCombo *m_combo;
00076     QString m_fontName;
00077     QFont *m_font;
00078     bool m_canPaintName;
00079 };
00080 
00081 KFontListItem::KFontListItem(const QString &fontName, KFontCombo *combo)
00082     : QListBoxItem(combo->listBox()),
00083       m_combo(combo),
00084       m_fontName(fontName),
00085       m_font(0),
00086       m_canPaintName(true)
00087 {
00088     setText(fontName);
00089 }
00090 
00091 KFontListItem::~KFontListItem()
00092 {
00093     delete m_font;
00094 }
00095 
00096 int KFontListItem::width(const QListBox *lb) const
00097 {
00098     if (m_font)
00099        return QFontMetrics(*m_font).width(text()) + 6;
00100     return lb->fontMetrics().width(text()) + 6;
00101 }
00102 
00103 int KFontListItem::height(const QListBox *lb) const
00104 {
00105     if (m_combo->d->displayFonts)
00106         return m_combo->d->lineSpacing + 2;
00107     QFontMetrics fm(lb->fontMetrics());
00108     return fm.lineSpacing() + 2;
00109 }
00110 
00111 void KFontListItem::paint(QPainter *p)
00112 {
00113     if (m_combo->d->displayFonts)
00114     {
00115         if (!m_font)
00116             createFont();
00117 
00118         QString t = m_fontName;
00119         if (p->device() != m_combo)
00120         {
00121             if (m_canPaintName)
00122                 p->setFont(*m_font);
00123             else
00124                 t = QString::fromLatin1("(%1)").arg(m_fontName);
00125         }
00126         QFontMetrics fm(p->fontMetrics());
00127         p->drawText(3, (m_combo->d->lineSpacing + fm.ascent() + fm.leading() / 2) / 2, t);
00128     }
00129     else
00130     {
00131         QFontMetrics fm(p->fontMetrics());
00132         p->drawText(3, fm.ascent() + fm.leading() / 2, m_fontName);
00133     }
00134 }
00135 
00136 void KFontListItem::updateFont()
00137 {
00138     if (!m_font)
00139         return;
00140 
00141     m_font->setBold(m_combo->d->bold);
00142     m_font->setItalic(m_combo->d->italic);
00143     m_font->setUnderline(m_combo->d->underline);
00144     m_font->setStrikeOut(m_combo->d->strikeOut);
00145     m_font->setPointSize(m_combo->d->size);
00146 }
00147 
00148 void KFontListItem::createFont()
00149 {
00150     if (m_font)
00151         return;
00152 
00153     m_font = new QFont(m_fontName);
00154     QFontMetrics fm(*m_font);
00155     for (unsigned int i = 0; i < m_fontName.length(); ++i)
00156         if (!fm.inFont(m_fontName[i]))
00157         {
00158             m_canPaintName = false;
00159             break;
00160         }
00161     updateFont();
00162 }
00163 
00164 KFontCombo::KFontCombo(QWidget *parent, const char *name)
00165     : KComboBox(true, parent, name)
00166 {
00167     init();
00168     QStringList families;
00169     KFontChooser::getFontList(families, 0);
00170     setFonts(families);
00171 }
00172 
00173 KFontCombo::KFontCombo(const QStringList &fonts, QWidget *parent, const char *name)
00174     : KComboBox(true, parent, name)
00175 {
00176     init();
00177     setFonts(fonts);
00178 }
00179 
00180 void KFontCombo::setFonts(const QStringList &fonts)
00181 {
00182     clear();
00183     for (QStringList::ConstIterator it = fonts.begin(); it != fonts.end(); ++it)
00184         new KFontListItem(*it, this);
00185 }
00186 
00187 /*
00188  * Maintenance note: Keep in sync with KFontAction::setFont()
00189  */
00190 void KFontCombo::setCurrentFont(const QString &family)
00191 {
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->defaultFamily = text(i);
00200       d->modified = false;
00201           return;
00202        }
00203     }
00204     int x = lowerName.find(" [");
00205     if (x>-1)
00206     {
00207        lowerName = lowerName.left(x);
00208        for(int i = 0; i < c; i++)
00209        {
00210           if (text(i).lower() == lowerName)
00211           {
00212              setCurrentItem(i);
00213              d->defaultFamily = text(i);
00214          d->modified = false;
00215              return;
00216           }
00217        }
00218     }
00219 
00220     lowerName += " [";
00221     for(int i = 0; i < c; i++)
00222     {
00223        if (text(i).lower().startsWith(lowerName))
00224        {
00225           setCurrentItem(i);
00226           d->defaultFamily = text(i);
00227       d->modified = false;
00228           return;
00229        }
00230     }
00231 }
00232 
00233 QString KFontCombo::currentFont() const
00234 {
00235    if (d->modified)
00236       return currentText();
00237    return d->defaultFamily;
00238 }
00239 
00240 void KFontCombo::setCurrentItem(int i)
00241 {
00242     d->modified = true;
00243     QComboBox::setCurrentItem(i);
00244 }
00245 
00246 void KFontCombo::init()
00247 {
00248     d = new KFontComboPrivate;
00249     d->displayFonts = displayFonts();
00250     setInsertionPolicy(NoInsertion);
00251     setAutoCompletion(true);
00252     setSize(12);
00253 }
00254 
00255 KFontCombo::~KFontCombo()
00256 {
00257     delete d;
00258 }
00259 
00260 void KFontCombo::setBold(bool bold)
00261 {
00262     if (d->bold == bold)
00263         return;
00264     d->bold = bold;
00265     updateFonts();
00266 }
00267 
00268 bool KFontCombo::bold() const
00269 {
00270     return d->bold;
00271 }
00272 
00273 void KFontCombo::setItalic(bool italic)
00274 {
00275     if (d->italic == italic)
00276         return;
00277     d->italic = italic;
00278     updateFonts();
00279 }
00280 
00281 bool KFontCombo::italic() const
00282 {
00283     return d->italic;
00284 }
00285 
00286 void KFontCombo::setUnderline(bool underline)
00287 {
00288     if (d->underline == underline)
00289         return;
00290     d->underline = underline;
00291     updateFonts();
00292 }
00293 
00294 bool KFontCombo::underline() const
00295 {
00296     return d->underline;
00297 }
00298 
00299 void KFontCombo::setStrikeOut(bool strikeOut)
00300 {
00301     if (d->strikeOut == strikeOut)
00302         return;
00303     d->strikeOut = strikeOut;
00304     updateFonts();
00305 }
00306 
00307 bool KFontCombo::strikeOut() const
00308 {
00309     return d->strikeOut;
00310 }
00311 
00312 void KFontCombo::setSize(int size)
00313 {
00314     if (d->size == size)
00315         return;
00316     d->size = size;
00317     QFont f;
00318     f.setPointSize(size);
00319     QFontMetrics fm(f);
00320     d->lineSpacing = fm.lineSpacing();
00321     updateFonts();
00322 }
00323 
00324 int KFontCombo::size() const
00325 {
00326     return d->size;
00327 }
00328 
00329 void KFontCombo::updateFonts()
00330 {
00331     if (!d->displayFonts)
00332         return;
00333 
00334     for (unsigned int i = 0; i < listBox()->count(); ++i)
00335     {
00336         KFontListItem *item = static_cast<KFontListItem *>(listBox()->item(i));
00337         item->updateFont();
00338     }
00339 }
00340 
00341 bool KFontCombo::displayFonts()
00342 {
00343     KConfigGroupSaver saver(KGlobal::config(), "KDE");
00344     return KGlobal::config()->readBoolEntry("DisplayFontItems", true);
00345 }
00346 
00347 void KFontCombo::virtual_hook( int id, void* data )
00348 { KComboBox::virtual_hook( id, data ); }
00349 
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 3 19:23:10 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003