From 5d722cffd1ea7111b9aea0645206700652df4867 Mon Sep 17 00:00:00 2001 From: Victor Tran Date: Wed, 18 Oct 2023 22:23:26 +1100 Subject: [PATCH] Implement automatic text elision in tStandardLabel --- lib/tstandardlabel.cpp | 59 +++++++++++++++++++++++++++++++++++++----- lib/tstandardlabel.h | 46 ++++++++++++++++++++++---------- 2 files changed, 84 insertions(+), 21 deletions(-) diff --git a/lib/tstandardlabel.cpp b/lib/tstandardlabel.cpp index 52119d2e..1c37dd37 100644 --- a/lib/tstandardlabel.cpp +++ b/lib/tstandardlabel.cpp @@ -5,10 +5,14 @@ #include "tstandardlabel.h" struct tStandardLabelPrivate { - tStandardLabel::StandardLabelType type = tStandardLabel::Normal; + tStandardLabel::StandardLabelType type = tStandardLabel::Normal; + + QString text; + tStandardLabel::ElideMode elideMode = tStandardLabel::ElideRight; }; -tStandardLabel::tStandardLabel(QWidget* parent) : QLabel(parent) { +tStandardLabel::tStandardLabel(QWidget* parent) : + QLabel(parent) { d = new tStandardLabelPrivate(); } @@ -25,14 +29,55 @@ void tStandardLabel::setType(tStandardLabel::StandardLabelType type) { QFont font = QApplication::font(); switch (type) { - case Subtitle: { - font.setBold(true); - font.setCapitalization(QFont::AllUppercase); - break; - } + case Subtitle: + { + font.setBold(true); + font.setCapitalization(QFont::AllUppercase); + break; + } default: break; } this->setFont(font); } + +void tStandardLabel::setText(QString text) { + d->text = text; + this->updateText(); +} + +QString tStandardLabel::text() { + return d->text; +} + +void tStandardLabel::setElideMode(ElideMode elideMode) { + d->elideMode = elideMode; +} + +tStandardLabel::ElideMode tStandardLabel::elideMode() { + return d->elideMode; +} + +void tStandardLabel::updateText() { + int availableWidth = this->width(); + auto text = d->text; + switch (d->elideMode) { + case ElideRight: + text = this->fontMetrics().elidedText(d->text, Qt::ElideRight, availableWidth); + break; + case ElideCenter: + text = this->fontMetrics().elidedText(d->text, Qt::ElideMiddle, availableWidth); + break; + case ElideLeft: + text = this->fontMetrics().elidedText(d->text, Qt::ElideLeft, availableWidth); + break; + case NoElide: + break; + } + QLabel::setText(text); +} + +void tStandardLabel::resizeEvent(QResizeEvent* event) { + this->updateText(); +} diff --git a/lib/tstandardlabel.h b/lib/tstandardlabel.h index 1d098d8d..18497706 100644 --- a/lib/tstandardlabel.h +++ b/lib/tstandardlabel.h @@ -10,23 +10,41 @@ struct tStandardLabelPrivate; class LIBCONTEMPORARY_EXPORT tStandardLabel : public QLabel { - Q_OBJECT + Q_OBJECT -public: - explicit tStandardLabel(QWidget* parent = nullptr); - ~tStandardLabel(); + public: + explicit tStandardLabel(QWidget* parent = nullptr); + ~tStandardLabel(); - enum StandardLabelType { - Normal, - Subtitle - }; + enum StandardLabelType { + Normal, + Subtitle + }; - StandardLabelType type(); - void setType(StandardLabelType type); + enum ElideMode { + NoElide, + ElideRight, + ElideCenter, + ElideLeft + }; -private: - tStandardLabelPrivate* d; -}; + StandardLabelType type(); + void setType(StandardLabelType type); + + void setText(QString text); + QString text(); + + void setElideMode(ElideMode elideMode); + ElideMode elideMode(); + private: + tStandardLabelPrivate* d; + + void updateText(); + + // QWidget interface + protected: + void resizeEvent(QResizeEvent* event); +}; -#endif //LIBCONTEMPORARY_TSTANDARDLABEL_H +#endif // LIBCONTEMPORARY_TSTANDARDLABEL_H