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 #include <cmath> 00032 00033 // Determine if the font with given properties is available on the system, 00034 // otherwise find and return the best fitting combination. 00035 static QFont nearestExistingFont (const QFont &font) 00036 { 00037 QFontDatabase dbase; 00038 00039 // Initialize font data accoring to given font object. 00040 QString family = font.family(); 00041 QString style = dbase.styleString(font); 00042 qreal size = font.pointSizeF(); 00043 00044 // Check if the family exists. 00045 const QStringList families = dbase.families(); 00046 if (!families.contains(family)) { 00047 // Chose another family. 00048 family = families.count() ? families[0] : "fixed"; 00049 // TODO: Try to find nearest match? 00050 } 00051 00052 // Check if the family has the requested style. 00053 // Easiest by piping it through font selection in the database. 00054 QString retStyle = dbase.styleString(dbase.font(family, style, 10)); 00055 style = retStyle; 00056 00057 // Check if the family has the requested size. 00058 // Only for bitmap fonts. 00059 if (!dbase.isSmoothlyScalable(family, style)) { 00060 QList<int> sizes = dbase.smoothSizes(family, style); 00061 if (!sizes.contains(size)) { 00062 // Find nearest available size. 00063 int mindiff = 1000; 00064 int refsize = size; 00065 foreach (int lsize, sizes) { 00066 int diff = qAbs(refsize - lsize); 00067 if (mindiff > diff) { 00068 mindiff = diff; 00069 size = lsize; 00070 } 00071 } 00072 } 00073 } 00074 00075 // Select the font with confirmed properties. 00076 QFont result = dbase.font(family, style, int(size)); 00077 if (dbase.isSmoothlyScalable(family, style) && result.pointSize() == floor(size)) { 00078 result.setPointSizeF(size); 00079 } 00080 return result; 00081 } 00082 00083 class KFontRequester::KFontRequesterPrivate 00084 { 00085 public: 00086 KFontRequesterPrivate(KFontRequester *q): q(q) {} 00087 00088 void displaySampleText(); 00089 void setToolTip(); 00090 00091 void _k_buttonClicked(); 00092 00093 KFontRequester *q; 00094 bool m_onlyFixed; 00095 QString m_sampleText, m_title; 00096 QLabel *m_sampleLabel; 00097 QPushButton *m_button; 00098 QFont m_selFont; 00099 }; 00100 00101 KFontRequester::KFontRequester( QWidget *parent, bool onlyFixed ) 00102 : QWidget( parent ), d(new KFontRequesterPrivate(this)) 00103 { 00104 d->m_onlyFixed = onlyFixed; 00105 00106 QHBoxLayout *layout = new QHBoxLayout( this ); 00107 layout->setMargin( 0 ); 00108 00109 d->m_sampleLabel = new QLabel( this ); 00110 d->m_button = new QPushButton( i18n( "Choose..." ), this ); 00111 00112 d->m_sampleLabel->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken ); 00113 setFocusProxy( d->m_button ); 00114 00115 layout->addWidget( d->m_sampleLabel, 1 ); 00116 layout->addWidget( d->m_button ); 00117 00118 connect( d->m_button, SIGNAL( clicked() ), SLOT( _k_buttonClicked() ) ); 00119 00120 d->displaySampleText(); 00121 d->setToolTip(); 00122 } 00123 00124 KFontRequester::~KFontRequester() 00125 { 00126 delete d; 00127 } 00128 00129 QFont KFontRequester::font() const 00130 { 00131 return d->m_selFont; 00132 } 00133 00134 bool KFontRequester::isFixedOnly() const 00135 { 00136 return d->m_onlyFixed; 00137 } 00138 00139 QString KFontRequester::sampleText() const 00140 { 00141 return d->m_sampleText; 00142 } 00143 00144 QString KFontRequester::title() const 00145 { 00146 return d->m_title; 00147 } 00148 00149 QLabel *KFontRequester::label() const 00150 { 00151 return d->m_sampleLabel; 00152 } 00153 00154 QPushButton *KFontRequester::button() const 00155 { 00156 return d->m_button; 00157 } 00158 00159 void KFontRequester::setFont( const QFont &font, bool onlyFixed ) 00160 { 00161 d->m_selFont = nearestExistingFont(font); 00162 d->m_onlyFixed = onlyFixed; 00163 00164 d->displaySampleText(); 00165 emit fontSelected( d->m_selFont ); 00166 } 00167 00168 void KFontRequester::setSampleText( const QString &text ) 00169 { 00170 d->m_sampleText = text; 00171 d->displaySampleText(); 00172 } 00173 00174 void KFontRequester::setTitle( const QString &title ) 00175 { 00176 d->m_title = title; 00177 d->setToolTip(); 00178 } 00179 00180 void KFontRequester::KFontRequesterPrivate::_k_buttonClicked() 00181 { 00182 KFontChooser::DisplayFlags flags = KFontChooser::NoDisplayFlags; 00183 if ( m_onlyFixed ) { 00184 flags |= KFontChooser::FixedFontsOnly; 00185 } 00186 00187 int result = KFontDialog::getFont( m_selFont, flags, q->parentWidget() ); 00188 00189 if ( result == KDialog::Accepted ) 00190 { 00191 displaySampleText(); 00192 emit q->fontSelected( m_selFont ); 00193 } 00194 } 00195 00196 void KFontRequester::KFontRequesterPrivate::displaySampleText() 00197 { 00198 m_sampleLabel->setFont( m_selFont ); 00199 00200 qreal size = m_selFont.pointSizeF(); 00201 if(size == -1) 00202 size = m_selFont.pixelSize(); 00203 00204 if ( m_sampleText.isEmpty() ) { 00205 QString family = translateFontName(m_selFont.family()); 00206 m_sampleLabel->setText( QString( "%1 %2" ).arg( family ).arg( KGlobal::locale()->formatNumber( size, (size == floor(size)) ? 0 : 1 ) ) ); 00207 } 00208 else { 00209 m_sampleLabel->setText( m_sampleText ); 00210 } 00211 } 00212 00213 void KFontRequester::KFontRequesterPrivate::setToolTip() 00214 { 00215 m_button->setToolTip( i18n( "Click to select a font" ) ); 00216 00217 m_sampleLabel->setToolTip( QString() ); 00218 m_sampleLabel->setWhatsThis(QString()); 00219 00220 if ( m_title.isNull() ) 00221 { 00222 m_sampleLabel->setToolTip( i18n( "Preview of the selected font" ) ); 00223 m_sampleLabel->setWhatsThis( i18n( "This is a preview of the selected font. You can change it" 00224 " by clicking the \"Choose...\" button." ) ); 00225 } 00226 else 00227 { 00228 m_sampleLabel->setToolTip( i18n( "Preview of the \"%1\" font" , m_title ) ); 00229 m_sampleLabel->setWhatsThis( i18n( "This is a preview of the \"%1\" font. You can change it" 00230 " by clicking the \"Choose...\" button." , m_title ) ); 00231 } 00232 } 00233 00234 #include "kfontrequester.moc" 00235 00236 /* vim: et sw=2 ts=2 00237 */
KDE 4.6 API Reference