Debian Codegraph @

src/Gui/ElideLabel.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2025 Alfredo Monclus <[email protected]> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24 // This custom widget adds the missing ellipsize functionality in QT5
25
26 #include "ElideLabel.h"
27
28 namespace Gui
29 {
30
31 ElideLabel::ElideLabel(QWidget* parent)
32 : QLabel(parent)
33 {}
34
35 void ElideLabel::paintEvent(QPaintEvent* event)
36 {
37 Q_UNUSED(event);
38 QPainter painter(this);
39 painter.setPen(palette().color(QPalette::WindowText));
40 painter.setFont(font());
41
42 constexpr int padding = 4;
43 QFontMetrics fm(painter.fontMetrics());
44
45 int availableWidth = width() - padding * 2;
46 if (availableWidth < 0) {
47 return;
48 }
49
50 QString elidedText = fm.elidedText(text(), Qt::ElideRight, availableWidth);
51
52 QRect textRect = rect().adjusted(padding, 0, -padding, 0);
53 painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, elidedText);
54 }
55
56 QSize ElideLabel::sizeHint() const
57 {
58 QFontMetrics fm(font());
59 int width = fm.horizontalAdvance(this->text());
60 int height = fm.height();
61 return {width, height};
62 }
63
64 QSize ElideLabel::minimumSizeHint() const
65 {
66 QFontMetrics fm(font());
67 QString minimumText = QStringLiteral("A...");
68 int width = fm.horizontalAdvance(minimumText);
69 int height = fm.height();
70 return {width, height};
71 }
72
73 } // namespace Gui
74
75 #include "moc_ElideLabel.cpp" // NOLINT