kfontdialog.cpp

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