From 71fa98279755dbd5fdd658870e935803e8ae36c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petri=20Damst=C3=A9n?= Date: Tue, 10 Nov 2009 09:46:14 +0000 Subject: [PATCH] * Use correct font for size hint in Frame * Use correct font in IconWidget setLayoutOptions * layout fonts are set in IconWidget setLayoutOptions and layoutTextItems. No need to set them in paint. * Fix using QGraphicsWidget font when customFont == true in several widgets. svn path=/trunk/KDE/kdelibs/; revision=1047017 --- widgets/combobox.cpp | 6 +++--- widgets/frame.cpp | 35 ++++++++++++++----------------- widgets/iconwidget.cpp | 47 +++++++++++------------------------------- widgets/iconwidget_p.h | 3 ++- widgets/pushbutton.cpp | 4 ++-- widgets/tabbar.cpp | 2 +- widgets/toolbutton.cpp | 4 ++-- 7 files changed, 38 insertions(+), 63 deletions(-) diff --git a/widgets/combobox.cpp b/widgets/combobox.cpp index 0d4a51960..56b4d00a7 100644 --- a/widgets/combobox.cpp +++ b/widgets/combobox.cpp @@ -42,7 +42,7 @@ public: ComboBoxPrivate(ComboBox *comboBox) : q(comboBox), background(0), - customFont(0), + customFont(false), underMouse(false) { } @@ -101,9 +101,9 @@ void ComboBoxPrivate::syncBorders() KComboBox *native = q->nativeWidget(); if (customFont) { - native->setFont(Theme::defaultTheme()->font(Theme::DefaultFont)); - } else { native->setFont(q->font()); + } else { + native->setFont(Theme::defaultTheme()->font(Theme::DefaultFont)); } } diff --git a/widgets/frame.cpp b/widgets/frame.cpp index 84f435734..fad77e1ed 100644 --- a/widgets/frame.cpp +++ b/widgets/frame.cpp @@ -43,7 +43,8 @@ public: : q(parent), svg(0), image(0), - pixmap(0) + pixmap(0), + customFont(false) { } @@ -53,6 +54,7 @@ public: } void syncBorders(); + QFont widgetFont() const; Frame *q; FrameSvg *svg; @@ -66,6 +68,15 @@ public: bool customFont; }; +QFont FramePrivate::widgetFont() const +{ + if (customFont) { + return q->font(); + } else { + return Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); + } +} + void FramePrivate::syncBorders() { //set margins from the normal element @@ -73,15 +84,8 @@ void FramePrivate::syncBorders() svg->getMargins(left, top, right, bottom); - QFont widgetFont; - if (customFont) { - widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); - } else { - widgetFont = q->font(); - } - if (!text.isNull()) { - QFontMetricsF fm(widgetFont); + QFontMetricsF fm(widgetFont()); top += fm.height(); } @@ -225,18 +229,11 @@ void Frame::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWi d->svg->paintFrame(painter); - QFont widgetFont; - if (d->customFont) { - widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); - } else { - widgetFont = font(); - } - if (!d->text.isNull()) { - QFontMetricsF fm(widgetFont); + QFontMetricsF fm(d->widgetFont()); QRectF textRect = d->svg->contentsRect(); textRect.setHeight(fm.height()); - painter->setFont(widgetFont); + painter->setFont(d->widgetFont()); painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::TextColor)); painter->drawText(textRect, Qt::AlignHCenter|Qt::AlignTop, d->text); } @@ -264,7 +261,7 @@ QSizeF Frame::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const QSizeF hint = QGraphicsWidget::sizeHint(which, constraint); if (!d->image && !layout()) { - QFontMetricsF fm(QApplication::font()); + QFontMetricsF fm(d->widgetFont()); qreal left, top, right, bottom; d->svg->getMargins(left, top, right, bottom); diff --git a/widgets/iconwidget.cpp b/widgets/iconwidget.cpp index fe917c2c4..c3c2cdd25 100644 --- a/widgets/iconwidget.cpp +++ b/widgets/iconwidget.cpp @@ -118,6 +118,15 @@ void IconWidgetPrivate::iconConfigChanged() } } +QFont IconWidgetPrivate::widgetFont() const +{ + if (customFont) { + return q->font(); + } else { + return Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); + } +} + IconAction::IconAction(IconWidget *icon, QAction *action) : m_icon(icon), m_action(action), @@ -477,16 +486,9 @@ QSizeF IconWidgetPrivate::displaySizeHint(const QStyleOptionGraphicsItem *option horizontalMargin[IconWidgetPrivate::TextMargin].left - horizontalMargin[IconWidgetPrivate::TextMargin].right; - QFont widgetFont; - if (customFont) { - widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); - } else { - widgetFont = q->font(); - } - //allow only five lines of text const qreal maxHeight = - numDisplayLines * QFontMetrics(widgetFont).lineSpacing(); + numDisplayLines * QFontMetrics(widgetFont()).lineSpacing(); // To compute the nominal size for the label + info, we'll just append // the information string to the label @@ -496,7 +498,7 @@ QSizeF IconWidgetPrivate::displaySizeHint(const QStyleOptionGraphicsItem *option QTextLayout layout; setLayoutOptions(layout, option, q->orientation()); - layout.setFont(widgetFont); + layout.setFont(widgetFont()); QSizeF size = layoutText(layout, option, label, QSizeF(textWidth, maxHeight)); return addMargin(size, TextMargin); @@ -540,15 +542,7 @@ void IconWidgetPrivate::layoutIcons(const QStyleOptionGraphicsItem *option) } iconWidth -= horizontalMargin[IconWidgetPrivate::ItemMargin].left + horizontalMargin[IconWidgetPrivate::ItemMargin].right; } else { - QFont widgetFont; - if (customFont) { - widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); - } else { - widgetFont = q->font(); - } //Horizontal layout - QFontMetricsF fm = QFontMetrics(widgetFont); - //if there is text resize the icon in order to make room for the text if (text.isEmpty() && infoText.isEmpty()) { // with no text, we just take up the whole geometry @@ -1010,20 +1004,10 @@ void IconWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option } } - QFont widgetFont; - if (d->customFont) { - widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); - } else { - widgetFont = font(); - } - // Draw text last because it is overlayed QTextLayout labelLayout, infoLayout; - labelLayout.setFont(widgetFont); - infoLayout.setFont(widgetFont); QRectF textBoundingRect; - d->layoutTextItems(option, icon, &labelLayout, &infoLayout, &textBoundingRect); QImage shadow(textBoundingRect.size().toSize() + QSize(4, 4), @@ -1352,14 +1336,7 @@ QSizeF IconWidget::sizeFromIconSize(const qreal iconWidth) const IconWidgetPrivate::ItemMargin); } - QFont widgetFont; - if (d->customFont) { - widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); - } else { - widgetFont = font(); - } - - QFontMetricsF fm(widgetFont); + QFontMetricsF fm(d->widgetFont()); qreal width = 0; if (d->orientation == Qt::Vertical) { diff --git a/widgets/iconwidget_p.h b/widgets/iconwidget_p.h index eca41ce16..be1b5cee3 100644 --- a/widgets/iconwidget_p.h +++ b/widgets/iconwidget_p.h @@ -187,6 +187,7 @@ public: void readColors(); void colorConfigChanged(); void iconConfigChanged(); + QFont widgetFont() const; void hoverAnimationUpdate(qreal progress); void init(); void layoutIcons(const QStyleOptionGraphicsItem *option); @@ -246,7 +247,7 @@ void IconWidgetPrivate::setLayoutOptions(QTextLayout &layout, textoption.setWrapMode(QTextOption::WordWrap); // NOTE: assumption as well - layout.setFont(QApplication::font()); // NOTE: find better ways to get the font + layout.setFont(widgetFont()); layout.setTextOption(textoption); } diff --git a/widgets/pushbutton.cpp b/widgets/pushbutton.cpp index f23dcb4b0..1791b6247 100644 --- a/widgets/pushbutton.cpp +++ b/widgets/pushbutton.cpp @@ -410,9 +410,9 @@ void PushButton::paint(QPainter *painter, QFont widgetFont; if (d->customFont) { - widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); - } else { widgetFont = font(); + } else { + widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); } //if there is not enough room for the text make it to fade out diff --git a/widgets/tabbar.cpp b/widgets/tabbar.cpp index 521d87192..4ec489afa 100644 --- a/widgets/tabbar.cpp +++ b/widgets/tabbar.cpp @@ -77,7 +77,7 @@ public: newPage(0), oldPageAnimId(-1), newPageAnimId(-1), - customFont(true) + customFont(false) { } diff --git a/widgets/toolbutton.cpp b/widgets/toolbutton.cpp index 44aaad8fe..86e2ec3d8 100644 --- a/widgets/toolbutton.cpp +++ b/widgets/toolbutton.cpp @@ -365,9 +365,9 @@ void ToolButton::paint(QPainter *painter, QFont widgetFont; if (d->customFont) { - widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); - } else { widgetFont = font(); + } else { + widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); } buttonOpt.font = widgetFont;