kdeui Library API Documentation

kfontdialog.cpp

00001 /*
00002     $Id: kfontdialog.cpp,v 1.95.2.1 2003/07/17 16:14:06 waba Exp $
00003 
00004     Requires the Qt widget libraries, available at no cost at
00005     http://www.troll.no
00006 
00007     Copyright (C) 1996 Bernd Johannes Wuebben  <wuebben@kde.org>
00008     Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00009     Copyright (c) 1999 Mario Weilguni <mweilguni@kde.org>
00010 
00011     This library is free software; you can redistribute it and/or
00012     modify it under the terms of the GNU Library General Public
00013     License as published by the Free Software Foundation; either
00014     version 2 of the License, or (at your option) any later version.
00015 
00016     This library is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019     Library General Public License for more details.
00020 
00021     You should have received a copy of the GNU Library General Public License
00022     along with this library; see the file COPYING.LIB.  If not, write to
00023     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00024     Boston, MA 02111-1307, USA.
00025 */
00026 
00027 #include <config.h>
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 
00032 #include <qcombobox.h>
00033 #include <qcheckbox.h>
00034 #include <qfile.h>
00035 #include <qfont.h>
00036 #include <qgroupbox.h>
00037 #include <qlabel.h>
00038 #include <qlayout.h>
00039 #include <qscrollbar.h>
00040 #include <qstringlist.h>
00041 #include <qfontdatabase.h>
00042 #include <qwhatsthis.h>
00043 #include <qtooltip.h>
00044 
00045 #include <kapplication.h>
00046 #include <kcharsets.h>
00047 #include <kconfig.h>
00048 #include <kdialog.h>
00049 #include <kglobal.h>
00050 #include <kglobalsettings.h>
00051 #include <qlineedit.h>
00052 #include <klistbox.h>
00053 #include <klocale.h>
00054 #include <kstandarddirs.h>
00055 #include <kdebug.h>
00056 #include <knuminput.h>
00057 #ifdef Q_WS_X11
00058 #include <X11/Xlib.h>
00059 #endif
00060 
00061 #include "kfontdialog.moc"
00062 
00063 static int minimumListWidth( const QListBox *list )
00064 {
00065   int w=0;
00066   for( uint i=0; i<list->count(); i++ )
00067   {
00068     int itemWidth = list->item(i)->width(list);
00069     w = QMAX(w,itemWidth);
00070   }
00071   if( w == 0 ) { w = 40; }
00072   w += list->frameWidth() * 2;
00073   w += list->verticalScrollBar()->sizeHint().width();
00074   return( w );
00075 }
00076 
00077 static int minimumListHeight( const QListBox *list, int numVisibleEntry )
00078 {
00079   int w = list->count() > 0 ? list->item(0)->height(list) :
00080     list->fontMetrics().lineSpacing();
00081 
00082   if( w < 0 ) { w = 10; }
00083   if( numVisibleEntry <= 0 ) { numVisibleEntry = 4; }
00084   return( w * numVisibleEntry + 2 * list->frameWidth() );
00085 }
00086 
00087 class KFontChooser::KFontChooserPrivate
00088 {
00089 public:
00090     KFontChooserPrivate()
00091         { m_palette.setColor(QPalette::Active, QColorGroup::Text, Qt::black);
00092           m_palette.setColor(QPalette::Active, QColorGroup::Base, Qt::white); }
00093     QPalette m_palette;
00094 };
00095 
00096 KFontChooser::KFontChooser(QWidget *parent, const char *name,
00097                            bool onlyFixed, const QStringList &fontList,
00098                            bool makeFrame, int visibleListSize, bool diff,
00099                            QButton::ToggleState *sizeIsRelativeState )
00100   : QWidget(parent, name), usingFixed(onlyFixed)
00101 {
00102   charsetsCombo = 0;
00103 
00104   QString mainWhatsThisText =
00105     i18n( "Here you can choose the font to be used." );
00106   QWhatsThis::add( this, mainWhatsThisText );
00107 
00108   d = new KFontChooserPrivate;
00109   QVBoxLayout *topLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00110   int checkBoxGap = KDialog::spacingHint() / 2;
00111 
00112   QWidget *page;
00113   QGridLayout *gridLayout;
00114   int row = 0;
00115   if( makeFrame == true )
00116   {
00117     page = new QGroupBox( i18n("Requested Font"), this );
00118     topLayout->addWidget(page);
00119     gridLayout = new QGridLayout( page, 5, 3, KDialog::marginHint(), KDialog::spacingHint() );
00120     gridLayout->addRowSpacing( 0, fontMetrics().lineSpacing() );
00121     row = 1;
00122   }
00123   else
00124   {
00125     page = new QWidget( this );
00126     topLayout->addWidget(page);
00127     gridLayout = new QGridLayout( page, 4, 3, 0, KDialog::spacingHint() );
00128   }
00129 
00130   //
00131   // first, create the labels across the top
00132   //
00133   QHBoxLayout *familyLayout = new QHBoxLayout();
00134   familyLayout->addSpacing( checkBoxGap );
00135   if (diff) {
00136     familyCheckbox = new QCheckBox(i18n("Font"), page);
00137     connect(familyCheckbox, SIGNAL(toggled(bool)), SLOT(toggled_checkbox()));
00138     familyLayout->addWidget(familyCheckbox, 0, Qt::AlignLeft);
00139     QString familyCBToolTipText =
00140       i18n("Change font family?");
00141     QString familyCBWhatsThisText =
00142       i18n("Enable this checkbox to change the font family settings.");
00143     QWhatsThis::add( familyCheckbox, familyCBWhatsThisText );
00144     QToolTip::add(   familyCheckbox, familyCBToolTipText );
00145     familyLabel = 0;
00146   } else {
00147     familyCheckbox = 0;
00148     familyLabel = new QLabel( i18n("Font:"), page, "familyLabel" );
00149     familyLayout->addWidget(familyLabel, 1, Qt::AlignLeft);
00150   }
00151   gridLayout->addLayout(familyLayout, row, 0 );
00152 
00153   QHBoxLayout *styleLayout = new QHBoxLayout();
00154   if (diff) {
00155      styleCheckbox = new QCheckBox(i18n("Font style"), page);
00156      connect(styleCheckbox, SIGNAL(toggled(bool)), SLOT(toggled_checkbox()));
00157      styleLayout->addWidget(styleCheckbox, 0, Qt::AlignLeft);
00158     QString styleCBToolTipText =
00159       i18n("Change font style?");
00160     QString styleCBWhatsThisText =
00161       i18n("Enable this checkbox to change the font style settings.");
00162     QWhatsThis::add( styleCheckbox, styleCBWhatsThisText );
00163     QToolTip::add(   styleCheckbox, styleCBToolTipText );
00164     styleLabel = 0;
00165   } else {
00166     styleCheckbox = 0;
00167     styleLabel = new QLabel( i18n("Font style:"), page, "styleLabel");
00168     styleLayout->addWidget(styleLabel, 1, Qt::AlignLeft);
00169   }
00170   styleLayout->addSpacing( checkBoxGap );
00171   gridLayout->addLayout(styleLayout, row, 1 );
00172 
00173   QHBoxLayout *sizeLayout = new QHBoxLayout();
00174   if (diff) {
00175     sizeCheckbox = new QCheckBox(i18n("Size"),page);
00176     connect(sizeCheckbox, SIGNAL(toggled(bool)), SLOT(toggled_checkbox()));
00177     sizeLayout->addWidget(sizeCheckbox, 0, Qt::AlignLeft);
00178     QString sizeCBToolTipText =
00179       i18n("Change font size?");
00180     QString sizeCBWhatsThisText =
00181       i18n("Enable this checkbox to change the font size settings.");
00182     QWhatsThis::add( sizeCheckbox, sizeCBWhatsThisText );
00183     QToolTip::add(   sizeCheckbox, sizeCBToolTipText );
00184     sizeLabel = 0;
00185   } else {
00186     sizeCheckbox = 0;
00187     sizeLabel = new QLabel( i18n("Size:"), page, "sizeLabel");
00188     sizeLayout->addWidget(sizeLabel, 1, Qt::AlignLeft);
00189   }
00190   sizeLayout->addSpacing( checkBoxGap );
00191   sizeLayout->addSpacing( checkBoxGap ); // prevent label from eating border
00192   gridLayout->addLayout(sizeLayout, row, 2 );
00193 
00194   row ++;
00195 
00196   //
00197   // now create the actual boxes that hold the info
00198   //
00199   familyListBox = new KListBox( page, "familyListBox");
00200   familyListBox->setEnabled( !diff );
00201   gridLayout->addWidget( familyListBox, row, 0 );
00202   QString fontFamilyWhatsThisText =
00203     i18n("Here you can choose the font family to be used." );
00204   QWhatsThis::add( familyListBox, fontFamilyWhatsThisText );
00205   QWhatsThis::add(diff?(QWidget *) familyCheckbox:(QWidget *) familyLabel, fontFamilyWhatsThisText );
00206   connect(familyListBox, SIGNAL(highlighted(const QString &)),
00207           SLOT(family_chosen_slot(const QString &)));
00208   if(fontList.count() != 0)
00209   {
00210     familyListBox->insertStringList(fontList);
00211   }
00212   else
00213   {
00214     fillFamilyListBox(onlyFixed);
00215   }
00216 
00217   familyListBox->setMinimumWidth( minimumListWidth( familyListBox ) );
00218   familyListBox->setMinimumHeight(
00219     minimumListHeight( familyListBox, visibleListSize  ) );
00220 
00221   styleListBox = new KListBox( page, "styleListBox");
00222   styleListBox->setEnabled( !diff );
00223   gridLayout->addWidget(styleListBox, row, 1);
00224   QString fontStyleWhatsThisText =
00225     i18n("Here you can choose the font style to be used." );
00226   QWhatsThis::add( styleListBox, fontStyleWhatsThisText );
00227   QWhatsThis::add(diff?(QWidget *)styleCheckbox:(QWidget *)styleLabel, fontFamilyWhatsThisText );
00228   styleListBox->insertItem(i18n("Regular"));
00229   styleListBox->insertItem(i18n("Italic"));
00230   styleListBox->insertItem(i18n("Bold"));
00231   styleListBox->insertItem(i18n("Bold Italic"));
00232   styleListBox->setMinimumWidth( minimumListWidth( styleListBox ) );
00233   styleListBox->setMinimumHeight(
00234     minimumListHeight( styleListBox, visibleListSize  ) );
00235 
00236   connect(styleListBox, SIGNAL(highlighted(const QString &)),
00237           SLOT(style_chosen_slot(const QString &)));
00238 
00239 
00240   sizeListBox = new KListBox( page, "sizeListBox");
00241   sizeOfFont = new KIntNumInput( page, "sizeOfFont");
00242   sizeOfFont->setMinValue(4);
00243 
00244   sizeListBox->setEnabled( !diff );
00245   sizeOfFont->setEnabled( !diff );
00246   if( sizeIsRelativeState ) {
00247     QString sizeIsRelativeCBText =
00248       i18n("relative");
00249     QString sizeIsRelativeCBToolTipText =
00250       i18n("Font size<br><i>fixed</i> or <i>relative</i><br>to environment");
00251     QString sizeIsRelativeCBWhatsThisText =
00252       i18n("Here you can switch between fixed font size and font size "
00253            "to be calculated dynamically and adjusted to changing "
00254            "environment (e.g. widget dimensions, paper size)." );
00255     sizeIsRelativeCheckBox = new QCheckBox( sizeIsRelativeCBText,
00256                                             page,
00257                                            "sizeIsRelativeCheckBox" );
00258     sizeIsRelativeCheckBox->setTristate( diff );
00259     QGridLayout *sizeLayout2 = new QGridLayout( 3,2, KDialog::spacingHint()/2, "sizeLayout2" );
00260     gridLayout->addLayout(sizeLayout2, row, 2);
00261     sizeLayout2->setColStretch( 1, 1 ); // to prevent text from eating the right border
00262     sizeLayout2->addMultiCellWidget( sizeOfFont, 0, 0, 0, 1);
00263     sizeLayout2->addMultiCellWidget(sizeListBox, 1,1, 0,1);
00264     sizeLayout2->addWidget(sizeIsRelativeCheckBox, 2, 0, Qt::AlignLeft);
00265     QWhatsThis::add( sizeIsRelativeCheckBox, sizeIsRelativeCBWhatsThisText );
00266     QToolTip::add(   sizeIsRelativeCheckBox, sizeIsRelativeCBToolTipText );
00267   }
00268   else {
00269     sizeIsRelativeCheckBox = 0L;
00270     QGridLayout *sizeLayout2 = new QGridLayout( 2,1, KDialog::spacingHint()/2, "sizeLayout2" );
00271     gridLayout->addLayout(sizeLayout2, row, 2);
00272     sizeLayout2->addWidget( sizeOfFont, 0, 0);
00273     sizeLayout2->addMultiCellWidget(sizeListBox, 1,1, 0,0);
00274   }
00275   QString fontSizeWhatsThisText =
00276     i18n("Here you can choose the font size to be used." );
00277   QWhatsThis::add( sizeListBox, fontSizeWhatsThisText );
00278   QWhatsThis::add( diff?(QWidget *)sizeCheckbox:(QWidget *)sizeLabel, fontSizeWhatsThisText );
00279 
00280   fillSizeList();
00281   sizeListBox->setMinimumWidth( minimumListWidth(sizeListBox) +
00282     sizeListBox->fontMetrics().maxWidth() );
00283   sizeListBox->setMinimumHeight(
00284     minimumListHeight( sizeListBox, visibleListSize  ) );
00285 
00286   connect( sizeOfFont, SIGNAL( valueChanged(int) ),
00287            SLOT(size_value_slot(int)));
00288 
00289   connect( sizeListBox, SIGNAL(highlighted(const QString&)),
00290            SLOT(size_chosen_slot(const QString&)) );
00291   sizeListBox->setSelected(sizeListBox->findItem(QString::number(10)), true); // default to 10pt.
00292 
00293   row ++;
00294 
00295   row ++;
00296   sampleEdit = new QLineEdit( page, "sampleEdit");
00297   QFont tmpFont( KGlobalSettings::generalFont().family(), 64, QFont::Black );
00298   sampleEdit->setFont(tmpFont);
00299   sampleEdit->setText(i18n("The Quick Brown Fox Jumps Over The Lazy Dog"));
00300   sampleEdit->setMinimumHeight( sampleEdit->fontMetrics().lineSpacing() );
00301   sampleEdit->setAlignment(Qt::AlignCenter);
00302   gridLayout->addMultiCellWidget(sampleEdit, 4, 4, 0, 2);
00303   QString sampleEditWhatsThisText =
00304     i18n("This sample text illustrates the current settings. "
00305          "You may edit it to test special characters." );
00306   QWhatsThis::add( sampleEdit, sampleEditWhatsThisText );
00307   connect(this, SIGNAL(fontSelected(const QFont &)),
00308           SLOT(displaySample(const QFont &)));
00309 
00310   QVBoxLayout *vbox;
00311   if( makeFrame == true )
00312   {
00313     page = new QGroupBox( i18n("Actual Font"), this );
00314     topLayout->addWidget(page);
00315     vbox = new QVBoxLayout( page, KDialog::spacingHint() );
00316     vbox->addSpacing( fontMetrics().lineSpacing() );
00317   }
00318   else
00319   {
00320     page = new QWidget( this );
00321     topLayout->addWidget(page);
00322     vbox = new QVBoxLayout( page, 0, KDialog::spacingHint() );
00323     QLabel *label = new QLabel( i18n("Actual Font"), page );
00324     vbox->addWidget( label );
00325   }
00326 
00327   xlfdEdit = new QLineEdit( page, "xlfdEdit" );
00328   vbox->addWidget( xlfdEdit );
00329 
00330   // lets initialize the display if possible
00331   setFont( KGlobalSettings::generalFont(), usingFixed );
00332   // check or uncheck or gray out the "relative" checkbox
00333   if( sizeIsRelativeState && sizeIsRelativeCheckBox )
00334     setSizeIsRelative( *sizeIsRelativeState );
00335 
00336   KConfig *config = KGlobal::config();
00337   KConfigGroupSaver saver(config, QString::fromLatin1("General"));
00338   showXLFDArea(config->readBoolEntry(QString::fromLatin1("fontSelectorShowXLFD"), false));
00339 }
00340 
00341 KFontChooser::~KFontChooser()
00342 {
00343   delete d;
00344 }
00345 
00346 void KFontChooser::fillSizeList() {
00347   if(! sizeListBox) return; //assertion.
00348 
00349   static const int c[] =
00350   {
00351     4,  5,  6,  7,
00352     8,  9,  10, 11,
00353     12, 13, 14, 15,
00354     16, 17, 18, 19,
00355     20, 22, 24, 26,
00356     28, 32, 48, 64,
00357     0
00358   };
00359   for(int i = 0; c[i] != 0; i++)
00360   {
00361     sizeListBox->insertItem(QString::number(c[i]));
00362   }
00363 }
00364 
00365 void KFontChooser::setColor( const QColor & col )
00366 {
00367   d->m_palette.setColor( QPalette::Active, QColorGroup::Text, col );
00368   QPalette pal = sampleEdit->palette();
00369   pal.setColor( QPalette::Active, QColorGroup::Text, col );
00370   sampleEdit->setPalette( pal );
00371 }
00372 
00373 QColor KFontChooser::color() const
00374 {
00375   return d->m_palette.color( QPalette::Active, QColorGroup::Text );
00376 }
00377 
00378 void KFontChooser::setBackgroundColor( const QColor & col )
00379 {
00380   d->m_palette.setColor( QPalette::Active, QColorGroup::Base, col );
00381   QPalette pal = sampleEdit->palette();
00382   pal.setColor( QPalette::Active, QColorGroup::Base, col );
00383   sampleEdit->setPalette( pal );
00384 }
00385 
00386 QColor KFontChooser::backgroundColor() const
00387 {
00388   return d->m_palette.color( QPalette::Active, QColorGroup::Base );
00389 }
00390 
00391 void KFontChooser::setSizeIsRelative( QButton::ToggleState relative )
00392 {
00393   // check or uncheck or gray out the "relative" checkbox
00394   if( sizeIsRelativeCheckBox ) {
00395     if( QButton::NoChange == relative )
00396       sizeIsRelativeCheckBox->setNoChange();
00397     else
00398       sizeIsRelativeCheckBox->setChecked(  QButton::On == relative );
00399   }
00400 }
00401 
00402 QButton::ToggleState KFontChooser::sizeIsRelative() const
00403 {
00404   return sizeIsRelativeCheckBox
00405        ? sizeIsRelativeCheckBox->state()
00406        : QButton::NoChange;
00407 }
00408 
00409 QSize KFontChooser::sizeHint( void ) const
00410 {
00411   return( minimumSizeHint() );
00412 }
00413 
00414 
00415 void KFontChooser::enableColumn( int column, bool state )
00416 {
00417   if( column & FamilyList )
00418   {
00419     familyListBox->setEnabled(state);
00420   }
00421   if( column & StyleList )
00422   {
00423     styleListBox->setEnabled(state);
00424   }
00425   if( column & SizeList )
00426   {
00427     sizeListBox->setEnabled(state);
00428   }
00429 }
00430 
00431 
00432 void KFontChooser::setFont( const QFont& aFont, bool onlyFixed )
00433 {
00434   selFont = aFont;
00435   selectedSize=aFont.pointSize();
00436   if (selectedSize == -1)
00437      selectedSize = QFontInfo(aFont).pointSize();
00438 
00439   if( onlyFixed != usingFixed)
00440   {
00441     usingFixed = onlyFixed;
00442     fillFamilyListBox(usingFixed);
00443   }
00444   setupDisplay();
00445   displaySample(selFont);
00446 }
00447 
00448 
00449 int KFontChooser::fontDiffFlags() {
00450    int diffFlags = 0;
00451    if (familyCheckbox && styleCheckbox && sizeCheckbox) {
00452       diffFlags = (int)(familyCheckbox->isChecked() ? FontDiffFamily : 0)
00453                 | (int)( styleCheckbox->isChecked() ? FontDiffStyle  : 0)
00454                 | (int)(  sizeCheckbox->isChecked() ? FontDiffSize   : 0);
00455    }
00456    return diffFlags;
00457 }
00458 
00459 void KFontChooser::toggled_checkbox()
00460 {
00461   familyListBox->setEnabled( familyCheckbox->isChecked() );
00462   styleListBox->setEnabled( styleCheckbox->isChecked() );
00463   sizeListBox->setEnabled( sizeCheckbox->isChecked() );
00464   sizeOfFont->setEnabled( sizeCheckbox->isChecked() );
00465 }
00466 
00467 void KFontChooser::family_chosen_slot(const QString& family)
00468 {
00469     QFontDatabase dbase;
00470     QStringList styles = QStringList(dbase.styles(family));
00471     styleListBox->clear();
00472     currentStyles.clear();
00473     for ( QStringList::Iterator it = styles.begin(); it != styles.end(); ++it ) {
00474         QString style = *it;
00475         int pos = style.find("Plain");
00476         if(pos >=0) style = style.replace(pos,5,i18n("Regular"));
00477         pos = style.find("Normal");
00478         if(pos >=0) style = style.replace(pos,6,i18n("Regular"));
00479         pos = style.find("Oblique");
00480         if(pos >=0) style = style.replace(pos,7,i18n("Italic"));
00481         if(styleListBox->findItem(style) ==0) {
00482             styleListBox->insertItem(i18n(style.utf8()));
00483             currentStyles.insert(i18n(style.utf8()), *it);
00484         }
00485     }
00486     if(styleListBox->count()==0) {
00487         styleListBox->insertItem(i18n("Regular"));
00488         currentStyles.insert(i18n("Regular"), "Normal");
00489     }
00490 
00491     styleListBox->blockSignals(true);
00492     QListBoxItem *item = styleListBox->findItem(selectedStyle);
00493     if (item)
00494        styleListBox->setSelected(styleListBox->findItem(selectedStyle), true);
00495     else
00496        styleListBox->setSelected(0, true);
00497     styleListBox->blockSignals(false);
00498 
00499     style_chosen_slot(QString::null);
00500 }
00501 
00502 void KFontChooser::size_chosen_slot(const QString& size){
00503 
00504   selectedSize=size.toInt();
00505   sizeOfFont->setValue(selectedSize);
00506   selFont.setPointSize(selectedSize);
00507   emit fontSelected(selFont);
00508 }
00509 
00510 void KFontChooser::size_value_slot(int val) {
00511   selFont.setPointSize(val);
00512   emit fontSelected(selFont);
00513 }
00514 
00515 void KFontChooser::style_chosen_slot(const QString& style)
00516 {
00517     QString currentStyle;
00518     if (style.isEmpty())
00519        currentStyle = styleListBox->currentText();
00520     else
00521        currentStyle = style;
00522 
00523     int diff=0; // the difference between the font size requested and what we can show.
00524 
00525     sizeListBox->clear();
00526     QFontDatabase dbase;
00527     if(dbase.isSmoothlyScalable(familyListBox->currentText(), currentStyles[currentStyle])) {  // is vector font
00528         //sampleEdit->setPaletteBackgroundPixmap( VectorPixmap ); // TODO
00529         fillSizeList();
00530     } else {                                // is bitmap font.
00531         //sampleEdit->setPaletteBackgroundPixmap( BitmapPixmap ); // TODO
00532         QValueList<int> sizes = dbase.smoothSizes(familyListBox->currentText(), currentStyles[currentStyle]);
00533         if(sizes.count() > 0) {
00534             QValueList<int>::iterator it;
00535             diff=1000;
00536             for ( it = sizes.begin(); it != sizes.end(); ++it ) {
00537                 if(*it <= selectedSize || diff > *it - selectedSize) diff = selectedSize - *it;
00538                 sizeListBox->insertItem(QString::number(*it));
00539             }
00540         } else // there are times QT does not provide the list..
00541             fillSizeList();
00542     }
00543     sizeListBox->blockSignals(true);
00544     sizeListBox->setSelected(sizeListBox->findItem(QString::number(selectedSize)), true);
00545     sizeListBox->blockSignals(false);
00546     sizeListBox->ensureCurrentVisible();
00547 
00548     //kdDebug() << "Showing: " << familyListBox->currentText() << ", " << currentStyles[currentStyle] << ", " << selectedSize-diff << endl;
00549     selFont = dbase.font(familyListBox->currentText(), currentStyles[currentStyle], selectedSize-diff);
00550     emit fontSelected(selFont);
00551     if (!style.isEmpty())
00552         selectedStyle = style;
00553 }
00554 
00555 void KFontChooser::displaySample(const QFont& font)
00556 {
00557   sampleEdit->setFont(font);
00558   sampleEdit->setCursorPosition(0);
00559   xlfdEdit->setText(font.rawName());
00560   xlfdEdit->setCursorPosition(0);
00561 
00562   //QFontInfo a = QFontInfo(font);
00563   //kdDebug() << "font: " << a.family () << ", " << a.pointSize () << endl;
00564   //kdDebug() << "      (" << font.toString() << ")\n";
00565 }
00566 
00567 void KFontChooser::setupDisplay()
00568 {
00569   // Calling familyListBox->setCurrentItem() causes the value of selFont
00570   // to change, so we save the family, style and size beforehand.
00571   QString family = selFont.family().lower();
00572   int style = (selFont.bold() ? 2 : 0) + (selFont.italic() ? 1 : 0);
00573   int size = selFont.pointSize();
00574   if (size == -1)
00575      size = QFontInfo(selFont).pointSize();
00576   QString sizeStr = QString::number(size);
00577 
00578   int numEntries, i;
00579 
00580   numEntries = familyListBox->count();
00581   for (i = 0; i < numEntries; i++) {
00582     if (family == familyListBox->text(i).lower()) {
00583       familyListBox->setCurrentItem(i);
00584       break;
00585     }
00586   }
00587 
00588   // 1st Fallback
00589   if ( (i == numEntries) )
00590   {
00591     if (family.contains('['))
00592     {
00593       family = family.left(family.find('[')).stripWhiteSpace();
00594       for (i = 0; i < numEntries; i++) {
00595         if (family == familyListBox->text(i).lower()) {
00596           familyListBox->setCurrentItem(i);
00597           break;
00598         }
00599       }
00600     }
00601   }
00602 
00603   // 2nd Fallback
00604   if ( (i == numEntries) )
00605   {
00606     QString fallback = family+" [";
00607     for (i = 0; i < numEntries; i++) {
00608       if (familyListBox->text(i).lower().startsWith(fallback)) {
00609         familyListBox->setCurrentItem(i);
00610         break;
00611       }
00612     }
00613   }
00614 
00615   // 3rd Fallback
00616   if ( (i == numEntries) )
00617   {
00618     for (i = 0; i < numEntries; i++) {
00619       if (familyListBox->text(i).lower().startsWith(family)) {
00620         familyListBox->setCurrentItem(i);
00621         break;
00622       }
00623     }
00624   }
00625 
00626   styleListBox->setCurrentItem(style);
00627 
00628   numEntries = sizeListBox->count();
00629   for (i = 0; i < numEntries; i++){
00630     if (sizeStr == sizeListBox->text(i)) {
00631       sizeListBox->setCurrentItem(i);
00632       break;
00633     }
00634   }
00635 
00636   sizeOfFont->setValue(size);
00637 }
00638 
00639 
00640 void KFontChooser::getFontList( QStringList &list, uint fontListCriteria)
00641 {
00642   QFontDatabase dbase;
00643   QStringList lstSys(dbase.families());
00644 
00645   // if we have criteria; then check fonts before adding
00646   if (fontListCriteria)
00647   {
00648     QStringList lstFonts;
00649     for (QStringList::Iterator it = lstSys.begin(); it != lstSys.end(); ++it)
00650     {
00651         if ((fontListCriteria & FixedWidthFonts) > 0 && !dbase.isFixedPitch(*it)) continue;
00652         if ((fontListCriteria & (SmoothScalableFonts | ScalableFonts) == ScalableFonts) &&
00653                 !dbase.isBitmapScalable(*it)) continue;
00654         if ((fontListCriteria & SmoothScalableFonts) > 0 && !dbase.isSmoothlyScalable(*it)) continue;
00655         lstFonts.append(*it);
00656     }
00657 
00658     if((fontListCriteria & FixedWidthFonts) > 0) {
00659         // Fallback.. if there are no fixed fonts found, it's probably a
00660         // bug in the font server or Qt.  In this case, just use 'fixed'
00661         if (lstFonts.count() == 0)
00662           lstFonts.append("fixed");
00663     }
00664 
00665     lstSys = lstFonts;
00666   }
00667 
00668   lstSys.sort();
00669 
00670   list = lstSys;
00671 }
00672 
00673 void KFontChooser::addFont( QStringList &list, const char *xfont )
00674 {
00675   const char *ptr = strchr( xfont, '-' );
00676   if ( !ptr )
00677     return;
00678 
00679   ptr = strchr( ptr + 1, '-' );
00680   if ( !ptr )
00681     return;
00682 
00683   QString font = QString::fromLatin1(ptr + 1);
00684 
00685   int pos;
00686   if ( ( pos = font.find( '-' ) ) > 0 ) {
00687     font.truncate( pos );
00688 
00689     if ( font.find( QString::fromLatin1("open look"), 0, false ) >= 0 )
00690       return;
00691 
00692     QStringList::Iterator it = list.begin();
00693 
00694     for ( ; it != list.end(); ++it )
00695       if ( *it == font )
00696         return;
00697     list.append( font );
00698   }
00699 }
00700 
00701 void KFontChooser::fillFamilyListBox(bool onlyFixedFonts)
00702 {
00703   QStringList fontList;
00704   getFontList(fontList, onlyFixedFonts?FixedWidthFonts:0);
00705   familyListBox->clear();
00706   familyListBox->insertStringList(fontList);
00707 }
00708 
00709 void KFontChooser::showXLFDArea(bool show)
00710 {
00711   if( show == true )
00712   {
00713     xlfdEdit->parentWidget()->show();
00714   }
00715   else
00716   {
00717     xlfdEdit->parentWidget()->hide();
00718   }
00719 }
00720 
00722 
00723 KFontDialog::KFontDialog( QWidget *parent, const char* name,
00724                           bool onlyFixed, bool modal,
00725                           const QStringList &fontList, bool makeFrame, bool diff,
00726                           QButton::ToggleState *sizeIsRelativeState )
00727   : KDialogBase( parent, name, modal, i18n("Select Font"), Ok|Cancel, Ok )
00728 {
00729   chooser = new KFontChooser( this, "fontChooser",
00730                               onlyFixed, fontList, makeFrame, 8,
00731                               diff, sizeIsRelativeState );
00732   setMainWidget(chooser);
00733 }
00734 
00735 
00736 int KFontDialog::getFontDiff( QFont &theFont, int &diffFlags, bool onlyFixed,
00737                              QWidget *parent, bool makeFrame,
00738                              QButton::ToggleState *sizeIsRelativeState )
00739 {
00740   KFontDialog dlg( parent, "Font Selector", onlyFixed, true, QStringList(),
00741                    makeFrame, true, sizeIsRelativeState );
00742   dlg.setFont( theFont, onlyFixed );
00743 
00744   int result = dlg.exec();
00745   if( result == Accepted )
00746   {
00747     theFont = dlg.chooser->font();
00748     diffFlags = dlg.chooser->fontDiffFlags();
00749     if( sizeIsRelativeState )
00750       *sizeIsRelativeState = dlg.chooser->sizeIsRelative();
00751   }
00752   return( result );
00753 }
00754 
00755 int KFontDialog::getFont( QFont &theFont, bool onlyFixed,
00756                           QWidget *parent, bool makeFrame,
00757                           QButton::ToggleState *sizeIsRelativeState )
00758 {
00759   KFontDialog dlg( parent, "Font Selector", onlyFixed, true, QStringList(),
00760                    makeFrame, false, sizeIsRelativeState );
00761   dlg.setFont( theFont, onlyFixed );
00762 
00763   int result = dlg.exec();
00764   if( result == Accepted )
00765   {
00766     theFont = dlg.chooser->font();
00767     if( sizeIsRelativeState )
00768       *sizeIsRelativeState = dlg.chooser->sizeIsRelative();
00769   }
00770   return( result );
00771 }
00772 
00773 
00774 int KFontDialog::getFontAndText( QFont &theFont, QString &theString,
00775                                  bool onlyFixed, QWidget *parent,
00776                                  bool makeFrame,
00777                                  QButton::ToggleState *sizeIsRelativeState )
00778 {
00779   KFontDialog dlg( parent, "Font and Text Selector", onlyFixed, true,
00780                    QStringList(), makeFrame, false, sizeIsRelativeState );
00781   dlg.setFont( theFont, onlyFixed );
00782 
00783   int result = dlg.exec();
00784   if( result == Accepted )
00785   {
00786     theFont   = dlg.chooser->font();
00787     theString = dlg.chooser->sampleText();
00788     if( sizeIsRelativeState )
00789       *sizeIsRelativeState = dlg.chooser->sizeIsRelative();
00790   }
00791   return( result );
00792 }
00793 
00794 void KFontChooser::virtual_hook( int, void* )
00795 { /*BASE::virtual_hook( id, data );*/ }
00796 
00797 void KFontDialog::virtual_hook( int id, void* data )
00798 { KDialogBase::virtual_hook( id, data ); }
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:50 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001