From 8d697d9aa2f07a3222dea561174f063c382449f1 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Wed, 26 May 2010 22:24:39 +0100 Subject: 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 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); } } diff --git a/Swift/QtUI/QtElidingLabel.h b/Swift/QtUI/QtElidingLabel.h index 589d6f6..79701ec 100644 --- a/Swift/QtUI/QtElidingLabel.h +++ b/Swift/QtUI/QtElidingLabel.h @@ -14,10 +14,14 @@ namespace Swift { public: QtElidingLabel(QWidget* parent = NULL, Qt::WindowFlags f = 0); QtElidingLabel(const QString &text, QWidget* parent = NULL, Qt::WindowFlags f = 0); + void setText(const QString& text); virtual ~QtElidingLabel(); virtual void paintEvent(QPaintEvent* event); private: void setSizes(); + bool dirty_; + QString fullText_; + QRect lastRect_; }; } diff --git a/Swift/QtUI/QtRosterHeader.cpp b/Swift/QtUI/QtRosterHeader.cpp index 5c89e2a..0270a5e 100644 --- a/Swift/QtUI/QtRosterHeader.cpp +++ b/Swift/QtUI/QtRosterHeader.cpp @@ -41,6 +41,9 @@ QtRosterHeader::QtRosterHeader(QWidget* parent) : QWidget(parent) { nameLabel_ = new QtElidingLabel(this); setName("Me"); + QFont font = nameLabel_->font(); + font.setBold(true); + nameLabel_->setFont(font); rightLayout->addWidget(nameLabel_); @@ -92,7 +95,8 @@ void QtRosterHeader::setName(const QString& name) { name_ = name; QString escapedName = name_; escapedName.replace("<","<"); - nameLabel_->setText("" + escapedName + ""); +// nameLabel_->setText("" + escapedName + ""); + nameLabel_->setText(escapedName); // resizeNameLabel(); } diff --git a/Swift/QtUI/QtRosterHeader.h b/Swift/QtUI/QtRosterHeader.h index 6d36b75..d4948cb 100644 --- a/Swift/QtUI/QtRosterHeader.h +++ b/Swift/QtUI/QtRosterHeader.h @@ -21,6 +21,7 @@ class QHBoxLayout; namespace Swift { class QtStatusWidget; + class QtElidingLabel; class QtRosterHeader : public QWidget { Q_OBJECT @@ -37,7 +38,7 @@ namespace Swift { private: QString name_; QLabel* avatarLabel_; - QLabel* nameLabel_; + QtElidingLabel* nameLabel_; QtTextEdit* statusEdit_; QToolBar* toolBar_; QtStatusWidget* statusWidget_; diff --git a/Swift/QtUI/QtStatusWidget.cpp b/Swift/QtUI/QtStatusWidget.cpp index 784c0d4..7d39531 100644 --- a/Swift/QtUI/QtStatusWidget.cpp +++ b/Swift/QtUI/QtStatusWidget.cpp @@ -50,6 +50,9 @@ QtStatusWidget::QtStatusWidget(QWidget *parent) : QWidget(parent), editCursor_(Q page1Layout->addWidget(statusIcon_); statusTextLabel_ = new QtElidingLabel(this); + QFont font = statusTextLabel_->font(); + font.setItalic(true); + statusTextLabel_->setFont(font); page1Layout->addWidget(statusTextLabel_); icons_[StatusShow::Online] = QIcon(":/icons/online.png"); @@ -210,7 +213,8 @@ void QtStatusWidget::setStatusText(const QString& text) { statusEdit_->setText(text); QString escapedText(text.isEmpty() ? "(No message)" : text); escapedText.replace("<","<"); - statusTextLabel_->setText("" + escapedText + ""); +// statusTextLabel_->setText("" + escapedText + ""); + statusTextLabel_->setText(escapedText); } void QtStatusWidget::setStatusType(StatusShow::Type type) { diff --git a/Swift/QtUI/QtStatusWidget.h b/Swift/QtUI/QtStatusWidget.h index 111bc84..4425b1d 100644 --- a/Swift/QtUI/QtStatusWidget.h +++ b/Swift/QtUI/QtStatusWidget.h @@ -20,6 +20,7 @@ class QListWidgetItem; namespace Swift { class QtLineEdit; + class QtElidingLabel; class QtStatusWidget : public QWidget { Q_OBJECT public: @@ -45,7 +46,7 @@ namespace Swift { //QComboBox *types_; QStackedWidget* stack_; QLabel* statusIcon_; - QLabel* statusTextLabel_; + QtElidingLabel* statusTextLabel_; QtLineEdit* statusEdit_; QString statusText_; QString newStatusText_; -- cgit v0.10.2-6-g49f6