23 #include "fonthelpers_p.h"
24 #include "sampleedit_p.h"
26 #include <QtGui/QCheckBox>
27 #include <QtGui/QLabel>
28 #include <QtGui/QLayout>
29 #include <QtGui/QSplitter>
30 #include <QtGui/QScrollBar>
31 #include <QtGui/QFontDatabase>
32 #include <QtGui/QGroupBox>
49 #define I18NC_NOX i18nc
54 for(
int i=0; i<list->
count(); i++ )
59 w = qMax(w,itemWidth);
61 if( w == 0 ) { w = 40; }
72 if( w < 0 ) { w = 10; }
73 if( numVisibleEntry <= 0 ) { numVisibleEntry = 4; }
74 return ( w * numVisibleEntry + 2 * list->
frameWidth() );
82 class KFontChooser::Private
88 m_palette.setColor(QPalette::Active, QPalette::Text, Qt::black);
89 m_palette.setColor(QPalette::Active, QPalette::Base, Qt::white);
90 signalsAllowed =
true;
100 void fillFamilyListBox(
bool onlyFixedFonts =
false);
101 int nearestSizeRow(qreal val,
bool customize);
108 void _k_toggled_checkbox();
109 void _k_family_chosen_slot(
const QString&);
110 void _k_size_chosen_slot(
const QString&);
111 void _k_style_chosen_slot(
const QString&);
112 void _k_displaySample(
const QFont &
font);
113 void _k_showXLFDArea(
bool);
114 void _k_size_value_slot(
double);
119 bool signalsAllowed:1;
125 SampleEdit *sampleEdit;
157 const DisplayFlags& flags,
160 Qt::CheckState *sizeIsRelativeState )
165 setWhatsThis(
i18nc(
"@info:whatsthis",
"Here you can choose the font to be used." ));
206 d->familyCheckbox =
new QCheckBox(
i18nc(
"@option:check",
"Font"), page);
207 connect(d->familyCheckbox, SIGNAL(toggled(
bool)),
208 this, SLOT(_k_toggled_checkbox()));
209 familyLayout->
addWidget(d->familyCheckbox, 0, Qt::AlignLeft);
210 d->familyCheckbox->setWhatsThis(
i18nc(
"@info:whatsthis",
"Enable this checkbox to change the font family settings."));
211 d->familyCheckbox->setToolTip(
i18nc(
"@info:tooltip",
"Change font family?") );
214 d->familyCheckbox = 0;
215 d->familyLabel =
new QLabel(
i18nc(
"@label",
"Font:"), page );
216 familyLayout->
addWidget(d->familyLabel, 1, Qt::AlignLeft);
218 gridLayout->
addLayout(familyLayout, row, 0 );
221 if ( flags & ShowDifferences ) {
222 d->styleCheckbox =
new QCheckBox(
i18nc(
"@option:check",
"Font style"), page);
223 connect(d->styleCheckbox, SIGNAL(toggled(
bool)),
224 this, SLOT(_k_toggled_checkbox()));
225 styleLayout->
addWidget(d->styleCheckbox, 0, Qt::AlignLeft);
226 d->styleCheckbox->setWhatsThis(
i18nc(
"@info:whatsthis",
"Enable this checkbox to change the font style settings."));
227 d->styleCheckbox->setToolTip(
i18nc(
"@info:tooltip",
"Change font style?"));
230 d->styleCheckbox = 0;
231 d->styleLabel =
new QLabel(
i18n(
"Font style:"), page );
232 styleLayout->
addWidget(d->styleLabel, 1, Qt::AlignLeft);
235 gridLayout->
addLayout(styleLayout, row, 1 );
238 if ( flags & ShowDifferences ) {
239 d->sizeCheckbox =
new QCheckBox(
i18nc(
"@option:check",
"Size"),page);
240 connect(d->sizeCheckbox, SIGNAL(toggled(
bool)),
241 this, SLOT(_k_toggled_checkbox()));
242 sizeLayout->
addWidget(d->sizeCheckbox, 0, Qt::AlignLeft);
243 d->sizeCheckbox->setWhatsThis(
i18nc(
"@info:whatsthis",
"Enable this checkbox to change the font size settings."));
244 d->sizeCheckbox->setToolTip(
i18nc(
"@info:tooltip",
"Change font size?"));
248 d->sizeLabel =
new QLabel(
i18nc(
"@label:listbox Font size",
"Size:"), page );
249 sizeLayout->
addWidget(d->sizeLabel, 1, Qt::AlignLeft);
253 gridLayout->
addLayout(sizeLayout, row, 2 );
261 d->familyListBox->setEnabled( flags ^ ShowDifferences );
262 gridLayout->
addWidget( d->familyListBox, row, 0 );
263 QString fontFamilyWhatsThisText (
264 i18nc(
"@info:whatsthis",
"Here you can choose the font family to be used." ));
265 d->familyListBox->setWhatsThis(fontFamilyWhatsThisText );
267 if ( flags & ShowDifferences ) {
268 d->familyCheckbox->setWhatsThis(fontFamilyWhatsThisText );
270 d->familyLabel->setWhatsThis(fontFamilyWhatsThisText );
274 this, SLOT(_k_family_chosen_slot(
QString)));
276 d->setFamilyBoxItems(fontList);
280 d->fillFamilyListBox( flags & FixedFontsOnly );
284 d->familyListBox->setMinimumHeight(
288 d->styleListBox->setEnabled( flags ^ ShowDifferences );
289 gridLayout->
addWidget(d->styleListBox, row, 1);
290 d->styleListBox->setWhatsThis(
i18nc(
"@info:whatsthis",
"Here you can choose the font style to be used." ));
291 if ( flags & ShowDifferences ) {
292 ((
QWidget *)d->styleCheckbox)->setWhatsThis(fontFamilyWhatsThisText );
294 ((
QWidget *)d->styleLabel)->setWhatsThis( fontFamilyWhatsThisText );
298 d->styleListBox->addItem(
I18NC_NOX(
"QFontDatabase",
"Normal"));
299 d->styleListBox->addItem(
i18nc(
"@item font",
"Italic"));
300 d->styleListBox->addItem(
i18nc(
"@item font",
"Oblique"));
301 d->styleListBox->addItem(
i18nc(
"@item font",
"Bold"));
302 d->styleListBox->addItem(
i18nc(
"@item font",
"Bold Italic"));
304 d->styleListBox->setMinimumHeight(
308 this, SLOT(_k_style_chosen_slot(
QString)));
313 d->sizeOfFont->setMinimum(4);
314 d->sizeOfFont->setMaximum(999);
315 d->sizeOfFont->setDecimals(1);
316 d->sizeOfFont->setSingleStep(1);
317 d->sizeOfFont->setSliderEnabled(
false);
319 d->sizeListBox->setEnabled( flags ^ ShowDifferences );
320 d->sizeOfFont->setEnabled( flags ^ ShowDifferences );
321 if( sizeIsRelativeState ) {
323 i18nc(
"@item font size",
"Relative");
324 QString sizeIsRelativeCBToolTipText =
325 i18n(
"Font size<br /><i>fixed</i> or <i>relative</i><br />to environment");
326 QString sizeIsRelativeCBWhatsThisText =
327 i18n(
"Here you can switch between fixed font size and font size "
328 "to be calculated dynamically and adjusted to changing "
329 "environment (e.g. widget dimensions, paper size)." );
330 d->sizeIsRelativeCheckBox =
new QCheckBox( sizeIsRelativeCBText,
332 d->sizeIsRelativeCheckBox->setTristate( flags & ShowDifferences );
335 gridLayout->
addLayout(sizeLayout2, row, 2);
337 sizeLayout2->
addWidget( d->sizeOfFont, 0, 0, 1, 2);
338 sizeLayout2->
addWidget(d->sizeListBox, 1,0, 1,2);
339 sizeLayout2->
addWidget(d->sizeIsRelativeCheckBox, 2, 0, Qt::AlignLeft);
340 d->sizeIsRelativeCheckBox->setWhatsThis(sizeIsRelativeCBWhatsThisText );
341 d->sizeIsRelativeCheckBox->setToolTip( sizeIsRelativeCBToolTipText );
344 d->sizeIsRelativeCheckBox = 0L;
347 gridLayout->
addLayout(sizeLayout2, row, 2);
348 sizeLayout2->
addWidget( d->sizeOfFont, 0, 0);
349 sizeLayout2->
addWidget(d->sizeListBox, 1,0);
351 QString fontSizeWhatsThisText =
352 i18n(
"Here you can choose the font size to be used." );
353 d->sizeListBox->setWhatsThis(fontSizeWhatsThisText );
355 if ( flags & ShowDifferences ) {
356 ((
QWidget *)d->sizeCheckbox)->setWhatsThis(fontSizeWhatsThisText );
358 ((
QWidget *)d->sizeLabel)->setWhatsThis( fontSizeWhatsThisText );
365 d->sizeListBox->fontMetrics().maxWidth() );
366 d->sizeListBox->setMinimumHeight(
369 connect( d->sizeOfFont, SIGNAL(valueChanged(
double)),
370 this, SLOT(_k_size_value_slot(
double)));
373 this, SLOT(_k_size_chosen_slot(
QString)) );
381 d->sampleEdit =
new SampleEdit(page);
382 d->sampleEdit->setAcceptRichText(
false);
384 d->sampleEdit->setFont(tmpFont);
385 d->sampleEdit->setMinimumHeight( d->sampleEdit->fontMetrics().lineSpacing() );
391 d->sampleEdit->setTextCursor(
QTextCursor(d->sampleEdit->document()));
392 QString sampleEditWhatsThisText =
393 i18n(
"This sample text illustrates the current settings. "
394 "You may edit it to test special characters." );
395 d->sampleEdit->setWhatsThis(sampleEditWhatsThisText );
398 this, SLOT(_k_displaySample(
QFont)));
407 if( flags & DisplayFrame )
433 if( sizeIsRelativeState && d->sizeIsRelativeCheckBox )
440 d->sizeListBox->setFocus();
450 d->m_palette.setColor( QPalette::Active, QPalette::Text, col );
451 QPalette pal = d->sampleEdit->palette();
452 pal.
setColor( QPalette::Active, QPalette::Text, col );
453 d->sampleEdit->setPalette( pal );
455 d->sampleEdit->selectAll();
456 d->sampleEdit->setTextColor( col );
457 d->sampleEdit->setTextCursor( cursor );
462 return d->m_palette.color( QPalette::Active, QPalette::Text );
467 d->m_palette.setColor( QPalette::Active, QPalette::Base, col );
468 QPalette pal = d->sampleEdit->palette();
469 pal.
setColor( QPalette::Active, QPalette::Base, col );
470 d->sampleEdit->setPalette( pal );
475 return d->m_palette.color( QPalette::Active, QPalette::Base );
481 if( d->sizeIsRelativeCheckBox ) {
482 if( Qt::PartiallyChecked == relative )
483 d->sizeIsRelativeCheckBox->setCheckState(Qt::PartiallyChecked);
485 d->sizeIsRelativeCheckBox->setCheckState( (Qt::Checked == relative ) ? Qt::Checked : Qt::Unchecked);
491 return d->sizeIsRelativeCheckBox
492 ? d->sizeIsRelativeCheckBox->checkState()
493 : Qt::PartiallyChecked;
498 return d->sampleEdit->toPlainText();
503 d->sampleEdit->setPlainText(text);
508 d->sampleEdit->setVisible( visible );
521 d->familyListBox->setEnabled(state);
525 d->styleListBox->setEnabled(state);
529 d->sizeListBox->setEnabled(state);
530 d->sizeOfFont->setEnabled(state);
539 if (d->selectedSize == -1)
542 if( onlyFixed != d->usingFixed)
544 d->usingFixed = onlyFixed;
545 d->fillFamilyListBox(d->usingFixed);
555 if ( d->familyCheckbox && d->familyCheckbox->isChecked() ) {
559 if ( d->styleCheckbox && d->styleCheckbox->isChecked() ) {
563 if ( d->sizeCheckbox && d->sizeCheckbox->isChecked() ) {
575 void KFontChooser::Private::_k_toggled_checkbox()
577 familyListBox->setEnabled( familyCheckbox->isChecked() );
578 styleListBox->setEnabled( styleCheckbox->isChecked() );
579 sizeListBox->setEnabled( sizeCheckbox->isChecked() );
580 sizeOfFont->setEnabled( sizeCheckbox->isChecked() );
583 void KFontChooser::Private::_k_family_chosen_slot(
const QString& family)
585 if ( !signalsAllowed ) {
588 signalsAllowed =
false;
592 Q_ASSERT( familyListBox->currentItem() );
593 if (familyListBox->currentItem()) {
594 currentFamily = qtFamilies[familyListBox->currentItem()->text()];
598 currentFamily = qtFamilies[family];
615 foreach (
const QString &style, styles) {
619 QFont testFont = dbase.
font(currentFamily, style, 10);
631 if (!filteredStyles.
contains(fstyle)) {
632 filteredStyles.
append(fstyle);
633 qtStyles.insert(fstyle, style);
634 styleIDs.insert(fstyle, styleIdentifier(testFont));
637 styleListBox->clear();
638 styleListBox->addItems(filteredStyles);
641 int listPos = filteredStyles.
indexOf(selectedStyle.isEmpty() ?
I18NC_NOX(
"QFontDatabase",
"Normal") : selectedStyle);
647 for (
int i = 0; i < 2; ++i) {
648 int pos = selectedStyle.indexOf(styleIt);
652 listPos = filteredStyles.
indexOf(style);
653 if (listPos >= 0)
break;
655 qSwap(styleIt, styleOb);
658 styleListBox->setCurrentRow(listPos >= 0 ? listPos : 0);
659 QString currentStyle = qtStyles[styleListBox->currentItem()->text()];
662 qreal currentSize = setupSizeListBox(currentFamily, currentStyle);
663 sizeOfFont->setValue(currentSize);
665 selFont = dbase.
font(currentFamily, currentStyle,
int(currentSize));
666 if (dbase.
isSmoothlyScalable(currentFamily, currentStyle) && selFont.pointSize() == floor(currentSize)) {
669 emit q->fontSelected(selFont);
671 signalsAllowed =
true;
674 void KFontChooser::Private::_k_style_chosen_slot(
const QString& style)
676 if ( !signalsAllowed ) {
679 signalsAllowed =
false;
682 QString currentFamily = qtFamilies[familyListBox->currentItem()->text()];
685 currentStyle = qtStyles[styleListBox->currentItem()->text()];
687 currentStyle = qtStyles[style];
691 qreal currentSize = setupSizeListBox(currentFamily, currentStyle);
692 sizeOfFont->setValue(currentSize);
694 selFont = dbase.
font(currentFamily, currentStyle,
int(currentSize));
695 if (dbase.
isSmoothlyScalable(currentFamily, currentStyle) && selFont.pointSize() == floor(currentSize)) {
698 emit q->fontSelected(selFont);
701 selectedStyle = currentStyle;
704 signalsAllowed =
true;
707 void KFontChooser::Private::_k_size_chosen_slot(
const QString& size)
709 if ( !signalsAllowed ) {
713 signalsAllowed =
false;
723 if (customSizeRow >= 0 && selFont.pointSizeF() != currentSize) {
724 sizeListBox->item(customSizeRow)->setText(standardSizeAtCustom);
728 sizeOfFont->setValue(currentSize);
729 selFont.setPointSizeF(currentSize);
730 emit q->fontSelected(selFont);
733 selectedSize = currentSize;
736 signalsAllowed =
true;
739 void KFontChooser::Private::_k_size_value_slot(
double dval)
741 if ( !signalsAllowed ) {
744 signalsAllowed =
false;
747 qreal val = qreal(dval);
750 QString family = qtFamilies[familyListBox->currentItem()->text()];
751 QString style = qtStyles[styleListBox->currentItem()->text()];
754 if (customSizeRow >= 0 && sizeListBox->currentRow() == customSizeRow) {
755 sizeListBox->item(customSizeRow)->setText(standardSizeAtCustom);
759 bool canCustomize =
true;
765 canCustomize =
false;
766 int nrows = sizeListBox->count();
767 int row = sizeListBox->currentRow();
769 if (val - selFont.pointSizeF() > 0) {
770 for (nrow = row + 1; nrow < nrows; ++nrow)
775 for (nrow = row - 1; nrow >= 0; --nrow)
780 nrow = nrow < 0 ? 0 : nrow >= nrows ? nrows - 1 : nrow;
783 sizeOfFont->setValue(val);
787 int row = nearestSizeRow(val, canCustomize);
788 sizeListBox->setCurrentRow(row);
791 selFont.setPointSizeF(val);
792 emit q->fontSelected( selFont );
794 signalsAllowed =
true;
797 void KFontChooser::Private::_k_displaySample(
const QFont& font )
799 sampleEdit->setFont(font);
802 xlfdEdit->setText(font.
rawName());
803 xlfdEdit->setCursorPosition(0);
810 int KFontChooser::Private::nearestSizeRow (qreal val,
bool customize)
814 for (
int r = 0; r < sizeListBox->count(); ++r) {
816 if (qAbs(cval - val) < diff) {
817 diff = qAbs(cval - val);
822 if (customize && diff > 0) {
824 standardSizeAtCustom = sizeListBox->item(row)->text();
830 qreal KFontChooser::Private::fillSizeList (
const QList<qreal> &sizes_)
832 if ( !sizeListBox ) {
837 bool canCustomize =
false;
838 if (sizes.
count() == 0) {
839 static const int c[] = {
849 for (
int i = 0; c[i]; ++i) {
858 sizeListBox->clear();
860 foreach (qreal size, sizes) {
870 int row = nearestSizeRow(selectedSize, canCustomize);
874 qreal KFontChooser::Private::setupSizeListBox (
const QString& family,
const QString& style)
886 foreach (
int size, smoothSizes) {
893 qreal bestFitSize = fillSizeList(sizes);
899 if ( !selectedSizeList.
isEmpty() ) {
900 sizeListBox->setCurrentItem(selectedSizeList.
first());
907 void KFontChooser::Private::setupDisplay()
911 QString styleID = styleIdentifier(selFont);
912 qreal size = selFont.pointSizeF();
919 numEntries = familyListBox->count();
920 for (i = 0; i < numEntries; i++) {
921 if (family == qtFamilies[familyListBox->item(i)->text()].
toLower()) {
922 familyListBox->setCurrentRow(i);
928 if ( i == numEntries )
932 family = family.
left(family.
indexOf(
'[')).trimmed();
933 for (i = 0; i < numEntries; i++) {
934 if (family == qtFamilies[familyListBox->item(i)->text()].
toLower()) {
935 familyListBox->setCurrentRow(i);
943 if ( i == numEntries )
945 QString fallback = family+
" [";
946 for (i = 0; i < numEntries; i++) {
947 if (qtFamilies[familyListBox->item(i)->text()].toLower().startsWith(fallback)) {
948 familyListBox->setCurrentRow(i);
955 if ( i == numEntries )
957 for (i = 0; i < numEntries; i++) {
958 if (qtFamilies[familyListBox->item(i)->text()].toLower().startsWith(family)) {
959 familyListBox->setCurrentRow(i);
966 if ( i == numEntries ) {
967 familyListBox->setCurrentRow( 0 );
975 numEntries = styleListBox->
count();
976 for (i = 0; i < numEntries; i++) {
977 if (styleID == styleIDs[styleListBox->item(i)->text()]) {
978 styleListBox->setCurrentRow(i);
982 if (i == numEntries) {
984 styleListBox->setCurrentRow(0);
990 QString currentFamily = qtFamilies[familyListBox->currentItem()->text()];
991 QString currentStyle = qtStyles[styleListBox->currentItem()->text()];
993 sizeListBox->setCurrentRow(nearestSizeRow(size, canCustomize));
996 sizeOfFont->setValue(
KGlobal::locale()->readNumber(sizeListBox->currentItem()->text()));
1006 if (fontListCriteria)
1021 if (lstFonts.
count() == 0)
1022 lstFonts.
append(
"fixed");
1033 void KFontChooser::Private::setFamilyBoxItems(
const QStringList &fonts)
1035 signalsAllowed =
false;
1038 familyListBox->clear();
1039 familyListBox->addItems(trfonts);
1041 signalsAllowed =
true;
1044 void KFontChooser::Private::fillFamilyListBox(
bool onlyFixedFonts)
1047 getFontList(fontList, onlyFixedFonts?FixedWidthFonts:0);
1048 setFamilyBoxItems(fontList);
1051 void KFontChooser::Private::_k_showXLFDArea(
bool show)
1055 xlfdEdit->parentWidget()->show();
1059 xlfdEdit->parentWidget()->hide();
1069 QString KFontChooser::Private::styleIdentifier(
const QFont &font)
1074 +
QString::number(font.stretch());
1077 #include "kfontchooser.moc"
1078 #include "sampleedit_p.moc"
QString i18n(const char *text)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
static QString formatFontSize(qreal size)
void setColor(ColorGroup group, ColorRole role, const QColor &color)
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
static void getFontList(QStringList &list, uint fontListCriteria)
Creates a list of font strings.
void splitFontString(const QString &name, QString *family, QString *foundry)
bool contains(const QString &str, Qt::CaseSensitivity cs) const
bool isSmoothlyScalable(const QString &family, const QString &style) const
void setColor(const QColor &col)
Sets the color to use in the preview.
void addSpacing(int size)
KLocalizedString inContext(const QString &key, const QString &text) const
Qt::CheckState sizeIsRelative() const
QStringList styles(const QString &family) const
void setSampleText(const QString &text)
Sets the sample text.
void setSpacing(int spacing)
QString i18nc(const char *ctxt, const char *text)
FontDiffFlags fontDiffFlags() const
KSharedConfigPtr config()
QColor backgroundColor() const
QString number(int n, int base)
int count(const T &value) const
void enableColumn(int column, bool state)
Enables or disable a font column in the chooser.
void append(const T &value)
double readNumber(const QString &numStr, bool *ok=0) const
void setChildrenCollapsible(bool)
void setFont(const QFont &font, bool onlyFixed=false)
Sets the currently selected font in the chooser.
static int spacingHint()
Returns the number of pixels that should be used between widgets inside a dialog according to the KDE...
int removeAll(const T &value)
static int minimumListWidth(const QListWidget *list)
KFontChooser(QWidget *parent=0L, const DisplayFlags &flags=DisplayFrame, const QStringList &fontList=QStringList(), int visibleListSize=8, Qt::CheckState *sizeIsRelativeState=0L)
Constructs a font picker widget.
void setMargin(int margin)
void setSizeIsRelative(Qt::CheckState relative)
Sets the state of the checkbox indicating whether the font size is to be interpreted as relative size...
QList< int > smoothSizes(const QString &family, const QString &style)
QString styleString(const QFont &font)
bool contains(QChar ch, Qt::CaseSensitivity cs) const
int width(const QString &text, int len) const
void setPointSizeF(qreal pointSize)
void addLayout(QLayout *layout, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QString & replace(int position, int n, QChar after)
An enhanced QLineEdit widget for inputting text.
static QFont generalFont()
Returns the default general font.
QString sampleText() const
virtual QSize sizeHint(void) const
Reimplemented for internal reasons.
QStringList translateFontNameList(const QStringList &names, QHash< QString, QString > *trToRawNames)
QStringList families(WritingSystem writingSystem) const
void setColumnStretch(int column, int stretch)
void setBackgroundColor(const QColor &col)
Sets the background color to use in the preview.
void fontSelected(const QFont &font)
Emitted whenever the selected font changes.
KLocalizedString ki18nc(const char *ctxt, const char *msg)
static int minimumListHeight(const QListWidget *list, int numVisibleEntry)
QString left(int n) const
KLocalizedString subs(int a, int fieldWidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const
int indexOf(const QRegExp &rx, int from) const
virtual ~KFontChooser()
Destructs the font chooser.
bool isBitmapScalable(const QString &family, const QString &style) const
QString formatNumber(double num, int precision=-1) const
bool isFixedPitch(const QString &family, const QString &style) const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QFont font(const QString &family, const QString &style, int pointSize) const
void setSampleBoxVisible(bool visible)
Shows or hides the sample text box.
T readEntry(const QString &key, const T &aDefault) const
static QFont fixedFont()
Returns the default fixed font.