22 #include "fonthelpers_p.h"
33 #include <QFontDatabase>
35 #include <QAbstractItemDelegate>
36 #include <QStringListModel>
49 "The Quick Brown Fox Jumps Over The Lazy Dog");
52 class KFontFamilyDelegate :
public QAbstractItemDelegate
56 explicit KFontFamilyDelegate (
QObject *parent);
58 void paint (QPainter *painter,
59 const QStyleOptionViewItem &option,
60 const QModelIndex &index)
const;
62 QSize sizeHint (
const QStyleOptionViewItem &option,
63 const QModelIndex &index)
const;
67 double sizeFactFamily;
68 double sizeFactSample;
73 KFontFamilyDelegate::KFontFamilyDelegate (
QObject *parent)
74 : QAbstractItemDelegate(parent)
76 truetype = QIcon(QLatin1String(
":/trolltech/styles/commonstyle/images/fonttruetype-16.png"));
77 bitmap = QIcon(QLatin1String(
":/trolltech/styles/commonstyle/images/fontbitmap-16.png"));
85 void KFontFamilyDelegate::paint (QPainter *painter,
86 const QStyleOptionViewItem &option,
87 const QModelIndex &index)
const
90 if (option.state & QStyle::State_Selected) {
92 painter->setBrush(option.palette.highlight());
93 painter->setPen(Qt::NoPen);
94 painter->drawRect(option.rect);
95 painter->setPen(QPen(option.palette.highlightedText(), 0));
96 sampleBrush = option.palette.highlightedText();
102 QString trFontFamily = index.data(Qt::DisplayRole).toString();
103 QString fontFamily = fontFamilyTrMap[trFontFamily];
111 bool canShowLanguageSample =
true;
112 if (availableSystems.count() > 0) {
113 canShowLanguageSample =
false;
114 QString scriptsSpec =
i18nc(
"Numeric IDs of scripts for font previews",
122 foreach (
const QString &scriptStrId, scriptStrIds) {
124 int ws = scriptStrId.toInt(&convOk);
125 if ( convOk && ws > 0 && ws < QFontDatabase::WritingSystemsCount
126 && availableSystems.contains(static_cast<QFontDatabase::WritingSystem>(ws))) {
127 canShowLanguageSample =
true;
134 const QIcon *icon = &bitmap;
135 if (QFontDatabase().isSmoothlyScalable(fontFamily)) {
138 QRect r = option.rect;
139 icon->paint(painter, r, Qt::AlignLeft|Qt::AlignTop);
143 if (option.direction == Qt::RightToLeft) {
144 r.setRight(r.right() - actualSize.width() - 4);
146 r.setLeft(r.left() + actualSize.width() + 4);
150 QFont oldPainterFont = painter->font();
151 QFont familyFont = baseFont;
152 familyFont.setPointSizeF(familyFont.pointSizeF() * sizeFactFamily);
153 painter->setFont(familyFont);
154 painter->drawText(r, Qt::AlignTop|Qt::AlignLeading|Qt::TextSingleLine, trFontFamily);
157 int h = painter->fontMetrics().lineSpacing();
158 r.setTop(r.top() + h);
164 if (canShowLanguageSample) {
167 foreach (
const QFontDatabase::WritingSystem &ws, availableSystems) {
168 sample += QFontDatabase::writingSystemSample(ws) +
" ";
169 if (sample.length() > 40) {
173 sample = sample.trimmed();
176 sampleFont.setFamily(fontFamily);
177 sampleFont.setPointSizeF(sampleFont.pointSizeF() * sizeFactSample);
178 painter->setFont(sampleFont);
179 QPen oldPen = painter->pen();
180 painter->setPen(sampleBrush.color());
181 painter->drawText(r, Qt::AlignTop|Qt::AlignLeading|Qt::TextSingleLine, sample);
182 painter->setFont(oldPainterFont);
183 painter->setPen(oldPen);
185 if (option.state & QStyle::State_Selected) {
190 QSize KFontFamilyDelegate::sizeHint (
const QStyleOptionViewItem &option,
191 const QModelIndex &index)
const
196 QString trFontFamily = index.data(Qt::DisplayRole).toString();
197 QString fontFamily = fontFamilyTrMap[trFontFamily];
199 QFont familyFont = baseFont;
200 familyFont.setPointSizeF(familyFont.pointSizeF() * sizeFactFamily);
201 QFontMetrics familyMetrics(familyFont);
203 QFont sampleFont = baseFont;
204 sampleFont.setFamily(fontFamily);
205 sampleFont.setPointSizeF(sampleFont.pointSizeF() * sizeFactSample);
206 QFontMetrics sampleMetrics(sampleFont);
210 return QSize(qMax(familyMetrics.width(trFontFamily), sampleMetrics.width(sample)),
211 qRound(familyMetrics.lineSpacing() + sampleMetrics.lineSpacing() * 1.2));
214 class KFontComboBoxPrivate
218 void updateDatabase ();
219 void updateIndexToFont ();
220 void _k_currentFontChanged (
int index);
226 KFontFamilyDelegate *delegate;
227 QStringListModel *model;
231 KFontComboBoxPrivate::KFontComboBoxPrivate (
KFontComboBox *parent)
239 void KFontComboBoxPrivate::updateDatabase ()
242 if (fontList.isEmpty()) {
248 delegate->fontFamilyTrMap.clear();
253 model->setStringList(trFontFamilies);
256 completion->
setItems(trFontFamilies);
261 void KFontComboBoxPrivate::updateIndexToFont ()
264 QString selectedFontFamily = QFontInfo(currentFont).family();
266 const QStringList trFontFamilies = model->stringList();
267 if (!trFontFamilies.count()) {
273 foreach (
const QString &trFontFamily, trFontFamilies) {
274 if (trSelectedFontFamily == trFontFamily) {
279 if (index == trFontFamilies.count()) {
282 currentFont =
QFont(delegate->fontFamilyTrMap[trFontFamilies[0]]);
283 emit k->currentFontChanged(currentFont);
287 signalsAllowed =
false;
288 k->setCurrentIndex(index);
289 signalsAllowed =
true;
292 void KFontComboBoxPrivate::_k_currentFontChanged (
int index)
294 if (!signalsAllowed) {
298 QString trFontFamily = k->itemText(index);
299 QString fontFamily = delegate->fontFamilyTrMap[trFontFamily];
300 if (!fontFamily.isEmpty()) {
301 currentFont =
QFont(fontFamily);
302 emit k->currentFontChanged(currentFont);
307 lst.removeAll(trFontFamily);
308 model->setStringList(lst);
313 :
KComboBox(true, parent), d(new KFontComboBoxPrivate(this))
316 setInsertPolicy(QComboBox::NoInsert);
319 d->delegate =
new KFontFamilyDelegate(
this);
320 setItemDelegate(d->delegate);
321 d->model =
new QStringListModel(
this);
325 connect(
this, SIGNAL(currentIndexChanged(
int)),
326 this, SLOT(_k_currentFontChanged(
int)));
330 d->updateIndexToFont();
340 if (onlyFixed != d->onlyFixed) {
341 d->onlyFixed = onlyFixed;
348 if (fontList != d->fontList) {
349 d->fontList = fontList;
356 return d->currentFont;
361 if (font != d->currentFont) {
362 d->currentFont = font;
364 d->updateIndexToFont();
370 if (e->type() == QEvent::Resize) {
375 if (sample.length() > 60) {
376 sample = sample.left(57) +
"...";
379 approxFont.setPointSizeF(approxFont.pointSizeF()
380 * d->delegate->sizeFactSample);
381 int widgetWidth = width();
382 int sampleWidth = QFontMetrics(approxFont).width(sample);
383 sampleWidth = qRound(sampleWidth * 1.1);
384 int iconWidth = d->delegate->truetype.actualSize(size()).width();
386 if (lview->verticalScrollBar()) {
387 vsbarWidth = lview->verticalScrollBar()->width();
389 lview->window()->setFixedWidth( qMax(widgetWidth, sampleWidth)
390 + iconWidth + vsbarWidth);
393 return KComboBox::event(e);
398 QSize sz = KComboBox::sizeHint();
400 sz.setWidth(fm.width(
"m") * 14);
404 #include "kfontcombobox.moc"
405 #include "moc_kfontcombobox.moc"
virtual QSize sizeHint() const
The recommended size of the widget.
A lightweight font selection widget.
void setOnlyFixed(bool onlyFixed)
Toggle selectable fonts to be only those of fixed width or all.
static void getFontList(QStringList &list, uint fontListCriteria)
Creates a list of font strings.
Second color; for example, comments, items which are old, inactive or disabled.
QString i18nc(const char *ctxt, const char *text)
QBrush foreground(ForegroundRole=NormalText) const
Retrieve the requested foreground brush.
A generic class for completing QStrings.
KAction * actualSize(const QObject *recvr, const char *slot, QObject *parent)
View the document at its actual size.
void setFontList(const QStringList &fontList)
Set selectable fonts to be only those present in the list.
virtual void setIgnoreCase(bool ignoreCase)
Setting this to true makes KCompletion behave case insensitively.
virtual void setItems(const QStringList &list)
Sets the list of items available for completion.
void currentFontChanged(const QFont &font)
Emitted when a new font has been selected, either through user input or by setFont().
Access the KDE global configuration.
static QFont generalFont()
Returns the default general font.
QStringList translateFontNameList(const QStringList &names, QHash< QString, QString > *trToRawNames)
A set of methods used to work with colors.
KFontComboBox(QWidget *parent=0)
Constructor.
QString translateFontName(const QString &name)
static QString alphabetSample()
virtual ~KFontComboBox()
Destructor.
void setCurrentFont(const QFont &font)
Set the font to show as selected in the combobox.
const KShortcut & completion()
Complete text in input widgets.
QFont currentFont() const
The font currently selected from the list.