give the selection cards their own parts instead of label blobs
This commit is contained in:
78
src/ui/selection/ItemChipRow.cpp
Normal file
78
src/ui/selection/ItemChipRow.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "ItemChipRow.h"
|
||||
|
||||
#include <QGridLayout>
|
||||
|
||||
#include "ItemChip.h"
|
||||
#include "ItemIconCache.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// Size the item icon is drawn at inside a chip, in device-independent pixels.
|
||||
const int kChipIconSizePx = 18;
|
||||
|
||||
// Chips per row. Two fit the panel's capped width side by side; a third would force the
|
||||
// counts to shrink.
|
||||
const int kChipsPerRow = 2;
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
ItemChipRow::ItemChipRow(ItemIconCache* itemIcons, QWidget* parent)
|
||||
: QWidget(parent)
|
||||
, m_itemIcons(itemIcons)
|
||||
{
|
||||
m_layout = new QGridLayout(this);
|
||||
m_layout->setContentsMargins(0, 0, 0, 0);
|
||||
m_layout->setSpacing(4);
|
||||
}
|
||||
|
||||
void ItemChipRow::setEntries(const std::vector<Entry>& entries)
|
||||
{
|
||||
std::vector<std::string> itemIds;
|
||||
itemIds.reserve(entries.size());
|
||||
for (const Entry& entry : entries)
|
||||
{
|
||||
itemIds.push_back(entry.itemId);
|
||||
}
|
||||
|
||||
// Only the set of items changing is a structural change; the counts change every
|
||||
// tick and must not cost a widget rebuild.
|
||||
if (itemIds != m_itemIds)
|
||||
{
|
||||
m_itemIds = std::move(itemIds);
|
||||
rebuildChips(entries);
|
||||
}
|
||||
|
||||
for (std::size_t index = 0; index < entries.size(); ++index)
|
||||
{
|
||||
m_chips[index]->setCount(entries[index].countText);
|
||||
m_chips[index]->setSubLine(entries[index].subLine);
|
||||
}
|
||||
setVisible(!entries.empty());
|
||||
}
|
||||
|
||||
void ItemChipRow::rebuildChips(const std::vector<Entry>& entries)
|
||||
{
|
||||
for (ItemChip* chip : m_chips)
|
||||
{
|
||||
m_layout->removeWidget(chip);
|
||||
chip->deleteLater();
|
||||
}
|
||||
m_chips.clear();
|
||||
|
||||
for (std::size_t index = 0; index < entries.size(); ++index)
|
||||
{
|
||||
const std::string& itemId = entries[index].itemId;
|
||||
// A missing icon file is not an error (REQ-UI-ITEM-ICON): the chip is then laid
|
||||
// out around its count alone.
|
||||
const QPixmap icon = m_itemIcons->hasIcon(itemId)
|
||||
? m_itemIcons->getPixmap(itemId, kChipIconSizePx)
|
||||
: QPixmap();
|
||||
|
||||
ItemChip* chip = new ItemChip(icon, this);
|
||||
m_layout->addWidget(chip, static_cast<int>(index) / kChipsPerRow,
|
||||
static_cast<int>(index) % kChipsPerRow);
|
||||
m_chips.push_back(chip);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user