From 8851dfcc9e7705d90f944fe2c94eddd986a59694 Mon Sep 17 00:00:00 2001 From: Sam V Date: Sun, 7 Apr 2024 13:37:08 +0200 Subject: [PATCH] Set camera panel label widths to fit largest label text --- .../dockpanels/CamerasPanel.cpp | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/hlam/ui/camera_operators/dockpanels/CamerasPanel.cpp b/src/hlam/ui/camera_operators/dockpanels/CamerasPanel.cpp index 0b05174a..75f20d52 100644 --- a/src/hlam/ui/camera_operators/dockpanels/CamerasPanel.cpp +++ b/src/hlam/ui/camera_operators/dockpanels/CamerasPanel.cpp @@ -1,6 +1,9 @@ #include #include +#include +#include +#include #include #include "qt/QtUtilities.hpp" @@ -44,15 +47,62 @@ QWidget* CamerasPanel::GetWidget(int index) const return _widgets[index]; } +static QVector GetCameraLabels(QWidget* panel) +{ + QVector labels; + + if (auto grid = qobject_cast(panel->layout()); grid) + { + for (int i = 0; i < grid->rowCount(); ++i) + { + auto item = grid->itemAtPosition(i, 0); + + if (auto label = qobject_cast(item->widget()); label) + { + labels.push_back(label); + } + } + } + + return labels; +} + void CamerasPanel::AddCameraOperator(const QString& name, QWidget* widget) { assert(widget); _widgets.emplace_back(widget); - //Add the widget before adding the combo box item because the combo box will select the first item that gets added automatically + // Add the widget before adding the combo box item because the combo box + // will select the first item that gets added automatically _ui.CamerasStack->addWidget(widget); _ui.Cameras->addItem(name); + + // Redo layout. + int widthToSet = -1; + + for (auto widget : _widgets) + { + for (auto label : GetCameraLabels(widget)) + { + QFontMetrics fm{ label->font() }; + + const int width = fm.horizontalAdvance(label->text()); + + if (width > widthToSet) + { + widthToSet = width; + } + } + } + + for (auto widget : _widgets) + { + for (auto label : GetCameraLabels(widget)) + { + label->setFixedWidth(widthToSet); + } + } } void CamerasPanel::ChangeCamera(int index)