Debian Codegraph @

kysdk-qtwidgets/src/kdirtabbar.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2024, 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: Yaxian Liu <[email protected]>
20 *
21 */
22
23 #include "kdirtabbar.h"
24 #include "themeController.h"
25 #include <QBoxLayout>
26 #include <QDebug>
27 #include <QDirIterator>
28 #include <QFileInfo>
29 #include <QFontMetrics>
30 #include <QList>
31 #include <QMenu>
32 #include <QPainter>
33 #include <QSpacerItem>
34 #include <QString>
35 #include <QStringList>
36 #include <QStyle>
37 #include <QStyleOptionToolButton>
38 #include <QStylePainter>
39 #include <QToolButton>
40
41 namespace kdk
42 {
43
44 class CustomButton : public QToolButton
45 {
46 Q_OBJECT
47 public:
48 CustomButton(QWidget *parent = nullptr);
49 QIcon setIconColor(QIcon icon, QColor color)
50 {
51 QPixmap pixmap = icon.pixmap(QSize(16, 16));
52 QPainter painter(&pixmap);
53 painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
54 painter.fillRect(pixmap.rect(), color);
55 QIcon colorIcon = QIcon(pixmap);
56 return colorIcon;
57 }
58
59 protected:
60 void paintEvent(QPaintEvent *event);
61 };
62
63 class KDirTabBarPrivate : public QObject, public ThemeController
64 {
65 Q_OBJECT
66 Q_DECLARE_PUBLIC(KDirTabBar)
67
68 public:
69 KDirTabBarPrivate(KDirTabBar *parent);
70
71 void clearButtons();
72 void addButton(const QString &uri, QString buttonName, bool setMenu = true);
73
74 void doLayout();
75
76 void changeTheme();
77
78 private:
79 KDirTabBar *q_ptr;
80 int m_visibleDepth;
81 int m_textLength;
82 QString m_currentPath;
83 QString m_rootPath;
84 QHBoxLayout *buttonLayout;
85 QHBoxLayout *m_layout;
86 // QSpacerItem *spacer;
87
88 QList<QToolButton *> m_buttonList;
89 QList<QFileInfo *> m_button_info;
90 };
91
92 KDirTabBar::KDirTabBar(QWidget *parent)
93 : QWidget(parent)
94 , d_ptr(new KDirTabBarPrivate(this))
95 {
96 Q_D(KDirTabBar);
97 d->m_visibleDepth = 5;
98 d->m_textLength = 5;
99 // d->spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
100
101 d->buttonLayout = new QHBoxLayout;
102 d->buttonLayout->setContentsMargins(0, 0, 0, 0);
103 d->buttonLayout->setSpacing(0);
104 d->m_layout = new QHBoxLayout(this);
105 d->m_layout->setContentsMargins(0, 0, 0, 0);
106 d->m_layout->setSpacing(0);
107 d->m_layout->addLayout(d->buttonLayout);
108 // d->m_layout->addItem(d->spacer);
109 d->m_layout->addStretch();
110 setLayout(d->m_layout);
111 // setFocusPolicy(Qt::ClickFocus);
112 setObjectName("KDirTabBar");
113
114 connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){
115 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
116 {
117 d->changeTheme();
118 }
119 });
120 }
121
122 KDirTabBar::~KDirTabBar()
123 {
124 }
125 void KDirTabBar::setCurrentPath(const QString Dirpath)
126 {
127 Q_D(KDirTabBar);
128 QList<QFileInfo *> lastInfo;
129 for (auto info : d->m_button_info) {
130 QFileInfo *fileinfo = new QFileInfo();
131 fileinfo = info;
132 lastInfo.append(fileinfo);
133 }
134
135 d->m_currentPath = Dirpath;
136 d->m_button_info.clear();
137 auto tmpUri = Dirpath;
138 QFileInfo *file = new QFileInfo(tmpUri);
139 while (tmpUri != "/" && tmpUri != "") {
140 d->m_button_info.prepend(file);
141 tmpUri = file->absolutePath();
142 file = new QFileInfo(tmpUri);
143 }
144 if (lastInfo.isEmpty()) {
145 d->clearButtons();
146 for (auto info : d->m_button_info) {
147 d->addButton(info->absoluteFilePath(), info->fileName(), true);
148 }
149 } else {
150 while (d->m_button_info.size() < d->m_buttonList.size()) // 新的比旧的列表短时,去掉多余的Qlist项
151 {
152 d->m_buttonList.last()->deleteLater();
153 d->m_buttonList.removeLast();
154 }
155 for (int i = d->m_buttonList.size() - 1; i >= 0; i--) {
156 if (lastInfo[i]->absoluteFilePath() != d->m_button_info[i]->absoluteFilePath()) // 对比新旧列表中的每一项,如果不相同则remove按钮列表中的元素
157 {
158 d->m_buttonList[i]->hide();
159 d->m_buttonList[i]->deleteLater();
160 d->m_buttonList.removeAt(i);
161 }
162 }
163 for (int i = d->m_buttonList.size(); i < d->m_button_info.size(); i++) {
164 d->addButton(d->m_button_info[i]->absoluteFilePath(), d->m_button_info[i]->fileName(), true);
165 }
166 }
167
168 for (auto info : lastInfo) {
169
170 delete info;
171 }
172 lastInfo.clear();
173
174 d->doLayout(); // 布局,最大显示个数
175 }
176
177 QString KDirTabBar::currentPath() const
178 {
179 Q_D(const KDirTabBar);
180 return d->m_currentPath;
181 }
182
183 void KDirTabBar::setVisibleDepth(const int depth)
184 {
185 Q_D(KDirTabBar);
186 d->m_visibleDepth = depth;
187 // dolayout
188 }
189
190 int KDirTabBar::visibleDepth()
191 {
192 Q_D(KDirTabBar);
193 return d->m_visibleDepth;
194 }
195
196 void KDirTabBar::setIcon(int index, const QIcon &icon)
197 {
198 Q_D(KDirTabBar);
199 if (index < d->m_buttonList.size()) {
200 d->m_buttonList[index - 1]->setIcon(icon);
201 d->doLayout();
202 }
203 }
204
205 QIcon KDirTabBar::icon(int index)
206 {
207 Q_D(KDirTabBar);
208 return d->m_buttonList[index - 1]->icon();
209 }
210
211 void KDirTabBar::setTextLength(int length)
212 {
213 Q_D(KDirTabBar);
214 d->m_textLength = length;
215 }
216
217 int KDirTabBar::textLength()
218 {
219 Q_D(KDirTabBar);
220 return d->m_textLength;
221 }
222
223 QList<QToolButton *> KDirTabBar::buttons()
224 {
225 Q_D(KDirTabBar);
226 return d->m_buttonList;
227 }
228
229 // void KDirTabBar::mousePressEvent(QMouseEvent *event)
230 //{
231 // QWidget::mousePressEvent(event);
232
233 //}
234
235 void KDirTabBarPrivate::addButton(const QString &uri, QString buttonName, bool setMenu)
236 {
237 Q_Q(KDirTabBar);
238 CustomButton *button = new CustomButton(q);
239 QString displayName = buttonName;
240 button->setFocusPolicy(Qt::FocusPolicy(button->focusPolicy() & ~Qt::TabFocus));
241 button->setAutoRaise(true);
242 button->setProperty("uri", uri);
243 button->setIconSize(QSize(16, 16));
244 button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
245 button->setPopupMode(CustomButton::MenuButtonPopup);
246
247 m_buttonList.append(static_cast<CustomButton *>(button));
248
249 buttonLayout->addWidget(button);
250 button->setIcon(QIcon::fromTheme("folder"));
251 QFontMetrics ftm(q->font());
252
253 if (displayName.length() > m_textLength) {
254 int charWidth = ftm.averageCharWidth();
255 displayName = ftm.elidedText(displayName, Qt::ElideRight, m_textLength * charWidth);
256 }
257
258 button->setText(displayName);
259
260 if (buttonName != displayName) {
261 button->setToolTip(buttonName);
262 }
263
264 connect(button, &CustomButton::clicked, [=]() {
265 q->setCurrentPath(uri);
266 emit q->tabBarClicked(uri);
267 });
268
269 if (setMenu) {
270 QList<QFileInfo> uris;
271 QDirIterator it(uri, QDir::Dirs | QDir::NoDotAndDotDot);
272 while (it.hasNext()) {
273 it.next();
274 QFileInfo fileInfo = it.fileInfo();
275 if (fileInfo.isDir()) {
276 uris << fileInfo;
277 }
278 }
279
280 QMenu *menu = new QMenu;
281 connect(button, &CustomButton::destroyed, menu, &QMenu::deleteLater);
282 const int WIDTH_EXTEND = 5;
283
284 connect(menu, &QMenu::aboutToShow, this, [=]() {
285 menu->setMinimumWidth(button->width() + WIDTH_EXTEND);
286 });
287 connect(menu, &QMenu::aboutToHide, this, [=]() {
288 });
289
290 if (uris.isEmpty())
291 button->setPopupMode(CustomButton::InstantPopup);
292 else {
293 QList<QAction *> actions;
294 for (auto uri : uris) {
295 QIcon icon;
296
297 if (uri.baseName() == "图片")
298 icon = QIcon::fromTheme("folder-pictures");
299 else if (uri.baseName() == "下载")
300 icon = QIcon::fromTheme("folder-download");
301 else if (uri.baseName() == "公共的")
302 icon = QIcon::fromTheme("folder-publicshare");
303 else if (uri.baseName() == "文档")
304 icon = QIcon::fromTheme("folder-documents");
305 else if (uri.baseName() == "桌面")
306 icon = QIcon::fromTheme("user-desktop");
307 else if (uri.baseName() == "模板")
308 icon = QIcon::fromTheme("folder-templates");
309 else if (uri.baseName() == "视频")
310 icon = QIcon::fromTheme("folder-videos");
311 else if (uri.baseName() == "音乐")
312 icon = QIcon::fromTheme("folder-music");
313 else
314 icon = QIcon::fromTheme("folder");
315
316 QAction *action = new QAction(icon, uri.fileName(), this);
317 actions << action;
318 connect(action, &QAction::triggered, [=]() {
319 q->setCurrentPath(uri.absoluteFilePath());
320 emit q->tabBarClicked(uri.absoluteFilePath());
321 });
322 }
323 menu->addActions(actions);
324 if (menu)
325 button->setMenu(menu);
326 }
327 }
328 }
329
330 void KDirTabBarPrivate::doLayout()
331 {
332 Q_Q(KDirTabBar);
333 int counter = 0;
334 if (m_buttonList.size() > m_visibleDepth) {
335 counter = m_buttonList.size() - m_visibleDepth;
336 }
337 for (int i = 0; i < m_buttonList.size(); i++) {
338 m_buttonList[i]->setVisible(false);
339 }
340 for (counter; counter < m_buttonList.size(); counter++) {
341 m_buttonList[counter]->setVisible(true);
342 m_buttonList[counter]->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
343 m_buttonList[counter]->adjustSize();
344 // buttonLayout->addWidget(m_buttonList[counter]);
345 }
346 }
347
348 void KDirTabBarPrivate::changeTheme()
349 {
350 Q_Q(KDirTabBar);
351 initThemeStyle();
352 }
353
354 void KDirTabBarPrivate::clearButtons()
355 {
356 Q_Q(KDirTabBar);
357 for (auto button : m_buttonList) {
358 button->hide();
359 button->deleteLater();
360 }
361 m_buttonList.clear();
362 }
363
364 KDirTabBarPrivate::KDirTabBarPrivate(KDirTabBar *parent)
365 : q_ptr(parent)
366 {
367 }
368
369 CustomButton::CustomButton(QWidget *parent)
370 {
371 }
372
373 void CustomButton::paintEvent(QPaintEvent *event)
374 {
375 QStyleOptionToolButton option;
376 initStyleOption(&option);
377 // QToolButton::paintEvent(event);
378
379 int ra = ThemeController::getRadiusFromDT("kradius-normal");
380 if (ra == -1)
381 ra = 6;
382 QColor textColor = ThemeController::getCustomColorFromDT("text-active");
383 QColor backgroundColor;
384 QPen pen;
385 QPainter painter(this);
386 painter.setRenderHint(QPainter::Antialiasing);
387
388 if (ThemeController::themeMode() == LightTheme) { // 浅色模式
389 if (!option.state.testFlag(QStyle::State_Enabled)) {
390 textColor = ThemeController::getCustomColorFromDT("text-disable");
391 } else {
392 // 三态
393 if (option.state.testFlag(QStyle::State_Sunken)) {
394 backgroundColor = ThemeController::getCustomColorFromDT("kgray-10");
395 // 绘制背景颜色
396 painter.setBrush(backgroundColor);
397 pen = QPen(backgroundColor);
398 painter.setPen(pen);
399 painter.drawRoundedRect(rect(), ra, ra);
400 } else if (option.state.testFlag(QStyle::State_MouseOver)) {
401 backgroundColor = ThemeController::getCustomColorFromDT("kgray-7");
402 // 绘制背景颜色
403 painter.setBrush(backgroundColor);
404 pen = QPen(backgroundColor);
405 painter.setPen(pen);
406 painter.drawRoundedRect(rect(), ra, ra);
407 }
408 }
409 } else {
410 if (!option.state.testFlag(QStyle::State_Enabled)) {
411 textColor = ThemeController::getCustomColorFromDT("text-disable");
412 } else {
413 // 三态
414 if (option.state.testFlag(QStyle::State_Sunken)) {
415 backgroundColor = ThemeController::getCustomColorFromDT("kgray-13");
416 // 绘制背景颜色
417 painter.setBrush(backgroundColor);
418 pen = QPen(backgroundColor);
419 painter.setPen(pen);
420 painter.drawRoundedRect(rect(), ra, ra);
421 } else if (option.state.testFlag(QStyle::State_MouseOver)) {
422 backgroundColor = QColor("#545454");
423 // 绘制背景颜色
424 painter.setBrush(backgroundColor);
425 pen = QPen(backgroundColor);
426 painter.setPen(pen);
427 painter.drawRoundedRect(rect(), ra, ra);
428 }
429 }
430 }
431
432 painter.end();
433
434 // 绘制图标、文本、箭头
435 QPainter p(this);
436 QRect textRect;
437 uint tf = Qt::AlignVCenter;
438
439 textRect = QRect(32, 0, option.rect.width() - 64, option.rect.height());
440
441 if (menu() && menu()->isVisible()) {
442 p.drawPixmap(this->width() - 24, (this->height() - 16) / 2, 16, 16,
443 ThemeController::drawColoredPixmap(QIcon::fromTheme("ukui-down-symbolic").pixmap(16, 16), textColor));
444 } else if (menu() && !menu()->isVisible()) {
445 p.drawPixmap(this->width() - 24, (this->height() - 16) / 2, 16, 16,
446 setIconColor(QIcon::fromTheme("ukui-end-symbolic"), textColor).pixmap(16.16));
447 } else
448 textRect = QRect(32, 0, option.rect.width() - 32, option.rect.height());
449
450 if (!icon().isNull())
451 p.drawPixmap(8, (option.rect.height() - 16) / 2, 16, 16, icon().pixmap(16, 16));
452 else
453 textRect = QRect(8, 0, option.rect.width() - 32, option.rect.height());
454
455 pen = QPen(textColor);
456 p.setPen(pen);
457 p.drawText(textRect, tf, this->text());
458
459 p.end();
460 }
461
462 }
463
464 #include "kdirtabbar.moc"
465 #include "moc_kdirtabbar.cpp"