Debian Codegraph @

kysdk-qtwidgets/src/klistviewdelegate.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhenyu Wang <[email protected]>
20 *
21 */
22
23 #include "klistviewdelegate.h"
24 #include "themeController.h"
25 #include <QAbstractTextDocumentLayout>
26 #include <QApplication>
27 #include <QCoreApplication>
28 #include <QDebug>
29 #include <QFont>
30 #include <QFontMetrics>
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QListView>
34 #include <QPainter>
35 #include <QPainterPath>
36 #include <QStandardItemModel>
37 #include <QString>
38 #include <QTextDocument>
39 #include <QLinearGradient>
40
41 namespace kdk
42 {
43
44 class KListViewDelegatePrivate : public QObject, public ThemeController
45 {
46 Q_OBJECT
47 Q_DECLARE_PUBLIC(KListViewDelegate)
48 public:
49 KListViewDelegatePrivate(KListViewDelegate *parent);
50 ~KListViewDelegatePrivate();
51
52 private:
53 KListViewDelegate *q_ptr;
54 QAbstractItemView *m_listView;
55 };
56
57 KListViewDelegatePrivate::KListViewDelegatePrivate(KListViewDelegate *parent)
58 : q_ptr(parent)
59 {
60 Q_Q(KListViewDelegate);
61 initThemeStyle();
62 connect(m_gsetting, &QGSettings::changed, this, [=]() {
63 initThemeStyle();
64 });
65 }
66
67 KListViewDelegatePrivate::~KListViewDelegatePrivate()
68 {
69 }
70
71 KListViewDelegate::KListViewDelegate(QAbstractItemView *parent)
72 : QStyledItemDelegate(parent)
73 , d_ptr(new KListViewDelegatePrivate(this))
74 {
75 Q_D(KListViewDelegate);
76 d->m_listView = parent;
77 }
78
79 KListViewDelegate::~KListViewDelegate()
80 {
81 }
82
83 void KListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
84 {
85 Q_D(const KListViewDelegate);
86
87 QFont font;
88 font = QApplication::font();
89 QFontMetrics fm(font);
90 int height = fm.height();
91
92
93 if (index.isValid()) {
94 QListView *view = dynamic_cast<QListView *>(d->m_listView);
95 if (!view)
96 return;
97 if (view->viewMode() == QListView::ListMode) {
98 QRectF rect;
99 rect.setX(option.rect.x());
100 rect.setY(option.rect.y());
101 rect.setWidth(option.rect.width());
102 rect.setHeight(option.rect.height());
103 // QPainterPath画圆角矩形
104 qreal radius = ThemeController::getRadiusFromDT("kradius-min");
105 if (radius == -1)
106 radius = 4;
107 QLinearGradient m_linearGradient = QLinearGradient(rect.width() / 2, rect.y(), rect.width() / 2, rect.height() + rect.y());
108 QColor startColor;
109 QColor endColor;
110 QPainterPath path;
111 path.moveTo(rect.topRight() - QPointF(radius, 0));
112 path.lineTo(rect.topLeft() + QPointF(radius, 0));
113 path.quadTo(rect.topLeft(), rect.topLeft() + QPointF(0, radius));
114 path.lineTo(rect.bottomLeft() + QPointF(0, -radius));
115 path.quadTo(rect.bottomLeft(), rect.bottomLeft() + QPointF(radius, 0));
116 path.lineTo(rect.bottomRight() - QPointF(radius, 0));
117 path.quadTo(rect.bottomRight(), rect.bottomRight() + QPointF(0, -radius));
118 path.lineTo(rect.topRight() + QPointF(0, radius));
119 path.quadTo(rect.topRight(), rect.topRight() + QPointF(-radius, -0));
120
121 QString mainText = index.data(Qt::DisplayRole).toString(); // 获取主文本和副文本
122 QString subText = index.data(Qt::UserRole).toString();
123
124 if (subText.isNull()) {
125 auto *model = dynamic_cast<QStandardItemModel *>(const_cast<QAbstractItemModel *>(index.model())); // 获取图标
126 QIcon icon = model->item(index.row())->icon();
127 QRect iconRect = QRect(rect.left() + 8, rect.top() + 10, d->m_listView->iconSize().width() + (height - 23) * 2, d->m_listView->iconSize().height() + (height - 23) * 2); // 绘制图标的位置
128 QRect mainTextRect = QRect(iconRect.right() + 8, rect.top() + 10 + iconRect.height() / 2 - height / 2, rect.width(), height); // 绘制文本的位置
129
130 painter->save();
131
132 // item可选中 (单选)
133 if (option.state.testFlag(QStyle::State_MouseOver) && !(option.state.testFlag(QStyle::State_Selected))) {
134 ThemeController::getGradientFromDT("kcontain-hover",startColor,endColor);
135 m_linearGradient.setColorAt(0,startColor);
136 m_linearGradient.setColorAt(1,endColor);
137 painter->setPen(QPen(Qt::NoPen));
138 painter->setBrush(m_linearGradient);
139 painter->drawPath(path);
140 } else if (option.state.testFlag(QStyle::State_MouseOver) && (option.state.testFlag(QStyle::State_Selected))) {
141 ThemeController::getGradientFromDT("kcomponent-hover",startColor,endColor);
142 m_linearGradient.setColorAt(0,startColor);
143 m_linearGradient.setColorAt(1,endColor);
144 painter->setPen(QPen(Qt::NoPen));
145 painter->setBrush(m_linearGradient);
146 painter->drawPath(path);
147 } else if (option.state.testFlag(QStyle::State_Selected)) {
148 ThemeController::getGradientFromDT("kcomponent-normal",startColor,endColor);
149 m_linearGradient.setColorAt(0,startColor);
150 m_linearGradient.setColorAt(1,endColor);
151 painter->setPen(QPen(Qt::NoPen));
152 painter->setBrush(m_linearGradient);
153 painter->drawPath(path);
154 }
155
156 QPixmap pixmap = icon.pixmap(d->m_listView->iconSize().width() + (height - 23) * 2, d->m_listView->iconSize().width() + (height - 23) * 2);
157 painter->drawPixmap(iconRect, pixmap);
158
159 // 点击反白效果,画icon和text
160
161 painter->setPen(ThemeController::getCustomColorFromDT("kfont-primary"));
162 painter->drawText(mainTextRect, mainText);
163 painter->restore();
164
165 } else {
166 auto *model = dynamic_cast<QStandardItemModel *>(const_cast<QAbstractItemModel *>(index.model())); // 获取图标
167 QIcon icon = model->item(index.row())->icon();
168 QRect iconRect = QRect(rect.left() + 8, rect.top() + 10, d->m_listView->iconSize().width() + (height - 23) * 2, d->m_listView->iconSize().height() + (height - 23) * 2); // 绘制图标的位置
169 QRect mainTextRect = QRect(iconRect.right() + 8, rect.top() + 5, rect.width(), height); // 绘制两个文本的位置
170 QRect subTextRect = QRect(iconRect.right() + 8, rect.bottom() - 5 - height, rect.width(), height);
171
172 painter->save();
173
174 // item可选中 (单选)
175 if (option.state.testFlag(QStyle::State_MouseOver) && !(option.state.testFlag(QStyle::State_Selected))) {
176 ThemeController::getGradientFromDT("kcontain-hover",startColor,endColor);
177 m_linearGradient.setColorAt(0,startColor);
178 m_linearGradient.setColorAt(1,endColor);
179 painter->setPen(QPen(Qt::NoPen));
180 painter->setBrush(m_linearGradient);
181 painter->drawPath(path);
182 } else if (option.state.testFlag(QStyle::State_MouseOver) && (option.state.testFlag(QStyle::State_Selected))) {
183 ThemeController::getGradientFromDT("kcomponent-hover",startColor,endColor);
184 m_linearGradient.setColorAt(0,startColor);
185 m_linearGradient.setColorAt(1,endColor);
186 painter->setPen(QPen(Qt::NoPen));
187 painter->setBrush(m_linearGradient);
188 painter->drawPath(path);
189 } else if (option.state.testFlag(QStyle::State_Selected)) {
190 ThemeController::getGradientFromDT("kcomponent-normal",startColor,endColor);
191 m_linearGradient.setColorAt(0,startColor);
192 m_linearGradient.setColorAt(1,endColor);
193 painter->setPen(QPen(Qt::NoPen));
194 painter->setBrush(m_linearGradient);
195 painter->drawPath(path);
196 }
197
198 // icon.paint(painter,iconRect);
199 QPixmap pixmap = icon.pixmap(QSize(d->m_listView->iconSize().width() + (height - 23) * 2, d->m_listView->iconSize().height() + (height - 23) * 2));
200 painter->drawPixmap(iconRect, pixmap);
201
202 // 点击反白效果,画icon和text
203 painter->setPen(ThemeController::getCustomColorFromDT("kfont-primary"));
204 painter->drawText(mainTextRect, mainText);
205 painter->setPen(ThemeController::getCustomColorFromDT("kfont-secondary"));
206 painter->drawText(subTextRect, subText);
207 painter->restore();
208 }
209 } else if (view->viewMode() == QListView::IconMode) {
210 QStyleOptionViewItem styleOptionViewItem(option); // 定义
211 initStyleOption(&styleOptionViewItem, index); // 初始化
212 QStyle *pStyle = styleOptionViewItem.widget->style();
213 pStyle->drawControl(QStyle::CE_ItemViewItem, &styleOptionViewItem, painter, styleOptionViewItem.widget);
214 }
215 }
216 }
217
218 QSize KListViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
219 {
220 Q_D(const KListViewDelegate);
221
222 QFont font; // 获取系统字体大小
223 font = QApplication::font();
224 QFontMetrics fm(font);
225 int height = fm.height();
226
227 return QSize(option.rect.width(), height * 2 + 6);
228 }
229
230 }
231
232 #include "klistviewdelegate.moc"
233 #include "moc_klistviewdelegate.cpp"