Debian Codegraph @

kysdk-qtwidgets/src/kcolorcombobox.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: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include "kcolorcombobox.h"
24 #include "themeController.h"
25 #include <QAbstractItemView>
26 #include <QCompleter>
27 #include <QDebug>
28 #include <QListView>
29 #include <QProxyStyle>
30 #include <QResizeEvent>
31 #include <QStyledItemDelegate>
32 #include <QVariant>
33 #include <qstylepainter.h>
34
35 namespace kdk
36 {
37
38 const static QSize defaultPopupItemSize(20, 20);
39 static QSize g_size;
40
41 class KComboStyle : public QProxyStyle
42 {
43 public:
44 KComboStyle()
45 {
46 }
47 ~KComboStyle()
48 {
49 }
50
51 QSize sizeFromContents(QStyle::ContentsType type,
52 const QStyleOption *option,
53 const QSize &contentsSize,
54 const QWidget *widget = nullptr) const override;
55 };
56
57 class KColorComboBoxDelegate : public QStyledItemDelegate
58 {
59 Q_OBJECT
60
61 public:
62 enum ItemRoles {
63 ColorRole = Qt::UserRole + 1
64 };
65
66 KColorComboBoxDelegate(QObject *parent = nullptr, KColorComboBox *combo = nullptr);
67 void paint(QPainter *painter,
68 const QStyleOptionViewItem &option, const QModelIndex &index) const override;
69 QSize sizeHint(const QStyleOptionViewItem &option,
70 const QModelIndex &index) const override;
71
72 private:
73 KColorComboBox *m_combo;
74 };
75
76 class KColorComboBoxPrivate : public QObject, public ThemeController
77 {
78 Q_OBJECT
79 Q_DECLARE_PUBLIC(KColorComboBox)
80
81 public:
82 KColorComboBoxPrivate(KColorComboBox *parent);
83 void updateList();
84
85 public Q_SLOTS:
86 void slotActivated(int);
87 void slotHighlighted(int);
88 void slotCurrentIndexChanged(int);
89
90 private:
91 KColorComboBox *q_ptr;
92 KColorComboBox::ComboType m_comboType;
93 QList<QColor> m_colorList;
94 int m_borderRadius;
95 QColor m_currentColor;
96 QSize m_popupItemSize;
97 };
98
99 KColorComboBox::KColorComboBox(QWidget *parent)
100 : QComboBox(parent)
101 , d_ptr(new KColorComboBoxPrivate(this))
102 {
103 Q_D(KColorComboBox);
104 setItemDelegate(new KColorComboBoxDelegate(this, this));
105 KComboStyle *style = new KComboStyle();
106 setStyle(style);
107 view()->setFixedWidth(d->m_popupItemSize.width());
108 connect(this, SIGNAL(activated(int)), d, SLOT(slotActivated(int)));
109 connect(this, SIGNAL(highlighted(int)), d, SLOT(slotHighlighted(int)));
110 connect(this, SIGNAL(currentIndexChanged(int)), d, SLOT(slotCurrentIndexChanged(int)));
111 }
112
113 void KColorComboBox::setColorList(const QList<QColor> &list)
114 {
115 Q_D(KColorComboBox);
116 d->m_colorList = list;
117 d->updateList();
118 }
119
120 QList<QColor> KColorComboBox::colorList()
121 {
122 Q_D(KColorComboBox);
123 return d->m_colorList;
124 }
125
126 void KColorComboBox::addColor(const QColor &color)
127 {
128 Q_D(KColorComboBox);
129 d->m_colorList.append(color);
130 addItem(QString());
131 setItemData(d->m_colorList.count() - 1, d->m_colorList.back(), KColorComboBoxDelegate::ColorRole);
132 update();
133 }
134
135 void KColorComboBox::setComboType(const KColorComboBox::ComboType &type)
136 {
137 Q_D(KColorComboBox);
138 d->m_comboType = type;
139 update();
140 }
141
142 KColorComboBox::ComboType KColorComboBox::comboType()
143 {
144 Q_D(KColorComboBox);
145 return d->m_comboType;
146 }
147
148 void KColorComboBox::setPopupItemSize(const QSize &size)
149 {
150 Q_D(KColorComboBox);
151
152 d->m_popupItemSize = size.expandedTo(this->size());
153 g_size = d->m_popupItemSize;
154
155 if (QListView *listView = qobject_cast<QListView *>(view())) {
156 listView->setFixedWidth(d->m_popupItemSize.width());
157 listView->setGridSize(d->m_popupItemSize);
158 }
159
160 view()->updateGeometry();
161 update();
162 }
163
164 QSize KColorComboBox::popupItemSzie()
165 {
166 Q_D(KColorComboBox);
167 return d->m_popupItemSize;
168 }
169
170 void KColorComboBox::paintEvent(QPaintEvent *event)
171 {
172 Q_UNUSED(event)
173 Q_D(KColorComboBox);
174 QStylePainter painter(this);
175 painter.setPen(Qt::NoPen);
176
177 QStyleOptionComboBox opt;
178 initStyleOption(&opt);
179
180 QRect frame = this->rect();
181 painter.setRenderHint(QPainter::Antialiasing);
182 painter.setPen(Qt::transparent);
183 if (d->m_currentColor.isValid())
184 painter.setBrush(d->m_currentColor);
185 if (d->m_comboType == KColorComboBox::Circle) {
186 painter.drawEllipse(frame.adjusted(1, 1, -1, -1));
187 } else {
188 painter.drawRoundedRect(frame.adjusted(1, 1, -1, -1), d->m_borderRadius, d->m_borderRadius);
189 }
190 }
191
192 void KColorComboBox::resizeEvent(QResizeEvent *event)
193 {
194 setPopupItemSize(event->size());
195 QComboBox::resizeEvent(event);
196 }
197
198 KColorComboBoxPrivate::KColorComboBoxPrivate(KColorComboBox *parent)
199 : q_ptr(parent)
200 , m_comboType(KColorComboBox::Circle)
201 , m_popupItemSize(defaultPopupItemSize)
202 {
203 setParent(parent);
204 m_borderRadius = ThemeController::getRadiusFromDT("kradius-min");
205 if (m_borderRadius == -1)
206 m_borderRadius = 4;
207 }
208
209 void KColorComboBoxPrivate::updateList()
210 {
211 Q_Q(KColorComboBox);
212 while (q->count()) {
213 q->removeItem(0);
214 }
215 for (int i = 0; i < m_colorList.count(); ++i) {
216 q->addItem(QString());
217 q->setItemData(i, m_colorList[i], KColorComboBoxDelegate::ColorRole);
218 }
219 q->update();
220 }
221
222 void KColorComboBoxPrivate::slotActivated(int index)
223 {
224 Q_Q(KColorComboBox);
225
226 m_currentColor = m_colorList[index];
227 q->update();
228 Q_EMIT q->activated(m_currentColor);
229 }
230
231 void KColorComboBoxPrivate::slotHighlighted(int index)
232 {
233 Q_Q(KColorComboBox);
234
235 auto color = m_colorList[index];
236 q->update();
237 Q_EMIT q->highlighted(color);
238 }
239
240 void KColorComboBoxPrivate::slotCurrentIndexChanged(int index)
241 {
242 Q_Q(KColorComboBox);
243
244 m_currentColor = m_colorList[index];
245 q->update();
246 Q_EMIT q->currentColorChanged(m_currentColor);
247 }
248
249 KColorComboBoxDelegate::KColorComboBoxDelegate(QObject *parent, KColorComboBox *combo)
250 : QStyledItemDelegate(parent)
251 , m_combo(combo)
252 {
253 }
254
255 void KColorComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
256 {
257 auto variant = index.data(ItemRoles::ColorRole);
258 auto color = variant.value<QColor>();
259 auto paintRect = option.rect.adjusted(5, 5, -5, -5);
260
261 switch (m_combo->comboType()) {
262 case KColorComboBox::Circle: {
263 if (color.isValid()) {
264 painter->save();
265 painter->setRenderHint(QPainter::Antialiasing);
266 painter->setPen(Qt::NoPen);
267 painter->setBrush(color);
268 painter->drawEllipse(paintRect);
269 painter->restore();
270 }
271 if (option.state & (QStyle::State_MouseOver | QStyle::State_Selected)) {
272 painter->save();
273 painter->setRenderHint(QPainter::Antialiasing);
274 painter->setPen(Qt::NoPen);
275 painter->setBrush(Qt::white);
276 QRect subRect(paintRect.top(), paintRect.left(), paintRect.width() / 2, paintRect.height() / 2);
277 subRect.moveCenter(paintRect.center());
278 painter->drawEllipse(subRect);
279 painter->restore();
280 }
281 break;
282 }
283 case KColorComboBox::RoundedRect: {
284 if (color.isValid()) {
285 painter->save();
286 painter->setRenderHint(QPainter::Antialiasing);
287 if (option.state & (QStyle::State_MouseOver | QStyle::State_Selected)) {
288 QPen pen;
289 pen.setWidth(2);
290 pen.setBrush(Qt::white);
291 painter->setBrush(color);
292 painter->setPen(pen);
293 } else {
294 painter->setBrush(color);
295 painter->setPen(Qt::NoPen);
296 }
297 int radius = ThemeController::getRadiusFromDT("kradius-min");
298 if (radius == -1)
299 radius = 4;
300 painter->drawRoundedRect(paintRect, radius, radius);
301 painter->restore();
302 }
303 break;
304 }
305 default:
306 break;
307 }
308 }
309
310 QSize KColorComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
311 {
312 return m_combo->popupItemSzie();
313 }
314
315 QSize KComboStyle::sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const
316 {
317 if (type == QStyle::CT_ComboBox)
318 return g_size;
319
320 return QProxyStyle::sizeFromContents(type, option, contentsSize, widget);
321 }
322
323 }
324 #include "kcolorcombobox.moc"
325 #include "moc_kcolorcombobox.cpp"