mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
* Simplify add card. Took 25 minutes Took 8 minutes # Commit time for manual adjustment: # Took 16 minutes Took 7 seconds * Refactor out db loading from card db. Took 39 minutes Took 9 minutes Took 2 minutes Took 17 seconds * Refactor out db queries from card db. Took 42 minutes * Lint. Took 3 minutes * I guess. Took 7 minutes * Tests. Took 15 minutes * I don't understand this. Took 9 minutes * fix linker errors * Rename to querier and promote to QObject Took 39 minutes * Lint. Took 3 minutes --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de> Co-authored-by: ebbit1q <ebbit1q@gmail.com>
99 lines
3 KiB
C++
99 lines
3 KiB
C++
#include "mana_curve_widget.h"
|
|
|
|
#include "../../../database/card_database.h"
|
|
#include "../../../database/card_database_manager.h"
|
|
#include "../../../deck/deck_loader.h"
|
|
#include "../../../main.h"
|
|
#include "../general/display/banner_widget.h"
|
|
#include "../general/display/bar_widget.h"
|
|
|
|
#include <deck_list.h>
|
|
#include <unordered_map>
|
|
|
|
ManaCurveWidget::ManaCurveWidget(QWidget *parent, DeckListModel *_deckListModel)
|
|
: QWidget(parent), deckListModel(_deckListModel)
|
|
{
|
|
layout = new QVBoxLayout(this);
|
|
setLayout(layout);
|
|
|
|
bannerWidget = new BannerWidget(this, tr("Mana Curve"), Qt::Vertical, 100);
|
|
bannerWidget->setMaximumHeight(100);
|
|
layout->addWidget(bannerWidget);
|
|
|
|
barContainer = new QWidget(this);
|
|
barLayout = new QHBoxLayout(barContainer);
|
|
layout->addWidget(barContainer);
|
|
|
|
connect(deckListModel, &DeckListModel::dataChanged, this, &ManaCurveWidget::analyzeManaCurve);
|
|
|
|
retranslateUi();
|
|
}
|
|
|
|
void ManaCurveWidget::retranslateUi()
|
|
{
|
|
bannerWidget->setText(tr("Mana Curve"));
|
|
}
|
|
|
|
void ManaCurveWidget::setDeckModel(DeckListModel *deckModel)
|
|
{
|
|
deckListModel = deckModel;
|
|
connect(deckListModel, &DeckListModel::dataChanged, this, &ManaCurveWidget::analyzeManaCurve);
|
|
analyzeManaCurve();
|
|
}
|
|
|
|
std::unordered_map<int, int> ManaCurveWidget::analyzeManaCurve()
|
|
{
|
|
manaCurveMap.clear();
|
|
InnerDecklistNode *listRoot = deckListModel->getDeckList()->getRoot();
|
|
for (int i = 0; i < listRoot->size(); i++) {
|
|
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
|
|
for (int j = 0; j < currentZone->size(); j++) {
|
|
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
|
|
if (!currentCard)
|
|
continue;
|
|
|
|
for (int k = 0; k < currentCard->getNumber(); ++k) {
|
|
CardInfoPtr info = CardDatabaseManager::query()->getCardInfo(currentCard->getName());
|
|
if (info) {
|
|
int cmc = info->getCmc().toInt();
|
|
manaCurveMap[cmc]++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
updateDisplay();
|
|
|
|
return manaCurveMap;
|
|
}
|
|
|
|
void ManaCurveWidget::updateDisplay()
|
|
{
|
|
// Clear the layout first
|
|
if (barLayout != nullptr) {
|
|
QLayoutItem *item;
|
|
while ((item = barLayout->takeAt(0)) != nullptr) {
|
|
item->widget()->deleteLater();
|
|
delete item;
|
|
}
|
|
}
|
|
|
|
int highestEntry = 0;
|
|
for (const auto &entry : manaCurveMap) {
|
|
if (entry.second > highestEntry) {
|
|
highestEntry = entry.second;
|
|
}
|
|
}
|
|
|
|
// Convert unordered_map to ordered map to ensure sorting by CMC
|
|
std::map<int, int> sortedManaCurve(manaCurveMap.begin(), manaCurveMap.end());
|
|
|
|
// Add new widgets to the layout in sorted order
|
|
for (const auto &entry : sortedManaCurve) {
|
|
BarWidget *barWidget =
|
|
new BarWidget(QString::number(entry.first), entry.second, highestEntry, QColor(122, 122, 122), this);
|
|
barLayout->addWidget(barWidget);
|
|
}
|
|
|
|
update(); // Update the widget display
|
|
}
|