From c135920b16639136d9fa35598c0af4a7b14416b8 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Fri, 1 Jan 2021 19:24:17 +0100 Subject: Fix highligth of collapsed separator. Move foreground color fix to delegate. --- src/modlistview.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'src/modlistview.cpp') diff --git a/src/modlistview.cpp b/src/modlistview.cpp index 9f94354e..d54563a1 100644 --- a/src/modlistview.cpp +++ b/src/modlistview.cpp @@ -49,16 +49,21 @@ public: void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const override { + // the parent version always overwrite the background brush, so + // we need to save it and restore it + auto backgroundColor = option->backgroundBrush.color(); QStyledItemDelegate::initStyleOption(option, index); - auto color = childrenColor(index, m_view, Qt::BackgroundRole); - if (color.isValid()) { - option->backgroundBrush = color; + + if (backgroundColor.isValid()) { + option->backgroundBrush = backgroundColor; } } void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { QStyleOptionViewItem opt(option); + + // remove items indentaiton when using collapsible separators if (index.column() == 0 && m_view->hasCollapsibleSeparators()) { if (!index.model()->hasChildren(index) && index.parent().isValid()) { auto parentIndex = index.parent().data(ModList::IndexRole).toInt(); @@ -67,6 +72,28 @@ public: } } } + + // compute required color from children, otherwise fallback to the + // color from the model, and draw the background here + auto color = childrenColor(index, m_view, Qt::BackgroundRole); + if (!color.isValid()) { + color = index.data(Qt::BackgroundRole).value(); + } + else { + // disable alternating row if the color is from the children + opt.features &= ~QStyleOptionViewItem::Alternate; + } + opt.backgroundBrush = color; + + // compute ideal foreground color for some rows + if (color.isValid()) { + if ((index.column() == ModList::COL_NAME + && ModInfo::getByIndex(index.data(ModList::IndexRole).toInt())->isSeparator()) + || index.column() == ModList::COL_NOTES) { + opt.palette.setBrush(QPalette::Text, ColorSettings::idealTextColor(color)); + } + } + QStyledItemDelegate::paint(painter, opt, index); } }; -- cgit v1.3.1