give a recipe line its own row for its identity

This commit is contained in:
2026-08-11 22:07:15 +02:00
parent 1ddd830476
commit a3fddf63cc
4 changed files with 80 additions and 38 deletions

View File

@@ -4,6 +4,7 @@
#include <QLabel>
#include <QLayoutItem>
#include <QPixmap>
#include <QVBoxLayout>
#include "BuildingIconCache.h"
#include "ItemIconCache.h"
@@ -28,6 +29,19 @@ void addAndShow(QHBoxLayout* layout, QLabel* label)
label->show();
}
// Empties one of the two rows, leaving the row widget and its layout in place.
void clearRow(QHBoxLayout* layout)
{
while (QLayoutItem* item = layout->takeAt(0))
{
if (item->widget())
{
item->widget()->deleteLater();
}
delete item;
}
}
} // namespace
@@ -37,9 +51,22 @@ RecipeLineRow::RecipeLineRow(ItemIconCache* itemIcons, BuildingIconCache* buildi
, m_itemIcons(itemIcons)
, m_buildingIcons(buildingIcons)
{
m_layout = new QHBoxLayout(this);
m_layout->setContentsMargins(0, 0, 0, 0);
m_layout->setSpacing(4);
QVBoxLayout* outerLayout = new QVBoxLayout(this);
outerLayout->setContentsMargins(0, 0, 0, 0);
outerLayout->setSpacing(1);
m_headerRow = new QWidget(this);
m_headerLayout = new QHBoxLayout(m_headerRow);
m_headerLayout->setContentsMargins(0, 0, 0, 0);
m_headerLayout->setSpacing(4);
m_headerRow->hide();
outerLayout->addWidget(m_headerRow);
m_amountsRow = new QWidget(this);
m_amountsLayout = new QHBoxLayout(m_amountsRow);
m_amountsLayout->setContentsMargins(0, 0, 0, 0);
m_amountsLayout->setSpacing(4);
outerLayout->addWidget(m_amountsRow);
hide();
}
@@ -66,39 +93,40 @@ void RecipeLineRow::setLine(const Spec& spec)
void RecipeLineRow::rebuild(const Spec& spec)
{
while (QLayoutItem* item = m_layout->takeAt(0))
{
if (item->widget())
{
item->widget()->deleteLater();
}
delete item;
}
clearRow(m_headerLayout);
clearRow(m_amountsLayout);
// The building the recipe runs in, where the line has to distinguish one producer
// from another (REQ-UI-ITEM-TOOLTIP). A building with no chip file leaves the icon
// off, as everywhere else (REQ-UI-BUILD-ICON).
// First line: which building runs the recipe and which recipe it is, where the line
// has to distinguish one producer from another (REQ-UI-ITEM-TOOLTIP). A building with
// no chip file leaves the icon off, as everywhere else (REQ-UI-BUILD-ICON).
if (spec.building.has_value() && m_buildingIcons != nullptr)
{
const QPixmap chip =
m_buildingIcons->getChip(buildingTypeId(*spec.building), kIconSizePx);
if (!chip.isNull())
{
QLabel* chipLabel = new QLabel(this);
QLabel* chipLabel = new QLabel(m_headerRow);
chipLabel->setPixmap(chip);
addAndShow(m_layout, chipLabel);
addAndShow(m_headerLayout, chipLabel);
}
}
if (!spec.name.isEmpty())
{
addAndShow(m_layout, new QLabel(spec.name, this));
addAndShow(m_headerLayout, new QLabel(spec.name, m_headerRow));
}
const bool hasHeader = m_headerLayout->count() > 0;
if (hasHeader)
{
m_headerLayout->addStretch(1);
}
m_headerRow->setVisible(hasHeader);
// Second line: what the cycle costs, makes and takes.
addAmounts(spec.inputs);
if (!spec.inputs.empty() && !spec.outputs.empty())
{
const QChar rightArrow(0x2192); // U+2192 RIGHTWARDS ARROW
addAndShow(m_layout, new QLabel(QString(rightArrow), this));
addAndShow(m_amountsLayout, new QLabel(QString(rightArrow), m_amountsRow));
}
addAmounts(spec.outputs);
@@ -108,10 +136,10 @@ void RecipeLineRow::rebuild(const Spec& spec)
const QString time = spec.durationIsAddition
? tr("+%1 s").arg(*spec.durationSeconds, 0, 'f', 1)
: tr("%1 s").arg(*spec.durationSeconds, 0, 'f', 1);
addAndShow(m_layout, new QLabel(
QStringLiteral("%1 %2").arg(middleDot).arg(time), this));
addAndShow(m_amountsLayout, new QLabel(
QStringLiteral("%1 %2").arg(middleDot).arg(time), m_amountsRow));
}
m_layout->addStretch(1);
m_amountsLayout->addStretch(1);
}
void RecipeLineRow::addAmounts(const std::vector<Amount>& amounts)
@@ -126,15 +154,16 @@ void RecipeLineRow::addAmounts(const std::vector<Amount>& amounts)
: QPixmap();
if (!icon.isNull())
{
QLabel* iconLabel = new QLabel(this);
QLabel* iconLabel = new QLabel(m_amountsRow);
iconLabel->setPixmap(icon);
addAndShow(m_layout, iconLabel);
addAndShow(m_amountsLayout, iconLabel);
}
else
{
addAndShow(m_layout,
new QLabel(QString::fromStdString(entry.itemId), this));
addAndShow(m_amountsLayout,
new QLabel(QString::fromStdString(entry.itemId), m_amountsRow));
}
addAndShow(m_layout, new QLabel(QString::number(entry.amount), this));
addAndShow(m_amountsLayout,
new QLabel(QString::number(entry.amount), m_amountsRow));
}
}