File indexing completed on 2024-05-19 04:29:28

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include <QPen>
0008 #include <QPainter>
0009 #include <QtMath> // qBound
0010 
0011 #include <kis_global.h>
0012 #include <KisPaletteModel.h>
0013 #include "kis_debug.h"
0014 
0015 #include "KisPaletteDelegate.h"
0016 
0017 const int KisPaletteDelegate::BORDER_WIDTH = 3;
0018 
0019 KisPaletteDelegate::KisPaletteDelegate(QObject *parent)
0020     : QAbstractItemDelegate(parent)
0021 { }
0022 
0023 KisPaletteDelegate::~KisPaletteDelegate()
0024 { }
0025 
0026 void KisPaletteDelegate::paint(QPainter *painter,
0027                                const QStyleOptionViewItem &option,
0028                                const QModelIndex &index) const
0029 {
0030     if (!index.isValid())
0031         return;
0032 
0033     painter->save();
0034     const bool isSelected = option.state & QStyle::State_Selected;
0035 
0036     if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
0037         // Paint group header
0038         QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
0039         if (isSelected) {
0040             painter->fillRect(option.rect, option.palette.highlight());
0041         }
0042         QRect paintRect = kisGrowRect(option.rect, -BORDER_WIDTH);
0043         painter->setBrush(QBrush(Qt::lightGray));
0044         painter->drawText(paintRect, name);
0045     }
0046     else {
0047         QRect paintRect = option.rect;
0048         if (isSelected) {
0049             painter->fillRect(option.rect, option.palette.highlight());
0050             paintRect = kisGrowRect(option.rect, -BORDER_WIDTH);
0051         }
0052         if (qvariant_cast<bool>(index.data(KisPaletteModel::CheckSlotRole))) {
0053             QBrush brush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
0054             painter->fillRect(paintRect, brush);
0055         } else {
0056             QBrush lightBrush(Qt::gray);
0057             QBrush darkBrush(Qt::darkGray);
0058             painter->fillRect(paintRect, lightBrush);
0059             painter->fillRect(QRect(paintRect.topLeft(), paintRect.center()), darkBrush);
0060             painter->fillRect(QRect(paintRect.center(), paintRect.bottomRight()), darkBrush);
0061         }
0062 
0063         QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
0064         if (!m_crossedKeyword.isNull() && name.toLower().contains(m_crossedKeyword)) {
0065             // paint crossed line
0066 
0067             QRect crossRect = kisGrowRect(option.rect, -qBound(2, option.rect.width() / 6, 4));
0068 
0069             painter->save();
0070             painter->setRenderHint(QPainter::Antialiasing, true);
0071             painter->setPen(QPen(Qt::white, 2.5));
0072             painter->drawLine(crossRect.topLeft(), crossRect.bottomRight());
0073             painter->setPen(QPen(Qt::red, 1.0));
0074             painter->drawLine(crossRect.topLeft(), crossRect.bottomRight());
0075             painter->restore();
0076 
0077         }
0078     }
0079 
0080     painter->restore();
0081 }
0082 
0083 QSize KisPaletteDelegate::sizeHint(const QStyleOptionViewItem &option,
0084                                    const QModelIndex &) const
0085 {
0086     return option.decorationSize;
0087 }