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/modlist.cpp | 4 +--- src/modlistview.cpp | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/modlist.cpp b/src/modlist.cpp index d4b2cc53..41b5c858 100644 --- a/src/modlist.cpp +++ b/src/modlist.cpp @@ -407,9 +407,7 @@ QVariant ModList::data(const QModelIndex &modelIndex, int role) const return QVariant(); } else if (role == Qt::ForegroundRole) { - if ((modInfo->hasFlag(ModInfo::FLAG_SEPARATOR) || (column == COL_NOTES)) && modInfo->color().isValid()) { - return ColorSettings::idealTextColor(modInfo->color()); - } else if (column == COL_NAME) { + if (column == COL_NAME) { int highlight = modInfo->getHighlight(); if (highlight & ModInfo::HIGHLIGHT_IMPORTANT) return QBrush(Qt::darkRed); 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