Debian Codegraph @

kysdk-qtwidgets/src/kbackgroundgroup.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 "kbackgroundgroup.h"
24 #include "klineframe.h"
25 #include "themeController.h"
26 #include <QEvent>
27 #include <QPainter>
28 #include <QPainterPath>
29 #include <QVBoxLayout>
30
31 namespace kdk
32 {
33
34 class KBackgroundGroupPrivate : public QObject, public ThemeController
35 {
36 Q_OBJECT
37 Q_DECLARE_PUBLIC(KBackgroundGroup)
38 public:
39 enum WidgetPosition {
40 Beginning,
41 Middle,
42 End
43 };
44 KBackgroundGroupPrivate(KBackgroundGroup *parent);
45 void updateLayout();
46 void updateVisibleLayout();
47 private:
48 KBackgroundGroup *q_ptr;
49 QVBoxLayout *m_pmainWidgetLayout;
50 QList<QWidget *> m_pwidgetList;
51 QList<QWidget *> m_pwidgetStateList;
52 QList<QWidget*> m_pwidgetVisibleList;
53 QList<QFrame*> m_pFrameList;
54 QPalette::ColorRole m_brushColorRole;
55 QColor m_backgroundColor;
56 WidgetPosition m_widgetPosition;
57 QRect m_rect;
58 int m_rectLocal;
59 int m_radius;
60 bool m_borderVisble;
61 };
62
63 KBackgroundGroup::KBackgroundGroup(QWidget *parent)
64 : QFrame(parent)
65 , d_ptr(new KBackgroundGroupPrivate(this))
66 {
67 Q_D(KBackgroundGroup);
68 setFrameShape(QFrame::Box);
69 setFrameShadow(QFrame::Plain);
70 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
71 connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){
72 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
73 {
74 d->initThemeStyle();
75 d->m_backgroundColor = palette().color(d->m_brushColorRole);
76 }
77 });
78 }
79
80 void KBackgroundGroup::addWidget(QWidget *widget)
81 {
82 Q_D(KBackgroundGroup);
83 widget->installEventFilter(this);
84 if (widget->maximumHeight() != widget->minimumHeight())
85 widget->setFixedHeight(60);
86 if (!d->m_pwidgetList.contains(widget))
87 d->m_pwidgetList.append(widget);
88 else
89 return;
90 d->updateLayout();
91 }
92
93 void KBackgroundGroup::addWidgetList(QList<QWidget *> list)
94 {
95 Q_D(KBackgroundGroup);
96 for (int i = 0; i < list.count(); i++) {
97 auto widget = list.at(i);
98 widget->installEventFilter(this);
99 if (widget->maximumHeight() != widget->minimumHeight())
100 widget->setFixedHeight(60);
101 if (!d->m_pwidgetList.contains(list.at(i)))
102 d->m_pwidgetList.append(list.at(i));
103 else
104 continue;
105 }
106 d->updateLayout();
107 }
108
109 void KBackgroundGroup::removeWidgetAt(int i)
110 {
111 Q_D(KBackgroundGroup);
112 if (d->m_pwidgetList.contains(d->m_pwidgetList.at(i)))
113 d->m_pwidgetList.removeAt(i);
114 else
115 return;
116 d->updateLayout();
117 }
118
119 void KBackgroundGroup::removeWidget(QWidget *widget)
120 {
121 Q_D(KBackgroundGroup);
122 if (d->m_pwidgetList.contains(widget))
123 d->m_pwidgetList.removeOne(widget);
124 else
125 return;
126 d->updateLayout();
127 }
128
129 void KBackgroundGroup::removeWidgetList(QList<QWidget *> list)
130 {
131 Q_D(KBackgroundGroup);
132 for (int i = 0; i < list.count(); i++) {
133 if (d->m_pwidgetList.contains(list.at(i)))
134 d->m_pwidgetList.removeOne(list.at(i));
135 else
136 continue;
137 }
138 d->updateLayout();
139 }
140
141 void KBackgroundGroup::insertWidgetAt(int index, QWidget *widget)
142 {
143 Q_D(KBackgroundGroup);
144 widget->installEventFilter(this);
145 if(widget->maximumHeight() != widget->minimumHeight())
146 widget->setFixedHeight(60);
147 if (!d->m_pwidgetList.contains(widget))
148 d->m_pwidgetList.insert(index, widget);
149 else
150 return;
151 d->updateLayout();
152 }
153
154 void KBackgroundGroup::insertWidgetList(int index, QList<QWidget *> list)
155 {
156 Q_D(KBackgroundGroup);
157 for (int i = 0; i < list.count(); i++) {
158 auto widget = list.at(i);
159 widget->installEventFilter(this);
160 if (widget->maximumHeight() != widget->minimumHeight())
161 widget->setFixedHeight(60);
162 if (!d->m_pwidgetList.contains(list.at(i)))
163 d->m_pwidgetList.insert(index++, list.at(i));
164 else
165 continue;
166 }
167 d->updateLayout();
168 }
169
170 void KBackgroundGroup::setBorderRadius(int radius)
171 {
172 Q_D(KBackgroundGroup);
173 d->m_radius = radius;
174 }
175
176 int KBackgroundGroup::borderRadius()
177 {
178 Q_D(KBackgroundGroup);
179 return d->m_radius;
180 }
181
182 void KBackgroundGroup::setBackgroundRole(QPalette::ColorRole role)
183 {
184 Q_D(KBackgroundGroup);
185 d->m_brushColorRole = role;
186 d->m_backgroundColor = palette().color(role);
187 }
188
189 QPalette::ColorRole KBackgroundGroup::backgroundRole() const
190 {
191 Q_D(const KBackgroundGroup);
192 return d->m_brushColorRole;
193 }
194
195 void KBackgroundGroup::setStateEnable(QWidget *widget, bool flag)
196 {
197 Q_D(KBackgroundGroup);
198 if (!d->m_pwidgetStateList.contains(widget) && flag) {
199 d->m_pwidgetStateList.append(widget);
200 widget->installEventFilter(this);
201 widget->setAttribute(Qt::WA_TranslucentBackground);
202 } else if (d->m_pwidgetStateList.contains(widget) && !flag) {
203 d->m_pwidgetStateList.removeOne(widget);
204 }
205 }
206
207 QList<QWidget *> KBackgroundGroup::widgetList()
208 {
209 Q_D(KBackgroundGroup);
210 return d->m_pwidgetList;
211 }
212
213 void KBackgroundGroup::setBorderVisible(bool flag)
214 {
215 Q_D(KBackgroundGroup);
216 d->m_borderVisble = flag;
217 }
218
219 void KBackgroundGroup::paintEvent(QPaintEvent *event)
220 {
221 Q_D(KBackgroundGroup);
222 // 绘制backgroundgroup
223 QRect rect =this->rect().adjusted(1,1,0,0);
224
225 QPainter painter(this);
226 painter.setRenderHint(QPainter::Antialiasing);
227 QPen pen;
228 pen.setWidth(1);
229 if(d->m_borderVisble)
230 {
231 QColor borderColor;
232 if(ThemeController::themeMode() == LightTheme)
233 {
234 borderColor = ThemeController::mixColor(Qt::gray,Qt::white,0.1);
235 borderColor.setAlphaF(0.2);
236 }
237 else
238 {
239 borderColor =ThemeController::mixColor(Qt::gray,Qt::white,0.1);
240 borderColor.setAlphaF(0.3);
241 }
242 pen.setColor(borderColor);
243 }
244 else
245 pen.setColor(Qt::transparent);
246 painter.setPen(pen);
247 painter.setBrush(palette().color(d->m_brushColorRole));
248
249 QPainterPath path;
250 path.moveTo(rect.topLeft() + QPointF(d->m_radius, 0));
251 path.quadTo(rect.topLeft(), rect.topLeft() + QPointF(0, d->m_radius));
252 path.lineTo(rect.bottomLeft() - QPointF(0, d->m_radius));
253 path.quadTo(rect.bottomLeft(), rect.bottomLeft() + QPointF(d->m_radius, 0));
254 path.lineTo(rect.bottomRight() - QPointF(d->m_radius, 0));
255 path.quadTo(rect.bottomRight(), rect.bottomRight() - QPointF(0, d->m_radius));
256 path.lineTo(rect.topRight() + QPointF(0, d->m_radius));
257 path.quadTo(rect.topRight(), rect.topRight() - QPointF(d->m_radius, 0));
258 path.lineTo(rect.topLeft() + QPointF(d->m_radius, 0));
259 painter.drawPath(path);
260
261 // 三态事件响应颜色
262 painter.save();
263 painter.setBrush(d->m_backgroundColor);
264 painter.setRenderHint(QPainter::Antialiasing);
265 painter.setPen(Qt::transparent);
266
267 // 确定三态区域
268 int index, offset = 0;
269 if (d->m_pwidgetStateList.count() > 0) {
270 for (index = 0; index < d->m_pwidgetList.count(); index++)
271 {
272 auto widget = d->m_pwidgetList.at(index);
273 if( widget != d->m_pwidgetStateList.at(d->m_rectLocal))
274 offset = /*offset +*/ d->m_pwidgetList.at(index)->height()*( d->m_pwidgetVisibleList.indexOf(d->m_pwidgetStateList.at(d->m_rectLocal)));
275 else
276 break;
277 }
278 d->m_rect = d->m_rect.translated(0, offset + d->m_pwidgetList.indexOf(d->m_pwidgetStateList.at(d->m_rectLocal)) * 1);
279 if(d->m_borderVisble)
280 d->m_rect = d->m_rect.adjusted(2,1,0,0);
281 else
282 d->m_rect = d->m_rect.adjusted(0,-1,1,0);
283 }
284
285 // 三态区域颜色调整
286 QPainterPath path1;
287 if (d->m_pwidgetList.count() == 1 && d->m_pwidgetStateList.count() == 1)
288 {
289 painter.drawRoundedRect(d->m_rect, d->m_radius, d->m_radius);
290 }
291 else if (d->m_pwidgetList.count() > 1 && d->m_pwidgetStateList.count() > 1)
292 {
293
294 if(d->m_pwidgetVisibleList.count() == 1)
295 {
296 painter.drawRoundedRect(d->m_rect,d->m_radius,d->m_radius);
297 }
298 else
299 {
300 switch (d->m_widgetPosition)
301 {
302 case KBackgroundGroupPrivate::Beginning:
303 path1.moveTo(d->m_rect.topLeft() + QPointF(0, d->m_radius));
304 path1.lineTo(d->m_rect.bottomLeft());
305 path1.lineTo(d->m_rect.bottomRight());
306 path1.lineTo(d->m_rect.topRight() + QPointF(0, d->m_radius));
307 path1.quadTo(d->m_rect.topRight(), d->m_rect.topRight() - QPointF(d->m_radius, 0));
308 path1.lineTo(d->m_rect.topLeft() + QPointF(d->m_radius, 0));
309 path1.quadTo(d->m_rect.topLeft(), d->m_rect.topLeft() + QPointF(0, d->m_radius));
310 painter.drawPath(path1);
311 break;
312 case KBackgroundGroupPrivate::Middle:
313 painter.drawRoundedRect(d->m_rect, 0, 0);
314 break;
315 case KBackgroundGroupPrivate::End:
316 d->m_rect = d->m_rect.adjusted(1,-1,2,2);
317 path1.moveTo(d->m_rect.topLeft());
318 path1.lineTo(d->m_rect.bottomLeft() - QPointF(0, d->m_radius));
319 path1.quadTo(d->m_rect.bottomLeft(), d->m_rect.bottomLeft() + QPointF(d->m_radius, 0));
320 path1.lineTo(d->m_rect.bottomRight() - QPointF(d->m_radius, 0));
321 path1.quadTo(d->m_rect.bottomRight(), d->m_rect.bottomRight() - QPointF(0, d->m_radius));
322 path1.lineTo(d->m_rect.topRight());
323 path1.lineTo(d->m_rect.topLeft());
324 painter.drawPath(path1);
325 break;
326 default:
327 break;
328 }
329 }
330 }
331 painter.restore();
332 }
333
334 bool KBackgroundGroup::eventFilter(QObject *watched, QEvent *event)
335 {
336 Q_D(KBackgroundGroup);
337 auto widget = static_cast<QWidget *>(watched);
338
339 if (d->m_pwidgetStateList.contains(widget))
340 {
341 // 事件过滤器判断事件设置颜色
342 switch (event->type())
343 {
344 case QEvent::Enter:
345 d->m_rect = widget->rect();
346 d->m_backgroundColor = ThemeController::getCustomColorFromDT("kgray-3");//kcontain-hover 不可用看配置文件用kgray-3替代
347 if (d->m_pwidgetVisibleList.at(0) == widget)
348 d->m_widgetPosition = KBackgroundGroupPrivate::Beginning;
349 else if(d->m_pwidgetVisibleList.at(d->m_pwidgetVisibleList.count()-1) == widget)
350 d->m_widgetPosition = KBackgroundGroupPrivate::End;
351 else
352 d->m_widgetPosition = KBackgroundGroupPrivate::Middle;
353 d->m_rectLocal = d->m_pwidgetStateList.indexOf(widget);
354 update();
355 break;
356 case QEvent::MouseButtonPress:
357 d->m_rect = widget->rect();
358 d->m_backgroundColor = ThemeController::getCustomColorFromDT("kgray-4");
359 if (d->m_pwidgetVisibleList.at(0) == widget)
360 d->m_widgetPosition = KBackgroundGroupPrivate::Beginning;
361 else if (d->m_pwidgetVisibleList.at(d->m_pwidgetVisibleList.count() - 1) == widget)
362 d->m_widgetPosition = KBackgroundGroupPrivate::End;
363 else
364 d->m_widgetPosition = KBackgroundGroupPrivate::Middle;
365 d->m_rectLocal = d->m_pwidgetStateList.indexOf(widget);
366 update();
367 break;
368 case QEvent::MouseButtonRelease:
369 {
370 d->m_rect = widget->rect();
371 d->m_backgroundColor = ThemeController::getCustomColorFromDT("kgray-3");
372 if (d->m_pwidgetVisibleList.at(0) == widget)
373 d->m_widgetPosition = KBackgroundGroupPrivate::Beginning;
374 else if (d->m_pwidgetVisibleList.at(d->m_pwidgetVisibleList.count() - 1) == widget)
375 d->m_widgetPosition = KBackgroundGroupPrivate::End;
376 else
377 d->m_widgetPosition = KBackgroundGroupPrivate::Middle;
378 Q_EMIT clicked(widget);
379 d->m_rectLocal = d->m_pwidgetStateList.indexOf(widget);
380 update();
381 break;
382 }
383 case QEvent::Leave:
384 d->m_rect = widget->rect();
385 d->m_backgroundColor = palette().color(d->m_brushColorRole);
386 if (d->m_pwidgetVisibleList.at(0) == widget)
387 d->m_widgetPosition = KBackgroundGroupPrivate::Beginning;
388 else if (d->m_pwidgetVisibleList.at(d->m_pwidgetVisibleList.count() - 1) == widget)
389 d->m_widgetPosition = KBackgroundGroupPrivate::End;
390 else
391 d->m_widgetPosition = KBackgroundGroupPrivate::Middle;
392 d->m_rectLocal = d->m_pwidgetStateList.indexOf(widget);
393 update();
394 break;
395 case QEvent::Paint:
396 d->m_rect = widget->rect();
397 break;
398 case QEvent::Show:
399 d->updateVisibleLayout();
400 break;
401 case QEvent::Hide:
402 d->updateVisibleLayout();
403 break;
404 default:
405 break;
406 }
407 }
408 else
409 {
410 switch (event->type())
411 {
412 case QEvent::Show:
413 d->updateVisibleLayout();
414 break;
415 case QEvent::Hide:
416 d->updateVisibleLayout();
417 break;
418 default:
419 break;
420 }
421 }
422 return QObject::eventFilter(watched,event);
423 }
424
425 KBackgroundGroupPrivate::KBackgroundGroupPrivate(KBackgroundGroup *parent)
426 :q_ptr(parent)
427 ,m_widgetPosition(KBackgroundGroupPrivate::Beginning),m_rectLocal(0)
428 ,m_radius(12)
429 ,m_brushColorRole(QPalette::Base)
430 ,m_backgroundColor(parent->palette().color(m_brushColorRole))
431 ,m_borderVisble(false)
432 {
433 Q_Q(KBackgroundGroup);
434 m_pmainWidgetLayout = new QVBoxLayout(q);
435 m_pmainWidgetLayout->setContentsMargins(0, 0, 0, 0);
436 m_pmainWidgetLayout->setSpacing(0);
437
438 int radius = ThemeController::getRadiusFromDT("kradius-window");
439 if (radius == -1)
440 m_radius = 12;
441 else
442 m_radius = radius;
443 // q->setFocus(Qt::MouseFocusReason);
444 connect(m_gsetting, &QGSettings::changed, this, [=]() {
445 initThemeStyle();
446 });
447 }
448
449 void KBackgroundGroupPrivate::updateLayout()
450 {
451 Q_Q(KBackgroundGroup);
452 // 清空
453 m_pwidgetVisibleList = m_pwidgetList;
454 QLayoutItem *child;
455 while ((child = m_pmainWidgetLayout->takeAt(0)) != 0) {
456 // 删除Stretch(弹簧)等布局
457 if (child->spacerItem()) {
458 m_pmainWidgetLayout->removeItem(child);
459 continue;
460 }
461 // 删除布局
462 m_pmainWidgetLayout->removeWidget(child->widget());
463 child->widget()->setParent(nullptr);
464 delete child;
465 child = nullptr;
466 }
467 m_pFrameList.clear();
468
469 // 添加布局
470 if (m_pwidgetList.count() < 1)
471 return;
472 else if (m_pwidgetList.count() == 1) {
473 m_pmainWidgetLayout->addWidget(m_pwidgetList.at(0));
474 } else {
475 for (int i = 0; i < m_pwidgetList.count(); ++i) {
476 m_pmainWidgetLayout->addWidget(m_pwidgetList.at(i));
477 if (i != m_pwidgetList.count() - 1) {
478 KHLineFrame *frame = new KHLineFrame();
479 m_pFrameList.append(frame);
480 m_pmainWidgetLayout->addWidget(frame);
481 }
482 }
483 }
484 }
485
486 void KBackgroundGroupPrivate::updateVisibleLayout()
487 {
488 QList<int> visibleIndices;
489 m_pwidgetVisibleList.clear();
490 for(auto widget : m_pwidgetList)
491 {
492 if(widget->isVisible())
493 m_pwidgetVisibleList.append(widget);
494 }
495
496 for (int i = 0; i < m_pwidgetList.size(); ++i)
497 {
498 if (m_pwidgetList[i]->isVisible())
499 {
500 visibleIndices.append(i);
501 }
502 }
503
504 if (visibleIndices.size() <= 1)
505 {
506 for (auto frame : m_pFrameList)
507 {
508 frame->setVisible(false);
509 }
510 return;
511 }
512
513 for (auto frame : m_pFrameList)
514 {
515 frame->setVisible(false);
516 }
517
518 for (int i = 0; i < visibleIndices.size() - 1; ++i) {
519 int index = visibleIndices[i];
520
521 if (index >= 0 && index < m_pFrameList.size())
522 {
523 m_pFrameList[index]->setVisible(true);
524 }
525 }
526 }
527
528 }
529
530 #include "kbackgroundgroup.moc"
531 #include "moc_kbackgroundgroup.cpp"