Propagate color of selected messages too
[trojita.git] / src / Gui / ColoredItemDelegate.cpp
blob25cd1c24f457322c666ed98e890b3ce329e650ae
1 /* Copyright (C) Roland Pallai <dap78@magex.hu>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "ColoredItemDelegate.h"
25 namespace Gui
28 ColoredItemDelegate::ColoredItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
32 void ColoredItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
34 QStyleOptionViewItem viewOption(option);
35 QColor itemForegroundColor = index.data(Qt::ForegroundRole).value<QColor>();
36 if (itemForegroundColor.isValid()) {
37 // Thunderbird works like this and we like it
38 viewOption.palette.setColor(QPalette::ColorGroup::Active, QPalette::Highlight, itemForegroundColor);
40 // We have to make sure that the text remains readable with the new Highlight color.
41 // This covers most of the cases out there if not all. Feel free to extend if you are using a very
42 // special theme.
43 auto fgLightnessF = viewOption.palette.color(QPalette::ColorGroup::Active, QPalette::HighlightedText).lightnessF();
44 auto bgLightnessF = viewOption.palette.color(QPalette::ColorGroup::Active, QPalette::Highlight).lightnessF();
45 if (fgLightnessF > 0.75 && bgLightnessF > 0.75) {
46 viewOption.palette.setColor(QPalette::ColorGroup::Active, QPalette::HighlightedText, Qt::black);
47 } else
48 if (fgLightnessF < 0.25 && bgLightnessF < 0.25) {
49 viewOption.palette.setColor(QPalette::ColorGroup::Active, QPalette::HighlightedText, Qt::white);
52 viewOption.palette.setColor(QPalette::ColorGroup::Inactive, QPalette::HighlightedText, itemForegroundColor);
54 QStyledItemDelegate::paint(painter, viewOption, index);