Skip to content

Commit a1bde36

Browse files
committed
Group Manager: center character token icons and size column to scaled icon; paint background first; move header resize after model set; recalc token column/rows on icon-size change
1 parent 4f3de6d commit a1bde36

1 file changed

Lines changed: 72 additions & 3 deletions

File tree

src/group/groupwidget.cpp

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,50 @@ NODISCARD static const char *getColumnFriendlyName(const ColumnTypeEnum column)
101101
}
102102
} // namespace
103103

104+
namespace {
105+
class TokenCenteringDelegate : public QStyledItemDelegate
106+
{
107+
public:
108+
using QStyledItemDelegate::QStyledItemDelegate;
109+
110+
void paint(QPainter *painter, const QStyleOptionViewItem &option,
111+
const QModelIndex &index) const override
112+
{
113+
if (index.column() == static_cast<int>(ColumnTypeEnum::CHARACTER_TOKEN)) {
114+
// Draw the default cell background (selection, grid, etc.)
115+
QStyledItemDelegate::paint(painter, option, index);
116+
117+
// Draw the icon centered over the background
118+
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
119+
if (!icon.isNull()) {
120+
QSize iconSize = option.decorationSize;
121+
QRect iconRect = QStyle::alignedRect(
122+
option.direction,
123+
Qt::AlignCenter,
124+
iconSize,
125+
option.rect
126+
);
127+
icon.paint(painter, iconRect, Qt::AlignCenter);
128+
}
129+
return;
130+
}
131+
132+
QStyledItemDelegate::paint(painter, option, index);
133+
}
134+
135+
136+
QSize sizeHint(const QStyleOptionViewItem &option,
137+
const QModelIndex &index) const override
138+
{
139+
if (index.column() == static_cast<int>(ColumnTypeEnum::CHARACTER_TOKEN)) {
140+
// Match icon size with padding to avoid clipping
141+
return option.decorationSize + QSize(4, 4);
142+
}
143+
return QStyledItemDelegate::sizeHint(option, index);
144+
}
145+
};
146+
} // namespace
147+
104148
GroupProxyModel::GroupProxyModel(QObject *const parent)
105149
: QSortFilterProxyModel(parent)
106150
{}
@@ -745,20 +789,35 @@ GroupWidget::GroupWidget(Mmapper2Group *const group, MapData *const md, QWidget
745789
m_table->setSelectionMode(QAbstractItemView::SingleSelection);
746790
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
747791

748-
m_table->horizontalHeader()->setStretchLastSection(true);
749-
m_table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
750-
751792
m_proxyModel = new GroupProxyModel(this);
752793
m_proxyModel->setSourceModel(&m_model);
753794
m_table->setModel(m_proxyModel);
754795

796+
// Token column: size to contents so it tracks icon size.
797+
// Name & Room: stretch to fill; others: contents is fine.
798+
auto *hh = m_table->horizontalHeader();
799+
hh->setStretchLastSection(false);
800+
for (int c = 0; c < m_table->model()->columnCount(); ++c) {
801+
if (c == static_cast<int>(ColumnTypeEnum::CHARACTER_TOKEN)) {
802+
hh->setSectionResizeMode(c, QHeaderView::ResizeToContents);
803+
} else if (c == static_cast<int>(ColumnTypeEnum::NAME)
804+
|| c == static_cast<int>(ColumnTypeEnum::ROOM_NAME)) {
805+
hh->setSectionResizeMode(c, QHeaderView::Stretch);
806+
} else {
807+
hh->setSectionResizeMode(c, QHeaderView::ResizeToContents);
808+
}
809+
}
810+
755811
m_table->setDragEnabled(true);
756812
m_table->setAcceptDrops(true);
757813
m_table->setDragDropMode(QAbstractItemView::InternalMove);
758814
m_table->setDefaultDropAction(Qt::MoveAction);
759815
m_table->setDropIndicatorShown(true);
760816

761817
m_table->setItemDelegate(new GroupDelegate(this));
818+
m_table->setItemDelegateForColumn(static_cast<int>(ColumnTypeEnum::CHARACTER_TOKEN),
819+
new TokenCenteringDelegate(m_table));
820+
762821
layout->addWidget(m_table);
763822

764823
// Row height follows configured size; draw token at 85% of that
@@ -768,6 +827,9 @@ GroupWidget::GroupWidget(Mmapper2Group *const group, MapData *const md, QWidget
768827

769828
const int shown = std::max(1, static_cast<int>(std::lround(row * 0.85f)));
770829
m_table->setIconSize(QSize(shown, shown));
830+
m_table->resizeColumnToContents(static_cast<int>(ColumnTypeEnum::CHARACTER_TOKEN));
831+
m_table->resizeRowsToContents();
832+
m_table->viewport()->update();
771833

772834
m_center = new QAction(QIcon(":/icons/roomfind.png"), tr("&Center"), this);
773835
connect(m_center, &QAction::triggered, this, [this]() {
@@ -908,6 +970,8 @@ void GroupWidget::updateColumnVisibility()
908970
const bool hide_tokens = !getConfig().groupManager.showTokens;
909971
m_table->setColumnHidden(static_cast<int>(ColumnTypeEnum::CHARACTER_TOKEN), hide_tokens);
910972

973+
// m_table->resizeColumnsToContents();
974+
911975
// Apply current icon-size preference every time settings change
912976
{
913977
const int icon = getConfig().groupManager.tokenIconSize;
@@ -917,6 +981,11 @@ void GroupWidget::updateColumnVisibility()
917981

918982
const int shown = std::max(1, static_cast<int>(std::lround(row * 0.85f)));
919983
m_table->setIconSize(QSize(shown, shown));
984+
985+
// Recalculate column/row sizes for token column
986+
m_table->resizeColumnToContents(static_cast<int>(ColumnTypeEnum::CHARACTER_TOKEN));
987+
m_table->resizeRowsToContents();
988+
m_table->viewport()->update();
920989
}
921990
}
922991

0 commit comments

Comments
 (0)