File indexing completed on 2024-05-12 16:02:01

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