Changeset View
Changeset View
Standalone View
Standalone View
src/kcategorydrawer.cpp
1 | /** | 1 | /** | ||
---|---|---|---|---|---|
2 | * This file is part of the KDE project | 2 | * This file is part of the KDE project | ||
3 | * Copyright 2019 David Redondo <kde@david-redondo.de> | ||||
3 | * Copyright (C) 2007, 2009 Rafael Fernández López <ereslibre@kde.org> | 4 | * Copyright (C) 2007, 2009 Rafael Fernández López <ereslibre@kde.org> | ||
4 | * | 5 | * | ||
5 | * This library is free software; you can redistribute it and/or | 6 | * This library is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU Library General Public | 7 | * modify it under the terms of the GNU Library General Public | ||
7 | * License as published by the Free Software Foundation; either | 8 | * License as published by the Free Software Foundation; either | ||
8 | * version 2 of the License, or (at your option) any later version. | 9 | * version 2 of the License, or (at your option) any later version. | ||
9 | * | 10 | * | ||
10 | * This library is distributed in the hope that it will be useful, | 11 | * This library is distributed in the hope that it will be useful, | ||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | 53 | { | |||
53 | delete d; | 54 | delete d; | ||
54 | } | 55 | } | ||
55 | 56 | | |||
56 | void KCategoryDrawer::drawCategory(const QModelIndex &index, | 57 | void KCategoryDrawer::drawCategory(const QModelIndex &index, | ||
57 | int /*sortRole*/, | 58 | int /*sortRole*/, | ||
58 | const QStyleOption &option, | 59 | const QStyleOption &option, | ||
59 | QPainter *painter) const | 60 | QPainter *painter) const | ||
60 | { | 61 | { | ||
62 | // Keep this in sync with Kirigami.ListSectionHeader | ||||
61 | painter->setRenderHint(QPainter::Antialiasing); | 63 | painter->setRenderHint(QPainter::Antialiasing); | ||
62 | 64 | | |||
63 | const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString(); | 65 | const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString(); | ||
64 | const QRect optRect = option.rect; | 66 | const QRect optRect = option.rect; | ||
65 | QFont font(QApplication::font()); | 67 | QFont font(QApplication::font()); | ||
66 | font.setBold(true); | 68 | // Match Heading with level 3 | ||
69 | font.setPointSizeF(font.pointSize() * 1.20); | ||||
67 | const QFontMetrics fontMetrics = QFontMetrics(font); | 70 | const QFontMetrics fontMetrics = QFontMetrics(font); | ||
68 | 71 | | |||
69 | QColor outlineColor = option.palette.text().color(); | 72 | QColor backgroundColor = option.palette.window().color(); | ||
70 | outlineColor.setAlphaF(0.35); | | |||
71 | 73 | | |||
72 | //BEGIN: top left corner | 74 | //BEGIN: background | ||
73 | { | 75 | { | ||
76 | QRect backgroundRect(option.rect); | ||||
77 | backgroundRect.setHeight(categoryHeight(index, option)); | ||||
74 | painter->save(); | 78 | painter->save(); | ||
75 | painter->setPen(outlineColor); | 79 | painter->setBrush(backgroundColor); | ||
76 | const QPointF topLeft(optRect.topLeft()); | 80 | painter->setPen(Qt::NoPen); | ||
77 | QRectF arc(topLeft, QSizeF(4, 4)); | 81 | painter->drawRect(backgroundRect); | ||
78 | arc.translate(0.5, 0.5); | | |||
79 | painter->drawArc(arc, 1440, 1440); | | |||
80 | painter->restore(); | 82 | painter->restore(); | ||
81 | } | 83 | } | ||
82 | //END: top left corner | 84 | //END: background | ||
83 | | ||||
84 | //BEGIN: left vertical line | | |||
85 | { | | |||
86 | QPoint start(optRect.topLeft()); | | |||
87 | start.ry() += 3; | | |||
88 | QPoint verticalGradBottom(optRect.topLeft()); | | |||
89 | verticalGradBottom.ry() += fontMetrics.height() + 5; | | |||
90 | QLinearGradient gradient(start, verticalGradBottom); | | |||
91 | gradient.setColorAt(0, outlineColor); | | |||
92 | gradient.setColorAt(1, Qt::transparent); | | |||
93 | painter->fillRect(QRect(start, QSize(1, fontMetrics.height() + 5)), gradient); | | |||
94 | } | | |||
95 | //END: left vertical line | | |||
96 | | ||||
97 | //BEGIN: horizontal line | | |||
98 | { | | |||
99 | QPoint start(optRect.topLeft()); | | |||
100 | start.rx() += 3; | | |||
101 | QPoint horizontalGradTop(optRect.topLeft()); | | |||
102 | horizontalGradTop.rx() += optRect.width() - 6; | | |||
103 | painter->fillRect(QRect(start, QSize(optRect.width() - 6, 1)), outlineColor); | | |||
104 | } | | |||
105 | //END: horizontal line | | |||
106 | | ||||
107 | //BEGIN: top right corner | | |||
108 | { | | |||
109 | painter->save(); | | |||
110 | painter->setPen(outlineColor); | | |||
111 | QPointF topRight(optRect.topRight()); | | |||
112 | topRight.rx() -= 4; | | |||
113 | QRectF arc(topRight, QSizeF(4, 4)); | | |||
114 | arc.translate(0.5, 0.5); | | |||
115 | painter->drawArc(arc, 0, 1440); | | |||
116 | painter->restore(); | | |||
117 | } | | |||
118 | //END: top right corner | | |||
119 | | ||||
120 | //BEGIN: right vertical line | | |||
121 | { | | |||
122 | QPoint start(optRect.topRight()); | | |||
123 | start.ry() += 3; | | |||
124 | QPoint verticalGradBottom(optRect.topRight()); | | |||
125 | verticalGradBottom.ry() += fontMetrics.height() + 5; | | |||
126 | QLinearGradient gradient(start, verticalGradBottom); | | |||
127 | gradient.setColorAt(0, outlineColor); | | |||
128 | gradient.setColorAt(1, Qt::transparent); | | |||
129 | painter->fillRect(QRect(start, QSize(1, fontMetrics.height() + 5)), gradient); | | |||
130 | } | | |||
131 | //END: right vertical line | | |||
132 | 85 | | |||
133 | //BEGIN: text | 86 | //BEGIN: text | ||
134 | { | 87 | { | ||
88 | // Kirgami.Units.{small/large}Spacing respectively | ||||
89 | constexpr int topPadding = 4; | ||||
90 | constexpr int sidePadding = 8; | ||||
135 | QRect textRect(option.rect); | 91 | QRect textRect(option.rect); | ||
136 | textRect.setTop(textRect.top() + 7); | 92 | textRect.setTop(textRect.top() + topPadding); | ||
137 | textRect.setLeft(textRect.left() + 7); | 93 | textRect.setLeft(textRect.left() + sidePadding); | ||
ognarb: Instead of using magic number, I would create a constant named padding. | |||||
138 | textRect.setHeight(fontMetrics.height()); | 94 | textRect.setHeight(fontMetrics.height()); | ||
139 | textRect.setRight(textRect.right() - 7); | 95 | textRect.setRight(textRect.right() - sidePadding); | ||
140 | 96 | | |||
141 | painter->save(); | 97 | painter->save(); | ||
142 | painter->setFont(font); | 98 | painter->setFont(font); | ||
143 | QColor penColor(option.palette.text().color()); | 99 | QColor penColor(option.palette.text().color()); | ||
144 | penColor.setAlphaF(0.6); | | |||
145 | painter->setPen(penColor); | 100 | painter->setPen(penColor); | ||
146 | painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, category); | 101 | painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, category); | ||
147 | painter->restore(); | 102 | painter->restore(); | ||
148 | } | 103 | } | ||
149 | //END: text | 104 | //END: text | ||
150 | } | 105 | } | ||
151 | 106 | | |||
152 | int KCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const | 107 | int KCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const | ||
153 | { | 108 | { | ||
154 | Q_UNUSED(index); | 109 | Q_UNUSED(index); | ||
155 | Q_UNUSED(option) | 110 | Q_UNUSED(option) | ||
156 | 111 | | |||
157 | QFont font(QApplication::font()); | 112 | QFont font(QApplication::font()); | ||
158 | font.setBold(true); | 113 | font.setPointSizeF(font.pointSize() * 1.20); | ||
159 | QFontMetrics fontMetrics(font); | 114 | QFontMetrics fontMetrics(font); | ||
160 | 115 | | |||
161 | const int height = fontMetrics.height() + 1 /* 1 pixel-width gradient */ | 116 | const int height = fontMetrics.height() + 8; | ||
162 | + 11 /* top and bottom separation */; | | |||
163 | return height; | 117 | return height; | ||
164 | } | 118 | } | ||
165 | 119 | | |||
166 | int KCategoryDrawer::leftMargin() const | 120 | int KCategoryDrawer::leftMargin() const | ||
167 | { | 121 | { | ||
168 | return 0; | 122 | return 0; | ||
169 | } | 123 | } | ||
170 | 124 | | |||
Show All 34 Lines |
Instead of using magic number, I would create a constant named padding.