Changeset View
Changeset View
Standalone View
Standalone View
libs/widgets/KisPaletteDelegate.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include <QPen> | ||||
20 | #include <QPainter> | ||||
21 | #include <QtMath> // qBound | ||||
22 | | ||||
23 | #include <kis_global.h> | ||||
24 | #include <KisPaletteModel.h> | ||||
25 | #include "kis_debug.h" | ||||
26 | | ||||
27 | #include "KisPaletteDelegate.h" | ||||
28 | | ||||
29 | const int KisPaletteDelegate::BORDER_WIDTH = 3; | ||||
30 | | ||||
31 | KisPaletteDelegate::KisPaletteDelegate(QObject *parent) | ||||
32 | : QAbstractItemDelegate(parent) | ||||
33 | { } | ||||
34 | | ||||
35 | KisPaletteDelegate::~KisPaletteDelegate() | ||||
36 | { } | ||||
37 | | ||||
38 | void KisPaletteDelegate::paintCrossedLine(const QStyleOptionViewItem &option, QPainter *painter) const | ||||
39 | { | ||||
40 | QRect crossRect = kisGrowRect(option.rect, -qBound(2, option.rect.width() / 6, 4)); | ||||
41 | | ||||
42 | painter->save(); | ||||
43 | painter->setRenderHint(QPainter::Antialiasing, true); | ||||
44 | painter->setPen(QPen(Qt::white, 2.5)); | ||||
45 | painter->drawLine(crossRect.topLeft(), crossRect.bottomRight()); | ||||
46 | painter->setPen(QPen(Qt::red, 1.0)); | ||||
47 | painter->drawLine(crossRect.topLeft(), crossRect.bottomRight()); | ||||
48 | painter->restore(); | ||||
49 | } | ||||
50 | | ||||
51 | void KisPaletteDelegate::paintNonCrossed(QPainter *painter, const QStyleOptionViewItem &option, | ||||
52 | const QModelIndex &index, const bool isSelected) const | ||||
53 | { | ||||
54 | } | ||||
55 | | ||||
56 | void KisPaletteDelegate::paintGroupName(QPainter *painter, const QStyleOptionViewItem &option, | ||||
57 | const QModelIndex &index, const bool isSelected) const | ||||
58 | { | ||||
59 | QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole)); | ||||
60 | if (isSelected) { | ||||
61 | painter->fillRect(option.rect, option.palette.highlight()); | ||||
62 | } | ||||
63 | QRect paintRect = kisGrowRect(option.rect, -BORDER_WIDTH); | ||||
64 | painter->setBrush(QBrush(Qt::lightGray)); | ||||
65 | painter->drawText(paintRect, name); | ||||
66 | } | ||||
67 | | ||||
68 | void KisPaletteDelegate::paint(QPainter *painter, | ||||
69 | const QStyleOptionViewItem &option, | ||||
70 | const QModelIndex &index) const | ||||
71 | { | ||||
72 | painter->save(); | ||||
73 | | ||||
74 | if (!index.isValid()) | ||||
75 | return; | ||||
76 | | ||||
77 | const bool isSelected = option.state & QStyle::State_Selected; | ||||
78 | | ||||
79 | if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) { | ||||
80 | paintGroupName(painter, option, index, isSelected); | ||||
81 | } else { | ||||
82 | QRect paintRect = option.rect; | ||||
83 | if (isSelected) { | ||||
84 | painter->fillRect(option.rect, option.palette.highlight()); | ||||
85 | paintRect = kisGrowRect(option.rect, -BORDER_WIDTH); | ||||
86 | } | ||||
87 | if (qvariant_cast<bool>(index.data(KisPaletteModel::CheckSlotRole))) { | ||||
88 | QBrush brush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole)); | ||||
89 | painter->fillRect(paintRect, brush); | ||||
90 | } else { | ||||
91 | QBrush lightBrush(Qt::gray); | ||||
92 | QBrush darkBrush(Qt::darkGray); | ||||
93 | painter->fillRect(paintRect, lightBrush); | ||||
94 | painter->fillRect(QRect(paintRect.topLeft(), paintRect.center()), darkBrush); | ||||
95 | painter->fillRect(QRect(paintRect.center(), paintRect.bottomRight()), darkBrush); | ||||
96 | } | ||||
97 | | ||||
98 | QString name = qvariant_cast<QString>(index.data(Qt::DisplayRole)); | ||||
99 | if (!m_crossedKeyword.isNull() && name.toLower().contains(m_crossedKeyword)) { | ||||
100 | paintCrossedLine(option, painter); | ||||
101 | } | ||||
102 | } | ||||
103 | | ||||
104 | painter->restore(); | ||||
105 | } | ||||
106 | | ||||
107 | QSize KisPaletteDelegate::sizeHint(const QStyleOptionViewItem &option, | ||||
108 | const QModelIndex &) const | ||||
109 | { | ||||
110 | return option.decorationSize; | ||||
111 | } |