StatusIndication.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * \package ArmarXGui::gui-plugins::StatusIndication
17 * \author Leonie Leven
18 * \date 2024
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23
24// ArmarX
25#include "StatusIndication.h"
26
27#include <QDebug>
28#include <QFontMetrics>
29
31
32namespace armarx
33
34{
35 StatusIndicationLabel::StatusIndicationLabel(QWidget* parent) : QLabel(parent)
36 {
37 setAlignment(Qt::AlignCenter);
38 setWordWrap(true);
39 }
40
41 void
43 {
44 this->m_text = text;
45 setText(this->m_text);
46 this->adjustFontSize();
47 }
48
49 void
51 {
52 QLabel::resizeEvent(event);
53 adjustFontSize();
54 }
55
56 void
57 StatusIndicationLabel::adjustFontSize()
58 {
59 if (m_text.isEmpty())
60 return;
61
62 QFont font = this->font();
63 QRect labelRect = contentsRect();
64
65 int fontSize = 1;
66 int maxFontSize = 200;
67 QFontMetrics fm(font);
68
69 while (fontSize < maxFontSize)
70 {
71 font.setPointSize(fontSize);
72 fm = QFontMetrics(font);
73 QRect textRect = fm.boundingRect(labelRect, alignment(), m_text);
74
75 if (textRect.width() > labelRect.width() || textRect.height() > labelRect.height())
76 break;
77
78 ++fontSize;
79 }
80
81 font.setPointSize(fontSize - 1);
82 setFont(font);
83 }
84} // namespace armarx
void setTextWithScaling(const QString &text)
StatusIndicationLabel(QWidget *parent=nullptr)
void resizeEvent(QResizeEvent *event) override
This file offers overloads of toIce() and fromIce() functions for STL container types.