diff --git a/cockatrice/src/game_graphics/game_view.cpp b/cockatrice/src/game_graphics/game_view.cpp index c1f7ba422..52349e14c 100644 --- a/cockatrice/src/game_graphics/game_view.cpp +++ b/cockatrice/src/game_graphics/game_view.cpp @@ -182,7 +182,7 @@ void GameView::clearSubtypeLabels() QtUtils::clearLayoutRec(subtypeCountLayout); } -void GameView::rebuildSubtypeLabels(const QList &entries) +QSize GameView::rebuildSubtypeLabels(const QList &entries) { clearSubtypeLabels(); @@ -190,6 +190,10 @@ void GameView::rebuildSubtypeLabels(const QList &entries) const QString countStyle = QStringLiteral("color: white; font-size: 14px; font-weight: bold; background: transparent;"); + int totalHeight = 0; + int maxNameWidth = 0; + int maxCountWidth = 0; + int row = 0; for (const SubtypeEntry &entry : entries) { auto *nameLabel = new QLabel(entry.name, subtypeCountContainer); @@ -202,8 +206,23 @@ void GameView::rebuildSubtypeLabels(const QList &entries) countLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter); subtypeCountLayout->addWidget(countLabel, row, 1); + QSize nameSize = nameLabel->sizeHint(); + QSize countSize = countLabel->sizeHint(); + maxNameWidth = qMax(maxNameWidth, nameSize.width()); + maxCountWidth = qMax(maxCountWidth, countSize.width()); + totalHeight += qMax(nameSize.height(), countSize.height()); + ++row; } + + int spacing = subtypeCountLayout->spacing(); + int margins = subtypeCountLayout->contentsMargins().left() + subtypeCountLayout->contentsMargins().right(); + int verticalMargins = subtypeCountLayout->contentsMargins().top() + subtypeCountLayout->contentsMargins().bottom(); + + int width = maxNameWidth + spacing + maxCountWidth + margins; + int height = totalHeight + (row - 1) * spacing + verticalMargins; + + return QSize(width, height); } void GameView::updateSelectionCount(const QSize &viewSize) @@ -255,16 +274,16 @@ void GameView::updateSelectionCount(const QSize &viewSize) return; } - rebuildSubtypeLabels(entries); - subtypeCountContainer->adjustSize(); + QSize containerSize = rebuildSubtypeLabels(entries); + subtypeCountContainer->resize(containerSize); - int x = availableWidth - subtypeCountContainer->width() - kMarginInPixels; + int x = availableWidth - containerSize.width() - kMarginInPixels; int y; if (totalCountLabel->isVisible()) { - y = totalCountLabel->y() - subtypeCountContainer->height() - kSpacingBetweenLabels; + y = totalCountLabel->y() - containerSize.height() - kSpacingBetweenLabels; } else { - y = availableHeight - subtypeCountContainer->height() - kMarginInPixels; + y = availableHeight - containerSize.height() - kMarginInPixels; } y = qMax(kMarginInPixels, y); diff --git a/cockatrice/src/game_graphics/game_view.h b/cockatrice/src/game_graphics/game_view.h index 7e9e31e2f..5fe0562cb 100644 --- a/cockatrice/src/game_graphics/game_view.h +++ b/cockatrice/src/game_graphics/game_view.h @@ -27,7 +27,7 @@ private: QGridLayout *subtypeCountLayout; ///< Grid layout for subtype name/count pairs QPointF selectionOrigin; - void rebuildSubtypeLabels(const QList &entries); + QSize rebuildSubtypeLabels(const QList &entries); void clearSubtypeLabels(); protected: