Krita Source Code Documentation
Loading...
Searching...
No Matches
KisPaletteDelegate.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include <QPen>
8#include <QPainter>
9#include <QtMath> // qBound
10
11#include <kis_global.h>
12#include <KisPaletteModel.h>
13#include "kis_debug.h"
14
15#include "KisPaletteDelegate.h"
16
18
20 : QAbstractItemDelegate(parent)
21{ }
22
24{ }
25
26void KisPaletteDelegate::paint(QPainter *painter,
27 const QStyleOptionViewItem &option,
28 const QModelIndex &index) const
29{
30 if (!index.isValid())
31 return;
32
33 painter->save();
34 const bool isSelected = option.state & QStyle::State_Selected;
35
36 if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
37 // Paint group header
38 QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
39 if (isSelected) {
40 painter->fillRect(option.rect, option.palette.highlight());
41 }
42 QRect paintRect = kisGrowRect(option.rect, -BORDER_WIDTH);
43 painter->setBrush(QBrush(Qt::lightGray));
44 painter->drawText(paintRect, name);
45 }
46 else {
47 QRect paintRect = option.rect;
48 if (isSelected) {
49 painter->fillRect(option.rect, option.palette.highlight());
50 paintRect = kisGrowRect(option.rect, -BORDER_WIDTH);
51 }
52 if (qvariant_cast<bool>(index.data(KisPaletteModel::CheckSlotRole))) {
53 QBrush brush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
54 painter->fillRect(paintRect, brush);
55 } else {
56 QBrush lightBrush(Qt::gray);
57 QBrush darkBrush(Qt::darkGray);
58 painter->fillRect(paintRect, lightBrush);
59 painter->fillRect(QRect(paintRect.topLeft(), paintRect.center()), darkBrush);
60 painter->fillRect(QRect(paintRect.center(), paintRect.bottomRight()), darkBrush);
61 }
62
63 QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
64 if (!m_crossedKeyword.isNull() && name.toLower().contains(m_crossedKeyword)) {
65 // paint crossed line
66
67 QRect crossRect = kisGrowRect(option.rect, -qBound(2, option.rect.width() / 6, 4));
68
69 painter->save();
70 painter->setRenderHint(QPainter::Antialiasing, true);
71 painter->setPen(QPen(Qt::white, 2.5));
72 painter->drawLine(crossRect.topLeft(), crossRect.bottomRight());
73 painter->setPen(QPen(Qt::red, 1.0));
74 painter->drawLine(crossRect.topLeft(), crossRect.bottomRight());
75 painter->restore();
76
77 }
78 }
79
80 painter->restore();
81}
82
83QSize KisPaletteDelegate::sizeHint(const QStyleOptionViewItem &option,
84 const QModelIndex &) const
85{
86 return option.decorationSize;
87}
KisPaletteDelegate(QObject *parent=0)
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &) const override
static const int BORDER_WIDTH
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186
const char * name(StandardAction id)
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327