| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
|---|---|
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "elidedlabel.h" |
| 6 | |
| 7 | #include <QString> |
| 8 | |
| 9 | class ElidedLabelPrivate |
| 10 | { |
| 11 | friend class ElidedLabel; |
| 12 | QString sourceText; |
| 13 | }; |
| 14 | |
| 15 | ElidedLabel::ElidedLabel(QWidget *parent) |
| 16 | : QLabel (parent) |
| 17 | , d (new ElidedLabelPrivate) |
| 18 | { |
| 19 | |
| 20 | } |
| 21 | |
| 22 | ElidedLabel::~ElidedLabel() |
| 23 | { |
| 24 | if (d) { |
| 25 | delete d; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | void ElidedLabel::setText(const QString &text) |
| 30 | { |
| 31 | d->sourceText = text; |
| 32 | QString resultText; |
| 33 | QFontMetrics font(this->font()); |
| 34 | int font_size = font.horizontalAdvance(text); |
| 35 | int resize_width = width(); |
| 36 | if(font_size > resize_width) { |
| 37 | resultText = font.elidedText(d->sourceText, Qt::ElideRight, resize_width); |
| 38 | } else { |
| 39 | resultText = d->sourceText; |
| 40 | } |
| 41 | QLabel::setText(resultText); |
| 42 | QLabel::setToolTip(text); |
| 43 | } |
| 44 | |
| 45 | QString ElidedLabel::text() |
| 46 | { |
| 47 | return d->sourceText; |
| 48 | } |
| 49 |