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 layout->setSpacing( KDialog::spacingHint() );
00103
00104 d->m_sampleLabel = new QLabel( this );
00105 d->m_button = new QPushButton( i18n( "Choose..." ), this );
00106
00107 d->m_sampleLabel->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
00108 setFocusProxy( d->m_button );
00109
00110 layout->addWidget( d->m_sampleLabel, 1 );
00111 layout->addWidget( d->m_button );
00112
00113 connect( d->m_button, SIGNAL( clicked() ), SLOT( _k_buttonClicked() ) );
00114
00115 d->displaySampleText();
00116 d->setToolTip();
00117 }
00118
00119 KFontRequester::~KFontRequester()
00120 {
00121 delete d;
00122 }
00123
00124 QFont KFontRequester::font() const
00125 {
00126 return d->m_selFont;
00127 }
00128
00129 bool KFontRequester::isFixedOnly() const
00130 {
00131 return d->m_onlyFixed;
00132 }
00133
00134 QString KFontRequester::sampleText() const
00135 {
00136 return d->m_sampleText;
00137 }
00138
00139 QString KFontRequester::title() const
00140 {
00141 return d->m_title;
00142 }
00143
00144 QLabel *KFontRequester::label() const
00145 {
00146 return d->m_sampleLabel;
00147 }
00148
00149 QPushButton *KFontRequester::button() const
00150 {
00151 return d->m_button;
00152 }
00153
00154 void KFontRequester::setFont( const QFont &font, bool onlyFixed )
00155 {
00156 d->m_selFont = nearestExistingFont(font);
00157 d->m_onlyFixed = onlyFixed;
00158
00159 d->displaySampleText();
00160 emit fontSelected( d->m_selFont );
00161 }
00162
00163 void KFontRequester::setSampleText( const QString &text )
00164 {
00165 d->m_sampleText = text;
00166 d->displaySampleText();
00167 }
00168
00169 void KFontRequester::setTitle( const QString &title )
00170 {
00171 d->m_title = title;
00172 d->setToolTip();
00173 }
00174
00175 void KFontRequester::KFontRequesterPrivate::_k_buttonClicked()
00176 {
00177 KFontChooser::DisplayFlags flags = KFontChooser::NoDisplayFlags;
00178 if ( m_onlyFixed ) {
00179 flags |= KFontChooser::FixedFontsOnly;
00180 }
00181
00182 int result = KFontDialog::getFont( m_selFont, flags, q->parentWidget() );
00183
00184 if ( result == KDialog::Accepted )
00185 {
00186 displaySampleText();
00187 emit q->fontSelected( m_selFont );
00188 }
00189 }
00190
00191 void KFontRequester::KFontRequesterPrivate::displaySampleText()
00192 {
00193 m_sampleLabel->setFont( m_selFont );
00194
00195 int size = m_selFont.pointSize();
00196 if(size == -1)
00197 size = m_selFont.pixelSize();
00198
00199 if ( m_sampleText.isEmpty() ) {
00200 QString family = translateFontName(m_selFont.family());
00201 m_sampleLabel->setText( QString( "%1 %2" ).arg( family ).arg( size ) );
00202 }
00203 else {
00204 m_sampleLabel->setText( m_sampleText );
00205 }
00206 }
00207
00208 void KFontRequester::KFontRequesterPrivate::setToolTip()
00209 {
00210 m_button->setToolTip( i18n( "Click to select a font" ) );
00211
00212 m_sampleLabel->setToolTip( QString() );
00213 m_sampleLabel->setWhatsThis(QString());
00214
00215 if ( m_title.isNull() )
00216 {
00217 m_sampleLabel->setToolTip( i18n( "Preview of the selected font" ) );
00218 m_sampleLabel->setWhatsThis( i18n( "This is a preview of the selected font. You can change it"
00219 " by clicking the \"Choose...\" button." ) );
00220 }
00221 else
00222 {
00223 m_sampleLabel->setToolTip( i18n( "Preview of the \"%1\" font" , m_title ) );
00224 m_sampleLabel->setWhatsThis( i18n( "This is a preview of the \"%1\" font. You can change it"
00225 " by clicking the \"Choose...\" button." , m_title ) );
00226 }
00227 }
00228
00229 #include "kfontrequester.moc"
00230
00231
00232