From b61bc7d44132da947485486bb371988237ec0cad Mon Sep 17 00:00:00 2001 From: Alex Spataru Date: Tue, 28 Sep 2021 19:05:35 -0500 Subject: [PATCH] Fix issues when widget text is too large --- src/Widgets/Common/BaseWidget.cpp | 23 ++++++++++++++++++----- src/Widgets/Common/BaseWidget.h | 1 + src/Widgets/Common/ExternalWindow.cpp | 1 - 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Widgets/Common/BaseWidget.cpp b/src/Widgets/Common/BaseWidget.cpp index 32b413d9..da84d180 100644 --- a/src/Widgets/Common/BaseWidget.cpp +++ b/src/Widgets/Common/BaseWidget.cpp @@ -40,9 +40,6 @@ BaseWidget::BaseWidget() setPalette(palette); // Configure label style - QFont font = UI::Dashboard::getInstance()->monoFont(); - font.setPixelSize(24); - m_label.setFont(font); m_label.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); // Set stylesheets @@ -57,8 +54,17 @@ BaseWidget::BaseWidget() void BaseWidget::setValue(const QString &label) { - if (m_label.text() != label) + // Change label text + if (m_label.text() != label) { m_label.setText(label); + + // Resize label font (so it fits inside the box) + while (QFontMetrics(m_label.font()).horizontalAdvance(label) + 12 > m_label.width()) { + QFont font = m_label.font(); + font.setPixelSize(font.pixelSize() - 1); + m_label.setFont(font); + } + } } void BaseWidget::setWidget(QWidget *widget, Qt::Alignment alignment, bool autoresize) @@ -91,8 +97,15 @@ void BaseWidget::resizeEvent(QResizeEvent *event) labelFont.setPixelSize(width / 18); gaugeFont.setPixelSize(width / 24); - // Set fonts + // Set label font (so it fits inside the box) m_label.setFont(labelFont); + while (QFontMetrics(m_label.font()).horizontalAdvance(m_label.text()) + 12 > m_label.width()) { + QFont font = m_label.font(); + font.setPixelSize(font.pixelSize() - 1); + m_label.setFont(font); + } + + // Set widget font if (m_widget) m_widget->setFont(gaugeFont); diff --git a/src/Widgets/Common/BaseWidget.h b/src/Widgets/Common/BaseWidget.h index a8e26068..5e39303f 100644 --- a/src/Widgets/Common/BaseWidget.h +++ b/src/Widgets/Common/BaseWidget.h @@ -52,6 +52,7 @@ private: QWidget *m_widget; bool m_resizeWidget; QHBoxLayout m_layout; + int m_labelTextWidth; }; } diff --git a/src/Widgets/Common/ExternalWindow.cpp b/src/Widgets/Common/ExternalWindow.cpp index 9b14050e..100575f2 100644 --- a/src/Widgets/Common/ExternalWindow.cpp +++ b/src/Widgets/Common/ExternalWindow.cpp @@ -30,7 +30,6 @@ using namespace Widgets; ExternalWindow::ExternalWindow() : m_widget(nullptr) { - m_layout.setContentsMargins(24, 24, 24, 24); setLayout(&m_layout); }