KDEUI
kfontrequester.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00032
00033 static QFont nearestExistingFont (const QFont &font)
00034 {
00035 QFontDatabase dbase;
00036
00037
00038 QString family = font.family();
00039 QString style = dbase.styleString(font);
00040 int size = font.pointSize();
00041
00042
00043 const QStringList families = dbase.families();
00044 if (!families.contains(family)) {
00045
00046 family = families.count() ? families[0] : "fixed";
00047
00048 }
00049
00050
00051
00052 QString retStyle = dbase.styleString(dbase.font(family, style, 10));
00053 style = retStyle;
00054
00055
00056
00057 if (!dbase.isSmoothlyScalable(family, style)) {
00058 QList<int> sizes = dbase.smoothSizes(family, style);
00059 if (!sizes.contains(size)) {
00060
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
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
00231