#include "ItemChip.h" #include #include #include #include #include #include #include #include "ItemTooltip.h" namespace { // Point-size rise of the count and drop of the sub-line, relative to the card's text. const int kCountSizeRisePt = 2; const int kSubLineSizeDropPt = 1; // How long the cursor rests on a chip before its tooltip appears, in milliseconds. // Matches the feel of an ordinary tooltip, which this one replaces (REQ-UI-ITEM-TOOLTIP). const int kHoverDelayMs = 500; } // namespace ItemChip::ItemChip(const SelectionContext& context, const std::string& itemId, const QPixmap& icon, QWidget* parent) : QWidget(parent) , m_context(context) , m_itemId(itemId) { // Its own boxed chrome, drawn with palette colors like the rest of the panel's // furniture rather than from visuals.toml, which is for world rendering. setAttribute(Qt::WA_StyledBackground, true); setStyleSheet(QStringLiteral( "ItemChip { border: 1px solid palette(mid); border-radius: 3px; }")); QHBoxLayout* layout = new QHBoxLayout(this); layout->setContentsMargins(4, 3, 4, 3); layout->setSpacing(6); m_iconLabel = new QLabel(this); m_iconLabel->setPixmap(icon); m_iconLabel->setVisible(!icon.isNull()); QWidget* text = new QWidget(this); QVBoxLayout* textLayout = new QVBoxLayout(text); textLayout->setContentsMargins(0, 0, 0, 0); textLayout->setSpacing(0); m_countLabel = new QLabel(text); QFont countFont = m_countLabel->font(); countFont.setPointSize(countFont.pointSize() + kCountSizeRisePt); m_countLabel->setFont(countFont); m_subLineLabel = new QLabel(text); QFont subLineFont = m_subLineLabel->font(); subLineFont.setPointSize(qMax(1, subLineFont.pointSize() - kSubLineSizeDropPt)); m_subLineLabel->setFont(subLineFont); QPalette subLinePalette = m_subLineLabel->palette(); subLinePalette.setColor(QPalette::WindowText, palette().color(QPalette::Disabled, QPalette::WindowText)); m_subLineLabel->setPalette(subLinePalette); m_subLineLabel->hide(); textLayout->addWidget(m_countLabel); textLayout->addWidget(m_subLineLabel); layout->addWidget(m_iconLabel); layout->addWidget(text); m_hoverTimer = new QTimer(this); m_hoverTimer->setSingleShot(true); m_hoverTimer->setInterval(kHoverDelayMs); connect(m_hoverTimer, &QTimer::timeout, this, &ItemChip::showTooltip); } void ItemChip::setCount(const QString& count) { m_countLabel->setText(count); } void ItemChip::setSubLine(const QString& subLine) { m_subLineLabel->setText(subLine); m_subLineLabel->setVisible(!subLine.isEmpty()); } void ItemChip::enterEvent(QEvent* event) { m_hoverTimer->start(); QWidget::enterEvent(event); } void ItemChip::leaveEvent(QEvent* event) { m_hoverTimer->stop(); if (m_tooltip != nullptr) { m_tooltip->hide(); } QWidget::leaveEvent(event); } void ItemChip::showTooltip() { if (m_tooltip == nullptr) { m_tooltip = new ItemTooltip(m_context, m_itemId, this); } m_tooltip->showAt(QCursor::pos()); }