Debian Codegraph @

kysdk-qtwidgets/src/kbreadcrumb.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 "kbreadcrumb.h"
24 #include "themeController.h"
25 #include <QApplication>
26 #include <QDebug>
27 #include <QFontMetrics>
28 #include <QPainter>
29 #include <QStyle>
30 #include <QStyleOptionTab>
31
32 namespace kdk
33 {
34 class KBreadCrumbPrivate : public QObject, public ThemeController
35 {
36 Q_OBJECT
37 Q_DECLARE_PUBLIC(KBreadCrumb)
38
39 public:
40 KBreadCrumbPrivate(KBreadCrumb *parent);
41
42 private:
43 KBreadCrumb *q_ptr;
44 QIcon m_icon;
45 bool m_isFlat;
46 };
47
48 KBreadCrumb::KBreadCrumb(QWidget *parent)
49 : QTabBar(parent)
50 , d_ptr(new KBreadCrumbPrivate(this))
51 {
52 }
53
54 void KBreadCrumb::setIcon(const QIcon &icon)
55 {
56 Q_D(KBreadCrumb);
57 d->m_icon = icon;
58 update();
59 }
60
61 QIcon KBreadCrumb::icon() const
62 {
63 Q_D(const KBreadCrumb);
64 return d->m_icon;
65 }
66
67 bool KBreadCrumb::isFlat() const
68 {
69 Q_D(const KBreadCrumb);
70 return d->m_isFlat;
71 }
72
73 void KBreadCrumb::setFlat(bool flat)
74 {
75 Q_D(KBreadCrumb);
76 d->m_isFlat = flat;
77 update();
78 }
79
80 QSize KBreadCrumb::tabSizeHint(int index) const
81 {
82 Q_D(const KBreadCrumb);
83 QSize size = QTabBar::tabSizeHint(index);
84 QFont font(QApplication::font());
85 QFontMetrics fm(font);
86 int width = fm.horizontalAdvance(tabText(index));
87 size.setWidth(width + 40);
88 if (index == 0 && !d->m_icon.isNull())
89 size.setWidth(size.width() + 50);
90 return size;
91 }
92
93 void KBreadCrumb::paintEvent(QPaintEvent *event)
94 {
95 Q_D(const KBreadCrumb);
96 QColor highLightColor = ThemeController::getCustomColorFromDT("highlight-active");
97 QColor baseColor;
98 QColor focusColor;
99 if (ThemeController::themeMode() == ThemeFlag::DarkTheme) {
100 if (d->m_isFlat)
101 baseColor = "#D9D9D9";
102 else
103 baseColor = "#47474A";
104 focusColor = "#D9D9D9";
105 } else {
106 if (d->m_isFlat)
107 baseColor = "#262626";
108 else
109 baseColor = "#B3B3B3";
110 focusColor = "#262626";
111 }
112 QPainter p(this);
113 p.setRenderHint(QPainter::Antialiasing);
114 p.setRenderHint(QPainter::TextAntialiasing);
115 p.setRenderHint(QPainter::SmoothPixmapTransform);
116 for (int i = 0; i < count(); i++) {
117 QRect rc = tabRect(i);
118 QStyleOptionTab option;
119 initStyleOption(&option, i);
120 p.setPen(Qt::NoPen);
121 p.drawRect(rc);
122 if (QStyle::State_MouseOver & option.state)
123 p.setPen(highLightColor);
124 else if (QStyle::State_Selected & option.state)
125 p.setPen(focusColor);
126 else
127 p.setPen(baseColor);
128 p.setBrush(Qt::NoBrush);
129
130 if (i == 0 && !d->m_icon.isNull()) {
131 p.drawPixmap(rc.left() + 10, (rc.height() - 24) / 2, 24, 24,
132 d->m_icon.pixmap(24, 24));
133 p.drawText(rc.adjusted(30, 0, -24, 0), Qt::AlignCenter, tabText(i));
134 } else
135 p.drawText(rc.adjusted(0, 0, -24, 0), Qt::AlignCenter, tabText(i));
136 if (i != count() - 1)
137 p.drawPixmap(rc.right() - 24, (rc.height() - 16) / 2, 16, 16,
138 ThemeController::drawColoredPixmap(QIcon::fromTheme("ukui-end-symbolic").pixmap(16, 16), baseColor));
139 }
140 }
141
142 KBreadCrumbPrivate::KBreadCrumbPrivate(KBreadCrumb *parent)
143 : q_ptr(parent)
144 , m_isFlat(true)
145 {
146 }
147
148 }
149
150 #include "kbreadcrumb.moc"
151 #include "moc_kbreadcrumb.cpp"