diff --git a/widgets/iconwidget.cpp b/widgets/iconwidget.cpp index ecd2897c9..0e5ced41b 100644 --- a/widgets/iconwidget.cpp +++ b/widgets/iconwidget.cpp @@ -349,6 +349,7 @@ void IconWidgetPrivate::init() setActiveMargins(); currentSize = QSizeF(-1, -1); + q->setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont)); } void IconWidget::addIconAction(QAction *action) @@ -482,6 +483,7 @@ QSizeF IconWidgetPrivate::displaySizeHint(const QStyleOptionGraphicsItem *option QTextLayout layout; setLayoutOptions(layout, option, q->orientation()); + layout.setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont)); QSizeF size = layoutText(layout, option, label, QSizeF(textWidth, maxHeight)); return addMargin(size, TextMargin); @@ -526,7 +528,7 @@ void IconWidgetPrivate::layoutIcons(const QStyleOptionGraphicsItem *option) iconWidth -= horizontalMargin[IconWidgetPrivate::ItemMargin].left + horizontalMargin[IconWidgetPrivate::ItemMargin].right; } else { //Horizontal layout - QFontMetricsF fm(q->font()); + QFontMetricsF fm = Plasma::Theme::defaultTheme()->fontMetrics(); //if there is text resize the icon in order to make room for the text if (text.isEmpty() && infoText.isEmpty()) { @@ -991,6 +993,8 @@ void IconWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option // Draw text last because it is overlayed QTextLayout labelLayout, infoLayout; + labelLayout.setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont)); + infoLayout.setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont)); QRectF textBoundingRect; @@ -1308,6 +1312,7 @@ bool IconWidget::invertedLayout() const QSizeF IconWidget::sizeFromIconSize(const qreal iconWidth) const { + d->setActiveMargins(); if (d->text.isEmpty() && d->infoText.isEmpty()) { //no text, less calculations return d->addMargin(d->addMargin(QSizeF(iconWidth, iconWidth), IconWidgetPrivate::IconMargin), @@ -1319,8 +1324,8 @@ QSizeF IconWidget::sizeFromIconSize(const qreal iconWidth) const if (d->orientation == Qt::Vertical) { // make room for at most 14 characters - width = qMax(fm.width(d->text.left(12)), - fm.width(d->infoText.left(12))) + + width = qMax(d->maxWordWidth(d->text), + d->maxWordWidth(d->infoText)) + fm.width("xx") + d->horizontalMargin[IconWidgetPrivate::TextMargin].left + d->horizontalMargin[IconWidgetPrivate::TextMargin].right; diff --git a/widgets/iconwidget_p.h b/widgets/iconwidget_p.h index 18e49bfd8..2d0ad0260 100644 --- a/widgets/iconwidget_p.h +++ b/widgets/iconwidget_p.h @@ -31,6 +31,7 @@ #include #include +#include #include #include "iconwidget.h" @@ -146,6 +147,8 @@ public: const QPixmap &icon, QTextLayout *labelLayout, QTextLayout *infoLayout, QRectF *textBoundingRect) const; + int maxWordWidth(const QString text) const; + inline void setLayoutOptions(QTextLayout &layout, const QStyleOptionGraphicsItem *options, const Qt::Orientation orientation) const; @@ -265,6 +268,15 @@ Qt::LayoutDirection IconWidgetPrivate::iconDirection(const QStyleOptionGraphicsI void IconWidgetPrivate::setActiveMargins() { + //sync here itemmargin with contentsrect, not overly pretty but it's where it's more reliable + qreal left, top, right, bottom; + q->getContentsMargins(&left, &top, &right, &bottom); + if (left || top || right || bottom) { + verticalMargin[ItemMargin].left = horizontalMargin[ItemMargin].left = left; + verticalMargin[ItemMargin].top = horizontalMargin[ItemMargin].top = top; + verticalMargin[ItemMargin].right = horizontalMargin[ItemMargin].right = right; + verticalMargin[ItemMargin].bottom = horizontalMargin[ItemMargin].bottom = bottom; + } activeMargins = (orientation == Qt::Horizontal ? horizontalMargin : verticalMargin); } @@ -300,56 +312,44 @@ QRectF IconWidgetPrivate::addMargin(const QRectF &rect, MarginType type) const { Q_ASSERT(activeMargins); - qreal left, top, right, bottom; - q->getContentsMargins(&left, &top, &right, &bottom); - if (type == ItemMargin && (left || top || right || bottom)) { - return rect.adjusted(-left, -top, right, bottom); - } else { - const Margin &m = activeMargins[type]; - return rect.adjusted(-m.left, -m.top, m.right, m.bottom); - } + const Margin &m = activeMargins[type]; + return rect.adjusted(-m.left, -m.top, m.right, m.bottom); } QRectF IconWidgetPrivate::subtractMargin(const QRectF &rect, MarginType type) const { Q_ASSERT(activeMargins); - qreal left, top, right, bottom; - q->getContentsMargins(&left, &top, &right, &bottom); - if (type == ItemMargin && (left || top || right || bottom)) { - return rect.adjusted(left, top, -right, -bottom); - } else { - const Margin &m = activeMargins[type]; - return rect.adjusted(m.left, m.top, -m.right, -m.bottom); - } + const Margin &m = activeMargins[type]; + return rect.adjusted(m.left, m.top, -m.right, -m.bottom); } QSizeF IconWidgetPrivate::addMargin(const QSizeF &size, MarginType type) const { Q_ASSERT(activeMargins); - qreal left, top, right, bottom; - q->getContentsMargins(&left, &top, &right, &bottom); - if (type == ItemMargin && (left || top || right || bottom)) { - return QSizeF(size.width() + left + right, size.height() + top + bottom); - } else { - const Margin &m = activeMargins[type]; - return QSizeF(size.width() + m.left + m.right, size.height() + m.top + m.bottom); - } + const Margin &m = activeMargins[type]; + return QSizeF(size.width() + m.left + m.right, size.height() + m.top + m.bottom); } QSizeF IconWidgetPrivate::subtractMargin(const QSizeF &size, MarginType type) const { Q_ASSERT(activeMargins); - qreal left, top, right, bottom; - q->getContentsMargins(&left, &top, &right, &bottom); - if (type == ItemMargin && (left || top || right || bottom)) { - return QSizeF(size.width() - left - right, size.height() - top - bottom); - } else { - const Margin &m = activeMargins[type]; - return QSizeF(size.width() - m.left - m.right, size.height() - m.top - m.bottom); + const Margin &m = activeMargins[type]; + return QSizeF(size.width() - m.left - m.right, size.height() - m.top - m.bottom); +} + +int IconWidgetPrivate::maxWordWidth(const QString text) const +{ + QFontMetricsF fm = Plasma::Theme::defaultTheme()->fontMetrics(); + QStringList words = text.split(' '); + + qreal maxWidth = 0; + foreach (QString word, words) { + maxWidth = qMax(maxWidth, fm.width(word)); } + return maxWidth; } QRectF IconWidgetPrivate::actionRect(ActionPosition position) const