diff --git tools/shared/fontpanel/fontpanel.cpp tools/shared/fontpanel/fontpanel.cpp index c92d09f..cdd2850 100644 --- tools/shared/fontpanel/fontpanel.cpp +++ tools/shared/fontpanel/fontpanel.cpp @@ -49,6 +49,11 @@ #include #include +#undef FONTPANEL_DEBUG +#ifdef FONTPANEL_DEBUG +#include +#endif + QT_BEGIN_NAMESPACE FontPanel::FontPanel(QWidget *parentWidget) : @@ -124,13 +129,29 @@ void FontPanel::setSelectedFont(const QFont &f) m_familyComboBox->setCurrentFont(f); } - updateFamily(family()); + updateFamily(family(), f.pointSize()); const int pointSizeIndex = closestPointSizeIndex(f.pointSize()); m_pointSizeComboBox->setCurrentIndex( pointSizeIndex); - const QString styleString = m_fontDatabase.styleString(f); - const int styleIndex = m_styleComboBox->findText(styleString); +#ifdef FONTPANEL_DEBUG + QList > items; + for (int i = 0 ; i < m_styleComboBox->count() ; ++i) { + items << QPair(m_styleComboBox->itemText(i), m_styleComboBox->itemData(i)); + } +#endif + int styleIndex = m_styleComboBox->findData(f); + if (styleIndex < 0) { + // use the style name synonym resolution in QFontDatabase::font() to obtain the true styleName of the requested font, + // rather than the Qt-standardised name obtained through QFontDatabase::styleString() + const QString styleString = m_fontDatabase.font(family(), m_fontDatabase.styleString(f), pointSize()).styleName(); + styleIndex = m_styleComboBox->findText(styleString); +#ifdef FONTPANEL_DEBUG + qDebug() << "FontPanel::setSelectedFont" << f << "found style" << styleString << "in" << items << "@" << styleIndex; + } else { + qDebug() << "FontPanel::setSelectedFont" << f << "found in" << items << "@" << styleIndex; +#endif + } m_styleComboBox->setCurrentIndex(styleIndex); slotUpdatePreviewFont(); } @@ -175,14 +196,19 @@ void FontPanel::slotWritingSystemChanged(int) delayedPreviewFontUpdate(); } -void FontPanel::slotFamilyChanged(const QFont &) +void FontPanel::slotFamilyChanged(const QFont &f) { - updateFamily(family()); + updateFamily(family(), f.pointSize()); delayedPreviewFontUpdate(); } -void FontPanel::slotStyleChanged(int) +void FontPanel::slotStyleChanged(int i) { +#ifdef FONTPANEL_DEBUG + qDebug() << "FontPanel::slotStyleChanged" << i << styleString(); +#else + Q_UNUSED(i); +#endif updatePointSizes(family(), styleString()); delayedPreviewFontUpdate(); } @@ -204,11 +230,13 @@ void FontPanel::updateWritingSystem(QFontDatabase::WritingSystem ws) } } -void FontPanel::updateFamily(const QString &family) +void FontPanel::updateFamily(const QString &family, int ptSize) { // Update styles and trigger update of point sizes. // Try to maintain selection or select normal const QString oldStyleString = styleString(); + // use QFontDatabase::font() to look up the style name `family` gives to `oldStyleString` + const QString realStyleString = m_fontDatabase.font(family, oldStyleString, pointSize()).styleName(); const QStringList styles = m_fontDatabase.styles(family); const bool hasStyles = !styles.empty(); @@ -219,17 +247,39 @@ void FontPanel::updateFamily(const QString &family) int normalIndex = -1; const QString normalStyle = QLatin1String("Normal"); + if (ptSize < 0) { + ptSize = pointSize(); + } if (hasStyles) { +#ifdef FONTPANEL_DEBUG + qDebug() << "FontPanel::updateFamily" << family << "matching" << realStyleString + << "(instead of" << oldStyleString << ") in family styles" << styles; +#endif foreach (const QString &style, styles) { // try to maintain selection or select 'normal' preferably const int newIndex = m_styleComboBox->count(); - m_styleComboBox->addItem(style); - if (oldStyleString == style) { + // create a QFont with the current style,size properties and associate it + // with the style name in the combo box on success. + QFont g = m_fontDatabase.font(family, style, ptSize); +#ifdef FONTPANEL_DEBUG + qDebug() << "style" << style << "gives font" << g << "," << g.styleName(); +#endif + if (g.styleName() == style) { + m_styleComboBox->addItem(style, g); + } else { + m_styleComboBox->addItem(style, QFont()); + } + if (realStyleString == style) { m_styleComboBox->setCurrentIndex(newIndex); } else { - if (oldStyleString == normalStyle) + if (oldStyleString == normalStyle) { +#ifdef FONTPANEL_DEBUG + // this block stymies me ... + qDebug() << oldStyleString << ": setting normalIndex from" << normalIndex << "to" << newIndex; +#endif normalIndex = newIndex; + } } } if (m_styleComboBox->currentIndex() == -1 && normalIndex != -1) diff --git tools/shared/fontpanel/fontpanel.h tools/shared/fontpanel/fontpanel.h index 7d178b7..5ba1cfc 100644 --- tools/shared/fontpanel/fontpanel.h +++ tools/shared/fontpanel/fontpanel.h @@ -90,7 +90,7 @@ private: int closestPointSizeIndex(int ps) const; void updateWritingSystem(QFontDatabase::WritingSystem ws); - void updateFamily(const QString &family); + void updateFamily(const QString &family, int ptSize=-1); void updatePointSizes(const QString &family, const QString &style); void delayedPreviewFontUpdate();