diff --git a/cockatrice/src/game_graphics/game_view.cpp b/cockatrice/src/game_graphics/game_view.cpp index d7d5bc7b0..78c8087f7 100644 --- a/cockatrice/src/game_graphics/game_view.cpp +++ b/cockatrice/src/game_graphics/game_view.cpp @@ -184,7 +184,7 @@ void GameView::clearSubtypeLabels() QtUtils::clearLayoutRec(subtypeCountLayout); } -void GameView::rebuildSubtypeLabels(const QList &entries) +QSize GameView::rebuildSubtypeLabels(const QList &entries) { clearSubtypeLabels(); @@ -192,6 +192,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); @@ -204,8 +208,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::updateTotalSelectionCount(const QSize &viewSize) @@ -257,16 +276,16 @@ void GameView::updateTotalSelectionCount(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 38a4510b2..d55849a2f 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: