Debian Codegraph @

kysdk-qtwidgets/src/kcrumb.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 "kcrumb.h"
24 #include "kborderlessbutton.h"
25 #include "kcustomtabbar.h"
26 #include "themeController.h"
27 #include <QApplication>
28 #include <QDebug>
29 #include <QFontMetrics>
30 #include <QPainter>
31 #include <QProxyStyle>
32 #include <QStyle>
33 #include <QStyleOptionTab>
34
35 namespace kdk
36 {
37
38 static int spacing = 4;
39
40 class Q_DECL_HIDDEN CrumbStyle : public QProxyStyle
41 {
42 Q_OBJECT
43
44 public:
45 CrumbStyle(QStyle *style = nullptr);
46
47 QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const override;
48 };
49
50 class Q_DECL_HIDDEN KCrumbPrivate : public QObject, public ThemeController
51 {
52 Q_OBJECT
53 Q_DECLARE_PUBLIC(KCrumb)
54
55 public:
56 KCrumbPrivate(KCrumb *parent);
57
58 private:
59 KCrumb *q_ptr;
60 QIcon m_icon;
61 bool m_isFlat;
62 QStringList m_stringList;
63 };
64
65 KCrumb::KCrumb(QWidget *parent)
66 : KCustomTabBar(parent)
67 , d_ptr(new KCrumbPrivate(this))
68 {
69 Q_D(KCrumb);
70 CrumbStyle *style = new CrumbStyle();
71 setStyle(style);
72
73 connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){
74 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
75 {
76 d->initThemeStyle();
77 update();
78 }
79 });
80 }
81
82 int KCrumb::addTab(const QString &text)
83 {
84 Q_D(KCrumb);
85 d->m_stringList.append(text);
86 return KCustomTabBar::addTab(text);
87 }
88
89 int KCrumb::addTab(const QIcon &icon, const QString &text)
90 {
91 Q_D(KCrumb);
92 d->m_stringList.append(text);
93 return KCustomTabBar::addTab(icon, text);
94 }
95
96 int KCrumb::insertTab(int index, const QString &text)
97 {
98 Q_D(KCrumb);
99 d->m_stringList.insert(index, text);
100 return KCustomTabBar::insertTab(index, text);
101 }
102
103 int KCrumb::insertTab(int index, const QIcon &icon, const QString &text)
104 {
105 Q_D(KCrumb);
106 d->m_stringList.insert(index, text);
107 return insertTab(index, icon, text);
108 }
109
110 void KCrumb::removeTab(int index)
111 {
112 Q_D(KCrumb);
113 d->m_stringList.removeAt(index);
114 KCustomTabBar::removeTab(index);
115 }
116
117 void KCrumb::moveTab(int from, int to)
118 {
119 Q_D(KCrumb);
120 d->m_stringList.move(from, to);
121 KCustomTabBar::moveTab(from, to);
122 }
123
124 void KCrumb::setIcon(const QIcon &icon)
125 {
126 Q_D(KCrumb);
127 d->m_icon = icon;
128 update();
129 }
130
131 QIcon KCrumb::icon() const
132 {
133 Q_D(const KCrumb);
134 return d->m_icon;
135 }
136
137 bool KCrumb::isFlat() const
138 {
139 Q_D(const KCrumb);
140 return d->m_isFlat;
141 }
142
143 void KCrumb::setFlat(bool flat)
144 {
145 Q_D(KCrumb);
146 d->m_isFlat = flat;
147 update();
148 }
149
150 void KCrumb::leftScroll()
151 {
152 Q_D(KCrumb);
153 leftScrollTabs();
154 }
155
156 void KCrumb::rightScroll()
157 {
158 Q_D(KCrumb);
159 rightScrollTabs();
160 }
161
162 QSize KCrumb::tabSizeHint(int index) const
163 {
164 Q_D(const KCrumb);
165 QSize size = KCustomTabBar::tabSizeHint(index);
166 QFont font(QApplication::font());
167
168 QFontMetrics fm(font);
169 int width = fm.horizontalAdvance(tabText(index));
170 size.setWidth(width + 40);
171 if (index == 0 && !d->m_icon.isNull())
172 size.setWidth(size.width() + 50);
173 return size;
174 }
175
176 void KCrumb::paintEvent(QPaintEvent *event)
177 {
178 Q_D(const KCrumb);
179 QPainter p(this);
180 p.setRenderHint(QPainter::Antialiasing);
181 p.setRenderHint(QPainter::TextAntialiasing);
182 p.setRenderHint(QPainter::SmoothPixmapTransform);
183 QFontMetrics fm = p.fontMetrics();
184
185 QColor highLightColor = ThemeController::getCustomColorFromDT("highlight-active");
186 QColor baseColor;
187 QColor focusColor;
188 if (ThemeController::themeMode() == ThemeFlag::DarkTheme) {
189 if (d->m_isFlat)
190 baseColor = "#D9D9D9";
191 else
192 baseColor = "#47474A";
193 focusColor = "#D9D9D9";
194 } else {
195 if (d->m_isFlat)
196 baseColor = "#262626";
197 else
198 baseColor = "#B3B3B3";
199 focusColor = "#262626";
200 }
201
202 int width = 0;
203 for (int i = 0; i < count(); i++) {
204 width += fm.horizontalAdvance(d->m_stringList.at(i)) + 40;
205 }
206 if (!d->m_icon.isNull())
207 width += 50;
208 int useWidth = fm.horizontalAdvance(d->m_stringList.at(count() - 1)) + 40;
209
210 for (int i = 0; i < count(); i++) {
211 QRect rc = tabRect(i);
212 QStyleOptionTab option;
213 initStyleOption(&option, i);
214 p.setPen(Qt::NoPen);
215 p.drawRect(rc);
216 if (QStyle::State_MouseOver & option.state)
217 p.setPen(highLightColor);
218 else if (QStyle::State_Selected & option.state)
219 p.setPen(focusColor);
220 else
221 p.setPen(baseColor);
222 p.setBrush(Qt::NoBrush);
223
224 if (rect().width() >= width) {
225 if (i == 0 && !d->m_icon.isNull()) {
226 p.drawPixmap(rc.left() + 10, (rc.height() - 24) / 2, 24, 24,
227 d->m_icon.pixmap(24, 24));
228 p.drawText(rc.adjusted(30, 0, -24, 0), Qt::AlignCenter, d->m_stringList.at(i));
229 } else
230 p.drawText(rc.adjusted(0, 0, -24, 0), Qt::AlignCenter, d->m_stringList.at(i));
231 if (i != count() - 1)
232 p.drawPixmap(rc.right() - 24, (rc.height() - 16) / 2, 16, 16,
233 ThemeController::drawColoredPixmap(QIcon::fromTheme("ukui-end-symbolic").pixmap(16, 16), baseColor));
234 } else {
235 if (i == 0) {
236 if (!d->m_icon.isNull()) {
237 p.drawPixmap(rc.left() + 10, (rc.height() - 24) / 2, 24, 24,
238 d->m_icon.pixmap(24, 24));
239 p.drawText(rc.adjusted(30, 0, -24, 0), Qt::AlignCenter, d->m_stringList.at(i));
240 useWidth += fm.horizontalAdvance(d->m_stringList.at(0)) + 90;
241 ;
242 } else {
243 p.drawText(rc.adjusted(0, 0, -24, 0), Qt::AlignCenter, d->m_stringList.at(i));
244 useWidth += fm.horizontalAdvance(d->m_stringList.at(0)) + 40;
245 }
246 } else if (i == count() - 1) {
247 p.drawText(rc.adjusted(0, 0, -24, 0), Qt::AlignCenter, d->m_stringList.at(i));
248 } else {
249 int usableWidth = 0;
250 for (int j = i; j < count() - 1; j++) {
251 usableWidth += fm.horizontalAdvance(d->m_stringList.at(j)) + 40;
252 }
253
254 if ((rect().width() - useWidth) <= usableWidth) {
255 setTabContent(i, "...");
256 setTabToolTip(i, d->m_stringList.at(i));
257 p.drawText(rc.adjusted(0, 0, -24, 0), Qt::AlignCenter, "...");
258 useWidth += fm.horizontalAdvance("...") + 40;
259 } else // 如果可用长度 大于 需用长度 则正常显示
260 {
261
262 setTabContent(i, d->m_stringList.at(i));
263 p.drawText(rc.adjusted(0, 0, -24, 0), Qt::AlignCenter, d->m_stringList.at(i));
264 useWidth += fm.horizontalAdvance(d->m_stringList.at(i)) + 40;
265 if (!tabToolTip(i).isEmpty())
266 setTabToolTip(i, "");
267 }
268 }
269 if (i != count() - 1) {
270 p.drawPixmap(rc.right() - 24, (rc.height() - 16) / 2, 16, 16,
271 ThemeController::drawColoredPixmap(QIcon::fromTheme("ukui-end-symbolic").pixmap(16, 16), baseColor));
272 }
273 }
274 }
275 }
276
277 KCrumbPrivate::KCrumbPrivate(KCrumb *parent)
278 : q_ptr(parent)
279 , m_isFlat(true)
280 {
281 }
282
283 CrumbStyle::CrumbStyle(QStyle *style)
284 {
285 }
286
287 QRect CrumbStyle::subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const
288 {
289 switch (element) {
290 case QStyle::SE_TabBarScrollLeftButton:
291 return QRect();
292 break;
293 case QStyle::SE_TabBarScrollRightButton:
294 return QRect();
295 break;
296 default:
297 break;
298 }
299 return QProxyStyle::subElementRect(element, option, widget);
300 }
301
302 }
303
304 #include "kcrumb.moc"
305 #include "moc_kcrumb.cpp"