Skip to content

Commit

Permalink
Implement automatic text elision in tStandardLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
vicr123 committed Oct 18, 2023
1 parent 6a5a3d2 commit 5d722cf
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 21 deletions.
59 changes: 52 additions & 7 deletions lib/tstandardlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}
46 changes: 32 additions & 14 deletions lib/tstandardlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5d722cf

Please sign in to comment.