[Client] Fix alt badge and trail review findings

- Drop the dead VPN warning branch: rowVisible() already hides IP rows
  above the threshold, so setVpnThreshold(), its member and the spin-box
  connect never took effect.
- Dim IP rows at shared_count > 2 instead of > 1: the count already
  includes the investigated account, so every IP row was dimmed.
- Truncate backStack when a breadcrumb is clicked instead of pushing the
  current user again, so the trail keeps a single path and Back works.
- Word-wrap the trail label so a long chain cannot widen the tab.
- Extract the status/match pill painting into widgets/utility/pill_painter
  and use it from both delegates instead of duplicating it.
This commit is contained in:
Lukas Brübach 2026-11-01 12:21:16 +01:00
parent d7b2786184
commit d09f4c5356
8 changed files with 120 additions and 92 deletions

View file

@ -287,6 +287,8 @@ set(cockatrice_SOURCES
src/interface/widgets/utility/custom_line_edit.cpp src/interface/widgets/utility/custom_line_edit.cpp
src/interface/widgets/utility/epoch_format.h src/interface/widgets/utility/epoch_format.h
src/interface/widgets/utility/get_text_with_max.cpp src/interface/widgets/utility/get_text_with_max.cpp
src/interface/widgets/utility/pill_painter.cpp
src/interface/widgets/utility/pill_painter.h
src/interface/widgets/utility/report_utils.cpp src/interface/widgets/utility/report_utils.cpp
src/interface/widgets/utility/report_utils.h src/interface/widgets/utility/report_utils.h
src/interface/widgets/utility/table_activation.cpp src/interface/widgets/utility/table_activation.cpp

View file

@ -1,5 +1,7 @@
#include "alt_table_delegate.h" #include "alt_table_delegate.h"
#include "../utility/pill_painter.h"
#include <QApplication> #include <QApplication>
#include <QModelIndex> #include <QModelIndex>
#include <QPainter> #include <QPainter>
@ -9,9 +11,6 @@
namespace namespace
{ {
constexpr qreal PILL_ALPHA = 0.30;
constexpr int PILL_PAD_X = 10;
constexpr int PILL_PAD_Y = 4;
constexpr int ICON_TEXT_GAP = 4; constexpr int ICON_TEXT_GAP = 4;
} // namespace } // namespace
@ -21,11 +20,6 @@ AltTableDelegate::AltTableDelegate(int viaColumn, int warnsColumn, int bansColum
{ {
} }
void AltTableDelegate::setVpnThreshold(int threshold)
{
vpnThreshold = threshold;
}
void AltTableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const void AltTableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
const bool selected = (option.state & QStyle::State_Selected) != 0; const bool selected = (option.state & QStyle::State_Selected) != 0;
@ -37,58 +31,27 @@ void AltTableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
const QString text = index.data(Qt::DisplayRole).toString(); const QString text = index.data(Qt::DisplayRole).toString();
const QPalette::ColorRole emphasisRole = selected ? QPalette::HighlightedText : QPalette::WindowText; const QPalette::ColorRole emphasisRole = selected ? QPalette::HighlightedText : QPalette::WindowText;
auto paintPill = [&](QPalette::ColorRole tintRole, QPalette::ColorRole textRole, const QIcon &icon,
const QString &pillText) {
const QRect pillRect = opt.rect.adjusted(PILL_PAD_X / 2, PILL_PAD_Y / 2, -PILL_PAD_X / 2, -PILL_PAD_Y / 2);
if (pillRect.width() <= 0 || pillRect.height() <= 0) {
QStyledItemDelegate::paint(painter, opt, index);
return;
}
if (!selected) {
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
QColor pill = opt.palette.color(tintRole);
pill.setAlphaF(PILL_ALPHA);
painter->setPen(Qt::NoPen);
painter->setBrush(pill);
painter->drawRoundedRect(pillRect, pillRect.height() / 2.0, pillRect.height() / 2.0);
painter->restore();
}
QRect textRect = pillRect;
if (!icon.isNull()) {
const int iconSize = qMin(pillRect.height(), pillRect.width());
if (iconSize > 0) {
const QRect iconRect(pillRect.left() + ICON_TEXT_GAP, pillRect.top(), iconSize, iconSize);
icon.paint(painter, iconRect);
textRect.setLeft(iconRect.right() + ICON_TEXT_GAP);
}
}
const QString elided = opt.fontMetrics.elidedText(pillText, opt.textElideMode, textRect.width());
style->drawItemText(painter, textRect, Qt::AlignCenter, opt.palette, true, elided, textRole);
};
if (index.column() == viaColumn) { if (index.column() == viaColumn) {
bool sharedOk = false; bool sharedOk = false;
const unsigned shared = index.sibling(index.row(), sharedColumn).data(Qt::DisplayRole).toUInt(&sharedOk); const unsigned shared = index.sibling(index.row(), sharedColumn).data(Qt::DisplayRole).toUInt(&sharedOk);
const bool dimmed = sharedOk && text.startsWith(QStringLiteral("ip:")) && shared > 1; const bool ipMatch = text.startsWith(QStringLiteral("ip:"));
const bool vpn = // shared_count for an IP match already includes the investigated account,
sharedOk && text.startsWith(QStringLiteral("ip:")) && shared > static_cast<unsigned>(vpnThreshold); // so anything above 2 means the address is shared beyond that pair.
const bool dimmed = sharedOk && ipMatch && shared > 2;
const QPalette::ColorRole textRole = (dimmed && !selected) ? QPalette::PlaceholderText : emphasisRole; const QPalette::ColorRole textRole = (dimmed && !selected) ? QPalette::PlaceholderText : emphasisRole;
QPalette::ColorRole tintRole = QPalette::NoRole;
if (text == QStringLiteral("email")) { if (text == QStringLiteral("email")) {
paintPill(QPalette::Link, emphasisRole, QIcon(), text); tintRole = QPalette::Link;
return; } else if (text.startsWith(QStringLiteral("clientid:"))) {
tintRole = QPalette::Highlight;
} else if (ipMatch) {
tintRole = QPalette::PlaceholderText;
} }
if (text.startsWith(QStringLiteral("clientid:"))) { if (tintRole != QPalette::NoRole) {
paintPill(QPalette::Highlight, emphasisRole, QIcon(), text); if (!pill_painter::paint(painter, opt, tintRole, textRole, text)) {
return; QStyledItemDelegate::paint(painter, opt, index);
}
if (text.startsWith(QStringLiteral("ip:"))) {
if (vpn) {
paintPill(QPalette::Highlight, textRole, style->standardIcon(QStyle::SP_MessageBoxWarning), text);
return;
} }
paintPill(QPalette::PlaceholderText, textRole, QIcon(), text);
return; return;
} }
QStyledItemDelegate::paint(painter, opt, index); QStyledItemDelegate::paint(painter, opt, index);
@ -123,7 +86,7 @@ QSize AltTableDelegate::sizeHint(const QStyleOptionViewItem &option, const QMode
{ {
const QSize hint = QStyledItemDelegate::sizeHint(option, index); const QSize hint = QStyledItemDelegate::sizeHint(option, index);
if (index.column() == viaColumn) { if (index.column() == viaColumn) {
return hint + QSize(PILL_PAD_X + 2 * ICON_TEXT_GAP, PILL_PAD_Y); return hint + QSize(pill_painter::PAD_X + 2 * ICON_TEXT_GAP, pill_painter::PAD_Y);
} }
if (index.column() == warnsColumn || index.column() == bansColumn) { if (index.column() == warnsColumn || index.column() == bansColumn) {
return hint + QSize(2 * ICON_TEXT_GAP, 0); return hint + QSize(2 * ICON_TEXT_GAP, 0);

View file

@ -6,10 +6,10 @@
/** /**
* Badge + icon painting for the User Alts table. The match-evidence column * Badge + icon painting for the User Alts table. The match-evidence column
* renders as a palette-tinted pill per match type (email / client id / IP), * renders as a palette-tinted pill per match type (email / client id / IP), and
* with the style's warning icon once an IP is shared by more than the VPN * an IP shared by more than one other account is dimmed. Warn/ban counts gain
* threshold. Warn/ban counts gain the style's warning/critical icons when * the style's warning/critical icons when nonzero. Selected rows and unknown
* nonzero. Selected rows and unknown content fall back to default painting. * content fall back to default painting.
*/ */
class AltTableDelegate : public QStyledItemDelegate class AltTableDelegate : public QStyledItemDelegate
{ {
@ -20,7 +20,6 @@ public:
int bansColumn, int bansColumn,
int sharedColumn, int sharedColumn,
QObject *parent = nullptr); QObject *parent = nullptr);
void setVpnThreshold(int threshold);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
[[nodiscard]] QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; [[nodiscard]] QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
@ -29,7 +28,6 @@ private:
int warnsColumn; int warnsColumn;
int bansColumn; int bansColumn;
int sharedColumn; int sharedColumn;
int vpnThreshold = 10;
}; };
#endif // ALT_TABLE_DELEGATE_H #endif // ALT_TABLE_DELEGATE_H

View file

@ -1,18 +1,13 @@
#include "report_status_delegate.h" #include "report_status_delegate.h"
#include "../utility/pill_painter.h"
#include <QApplication> #include <QApplication>
#include <QPainter> #include <QPainter>
#include <QPalette> #include <QPalette>
#include <QStyle> #include <QStyle>
#include <QStyleOptionViewItem> #include <QStyleOptionViewItem>
namespace
{
constexpr qreal PILL_ALPHA = 0.30;
constexpr int PILL_PAD_X = 10;
constexpr int PILL_PAD_Y = 4;
} // namespace
ReportStatusDelegate::ReportStatusDelegate(QObject *parent) : QStyledItemDelegate(parent) ReportStatusDelegate::ReportStatusDelegate(QObject *parent) : QStyledItemDelegate(parent)
{ {
} }
@ -46,32 +41,14 @@ void ReportStatusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
return; return;
} }
const QRect pillRect = opt.rect.adjusted(PILL_PAD_X / 2, PILL_PAD_Y / 2, -PILL_PAD_X / 2, -PILL_PAD_Y / 2);
if (pillRect.width() <= 0 || pillRect.height() <= 0) {
QStyledItemDelegate::paint(painter, opt, index);
return;
}
if (!selected) {
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
QColor pill = opt.palette.color(tintRole);
pill.setAlphaF(PILL_ALPHA);
painter->setPen(Qt::NoPen);
painter->setBrush(pill);
painter->drawRoundedRect(pillRect, pillRect.height() / 2.0, pillRect.height() / 2.0);
painter->restore();
}
const QString elided =
opt.fontMetrics.elidedText(opt.text.isEmpty() ? status : opt.text, opt.textElideMode, pillRect.width());
const QPalette::ColorRole textRole = selected ? QPalette::HighlightedText : QPalette::WindowText; const QPalette::ColorRole textRole = selected ? QPalette::HighlightedText : QPalette::WindowText;
QStyle *style = opt.widget != nullptr ? opt.widget->style() : QApplication::style(); if (!pill_painter::paint(painter, opt, tintRole, textRole, opt.text.isEmpty() ? status : opt.text)) {
style->drawItemText(painter, pillRect, Qt::AlignCenter, opt.palette, true, elided, textRole); QStyledItemDelegate::paint(painter, opt, index);
}
} }
QSize ReportStatusDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const QSize ReportStatusDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
const QSize hint = QStyledItemDelegate::sizeHint(option, index); const QSize hint = QStyledItemDelegate::sizeHint(option, index);
return hint + QSize(PILL_PAD_X, PILL_PAD_Y); return hint + QSize(pill_painter::PAD_X, pill_painter::PAD_Y);
} }

View file

@ -107,9 +107,12 @@ TabAlts::TabAlts(TabSupervisor *_tabSupervisor, AbstractClient *_client, const Q
trailLabel->setTextFormat(Qt::RichText); trailLabel->setTextFormat(Qt::RichText);
trailLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); trailLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
trailLabel->setOpenExternalLinks(false); trailLabel->setOpenExternalLinks(false);
// The trail grows with every hop, so let it wrap instead of forcing the tab
// wider than the window.
trailLabel->setWordWrap(true);
connect(trailLabel, &QLabel::linkActivated, this, [this](const QString &userName) { connect(trailLabel, &QLabel::linkActivated, this, [this](const QString &userName) {
if (!userName.isEmpty() && userName != currentUser) { if (!userName.isEmpty() && userName != currentUser) {
investigate(userName); goToTrailEntry(userName);
} }
}); });
@ -147,8 +150,6 @@ TabAlts::TabAlts(TabSupervisor *_tabSupervisor, AbstractClient *_client, const Q
altsTable = new QTableWidget(0, COL_ALTS_COUNT); altsTable = new QTableWidget(0, COL_ALTS_COUNT);
configureAltTable(altsTable); configureAltTable(altsTable);
altsDelegate = new AltTableDelegate(COL_ALTS_VIA, COL_ALTS_WARNS, COL_ALTS_BANS, COL_ALTS_SHARED, altsTable); altsDelegate = new AltTableDelegate(COL_ALTS_VIA, COL_ALTS_WARNS, COL_ALTS_BANS, COL_ALTS_SHARED, altsTable);
altsDelegate->setVpnThreshold(vpnSpin->value());
connect(vpnSpin, &QSpinBox::valueChanged, altsDelegate, &AltTableDelegate::setVpnThreshold);
altsTable->setItemDelegateForColumn(COL_ALTS_VIA, altsDelegate); altsTable->setItemDelegateForColumn(COL_ALTS_VIA, altsDelegate);
altsTable->setItemDelegateForColumn(COL_ALTS_WARNS, altsDelegate); altsTable->setItemDelegateForColumn(COL_ALTS_WARNS, altsDelegate);
altsTable->setItemDelegateForColumn(COL_ALTS_BANS, altsDelegate); altsTable->setItemDelegateForColumn(COL_ALTS_BANS, altsDelegate);
@ -271,6 +272,20 @@ void TabAlts::investigate(const QString &userName)
loadUser(userName); loadUser(userName);
} }
void TabAlts::goToTrailEntry(const QString &userName)
{
const int index = backStack.indexOf(userName);
if (index < 0) {
investigate(userName);
return;
}
// Clicking an earlier crumb truncates the history there instead of pushing
// another entry, so the trail keeps a single path and Back still works.
backStack.erase(backStack.begin() + index, backStack.end());
backButton->setEnabled(!backStack.isEmpty());
loadUser(userName);
}
void TabAlts::goBack() void TabAlts::goBack()
{ {
if (backStack.isEmpty()) { if (backStack.isEmpty()) {
@ -441,6 +456,8 @@ void TabAlts::renderTable()
altsTable->setItem(row, COL_ALTS_WARNS, new QTableWidgetItem(QString::number(alt.warn_count()))); altsTable->setItem(row, COL_ALTS_WARNS, new QTableWidgetItem(QString::number(alt.warn_count())));
altsTable->setItem(row, COL_ALTS_BANS, new QTableWidgetItem(QString::number(alt.ban_count()))); altsTable->setItem(row, COL_ALTS_BANS, new QTableWidgetItem(QString::number(alt.ban_count())));
altsTable->setItem(row, COL_ALTS_ACTIVE, new QTableWidgetItem(alt.is_active() ? tr("yes") : tr("no"))); altsTable->setItem(row, COL_ALTS_ACTIVE, new QTableWidgetItem(alt.is_active() ? tr("yes") : tr("no")));
// shared_count already includes the investigated account, so anything
// above 2 means the address is shared beyond that pair.
if (QString::fromStdString(alt.match_reason()).startsWith(QStringLiteral("ip:")) && alt.has_shared_count() && if (QString::fromStdString(alt.match_reason()).startsWith(QStringLiteral("ip:")) && alt.has_shared_count() &&
alt.shared_count() > 2) { alt.shared_count() > 2) {
for (int col = 0; col < COL_ALTS_COUNT; ++col) { for (int col = 0; col < COL_ALTS_COUNT; ++col) {

View file

@ -52,6 +52,7 @@ private slots:
private: private:
void loadUser(const QString &userName); void loadUser(const QString &userName);
void goToTrailEntry(const QString &userName);
void updateTrail(); void updateTrail();
void requestAlts(const QString &userName, int depth); void requestAlts(const QString &userName, int depth);
void requestDetails(const QString &userName); void requestDetails(const QString &userName);

View file

@ -0,0 +1,41 @@
#include "pill_painter.h"
#include <QApplication>
#include <QFontMetrics>
#include <QPainter>
#include <QRect>
#include <QStyle>
#include <QStyleOptionViewItem>
namespace
{
constexpr qreal PILL_ALPHA = 0.30;
} // namespace
bool pill_painter::paint(QPainter *painter,
const QStyleOptionViewItem &option,
QPalette::ColorRole tintRole,
QPalette::ColorRole textRole,
const QString &text)
{
const QRect pillRect = option.rect.adjusted(PAD_X / 2, PAD_Y / 2, -PAD_X / 2, -PAD_Y / 2);
if (pillRect.width() <= 0 || pillRect.height() <= 0) {
return false;
}
if ((option.state & QStyle::State_Selected) == 0) {
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
QColor pill = option.palette.color(tintRole);
pill.setAlphaF(PILL_ALPHA);
painter->setPen(Qt::NoPen);
painter->setBrush(pill);
painter->drawRoundedRect(pillRect, pillRect.height() / 2.0, pillRect.height() / 2.0);
painter->restore();
}
QStyle *style = option.widget != nullptr ? option.widget->style() : QApplication::style();
const QString elided = option.fontMetrics.elidedText(text, option.textElideMode, pillRect.width());
style->drawItemText(painter, pillRect, Qt::AlignCenter, option.palette, true, elided, textRole);
return true;
}

View file

@ -0,0 +1,29 @@
#ifndef PILL_PAINTER_H
#define PILL_PAINTER_H
#include <QPalette>
#include <QString>
class QPainter;
class QStyleOptionViewItem;
namespace pill_painter
{
constexpr int PAD_X = 10;
constexpr int PAD_Y = 4;
/**
* Draws a translucent, palette-tinted rounded pill inside the option's rect and
* paints the text elided and centred on it, so badges follow the active theme.
* Returns false when the cell is too small to hold a pill, leaving the caller to
* fall back to default painting.
*/
bool paint(QPainter *painter,
const QStyleOptionViewItem &option,
QPalette::ColorRole tintRole,
QPalette::ColorRole textRole,
const QString &text);
} // namespace pill_painter
#endif // PILL_PAINTER_H