mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-06 13:33:55 -07:00
create and wire up TallyWidget
This commit is contained in:
parent
f63279e12f
commit
1bead478b6
5 changed files with 157 additions and 13 deletions
76
cockatrice/src/game/tally/tally_widget.cpp
Normal file
76
cockatrice/src/game/tally/tally_widget.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include "tally_widget.h"
|
||||
|
||||
#include <QBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QScrollArea>
|
||||
|
||||
TallyWidget::TallyWidget(QWidget *parent, TallyManager *tallyManager) : QWidget(parent), tallyManager(tallyManager)
|
||||
{
|
||||
auto layout = new QVBoxLayout(this);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
layout->setAlignment(Qt::AlignCenter);
|
||||
setLayout(layout);
|
||||
|
||||
auto scrollArea = new QScrollArea(this);
|
||||
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||||
|
||||
layout->addWidget(scrollArea);
|
||||
|
||||
auto gridLayoutWidget = new QWidget(scrollArea);
|
||||
gridLayout = new QGridLayout(gridLayoutWidget);
|
||||
gridLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
|
||||
|
||||
resetGridLayout({TallyManager::TOTAL_POWER, TallyManager::TOTAL_MANA_VALUE});
|
||||
}
|
||||
|
||||
void TallyWidget::resetGridLayout(QList<TallyManager::EntryType> entryTypes)
|
||||
{
|
||||
// clear all existing
|
||||
while (auto item = gridLayout->takeAt(0)) {
|
||||
item->widget()->deleteLater();
|
||||
delete item;
|
||||
}
|
||||
|
||||
// create all labels
|
||||
for (auto entryType : entryTypes) {
|
||||
auto textLabel = createTextLabel(entryType);
|
||||
auto valueLabel = createValueLabel(entryType);
|
||||
|
||||
int row = gridLayout->rowCount();
|
||||
gridLayout->addWidget(textLabel, row, 0);
|
||||
gridLayout->addWidget(valueLabel, row, 1);
|
||||
}
|
||||
}
|
||||
|
||||
QLabel *TallyWidget::createTextLabel(TallyManager::EntryType entryType)
|
||||
{
|
||||
auto label = new QLabel;
|
||||
|
||||
auto updateLabel = [label, entryType](const QMap<TallyManager::EntryType, TallyResult> &results) {
|
||||
QString text = results.value(entryType).text;
|
||||
label->setText(text);
|
||||
};
|
||||
|
||||
updateLabel(tallyManager->getAllResults());
|
||||
|
||||
connect(tallyManager, &TallyManager::tallyChanged, label, updateLabel);
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
QLabel *TallyWidget::createValueLabel(TallyManager::EntryType entryType)
|
||||
{
|
||||
auto label = new QLabel;
|
||||
|
||||
auto updateLabel = [label, entryType](const QMap<TallyManager::EntryType, TallyResult> &results) {
|
||||
int value = results.value(entryType).value;
|
||||
label->setText(QString::number(value));
|
||||
};
|
||||
|
||||
updateLabel(tallyManager->getAllResults());
|
||||
|
||||
connect(tallyManager, &TallyManager::tallyChanged, label, updateLabel);
|
||||
|
||||
return label;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue