mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-23 10:22:48 +00:00
plasma: automatically elide the text from Plasma::Label when not wrapping words
this may have to be made an option tho but for now it is not because word wrapping is an option Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
e670fe2c33
commit
19bded13ce
2 changed files with 32 additions and 2 deletions
|
@ -36,11 +36,20 @@ public:
|
|||
: ThemedWidgetInterface<Label>(label)
|
||||
{
|
||||
}
|
||||
|
||||
void elideText()
|
||||
{
|
||||
QFontMetricsF fontmetricsf(q->font());
|
||||
const qreal dotx3width = fontmetricsf.width(QLatin1String("..."));
|
||||
q->nativeWidget()->setText(fontmetricsf.elidedText(originaltext, Qt::ElideRight, q->size().width() - dotx3width));
|
||||
}
|
||||
|
||||
QString originaltext;
|
||||
};
|
||||
|
||||
Label::Label(QGraphicsWidget *parent)
|
||||
: QGraphicsProxyWidget(parent),
|
||||
d(new LabelPrivate(this))
|
||||
d(new LabelPrivate(this))
|
||||
{
|
||||
QLabel *native = new QLabel();
|
||||
|
||||
|
@ -63,7 +72,12 @@ Label::~Label()
|
|||
|
||||
void Label::setText(const QString &text)
|
||||
{
|
||||
nativeWidget()->setText(text);
|
||||
if (!nativeWidget()->wordWrap()) {
|
||||
d->originaltext = text;
|
||||
d->elideText();
|
||||
} else {
|
||||
nativeWidget()->setText(text);
|
||||
}
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
|
@ -95,6 +109,13 @@ Qt::Alignment Label::alignment() const
|
|||
void Label::setWordWrap(bool wrap)
|
||||
{
|
||||
nativeWidget()->setWordWrap(wrap);
|
||||
if (!wrap) {
|
||||
d->originaltext = nativeWidget()->text();
|
||||
d->elideText();
|
||||
} else {
|
||||
nativeWidget()->setText(d->originaltext);
|
||||
d->originaltext.clear();
|
||||
}
|
||||
}
|
||||
|
||||
bool Label::wordWrap() const
|
||||
|
@ -121,6 +142,14 @@ void Label::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|||
}
|
||||
}
|
||||
|
||||
void Label::resizeEvent(QGraphicsSceneResizeEvent *event)
|
||||
{
|
||||
QGraphicsProxyWidget::resizeEvent(event);
|
||||
if (!nativeWidget()->wordWrap()) {
|
||||
d->elideText();
|
||||
}
|
||||
}
|
||||
|
||||
void Label::changeEvent(QEvent *event)
|
||||
{
|
||||
d->changeEvent(event);
|
||||
|
|
|
@ -127,6 +127,7 @@ Q_SIGNALS:
|
|||
void linkHovered(const QString &link);
|
||||
|
||||
protected:
|
||||
void resizeEvent(QGraphicsSceneResizeEvent *event);
|
||||
void changeEvent(QEvent *event);
|
||||
bool event(QEvent *event);
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant & value);
|
||||
|
|
Loading…
Add table
Reference in a new issue