• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kfontrequester.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2003 Nadeem Hasan <nhasan@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kfontrequester.h"
00021 #include "fonthelpers_p.h"
00022 
00023 #include <QtGui/QLabel>
00024 #include <QtGui/QPushButton>
00025 #include <QtGui/QLayout>
00026 #include <QtGui/QFontDatabase>
00027 
00028 #include <kfontdialog.h>
00029 #include <klocale.h>
00030 
00031 // Determine if the font with given properties is available on the system,
00032 // otherwise find and return the best fitting combination.
00033 static QFont nearestExistingFont (const QFont &font)
00034 {
00035     QFontDatabase dbase;
00036 
00037     // Initialize font data accoring to given font object.
00038     QString family = font.family();
00039     QString style = dbase.styleString(font);
00040     int size = font.pointSize();
00041 
00042     // Check if the family exists.
00043     const QStringList families = dbase.families();
00044     if (!families.contains(family)) {
00045         // Chose another family.
00046         family = families.count() ? families[0] : "fixed";
00047         // TODO: Try to find nearest match?
00048     }
00049 
00050     // Check if the family has the requested style.
00051     // Easiest by piping it through font selection in the database.
00052     QString retStyle = dbase.styleString(dbase.font(family, style, 10));
00053     style = retStyle;
00054 
00055     // Check if the family has the requested size.
00056     // Only for bitmap fonts.
00057     if (!dbase.isSmoothlyScalable(family, style)) {
00058         QList<int> sizes = dbase.smoothSizes(family, style);
00059         if (!sizes.contains(size)) {
00060             // Find nearest available size.
00061             int mindiff = 1000;
00062             int refsize = size;
00063             foreach (int lsize, sizes) {
00064                 int diff = qAbs(refsize - lsize);
00065                 if (mindiff > diff) {
00066                     mindiff = diff;
00067                     size = lsize;
00068                 }
00069             }
00070         }
00071     }
00072 
00073     // Select the font with confirmed properties.
00074     return dbase.font(family, style, size);
00075 }
00076 
00077 class KFontRequester::KFontRequesterPrivate
00078 {
00079 public:
00080   KFontRequesterPrivate(KFontRequester *q): q(q) {}
00081 
00082   void displaySampleText();
00083   void setToolTip();
00084 
00085   void _k_buttonClicked();
00086 
00087   KFontRequester *q;
00088   bool m_onlyFixed;
00089   QString m_sampleText, m_title;
00090   QLabel *m_sampleLabel;
00091   QPushButton *m_button;
00092   QFont m_selFont;
00093 };
00094 
00095 KFontRequester::KFontRequester( QWidget *parent, bool onlyFixed )
00096     : QWidget( parent ), d(new KFontRequesterPrivate(this))
00097 {
00098   d->m_onlyFixed = onlyFixed;
00099 
00100   QHBoxLayout *layout = new QHBoxLayout( this );
00101   layout->setMargin( 0 );
00102 
00103   d->m_sampleLabel = new QLabel( this );
00104   d->m_button = new QPushButton( i18n( "Choose..." ), this );
00105 
00106   d->m_sampleLabel->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
00107   setFocusProxy( d->m_button );
00108 
00109   layout->addWidget( d->m_sampleLabel, 1 );
00110   layout->addWidget( d->m_button );
00111 
00112   connect( d->m_button, SIGNAL( clicked() ), SLOT( _k_buttonClicked() ) );
00113 
00114   d->displaySampleText();
00115   d->setToolTip();
00116 }
00117 
00118 KFontRequester::~KFontRequester()
00119 {
00120   delete d;
00121 }
00122 
00123 QFont KFontRequester::font() const
00124 {
00125   return d->m_selFont;
00126 }
00127 
00128 bool KFontRequester::isFixedOnly() const
00129 {
00130   return d->m_onlyFixed;
00131 }
00132 
00133 QString KFontRequester::sampleText() const
00134 {
00135   return d->m_sampleText;
00136 }
00137 
00138 QString KFontRequester::title() const
00139 {
00140   return d->m_title;
00141 }
00142 
00143 QLabel *KFontRequester::label() const
00144 {
00145   return d->m_sampleLabel;
00146 }
00147 
00148 QPushButton *KFontRequester::button() const
00149 {
00150   return d->m_button;
00151 }
00152 
00153 void KFontRequester::setFont( const QFont &font, bool onlyFixed )
00154 {
00155   d->m_selFont = nearestExistingFont(font);
00156   d->m_onlyFixed = onlyFixed;
00157 
00158   d->displaySampleText();
00159   emit fontSelected( d->m_selFont );
00160 }
00161 
00162 void KFontRequester::setSampleText( const QString &text )
00163 {
00164   d->m_sampleText = text;
00165   d->displaySampleText();
00166 }
00167 
00168 void KFontRequester::setTitle( const QString &title )
00169 {
00170   d->m_title = title;
00171   d->setToolTip();
00172 }
00173 
00174 void KFontRequester::KFontRequesterPrivate::_k_buttonClicked()
00175 {
00176     KFontChooser::DisplayFlags flags = KFontChooser::NoDisplayFlags;
00177     if ( m_onlyFixed ) {
00178         flags |= KFontChooser::FixedFontsOnly;
00179     }
00180 
00181     int result = KFontDialog::getFont( m_selFont, flags, q->parentWidget() );
00182 
00183     if ( result == KDialog::Accepted )
00184     {
00185         displaySampleText();
00186         emit q->fontSelected( m_selFont );
00187     }
00188 }
00189 
00190 void KFontRequester::KFontRequesterPrivate::displaySampleText()
00191 {
00192   m_sampleLabel->setFont( m_selFont );
00193 
00194   int size = m_selFont.pointSize();
00195   if(size == -1)
00196     size = m_selFont.pixelSize();
00197 
00198   if ( m_sampleText.isEmpty() ) {
00199     QString family = translateFontName(m_selFont.family());
00200     m_sampleLabel->setText( QString( "%1 %2" ).arg( family ).arg( size ) );
00201   }
00202   else {
00203     m_sampleLabel->setText( m_sampleText );
00204   }
00205 }
00206 
00207 void KFontRequester::KFontRequesterPrivate::setToolTip()
00208 {
00209   m_button->setToolTip( i18n( "Click to select a font" ) );
00210 
00211   m_sampleLabel->setToolTip( QString() );
00212   m_sampleLabel->setWhatsThis(QString());
00213 
00214   if ( m_title.isNull() )
00215   {
00216     m_sampleLabel->setToolTip( i18n( "Preview of the selected font" ) );
00217     m_sampleLabel->setWhatsThis( i18n( "This is a preview of the selected font. You can change it"
00218         " by clicking the \"Choose...\" button." ) );
00219   }
00220   else
00221   {
00222     m_sampleLabel->setToolTip( i18n( "Preview of the \"%1\" font" ,  m_title ) );
00223     m_sampleLabel->setWhatsThis( i18n( "This is a preview of the \"%1\" font. You can change it"
00224         " by clicking the \"Choose...\" button." ,  m_title ) );
00225   }
00226 }
00227 
00228 #include "kfontrequester.moc"
00229 
00230 /* vim: et sw=2 ts=2
00231 */

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal