kdeui Library API Documentation

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., 59 Temple Place - Suite 330,
00023     Boston, MA 02111-1307, 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 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00057 #include <X11/Xlib.h> 
00058             // not needed any more...
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   // Fall back in case nothing matched. Otherwise, diff doesn't work
00627   if ( i == numEntries )
00628     familyListBox->setCurrentItem( 0 );
00629 
00630   styleListBox->setCurrentItem(style);
00631 
00632   numEntries = sizeListBox->count();
00633   for (i = 0; i < numEntries; i++){
00634     if (sizeStr == sizeListBox->text(i)) {
00635       sizeListBox->setCurrentItem(i);
00636       break;
00637     }
00638   }
00639 
00640   sizeOfFont->setValue(size);
00641 }
00642 
00643 
00644 void KFontChooser::getFontList( QStringList &list, uint fontListCriteria)
00645 {
00646   QFontDatabase dbase;
00647   QStringList lstSys(dbase.families());
00648 
00649   // if we have criteria; then check fonts before adding
00650   if (fontListCriteria)
00651   {
00652     QStringList lstFonts;
00653     for (QStringList::Iterator it = lstSys.begin(); it != lstSys.end(); ++it)
00654     {
00655         if ((fontListCriteria & FixedWidthFonts) > 0 && !dbase.isFixedPitch(*it)) continue;
00656         if (((fontListCriteria & (SmoothScalableFonts | ScalableFonts)) == ScalableFonts) &&
00657                 !dbase.isBitmapScalable(*it)) continue;
00658         if ((fontListCriteria & SmoothScalableFonts) > 0 && !dbase.isSmoothlyScalable(*it)) continue;
00659         lstFonts.append(*it);
00660     }
00661 
00662     if((fontListCriteria & FixedWidthFonts) > 0) {
00663         // Fallback.. if there are no fixed fonts found, it's probably a
00664         // bug in the font server or Qt.  In this case, just use 'fixed'
00665         if (lstFonts.count() == 0)
00666           lstFonts.append("fixed");
00667     }
00668 
00669     lstSys = lstFonts;
00670   }
00671 
00672   lstSys.sort();
00673 
00674   list = lstSys;
00675 }
00676 
00677 void KFontChooser::addFont( QStringList &list, const char *xfont )
00678 {
00679   const char *ptr = strchr( xfont, '-' );
00680   if ( !ptr )
00681     return;
00682 
00683   ptr = strchr( ptr + 1, '-' );
00684   if ( !ptr )
00685     return;
00686 
00687   QString font = QString::fromLatin1(ptr + 1);
00688 
00689   int pos;
00690   if ( ( pos = font.find( '-' ) ) > 0 ) {
00691     font.truncate( pos );
00692 
00693     if ( font.find( QString::fromLatin1("open look"), 0, false ) >= 0 )
00694       return;
00695 
00696     QStringList::Iterator it = list.begin();
00697 
00698     for ( ; it != list.end(); ++it )
00699       if ( *it == font )
00700     return;
00701     list.append( font );
00702   }
00703 }
00704 
00705 void KFontChooser::fillFamilyListBox(bool onlyFixedFonts)
00706 {
00707   QStringList fontList;
00708   getFontList(fontList, onlyFixedFonts?FixedWidthFonts:0);
00709   familyListBox->clear();
00710   familyListBox->insertStringList(fontList);
00711 }
00712 
00713 void KFontChooser::showXLFDArea(bool show)
00714 {
00715   if( show == true )
00716   {
00717     xlfdEdit->parentWidget()->show();
00718   }
00719   else
00720   {
00721     xlfdEdit->parentWidget()->hide();
00722   }
00723 }
00724 
00726 
00727 KFontDialog::KFontDialog( QWidget *parent, const char* name,
00728               bool onlyFixed, bool modal,
00729               const QStringList &fontList, bool makeFrame, bool diff,
00730                           QButton::ToggleState *sizeIsRelativeState )
00731   : KDialogBase( parent, name, modal, i18n("Select Font"), Ok|Cancel, Ok )
00732 {
00733   chooser = new KFontChooser( this, "fontChooser",
00734                               onlyFixed, fontList, makeFrame, 8,
00735                               diff, sizeIsRelativeState );
00736   setMainWidget(chooser);
00737 }
00738 
00739 
00740 int KFontDialog::getFontDiff( QFont &theFont, int &diffFlags, bool onlyFixed,
00741                              QWidget *parent, bool makeFrame,
00742                              QButton::ToggleState *sizeIsRelativeState )
00743 {
00744   KFontDialog dlg( parent, "Font Selector", onlyFixed, true, QStringList(),
00745            makeFrame, true, sizeIsRelativeState );
00746   dlg.setFont( theFont, onlyFixed );
00747 
00748   int result = dlg.exec();
00749   if( result == Accepted )
00750   {
00751     theFont = dlg.chooser->font();
00752     diffFlags = dlg.chooser->fontDiffFlags();
00753     if( sizeIsRelativeState )
00754       *sizeIsRelativeState = dlg.chooser->sizeIsRelative();
00755   }
00756   return( result );
00757 }
00758 
00759 int KFontDialog::getFont( QFont &theFont, bool onlyFixed,
00760                           QWidget *parent, bool makeFrame,
00761                           QButton::ToggleState *sizeIsRelativeState )
00762 {
00763   KFontDialog dlg( parent, "Font Selector", onlyFixed, true, QStringList(),
00764            makeFrame, false, sizeIsRelativeState );
00765   dlg.setFont( theFont, onlyFixed );
00766 
00767   int result = dlg.exec();
00768   if( result == Accepted )
00769   {
00770     theFont = dlg.chooser->font();
00771     if( sizeIsRelativeState )
00772       *sizeIsRelativeState = dlg.chooser->sizeIsRelative();
00773   }
00774   return( result );
00775 }
00776 
00777 
00778 int KFontDialog::getFontAndText( QFont &theFont, QString &theString,
00779                  bool onlyFixed, QWidget *parent,
00780                  bool makeFrame,
00781                                  QButton::ToggleState *sizeIsRelativeState )
00782 {
00783   KFontDialog dlg( parent, "Font and Text Selector", onlyFixed, true,
00784            QStringList(), makeFrame, false, sizeIsRelativeState );
00785   dlg.setFont( theFont, onlyFixed );
00786 
00787   int result = dlg.exec();
00788   if( result == Accepted )
00789   {
00790     theFont   = dlg.chooser->font();
00791     theString = dlg.chooser->sampleText();
00792     if( sizeIsRelativeState )
00793       *sizeIsRelativeState = dlg.chooser->sizeIsRelative();
00794   }
00795   return( result );
00796 }
00797 
00798 void KFontChooser::virtual_hook( int, void* )
00799 { /*BASE::virtual_hook( id, data );*/ }
00800 
00801 void KFontDialog::virtual_hook( int id, void* data )
00802 { KDialogBase::virtual_hook( id, data ); }
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:32:52 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003