fade out the end of the text when there is not enough room (and wordwrap

is off)

svn path=/trunk/KDE/kdelibs/; revision=1012310
This commit is contained in:
Marco Martin 2009-08-17 12:24:33 +00:00
parent bd352af269
commit c67decb4d9
2 changed files with 50 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include <QLabel> #include <QLabel>
#include <QPainter> #include <QPainter>
#include <QDir> #include <QDir>
#include <QStyleOptionGraphicsItem>
#include <kmimetype.h> #include <kmimetype.h>
#include <kglobalsettings.h> #include <kglobalsettings.h>
@ -208,6 +209,52 @@ void Label::resizeEvent(QGraphicsSceneResizeEvent *event)
QGraphicsProxyWidget::resizeEvent(event); QGraphicsProxyWidget::resizeEvent(event);
} }
void Label::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
QLabel *native = nativeWidget();
QFontMetrics fm = native->font();
if (native->wordWrap() || native->text().isEmpty() || size().width() > fm.width(native->text())) {
QGraphicsProxyWidget::paint(painter, option, widget);
} else {
const int gradientLength = 25;
QPixmap buffer(size().toSize());
buffer.fill(Qt::transparent);
QPainter buffPainter(&buffer);
QGraphicsProxyWidget::paint(&buffPainter, option, widget);
QLinearGradient gr;
buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
buffPainter.setPen(Qt::NoPen);
if (option->direction == Qt::LeftToRight) {
gr.setStart(size().width()-gradientLength, 0);
gr.setFinalStop(size().width(), 0);
gr.setColorAt(0, Qt::black);
gr.setColorAt(1, Qt::transparent);
buffPainter.setBrush(gr);
buffPainter.drawRect(QRect(gr.start().toPoint(), QSize(gradientLength, size().height())));
} else {
gr.setStart(0, 0);
gr.setFinalStop(gradientLength, 0);
gr.setColorAt(0, Qt::transparent);
gr.setColorAt(1, Qt::black);
buffPainter.setBrush(gr);
buffPainter.drawRect(QRect(0, 0, gradientLength, size().height()));
}
buffPainter.end();
painter->drawPixmap(buffer.rect(), buffer, buffer.rect());
}
}
} // namespace Plasma } // namespace Plasma
#include <label.moc> #include <label.moc>

View File

@ -132,6 +132,9 @@ public Q_SLOTS:
protected: protected:
void resizeEvent(QGraphicsSceneResizeEvent *event); void resizeEvent(QGraphicsSceneResizeEvent *event);
void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget);
private: private:
Q_PRIVATE_SLOT(d, void setPalette()) Q_PRIVATE_SLOT(d, void setPalette())