diff options
author | Kevin Smith <git@kismith.co.uk> | 2010-05-26 21:24:39 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2010-05-26 21:24:39 (GMT) |
commit | 8d697d9aa2f07a3222dea561174f063c382449f1 (patch) | |
tree | 5206c4927231f608d1ed50609fa5a361b1fa7b77 /Swift/QtUI/QtElidingLabel.cpp | |
parent | d65ae35e1e14c15c5ae9106e37254b163307934d (diff) | |
download | swift-contrib-8d697d9aa2f07a3222dea561174f063c382449f1.zip swift-contrib-8d697d9aa2f07a3222dea561174f063c382449f1.tar.bz2 |
Make better use of the elided text labels.
Cache the elided text to avoid recalculating and making the QLabel
superclass recalculate everything. Also using plaintext with a font
instead of richtext means that it doesn't have to deal with truncated
html tags.
Hopefully
Resolves: #398
Diffstat (limited to 'Swift/QtUI/QtElidingLabel.cpp')
-rw-r--r-- | Swift/QtUI/QtElidingLabel.cpp | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/Swift/QtUI/QtElidingLabel.cpp b/Swift/QtUI/QtElidingLabel.cpp index 80da607..475aa3b 100644 --- a/Swift/QtUI/QtElidingLabel.cpp +++ b/Swift/QtUI/QtElidingLabel.cpp @@ -8,10 +8,14 @@ namespace Swift { QtElidingLabel::QtElidingLabel(QWidget* parent, Qt::WindowFlags f) : QLabel(parent, f) { + fullText_ = ""; + dirty_ = true; setSizes(); } QtElidingLabel::QtElidingLabel(const QString& text, QWidget* parent, Qt::WindowFlags f) : QLabel(text, parent, f) { + fullText_ = text; + dirty_ = true; setSizes(); } @@ -23,15 +27,24 @@ void QtElidingLabel::setSizes() { setMinimumSize(1, minimumHeight()); } +void QtElidingLabel::setText(const QString& text) { + fullText_ = text; + QLabel::setText(text); + dirty_ = true; +} + void QtElidingLabel::paintEvent(QPaintEvent* event) { - //QPainter painter(this); - QString fullText(text()); - if (fontMetrics().width(fullText) > contentsRect().width()) { - //QString elidedText(fontMetrics().elidedText(fullText)); - setText(fontMetrics().elidedText(fullText, Qt::ElideRight, rect().width(), Qt::TextShowMnemonic)); + QRect rect = contentsRect(); + dirty_ = dirty_ || rect != lastRect_; + if (dirty_) { + lastRect_ = rect; + if (fontMetrics().width(fullText_) > rect.width()) { + QString elidedText(fontMetrics().elidedText(fullText_, Qt::ElideRight, rect.width(), Qt::TextShowMnemonic)); + QLabel::setText(elidedText); + } + dirty_ = false; } QLabel::paintEvent(event); - setText(fullText); } } |