use the width of the largest word as the width size hint

svn path=/trunk/KDE/kdelibs/; revision=1022099
This commit is contained in:
Marco Martin 2009-09-10 19:55:29 +00:00
parent f3ef2e4a0f
commit 664a262df2
2 changed files with 39 additions and 34 deletions

View File

@ -349,6 +349,7 @@ void IconWidgetPrivate::init()
setActiveMargins(); setActiveMargins();
currentSize = QSizeF(-1, -1); currentSize = QSizeF(-1, -1);
q->setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
} }
void IconWidget::addIconAction(QAction *action) void IconWidget::addIconAction(QAction *action)
@ -482,6 +483,7 @@ QSizeF IconWidgetPrivate::displaySizeHint(const QStyleOptionGraphicsItem *option
QTextLayout layout; QTextLayout layout;
setLayoutOptions(layout, option, q->orientation()); setLayoutOptions(layout, option, q->orientation());
layout.setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
QSizeF size = layoutText(layout, option, label, QSizeF(textWidth, maxHeight)); QSizeF size = layoutText(layout, option, label, QSizeF(textWidth, maxHeight));
return addMargin(size, TextMargin); return addMargin(size, TextMargin);
@ -526,7 +528,7 @@ void IconWidgetPrivate::layoutIcons(const QStyleOptionGraphicsItem *option)
iconWidth -= horizontalMargin[IconWidgetPrivate::ItemMargin].left + horizontalMargin[IconWidgetPrivate::ItemMargin].right; iconWidth -= horizontalMargin[IconWidgetPrivate::ItemMargin].left + horizontalMargin[IconWidgetPrivate::ItemMargin].right;
} else { } else {
//Horizontal layout //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 there is text resize the icon in order to make room for the text
if (text.isEmpty() && infoText.isEmpty()) { if (text.isEmpty() && infoText.isEmpty()) {
@ -991,6 +993,8 @@ void IconWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
// Draw text last because it is overlayed // Draw text last because it is overlayed
QTextLayout labelLayout, infoLayout; QTextLayout labelLayout, infoLayout;
labelLayout.setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
infoLayout.setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
QRectF textBoundingRect; QRectF textBoundingRect;
@ -1308,6 +1312,7 @@ bool IconWidget::invertedLayout() const
QSizeF IconWidget::sizeFromIconSize(const qreal iconWidth) const QSizeF IconWidget::sizeFromIconSize(const qreal iconWidth) const
{ {
d->setActiveMargins();
if (d->text.isEmpty() && d->infoText.isEmpty()) { if (d->text.isEmpty() && d->infoText.isEmpty()) {
//no text, less calculations //no text, less calculations
return d->addMargin(d->addMargin(QSizeF(iconWidth, iconWidth), IconWidgetPrivate::IconMargin), 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) { if (d->orientation == Qt::Vertical) {
// make room for at most 14 characters // make room for at most 14 characters
width = qMax(fm.width(d->text.left(12)), width = qMax(d->maxWordWidth(d->text),
fm.width(d->infoText.left(12))) + d->maxWordWidth(d->infoText)) +
fm.width("xx") + fm.width("xx") +
d->horizontalMargin[IconWidgetPrivate::TextMargin].left + d->horizontalMargin[IconWidgetPrivate::TextMargin].left +
d->horizontalMargin[IconWidgetPrivate::TextMargin].right; d->horizontalMargin[IconWidgetPrivate::TextMargin].right;

View File

@ -31,6 +31,7 @@
#include <plasma/plasma_export.h> #include <plasma/plasma_export.h>
#include <plasma/framesvg.h> #include <plasma/framesvg.h>
#include <plasma/theme.h>
#include <plasma/svg.h> #include <plasma/svg.h>
#include "iconwidget.h" #include "iconwidget.h"
@ -146,6 +147,8 @@ public:
const QPixmap &icon, QTextLayout *labelLayout, const QPixmap &icon, QTextLayout *labelLayout,
QTextLayout *infoLayout, QRectF *textBoundingRect) const; QTextLayout *infoLayout, QRectF *textBoundingRect) const;
int maxWordWidth(const QString text) const;
inline void setLayoutOptions(QTextLayout &layout, inline void setLayoutOptions(QTextLayout &layout,
const QStyleOptionGraphicsItem *options, const QStyleOptionGraphicsItem *options,
const Qt::Orientation orientation) const; const Qt::Orientation orientation) const;
@ -265,6 +268,15 @@ Qt::LayoutDirection IconWidgetPrivate::iconDirection(const QStyleOptionGraphicsI
void IconWidgetPrivate::setActiveMargins() 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); activeMargins = (orientation == Qt::Horizontal ? horizontalMargin : verticalMargin);
} }
@ -300,56 +312,44 @@ QRectF IconWidgetPrivate::addMargin(const QRectF &rect, MarginType type) const
{ {
Q_ASSERT(activeMargins); 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]; const Margin &m = activeMargins[type];
return rect.adjusted(-m.left, -m.top, m.right, m.bottom); return rect.adjusted(-m.left, -m.top, m.right, m.bottom);
}
} }
QRectF IconWidgetPrivate::subtractMargin(const QRectF &rect, MarginType type) const QRectF IconWidgetPrivate::subtractMargin(const QRectF &rect, MarginType type) const
{ {
Q_ASSERT(activeMargins); 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]; const Margin &m = activeMargins[type];
return rect.adjusted(m.left, m.top, -m.right, -m.bottom); return rect.adjusted(m.left, m.top, -m.right, -m.bottom);
}
} }
QSizeF IconWidgetPrivate::addMargin(const QSizeF &size, MarginType type) const QSizeF IconWidgetPrivate::addMargin(const QSizeF &size, MarginType type) const
{ {
Q_ASSERT(activeMargins); 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]; const Margin &m = activeMargins[type];
return QSizeF(size.width() + m.left + m.right, size.height() + m.top + m.bottom); return QSizeF(size.width() + m.left + m.right, size.height() + m.top + m.bottom);
}
} }
QSizeF IconWidgetPrivate::subtractMargin(const QSizeF &size, MarginType type) const QSizeF IconWidgetPrivate::subtractMargin(const QSizeF &size, MarginType type) const
{ {
Q_ASSERT(activeMargins); 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]; const Margin &m = activeMargins[type];
return QSizeF(size.width() - m.left - m.right, size.height() - m.top - m.bottom); 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 QRectF IconWidgetPrivate::actionRect(ActionPosition position) const