From 06295ace02525aa1a805989419ddad4b7587a543 Mon Sep 17 00:00:00 2001 From: Christopher Blauvelt Date: Wed, 26 Dec 2007 13:44:57 +0000 Subject: [PATCH] Piggyback on mart's patch. Add the ability to set the number of lines in the display, fixes Icon::Private::elidedText so the display won't be truncated to one line if the whole text doesn't fit, and fixes some crashes. svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=753049 --- applethandle.cpp | 4 ++-- widgets/icon.cpp | 28 ++++++++++++++++++++++------ widgets/icon.h | 15 +++++++++++++++ widgets/icon_p.h | 3 +++ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/applethandle.cpp b/applethandle.cpp index a0f577461..166d94b37 100644 --- a/applethandle.cpp +++ b/applethandle.cpp @@ -380,14 +380,14 @@ QVariant AppletHandle::itemChange(GraphicsItemChange change, const QVariant &val void AppletHandle::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event); - kDebug() << "hover enter"; + //kDebug() << "hover enter"; startFading(FadeIn); } void AppletHandle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { Q_UNUSED(event); - kDebug() << "hover leave"; + //kDebug() << "hover leave"; startFading(FadeOut); } diff --git a/widgets/icon.cpp b/widgets/icon.cpp index 71bd9bfc5..d89d40e66 100644 --- a/widgets/icon.cpp +++ b/widgets/icon.cpp @@ -374,6 +374,26 @@ void Icon::actionDestroyed(QObject* action) update(); // redraw since an action has been deleted. } +int Icon::numDisplayLines() +{ + return d->numDisplayLines; +} + +void Icon::setNumDisplayLines(int numLines) +{ + if(numLines > d->maxDisplayLines) { + d->numDisplayLines = d->maxDisplayLines; + } + else { + d->numDisplayLines = numLines; + } +} + +QSizeF Icon::sizeHint() const +{ + return currentSize; +} + QSizeF Icon::Private::displaySizeHint(const QStyleOptionGraphicsItem *option, const qreal width) const { if (text.isEmpty() && infoText.isEmpty()) { @@ -390,7 +410,7 @@ QSizeF Icon::Private::displaySizeHint(const QStyleOptionGraphicsItem *option, co horizontalMargin[Private::TextMargin].right; //allow only five lines of text - const int maxHeight = 5*Plasma::Theme::self()->fontMetrics().height(); + const qreal maxHeight = numDisplayLines*Plasma::Theme::self()->fontMetrics().lineSpacing(); // To compute the nominal size for the label + info, we'll just append // the information string to the label @@ -665,10 +685,6 @@ QString Icon::Private::elidedText(QTextLayout &layout, const QStyleOptionGraphic qreal maxHeight = size.height(); qreal height = 0; - // If the string contains a single line of text it shouldn't be word wrapped - if (text.indexOf(QChar::LineSeparator) == -1) - return metrics.elidedText(text, Qt::ElideRight, maxWidth); - // Elide each line that has already been laid out in the layout. QString elided; elided.reserve(text.length()); @@ -684,7 +700,7 @@ QString Icon::Private::elidedText(QTextLayout &layout, const QStyleOptionGraphic { // Unfortunately, if the line ends because of a line separator, elidedText() will be too // clever and keep adding lines until it finds one that's too wide. - if (line.naturalTextWidth() < maxWidth && text[start + length - 1] == QChar::LineSeparator) + if (line.naturalTextWidth() < maxWidth && start+length > 0 && text[start + length - 1] == QChar::LineSeparator) elided += text.mid(start, length - 1); else elided += metrics.elidedText(text.mid(start), Qt::ElideRight, maxWidth); diff --git a/widgets/icon.h b/widgets/icon.h index e1def88f7..4137eca90 100644 --- a/widgets/icon.h +++ b/widgets/icon.h @@ -154,6 +154,21 @@ public: */ QSizeF sizeFromIconSize(const qreal iconWidth) const; + /** + * @return the number of lines allowed to display + */ + int numDisplayLines(); + + /** + * @param numLines the number of lines to show in the display. + */ + void setNumDisplayLines(int numLines); + + /** + * @return the recommended size of the widget + */ + QSizeF sizeHint() const; + public Q_SLOTS: /** diff --git a/widgets/icon_p.h b/widgets/icon_p.h index 924861fa2..230c866b8 100644 --- a/widgets/icon_p.h +++ b/widgets/icon_p.h @@ -181,12 +181,15 @@ public: IconStates states; Qt::Orientation orientation; Qt::Alignment alignment; + int numDisplayLines; QList cornerActions; Margin verticalMargin[NMargins]; Margin horizontalMargin[NMargins]; Margin *activeMargins; + + static const int maxDisplayLines = 5; }; Q_DECLARE_OPERATORS_FOR_FLAGS(Icon::Private::IconStates)