kdeui Library API Documentation

kfontdialog.cpp

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