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
This commit is contained in:
Christopher Blauvelt 2007-12-26 13:44:57 +00:00
parent 27ce118c91
commit 06295ace02
4 changed files with 42 additions and 8 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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:
/**

View File

@ -181,12 +181,15 @@ public:
IconStates states;
Qt::Orientation orientation;
Qt::Alignment alignment;
int numDisplayLines;
QList<IconAction*> cornerActions;
Margin verticalMargin[NMargins];
Margin horizontalMargin[NMargins];
Margin *activeMargins;
static const int maxDisplayLines = 5;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Icon::Private::IconStates)