F:/KPlato/koffice/libs/kotext/KFontDialog_local.cpp

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

Généré le Wed Nov 22 23:41:07 2006 pour KPlato par  doxygen 1.5.1-p1