Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_palette_delegate.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 "kis_palette_delegate.h"
8
9#include <QPen>
10#include <QPainter>
11
12#include <kis_global.h>
13#include "kis_debug.h"
14#include <KisPaletteModel.h>
15
16
18 : QAbstractItemDelegate(parent)
19{
20}
21
25
27{
29}
30
31void KisPaletteDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
32{
33 painter->save();
34
35 if (! index.isValid())
36 return;
37
38 const bool isSelected = option.state & QStyle::State_Selected;
39 const int minSize = qMin(option.rect.width(), option.rect.height());
40 const int maxWidth = qBound(2, minSize / 6, 4);
41 const int width = isSelected ? maxWidth : 1;
42
43 if (qvariant_cast<bool>(index.data(KisPaletteModel::IsHeaderRole))) {
44 QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
45 if (isSelected) {
46 painter->fillRect(option.rect, option.palette.highlight());
47 }
48 QRect paintRect = kisGrowRect(option.rect, -width);
49 painter->drawText(paintRect, name);
50 } else {
51 if (isSelected) {
52 painter->fillRect(option.rect, option.palette.highlight());
53 }
54 QRect paintRect = kisGrowRect(option.rect, -width);
55 QBrush brush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
56 painter->fillRect(paintRect, brush);
57 }
58 painter->restore();
59
60 QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole));
61 if (!m_crossedKeyword.isNull() && name.toLower().contains(m_crossedKeyword)) {
62 QRect crossRect = kisGrowRect(option.rect, -maxWidth);
63
64 painter->save();
65 painter->setRenderHint(QPainter::Antialiasing, true);
66 painter->setPen(QPen(Qt::white, 2.5));
67 painter->drawLine(crossRect.topLeft(), crossRect.bottomRight());
68 painter->setPen(QPen(Qt::red, 1.0));
69 painter->drawLine(crossRect.topLeft(), crossRect.bottomRight());
70 painter->restore();
71 }
72}
73
74QSize KisPaletteDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex &) const
75{
76 return option.decorationSize;
77}
float value(const T *src, size_t ch)
KisPaletteDelegate(QObject *parent=0)
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &) const override
void setCrossedKeyword(const QString &value)
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186