Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Set camera panel label widths to fit largest label text
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVanheer committed Apr 7, 2024
1 parent 87b2a99 commit 8851dfc
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/hlam/ui/camera_operators/dockpanels/CamerasPanel.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <algorithm>
#include <cassert>

#include <QFontMetrics>
#include <QGridLayout>
#include <QLabel>
#include <QSignalBlocker>

#include "qt/QtUtilities.hpp"
Expand Down Expand Up @@ -44,15 +47,62 @@ QWidget* CamerasPanel::GetWidget(int index) const
return _widgets[index];
}

static QVector<QLabel*> GetCameraLabels(QWidget* panel)
{
QVector<QLabel*> labels;

if (auto grid = qobject_cast<QGridLayout*>(panel->layout()); grid)
{
for (int i = 0; i < grid->rowCount(); ++i)
{
auto item = grid->itemAtPosition(i, 0);

if (auto label = qobject_cast<QLabel*>(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)
Expand Down

0 comments on commit 8851dfc

Please sign in to comment.