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 
32 namespace 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 availableRect = contentsRect();
64 
65 
66  int fontSize = font.pointSize();
67  if (fontSize == -1)
68  fontSize = 12; // dafault value if no size is set
69 
70  QFontMetrics metrics(font);
71 
72  // upscaling font size
73  while (true)
74  {
75  font.setPointSize(fontSize + 1);
76  metrics = QFontMetrics(font);
77 
78  QRect textRect = metrics.boundingRect(availableRect, Qt::TextWordWrap, m_text);
79 
80  if (textRect.width() > availableRect.width() ||
81  textRect.height() > availableRect.height())
82  {
83  break; // exit if text does not fit
84  }
85 
86  fontSize++;
87  }
88 
89 
90  font.setPointSize(fontSize);
91  metrics = QFontMetrics(font);
92 
93  // downscaling font size
94  while (fontSize > 1)
95  {
96  QRect textRect = metrics.boundingRect(availableRect, Qt::TextWordWrap, m_text);
97 
98  if (textRect.width() <= availableRect.width() &&
99  textRect.height() <= availableRect.height())
100  {
101  break; // exit when maximum suitable font size has been found
102  }
103 
104  fontSize--;
105  font.setPointSize(fontSize);
106  metrics = QFontMetrics(font);
107  }
108 
109  setFont(font);
110  }
111 } // namespace armarx
StatusIndication.h
armarx::StatusIndicationLabel::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: StatusIndication.cpp:50
armarx::StatusIndicationLabel::StatusIndicationLabel
StatusIndicationLabel(QWidget *parent=nullptr)
Definition: StatusIndication.cpp:35
armarx::StatusIndicationLabel::setTextWithScaling
void setTextWithScaling(const QString &text)
Definition: StatusIndication.cpp:42
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
Application.h