File indexing completed on 2024-05-12 16:02:03
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::paintCrossedLine(const QStyleOptionViewItem &option, QPainter *painter) const 0027 { 0028 QRect crossRect = kisGrowRect(option.rect, -qBound(2, option.rect.width() / 6, 4)); 0029 0030 painter->save(); 0031 painter->setRenderHint(QPainter::Antialiasing, true); 0032 painter->setPen(QPen(Qt::white, 2.5)); 0033 painter->drawLine(crossRect.topLeft(), crossRect.bottomRight()); 0034 painter->setPen(QPen(Qt::red, 1.0)); 0035 painter->drawLine(crossRect.topLeft(), crossRect.bottomRight()); 0036 painter->restore(); 0037 } 0038 0039 void KisPaletteDelegate::paintNonCrossed(QPainter */*painter*/, const QStyleOptionViewItem &/*option*/, 0040 const QModelIndex &/*index*/, const bool /*isSelected*/) const 0041 { 0042 } 0043 0044 void KisPaletteDelegate::paintGroupName(QPainter *painter, const QStyleOptionViewItem &option, 0045 const QModelIndex &index, const bool isSelected) const 0046 { 0047 QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole)); 0048 if (isSelected) { 0049 painter->fillRect(option.rect, option.palette.highlight()); 0050 } 0051 QRect paintRect = kisGrowRect(option.rect, -BORDER_WIDTH); 0052 painter->setBrush(QBrush(Qt::lightGray)); 0053 painter->drawText(paintRect, name); 0054 } 0055 0056 void KisPaletteDelegate::paint(QPainter *painter, 0057 const QStyleOptionViewItem &option, 0058 const QModelIndex &index) const 0059 { 0060 if (!index.isValid()) 0061 return; 0062 0063 painter->save(); 0064 const bool isSelected = option.state & QStyle::State_Selected; 0065 0066 if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) { 0067 paintGroupName(painter, option, index, isSelected); 0068 } else { 0069 QRect paintRect = option.rect; 0070 if (isSelected) { 0071 painter->fillRect(option.rect, option.palette.highlight()); 0072 paintRect = kisGrowRect(option.rect, -BORDER_WIDTH); 0073 } 0074 if (qvariant_cast<bool>(index.data(KisPaletteModel::CheckSlotRole))) { 0075 QBrush brush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole)); 0076 painter->fillRect(paintRect, brush); 0077 } else { 0078 QBrush lightBrush(Qt::gray); 0079 QBrush darkBrush(Qt::darkGray); 0080 painter->fillRect(paintRect, lightBrush); 0081 painter->fillRect(QRect(paintRect.topLeft(), paintRect.center()), darkBrush); 0082 painter->fillRect(QRect(paintRect.center(), paintRect.bottomRight()), darkBrush); 0083 } 0084 0085 QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole)); 0086 if (!m_crossedKeyword.isNull() && name.toLower().contains(m_crossedKeyword)) { 0087 paintCrossedLine(option, painter); 0088 } 0089 } 0090 0091 painter->restore(); 0092 } 0093 0094 QSize KisPaletteDelegate::sizeHint(const QStyleOptionViewItem &option, 0095 const QModelIndex &) const 0096 { 0097 return option.decorationSize; 0098 }