split the selection panel into one card per kind of selection
The panel rendered every selection out of a single pool of member widgets,
hidden and shown per branch, so each build path had to remember to hide the
other branches' widgets. That coupling produced the two defects fixed in
668ce0f, and the per-type detail the requirements now ask for would only add
more of it.
The pool is gone. SelectionPanel keeps the category arbitration, the float and
the hide-when-empty behaviour, and hosts exactly one SelectionContent at a
time; SelectionContentFactory picks which one from the selection alone. Each
card is one row of the catalog in REQ-UI-SELECTION-CONTENT and owns only its
own widgets.
The card structure (REQ-UI-SELECTION-CARD) lives in the base class: a header
with an identity symbol, a name and one right slot, then a configuration group
and a runtime group. A construction site keeps its configuration and has its
whole runtime group replaced by the construction progress, decided once there
rather than in every card (REQ-BLD-SITE-CONFIG).
Also implemented here:
- REQ-UI-SELECTION-STATUS: the header status dot, taken from the simulation's
own getProductionStatus() so the panel and the world's status light cannot
disagree.
- REQ-UI-SELECTION-AGGREGATE: belt-subsystem tiles and debris-only selections
collapse into one card with a count instead of a count summary.
- REQ-UI-HQ-PANEL: the HQ shows the global block stock and its HP, neither of
which is a buffer.
- BuildingIconCache, extracted from BuildButtonBar's file-local chip loading so
the card headers and the build buttons rasterize the same SVGs once.
FieldSelectionPanel is deleted: ships, stations, debris and the field count
summary are four more cards in the same factory, so the two-panel arbitration
collapses into one decision.
The card parts are still today's labels and buttons; the item chips, bars,
recipe summary and stat rows follow.
Build clean, 541 tests pass, app runs with no Qt warnings. Visual check
pending.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcReq7hVk4KUPhTDKWAG7K
This commit is contained in:
120
src/ui/BuildingIconCache.cpp
Normal file
120
src/ui/BuildingIconCache.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#include "BuildingIconCache.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QGuiApplication>
|
||||
#include <QPainter>
|
||||
#include <QRegularExpression>
|
||||
#include <QSvgRenderer>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// Chip background color for a build button the player cannot afford
|
||||
// (REQ-UI-BUILD-DISABLED). Part of the icon rather than of any widget's palette, so it
|
||||
// lives with the recoloring step.
|
||||
const char* const kGreyFill = "fill=\"#5f636e\"";
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
BuildingIconCache::BuildingIconCache(const QString& iconDir)
|
||||
: m_iconDir(iconDir)
|
||||
{
|
||||
}
|
||||
|
||||
const QByteArray& BuildingIconCache::getSvg(const std::string& iconName)
|
||||
{
|
||||
const std::map<std::string, QByteArray>::const_iterator cached =
|
||||
m_svgByName.find(iconName);
|
||||
if (cached != m_svgByName.end())
|
||||
{
|
||||
return cached->second;
|
||||
}
|
||||
|
||||
// An absent or unreadable file caches an empty byte array so it is not retried;
|
||||
// a missing icon is not an error (REQ-UI-BUILD-ICON).
|
||||
QByteArray svg;
|
||||
QFile file(m_iconDir + "/" + QString::fromStdString(iconName) + ".svg");
|
||||
if (file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
svg = file.readAll();
|
||||
}
|
||||
return m_svgByName.emplace(iconName, std::move(svg)).first->second;
|
||||
}
|
||||
|
||||
const QByteArray& BuildingIconCache::getGreySvg(const std::string& iconName)
|
||||
{
|
||||
const std::map<std::string, QByteArray>::const_iterator cached =
|
||||
m_greySvgByName.find(iconName);
|
||||
if (cached != m_greySvgByName.end())
|
||||
{
|
||||
return cached->second;
|
||||
}
|
||||
|
||||
// Recolor only the chip background: the first "#rrggbb" fill in the file is the
|
||||
// rounded background rect; the white glyph uses fill="none" and is left alone.
|
||||
const QByteArray& svg = getSvg(iconName);
|
||||
QByteArray greyed = svg;
|
||||
if (!svg.isEmpty())
|
||||
{
|
||||
QString text = QString::fromUtf8(svg);
|
||||
static const QRegularExpression fillPattern(
|
||||
QStringLiteral("fill=\"#[0-9a-fA-F]{6}\""));
|
||||
const QRegularExpressionMatch match = fillPattern.match(text);
|
||||
if (match.hasMatch())
|
||||
{
|
||||
text.replace(match.capturedStart(), match.capturedLength(),
|
||||
QLatin1String(kGreyFill));
|
||||
}
|
||||
greyed = text.toUtf8();
|
||||
}
|
||||
return m_greySvgByName.emplace(iconName, std::move(greyed)).first->second;
|
||||
}
|
||||
|
||||
bool BuildingIconCache::hasIcon(const std::string& iconName)
|
||||
{
|
||||
return !getSvg(iconName).isEmpty();
|
||||
}
|
||||
|
||||
QPixmap BuildingIconCache::getChip(const std::string& iconName, int sizePx)
|
||||
{
|
||||
return getPixmap(iconName, getSvg(iconName), sizePx);
|
||||
}
|
||||
|
||||
QPixmap BuildingIconCache::getGreyChip(const std::string& iconName, int sizePx)
|
||||
{
|
||||
return getPixmap("grey:" + iconName, getGreySvg(iconName), sizePx);
|
||||
}
|
||||
|
||||
QPixmap BuildingIconCache::getPixmap(const std::string& cacheKey,
|
||||
const QByteArray& svg, int sizePx)
|
||||
{
|
||||
if (sizePx <= 0)
|
||||
{
|
||||
return QPixmap();
|
||||
}
|
||||
|
||||
const std::pair<std::string, int> key(cacheKey, sizePx);
|
||||
const std::map<std::pair<std::string, int>, QPixmap>::const_iterator cached =
|
||||
m_pixmapCache.find(key);
|
||||
if (cached != m_pixmapCache.end())
|
||||
{
|
||||
return cached->second;
|
||||
}
|
||||
|
||||
QPixmap pixmap;
|
||||
if (!svg.isEmpty())
|
||||
{
|
||||
// Rasterized straight at its on-screen size times the device pixel ratio, so it
|
||||
// stays crisp without a downscale step.
|
||||
const qreal dpr = qApp ? qApp->devicePixelRatio() : 1.0;
|
||||
QSvgRenderer renderer(svg);
|
||||
pixmap = QPixmap(static_cast<int>(sizePx * dpr), static_cast<int>(sizePx * dpr));
|
||||
pixmap.setDevicePixelRatio(dpr);
|
||||
pixmap.fill(Qt::transparent);
|
||||
QPainter painter(&pixmap);
|
||||
painter.setRenderHint(QPainter::Antialiasing, true);
|
||||
renderer.render(&painter);
|
||||
}
|
||||
return m_pixmapCache.emplace(key, std::move(pixmap)).first->second;
|
||||
}
|
||||
Reference in New Issue
Block a user