Debian Codegraph @

kysdk-qtwidgets/src/knavigationbar.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 "knavigationbar.h"
24 #include "parmscontroller.h"
25 #include "themeController.h"
26 #include <QApplication>
27 #include <QCoreApplication>
28 #include <QDebug>
29 #include <QFileInfo>
30 #include <QGSettings>
31 #include <QLinearGradient>
32 #include <QListView>
33 #include <QListWidgetItem>
34 #include <QModelIndex>
35 #include <QPainter>
36 #include <QPainterPath>
37 #include <QScrollBar>
38 #include <QStandardItemModel>
39 #include <QStyleOptionViewItem>
40 #include <QStyledItemDelegate>
41 #include <QToolTip>
42 #include <QVBoxLayout>
43 #include <QtMath>
44 #include "accessinfohelper.h"
45
46 namespace kdk
47 {
48
49 enum ItemType {
50 StandardItem = 0,
51 SubItem,
52 TagItem
53 };
54
55 class ListView : public QListView
56 {
57 public:
58 ListView(QWidget *parent);
59
60 QPoint mousePoint();
61
62 protected:
63 void mousePressEvent(QMouseEvent *event);
64 virtual void mouseMoveEvent(QMouseEvent *e) override;
65 virtual void leaveEvent(QEvent *event);
66 bool eventFilter(QObject *watched, QEvent *event);
67
68 private:
69 QPoint m_mousePoint;
70 };
71
72 class Delegate : public QStyledItemDelegate, public ThemeController
73 {
74 Q_OBJECT
75 public:
76 Delegate(QObject *parent, ListView *view);
77
78 void setTabValueVisible(bool visible);
79
80 void setAddIcon(QString tag, QIcon icon);
81
82 void setAddTagVisible(QString tag, bool visible);
83
84 void setExtendIcon(QString tag, QIcon icon);
85
86 void setExtendTagVisible(QString tag, bool visible);
87
88 Q_SIGNALS:
89 void addClicked(QString str);
90 void extendClicked(QString str, int row);
91
92 protected:
93 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
94 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
95 virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
96
97 private:
98 ListView *m_listView;
99 bool tabValueVisible;
100 QList<QString> m_extendList;
101 QList<QString> m_addList;
102 QMap<QString, bool> m_addVisibleMap;
103 QMap<QString, bool> m_extendVisibleMap;
104 QMap<QString, QIcon> m_addIconMap;
105 QMap<QString, QIcon> m_extendIconMap;
106 bool addPress;
107 bool extendPress;
108 };
109
110 class KNavigationBarPrivate : public QObject
111 {
112 Q_OBJECT
113 Q_DECLARE_PUBLIC(KNavigationBar)
114
115 public:
116 KNavigationBarPrivate(KNavigationBar *parent);
117 void changeTheme();
118 QString setTagForItem(QStandardItem *item);
119
120 private:
121 KNavigationBar *q_ptr;
122 ListView *m_pView;
123 QStandardItemModel *m_pModel;
124 Delegate *m_pDelegate;
125 QMap<QString, QList<QStandardItem *>> m_itemMap;
126 int num;
127 QModelIndex m_index;
128 bool m_indexFlag;
129 };
130
131 KNavigationBar::KNavigationBar(QWidget *parent)
132 : QScrollArea(parent)
133 , d_ptr(new KNavigationBarPrivate(this))
134 {
135 Q_D(KNavigationBar);
136 d->m_pView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
137 d->m_pView->verticalScrollBar()->setProperty("drawScrollBarGroove",false);
138 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
139 d->num = 0;
140 d->m_indexFlag = false;
141 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
142 // d->m_pView = new ListView (this);
143 d->m_pView->setResizeMode(QListView::Adjust);
144 // d->m_pModel = new QStandardItemModel(d->m_pView);
145 d->m_pView->setModel(d->m_pModel);
146 QVBoxLayout *vLayout = new QVBoxLayout(this);
147 vLayout->setSizeConstraint(QLayout::SizeConstraint::SetMaximumSize);
148 vLayout->setContentsMargins(0, 0, 0, 0);
149 vLayout->setSpacing(0);
150 vLayout->addWidget(d->m_pView);
151
152 QWidget *vp = viewport();
153 vp->setLayout(vLayout);
154 setWidget(d->m_pView);
155
156 d->m_pView->setFocus();
157 QPalette p = this->palette();
158 QColor color(0, 0, 0, 0);
159 p.setColor(QPalette::Base, color);
160 d->m_pView->setPalette(p);
161 this->setPalette(p);
162 d->m_pView->setFrameStyle(0);
163 // d->m_pDelegate = new Delegate(this,d->m_pView);
164 d->m_pView->setItemDelegate(d->m_pDelegate);
165 d->m_pView->setEditTriggers(QAbstractItemView::NoEditTriggers);
166 this->setFrameStyle(0);
167 this->setBackgroundRole(QPalette::Base);
168 this->setAutoFillBackground(true);
169 d->changeTheme();
170 connect(d->m_pDelegate->m_gsetting,&QGSettings::changed,this,[=](QString key){
171 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
172 {
173 d->changeTheme();
174 }
175 });
176 connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=]() {
177 // if(Parmscontroller::isTabletMode()) //解决导航栏滑动条切换主题为白条
178 // d->m_pView->setStyleSheet("QListView item {height : 48}");
179 // else
180 // d->m_pView->setStyleSheet("QListView item {height : 36}");
181 updateGeometry();
182 });
183 connect(d->m_pDelegate, &Delegate::addClicked, this, &KNavigationBar::addTagClicked);
184 connect(d->m_pDelegate, &Delegate::extendClicked, this, [=](QString str, int row) {
185 if (d->m_itemMap.contains(str)) {
186 QStringList list;
187 for (auto item : d->m_itemMap[str]) {
188 list.append(item->data(Qt::DisplayRole).toString());
189 }
190
191 if (list.contains(d->m_pView->currentIndex().data(Qt::DisplayRole).toString())) // 选中的为group中
192 {
193 d->m_index = d->m_pView->currentIndex();
194 d->m_pView->setCurrentIndex(QModelIndex());
195 } else if (!list.contains(d->m_pView->currentIndex().data(Qt::DisplayRole).toString()) && d->m_pView->currentIndex().isValid()) // 选中的为group外
196 {
197 d->m_index = d->m_pView->currentIndex();
198 d->m_pView->setCurrentIndex(QModelIndex());
199 }
200
201 for (auto item : d->m_itemMap[str]) {
202 bool flag = item->data(boolRole).toBool();
203 if (flag) {
204 item->setData(false, boolRole);
205 d->m_pModel->takeRow(item->row());
206 d->m_pDelegate->setExtendIcon(str, QIcon::fromTheme("ukui-down.symbolic"));
207 if (list.contains(d->m_index.data(Qt::DisplayRole).toString())) // 选中的group中item才更改标志位
208 d->m_indexFlag = true;
209 } else {
210 item->setData(true, boolRole);
211 d->m_pModel->insertRow(++row, item);
212 d->m_pDelegate->setExtendIcon(str, QIcon::fromTheme("ukui-up.symbolic"));
213 d->m_indexFlag = false;
214 }
215 }
216
217 if (!d->m_indexFlag && d->m_index.isValid()) {
218 d->m_pView->setCurrentIndex(d->m_index);
219 d->m_index = QModelIndex();
220 } else if (d->m_indexFlag && d->m_index.isValid()) {
221 d->m_pView->setCurrentIndex(QModelIndex());
222 }
223 }
224 });
225
226 setMouseTracking(true);
227 }
228
229 void KNavigationBar::addItem(QStandardItem *item)
230 {
231 Q_D(KNavigationBar);
232 item->setData(ItemType::StandardItem, Qt::UserRole);
233 item->setData(true, boolRole);
234 d->m_pModel->appendRow(item);
235 d->num++;
236 d->setTagForItem(item);
237 }
238
239 void KNavigationBar::addSubItem(QStandardItem *subItem)
240 {
241 Q_D(KNavigationBar);
242 subItem->setData(ItemType::SubItem, Qt::UserRole);
243 subItem->setData(true, boolRole);
244 QPixmap pix(24, 24);
245 pix.fill(Qt::transparent);
246 QIcon icon(pix);
247 subItem->setIcon(icon);
248 d->m_pModel->appendRow(subItem);
249 d->num++;
250 d->setTagForItem(subItem);
251 }
252
253 void KNavigationBar::addGroupItems(QList<QStandardItem *> items, const QString &tag)
254 {
255 Q_D(KNavigationBar);
256 QStandardItem *item = new QStandardItem(tag);
257
258 QList<QStandardItem *> itemList;
259 item->setEnabled(false);
260 item->setData(ItemType::TagItem, Qt::UserRole);
261 item->setData(true, boolRole);
262 d->num++;
263 QFileInfo cmdInfo(QApplication::arguments().at(0));
264 QString accessibleName = cmdInfo.fileName();
265 accessibleName += "_KNavigationBar_tag_" + tag;
266 item->setAccessibleText(accessibleName);
267
268 d->m_pModel->appendRow(item);
269 for (auto item : items) {
270 item->setData(true, boolRole);
271 item->setData(ItemType::StandardItem, Qt::UserRole);
272 itemList.append(item);
273 d->m_pModel->appendRow(item);
274 d->num++;
275 d->setTagForItem(item);
276 }
277 d->m_itemMap[tag] = itemList;
278 }
279
280 void KNavigationBar::insertGroupItems(QString tag, int row, QStandardItem *item)
281 {
282 Q_D(KNavigationBar);
283 if (d->m_itemMap.contains(tag)) {
284 QList<QStandardItem *> itemList = d->m_itemMap[tag];
285 if (!itemList.contains(item)) {
286 if ((0 <= row && row < itemList.count())) {
287 QStandardItem *privItem = itemList.at(row);
288 item->setData(itemList.at(0)->data(boolRole).toBool(), boolRole);
289 itemList.insert(row, item);
290 d->m_itemMap[tag] = itemList;
291 d->m_pModel->insertRow(privItem->row(), item);
292 } else if (0 <= row && row == itemList.count()) {
293 for (int insertRow = 0; insertRow < d->m_pModel->rowCount(); ++insertRow) {
294 QStandardItem *privItem = d->m_pModel->item(insertRow);
295 if (privItem && privItem->data(Qt::DisplayRole).toString() == tag) {
296 item->setData(itemList.at(0)->data(boolRole).toBool(), boolRole);
297 itemList.insert(insertRow, item);
298 d->m_itemMap[tag] = itemList;
299 d->m_pModel->insertRow(privItem->row() + itemList.count(), item);
300 break;
301 }
302 }
303 }
304 }
305 }
306 }
307
308 void KNavigationBar::removeGroupItem(QString tag, QStandardItem *item)
309 {
310 Q_D(KNavigationBar);
311 if (d->m_itemMap.contains(tag)) {
312 QList<QStandardItem *> itemList = d->m_itemMap[tag];
313 if (itemList.contains(item)) {
314 itemList.removeOne(item);
315 d->m_itemMap[tag] = itemList;
316
317 for (int row = 0; row < d->m_pModel->rowCount(); ++row) {
318 QStandardItem *modelItem = d->m_pModel->item(row);
319 if (modelItem == item) {
320 d->m_pModel->takeRow(modelItem->row());
321 }
322 }
323 }
324 }
325 }
326
327 void KNavigationBar::addTag(const QString &tag)
328 {
329 Q_D(KNavigationBar);
330 QStandardItem *item = new QStandardItem(tag);
331 d->num++;
332 d->setTagForItem(item);
333 QFileInfo cmdInfo(QApplication::arguments().at(0));
334 QString accessibleName = cmdInfo.fileName();
335 accessibleName += "_KNavigationBar_tag_" + tag;
336 item->setAccessibleText(accessibleName);
337
338 item->setEnabled(false);
339 item->setData(ItemType::TagItem, Qt::UserRole);
340 item->setData(true, boolRole);
341 d->m_pModel->appendRow(item);
342 }
343
344 QStandardItemModel *KNavigationBar::model()
345 {
346 Q_D(KNavigationBar);
347 return d->m_pModel;
348 }
349
350 QListView *KNavigationBar::listview()
351 {
352 Q_D(KNavigationBar);
353 return d->m_pView;
354 }
355
356 void KNavigationBar::setTabValue(QStandardItem *item, QString value)
357 {
358 Q_D(KNavigationBar);
359 item->setData(value, ValueRole);
360 update();
361 }
362
363 void KNavigationBar::setTabValueVisible(bool visible)
364 {
365 Q_D(KNavigationBar);
366 d->m_pDelegate->setTabValueVisible(visible);
367 }
368
369 void KNavigationBar::setAddIcon(QString tag, QIcon icon)
370 {
371 Q_D(KNavigationBar);
372 d->m_pDelegate->setAddIcon(tag, icon);
373 }
374
375 void KNavigationBar::setAddTagVisible(QString tag, bool visible)
376 {
377 Q_D(KNavigationBar);
378 d->m_pDelegate->setAddTagVisible(tag, visible);
379 }
380
381 void KNavigationBar::setExtendTagVisible(QString tag, bool visible)
382 {
383 Q_D(KNavigationBar);
384 d->m_pDelegate->setExtendTagVisible(tag, visible);
385 }
386
387 KNavigationBarPrivate::KNavigationBarPrivate(KNavigationBar *parent)
388 : q_ptr(parent)
389 {
390 Q_Q(KNavigationBar);
391 setParent(parent);
392 m_pView = new ListView(q);
393 m_pModel = new QStandardItemModel(m_pView);
394 m_pDelegate = new Delegate(q, m_pView);
395
396 KDK_ALL_INFO_FORMAT(m_pView,"");
397 KDK_OBJ_INFO_FORMAT(m_pModel);
398 KDK_OBJ_INFO_FORMAT(m_pDelegate);
399 }
400
401 void KNavigationBarPrivate::changeTheme()
402 {
403 Q_Q(KNavigationBar);
404 m_pDelegate->initThemeStyle();
405 }
406
407 QString KNavigationBarPrivate::setTagForItem(QStandardItem *item)
408 {
409 QFileInfo cmdInfo(QApplication::arguments().at(0));
410 QString accessibleName = cmdInfo.fileName();
411 accessibleName += "_";
412 accessibleName += "QStandardItem";
413 accessibleName += "_";
414 accessibleName += item->data(0).toString();
415 item->setAccessibleText(accessibleName);
416 return accessibleName;
417 }
418
419 Delegate::Delegate(QObject *parent, ListView *view)
420 : QStyledItemDelegate(parent)
421 , m_listView(view)
422 {
423 tabValueVisible = false;
424 addPress = false;
425 extendPress = false;
426 m_listView->setMouseTracking(true);
427 }
428
429 void Delegate::setTabValueVisible(bool visible)
430 {
431 tabValueVisible = visible;
432 }
433
434 void Delegate::setAddIcon(QString tag, QIcon icon)
435 {
436 m_addIconMap[tag] = icon;
437 }
438
439 void Delegate::setAddTagVisible(QString tag, bool visible)
440 {
441 if (!m_addList.contains(tag))
442 m_addList.append(tag);
443 if (!m_addIconMap.contains(tag))
444 m_addIconMap[tag] = QIcon::fromTheme("list-add.symbolic");
445 m_addVisibleMap[tag] = visible;
446 }
447
448 void Delegate::setExtendIcon(QString tag, QIcon icon)
449 {
450 m_extendIconMap[tag] = icon;
451 }
452
453 void Delegate::setExtendTagVisible(QString tag, bool visible)
454 {
455 if (!m_extendList.contains(tag))
456 m_extendList.append(tag);
457 if (!m_extendIconMap.contains(tag))
458 m_extendIconMap[tag] = QIcon::fromTheme("ukui-up.symbolic");
459 m_extendVisibleMap[tag] = visible;
460 }
461
462 void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
463 {
464 QLinearGradient m_linearGradient;
465 QRectF rect;
466 rect.setX(option.rect.x());
467 rect.setY(option.rect.y() + 1);
468 rect.setWidth(m_listView->width() - 16 -1 );//16滑动条宽度 1修正右侧显示滑动条选中item显示不全
469 // rect.setHeight(option.rect.height()-2);
470 rect.setHeight(option.rect.height() - Parmscontroller::parm(Parmscontroller::Parm::PM_NavigationBatInterval));
471
472 // QPainterPath画圆角矩形
473 qreal radius = ThemeController::getRadiusFromDT("kradius-normal"); // 圆角半径6px
474 if (radius == -1)
475 radius = 6;
476 QPainterPath path;
477
478 path.moveTo(rect.topRight() - QPointF(radius, 0));
479 path.lineTo(rect.topLeft() + QPointF(radius, 0));
480 path.quadTo(rect.topLeft(), rect.topLeft() + QPointF(0, radius));
481 path.lineTo(rect.bottomLeft() + QPointF(0, -radius));
482 path.quadTo(rect.bottomLeft(), rect.bottomLeft() + QPointF(radius, 0));
483 path.lineTo(rect.bottomRight() - QPointF(radius, 0));
484 path.quadTo(rect.bottomRight(), rect.bottomRight() + QPointF(0, -radius));
485 path.lineTo(rect.topRight() + QPointF(0, radius));
486 path.quadTo(rect.topRight(), rect.topRight() + QPointF(-radius, -0));
487
488 int flag = index.model()->data(index, Qt::UserRole).toInt();
489 painter->setRenderHint(QPainter::Antialiasing);
490 painter->setRenderHint(QPainter::TextAntialiasing);
491 painter->setRenderHint(QPainter::SmoothPixmapTransform);
492 QColor color;
493 QColor valueColor = ThemeController::getCustomColorFromDT("windowtext-active");
494 QColor borColor;
495 m_linearGradient = QLinearGradient(rect.width() / 2, rect.y(), rect.width() / 2, rect.height() + rect.y());
496 if (!(option.state & QStyle::State_Enabled))
497 {
498 color = ThemeController::getCustomColorFromDT("kcomponent-selected-alpha-disable");
499 borColor = ThemeController::getCustomColorFromDT("kfont-primary-disable");
500 }
501 else if (((m_listView->currentIndex() == index) || (option.state & QStyle::State_Selected) || (option.state & QStyle::State_MouseOver)) && flag != 2)
502 {
503 if ((m_listView->currentIndex() == index) || (option.state & QStyle::State_Selected))
504 {
505 color = ThemeController::getCustomColorFromDT("highlight-active");
506 borColor = ThemeController::getCustomColorFromDT("kline-brand-normal");
507
508 if(ThemeController::widgetTheme() == FashionTheme)
509 {
510 QColor startColor;
511 QColor endColor;
512
513 ThemeController::getGradientFromDT("highlight-active",startColor,endColor);
514
515 m_linearGradient.setColorAt(0,startColor);
516 m_linearGradient.setColorAt(1,endColor);
517 }
518 }
519 else
520 {
521 // hover时
522 color = ThemeController::getCustomColorFromDT("kcomponent-selected-alpha-normal");
523 borColor = ThemeController::getCustomColorFromDT("kline-component-normal");
524
525 if(ThemeController::widgetTheme() == FashionTheme)
526 {
527 QColor startColor;
528 QColor endColor;
529
530 ThemeController::getGradientFromDT("kcomponent-selected-alpha-normal",startColor,endColor);
531
532 m_linearGradient.setColorAt(0,startColor);
533 m_linearGradient.setColorAt(1,endColor);
534 }
535 }
536 painter->save();
537 painter->setPen(borColor);
538 if(ThemeController::widgetTheme() == FashionTheme)
539 painter->setBrush(m_linearGradient);
540 else
541 painter->setBrush(color);
542 if (painter->pen().width() == 1)
543 painter->translate(0.5, 0.5);
544 painter->drawPath(path);
545 painter->restore();
546 }
547
548 switch (flag) {
549 case 0: // standardItem
550 {
551 QRect iconRect;
552 if (ThemeController::systemLang())
553 iconRect = QRect(rect.right() - 32, rect.y() + (rect.height() - 16) / 2, 16, 16); // 图片大小16*16 左边距16
554 else
555 iconRect = QRect(rect.x() + 16, rect.y() + (rect.height() - 16) / 2, 16, 16);
556 auto *model = dynamic_cast<QStandardItemModel *>(const_cast<QAbstractItemModel *>(index.model()));
557 auto icon = model->item(index.row())->icon();
558
559 if (!icon.pixmap(16, 16).isNull()) {
560 if (ThemeController::isPixmapPureColor(icon.pixmap(16, 16))) {
561 if (ThemeController::themeMode() == DarkTheme)
562 icon = ThemeController::drawSymbolicColoredPixmap(icon.pixmap(16, 16));
563 if ((m_listView->currentIndex() == index) || (option.state & QStyle::State_Selected))
564 if (ThemeController::widgetTheme() != ClassicTheme)
565 icon = ThemeController::drawColoredPixmap(icon.pixmap(16, 16), ThemeController::getCustomColorFromDT("kwhite"));
566 if (!(option.state & QStyle::State_Enabled))
567 icon = ThemeController::drawColoredPixmap(icon.pixmap(16, 16), ThemeController::getCustomColorFromDT("kgray-14"));
568 }
569 }
570 icon.paint(painter, iconRect);
571 QFontMetrics fm = painter->fontMetrics();
572
573 QString mainText = index.data(Qt::DisplayRole).toString();
574 QString subText = index.data(Qt::UserRole + 1).toString();
575 QString elidedText;
576 QRect textRect, subRect;
577
578 if (subText.isEmpty()) {
579 elidedText = fm.elidedText(mainText, Qt::ElideRight, rect.width() - 56); // 左边距+图片宽度+文本图片间距+右边距
580 if (fm.horizontalAdvance(mainText) > rect.width() - 56)
581 model->item(index.row())->setToolTip(mainText);
582
583 if (!icon.isNull()) {
584 textRect = QRect(iconRect.right() + 8, rect.y(), rect.width() - 56, rect.height());
585 } else {
586 textRect = QRect(rect.x() + 16, rect.y(), rect.width() - 56, rect.height());
587 }
588 } else {
589 elidedText = fm.elidedText(mainText, Qt::ElideRight, rect.width() - 56 - fm.horizontalAdvance(subText));
590 subRect = QRect(rect.right() - 8 - fm.horizontalAdvance(subText), rect.y(), fm.horizontalAdvance(subText), rect.height());
591
592 if (fm.horizontalAdvance(mainText) > rect.width() - 56 - fm.horizontalAdvance(subText))
593 model->item(index.row())->setToolTip(mainText);
594
595 if (!icon.isNull()) {
596 textRect = QRect(iconRect.right() + 8, rect.y(), rect.width() - 56 - fm.horizontalAdvance(subText), rect.height());
597 } else {
598 textRect = QRect(rect.x() + 16, rect.y(), rect.width() - 56 - fm.horizontalAdvance(subText), rect.height());
599 }
600 }
601
602 painter->save();
603 if ((m_listView->currentIndex() == index) || (option.state & QStyle::State_Selected)) {
604 valueColor = ThemeController::getCustomColorFromDT("kwhite");
605 painter->setPen(ThemeController::getCustomColorFromDT("highlightedtext-active"));
606 }
607 QFont font;
608 font.setPointSize(ThemeController::systemFontSize());
609 if (!(option.state & QStyle::State_Enabled)) {
610 painter->setPen(borColor);
611 }
612 painter->setFont(font);
613 if (!subText.isEmpty() && tabValueVisible) {
614 if (ThemeController::themeMode() == LightTheme)
615 valueColor.setAlphaF(0.4);
616 painter->save();
617 painter->setPen(valueColor);
618 painter->drawText(subRect, Qt::AlignVCenter, subText);
619 painter->restore();
620 }
621 if (!icon.isNull()) {
622 if (ThemeController::systemLang()) {
623
624 painter->drawText(QRect(rect.left() + 16, rect.y(),
625 rect.width() - 56, rect.height()),
626 Qt::AlignVCenter, elidedText); // 文本 图片间距8px
627 } else
628 painter->drawText(QRect(iconRect.right() + 8, rect.y(),
629 rect.width() - 56, rect.height()),
630 Qt::AlignVCenter, elidedText);
631 } else {
632 if (ThemeController::systemLang())
633 painter->drawText(QRect(rect.x() + 16, rect.y(),
634 rect.width() - 56, rect.height()),
635 Qt::AlignVCenter, elidedText);
636 else
637 painter->drawText(QRect(rect.x() + 16, rect.y(),
638 rect.width() - 56, rect.height()),
639 Qt::AlignVCenter, elidedText);
640 }
641 painter->restore();
642 break;
643 }
644 case 1: // subItem
645 {
646 QRect iconRect = QRect(rect.x() + 16, rect.y() + (rect.height() - 16) / 2, 16, 16); // 图片大小16*16 左边距16
647 auto *model = dynamic_cast<QStandardItemModel *>(const_cast<QAbstractItemModel *>(index.model()));
648
649 QFontMetrics fm = painter->fontMetrics();
650 QString mainText = index.data(Qt::DisplayRole).toString();
651 QString subText = index.data(Qt::UserRole + 1).toString();
652 QString elidedText;
653 QRect textRect, subRect;
654
655 if (subText.isEmpty()) {
656 elidedText = fm.elidedText(mainText, Qt::ElideRight, rect.width() - 56); // 左边距+图片宽度+文本图片间距+右边距
657 textRect = QRect(iconRect.right() + 8, rect.y(), rect.width() - 56, rect.height());
658
659 if (fm.horizontalAdvance(mainText) > rect.width() - 56)
660 model->item(index.row())->setToolTip(mainText);
661 } else {
662 elidedText = fm.elidedText(mainText, Qt::ElideRight, rect.width() - 56 - fm.horizontalAdvance(subText));
663 textRect = QRect(iconRect.right() + 8, rect.y(), rect.width() - 56 - fm.horizontalAdvance(subText), rect.height());
664 subRect = QRect(rect.right() - 8 - fm.horizontalAdvance(subText), rect.y(), fm.horizontalAdvance(subText), rect.height());
665 if (fm.horizontalAdvance(mainText) > rect.width() - 56 - fm.horizontalAdvance(subText))
666 model->item(index.row())->setToolTip(mainText);
667 }
668 painter->save();
669 if ((option.state & QStyle::State_Selected)) {
670 valueColor = ThemeController::getCustomColorFromDT("kwhite");
671 painter->setPen(ThemeController::getCustomColorFromDT("highlightedtext-active"));
672 }
673 QFont font;
674 font.setPointSize(ThemeController::systemFontSize());
675 if (!(option.state & QStyle::State_Enabled)) {
676 painter->setPen(borColor);
677 }
678 painter->setFont(font);
679 if (!subText.isEmpty() && tabValueVisible) {
680 if (ThemeController::themeMode() == LightTheme)
681 valueColor.setAlphaF(0.4);
682 painter->setPen(valueColor);
683 painter->drawText(subRect, Qt::AlignVCenter, subText);
684 }
685 if (ThemeController::systemLang())
686 painter->drawText(QRect(rect.x() + 16, rect.y(),
687 rect.width() - 56, rect.height()),
688 Qt::AlignVCenter, elidedText); // 文本 图片间距8px
689 else
690 painter->drawText(QRect(iconRect.right() + 8, rect.y(),
691 rect.width() - 56, rect.height()),
692 Qt::AlignVCenter, elidedText);
693 painter->restore();
694 break;
695 }
696 case 2: // tagItem
697 {
698 painter->save();
699 auto *model = dynamic_cast<QStandardItemModel *>(const_cast<QAbstractItemModel *>(index.model()));
700 QFontMetrics fm = painter->fontMetrics();
701 QString elidedText = fm.elidedText(index.model()->data(index, Qt::DisplayRole).toString(), Qt::ElideRight, rect.width() - 56); // 左边距+图片宽度+文本图片间距+右边距
702
703 QString mainText = index.data(Qt::DisplayRole).toString();
704 if (fm.horizontalAdvance(mainText) > rect.width() - 56)
705 model->item(index.row())->setToolTip(mainText);
706 painter->setPen(ThemeController::getCustomColorFromDT("kfont-secondary"));
707
708 if (m_addList.contains(mainText) || m_extendList.contains(mainText)) {
709 QPixmap extendPixmap = m_extendIconMap.value(mainText).pixmap(16, 16);
710 QPixmap addPixmap = m_addIconMap.value(mainText).pixmap(16, 16);
711 if (ThemeController::themeMode() == DarkTheme) {
712 extendPixmap = ThemeController::drawColoredPixmap(extendPixmap, ThemeController::getCustomColorFromDT("windowtext-active"));
713 addPixmap = ThemeController::drawColoredPixmap(addPixmap, ThemeController::getCustomColorFromDT("windowtext-active"));
714 }
715 if (m_addVisibleMap.value(mainText)) {
716 painter->save();
717 painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
718 painter->setPen(Qt::NoPen);
719 painter->setBrush(Qt::NoBrush);
720
721 if (m_extendVisibleMap.value(mainText)) {
722 QRect extendRect; // = option.rect.adjusted(option.rect.width() - 28 , 22 , -12 , -10 );
723 QRect extendBkgRect; // = option.rect.adjusted(option.rect.width() - 32 , 18 , -8 , -6 );
724 if (m_listView->verticalScrollBar()->isVisible()) {
725 extendRect = option.rect.adjusted(option.rect.width() - 28, 22, -12, -10);
726 extendBkgRect = option.rect.adjusted(option.rect.width() - 32, 18, -8, -6);
727 } else {
728 extendRect = option.rect.adjusted(option.rect.width() - 28 - 16, 22, -12 - 16, -10);
729 extendBkgRect = option.rect.adjusted(option.rect.width() - 32 - 16, 18, -8 - 16, -6);
730 }
731 if (extendPress && extendBkgRect.contains(m_listView->mousePoint()))
732 painter->setBrush(ThemeController::mixColor(ThemeController::getCustomColorFromDT("button-active"), ThemeController::getCustomColorFromDT("brighttext-active"), 0.2));
733 else if (extendBkgRect.contains(m_listView->mousePoint())) {
734 if (m_extendIconMap.value(mainText).name().contains("ukui-down.symbolic"))
735 QToolTip::showText(QCursor::pos(), tr("Expand"), qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
736 else
737 QToolTip::showText(QCursor::pos(), tr("Fold"), qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
738 painter->setBrush(ThemeController::mixColor(ThemeController::getCustomColorFromDT("button-active"), ThemeController::getCustomColorFromDT("brighttext-active"), 0.05));
739 }
740 painter->drawRoundedRect(extendBkgRect, 6, 6);
741 painter->drawPixmap(extendRect, extendPixmap);
742 painter->setBrush(Qt::NoBrush);
743
744 QRect addRect; // = option.rect.adjusted(option.rect.width() - 60, 22 , -44 , -10 );
745 QRect addBkgRect; // = option.rect.adjusted(option.rect.width() - 64, 18 , -40 , -6 );
746 if (m_listView->verticalScrollBar()->isVisible()) {
747 addRect = option.rect.adjusted(option.rect.width() - 60, 22, -44, -10);
748 addBkgRect = option.rect.adjusted(option.rect.width() - 64, 18, -40, -6);
749 } else {
750 addRect = option.rect.adjusted(option.rect.width() - 60 - 16, 22, -44 - 16, -10);
751 addBkgRect = option.rect.adjusted(option.rect.width() - 64 - 16, 18, -40 - 16, -6);
752 }
753
754 if (addPress && addBkgRect.contains(m_listView->mousePoint())) {
755 painter->setBrush(ThemeController::mixColor(ThemeController::getCustomColorFromDT("button-active"), ThemeController::getCustomColorFromDT("brighttext-active"), 0.2));
756 } else if (addBkgRect.contains(m_listView->mousePoint())) {
757 QToolTip::showText(QCursor::pos(), tr("Add tag"), qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
758 painter->setBrush(ThemeController::mixColor(ThemeController::getCustomColorFromDT("button-active"), ThemeController::getCustomColorFromDT("brighttext-active"), 0.05));
759 }
760
761 if (!addBkgRect.contains(m_listView->mousePoint()) && !extendBkgRect.contains(m_listView->mousePoint())) {
762 QToolTip::showText(QCursor::pos(), "", qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
763 }
764 painter->drawRoundedRect(addBkgRect, 6, 6);
765 painter->drawPixmap(addRect, addPixmap);
766 } else {
767 QRect addRect; // = option.rect.adjusted(option.rect.width() - 28 , 22 , -12 , -10 );
768 QRect addBkgRect; // = option.rect.adjusted(option.rect.width() - 32 , 18 , -8 , -6);
769 if (m_listView->verticalScrollBar()->isVisible()) {
770 addRect = option.rect.adjusted(option.rect.width() - 28, 22, -12, -10);
771 addBkgRect = option.rect.adjusted(option.rect.width() - 32, 18, -8, -6);
772 } else {
773 addRect = option.rect.adjusted(option.rect.width() - 28 - 16, 22, -12 - 16, -10);
774 addBkgRect = option.rect.adjusted(option.rect.width() - 32 - 16, 18, -8 - 16, -6);
775 }
776 if (addPress && addBkgRect.contains(m_listView->mousePoint())) {
777 painter->setBrush(ThemeController::mixColor(ThemeController::getCustomColorFromDT("button-active"), ThemeController::getCustomColorFromDT("brighttext-active"), 0.2));
778 } else if (addBkgRect.contains(m_listView->mousePoint())) {
779 QToolTip::showText(QCursor::pos(), tr("Add tag"), qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
780 painter->setBrush(ThemeController::mixColor(ThemeController::getCustomColorFromDT("button-active"), ThemeController::getCustomColorFromDT("brighttext-active"), 0.05));
781 }
782
783 if (!addBkgRect.contains(m_listView->mousePoint())) {
784 QToolTip::showText(QCursor::pos(), "", qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
785 }
786 painter->drawRoundedRect(addBkgRect, 6, 6);
787 painter->drawPixmap(addRect, addPixmap);
788 }
789 painter->restore();
790 } else {
791 painter->save();
792 painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
793 painter->setBrush(Qt::NoBrush);
794 QPixmap extendPixmap = m_extendIconMap.value(mainText).pixmap(16, 16);
795 if (ThemeController::themeMode() == DarkTheme) {
796 extendPixmap = ThemeController::drawSymbolicColoredPixmap(extendPixmap);
797 }
798
799 if (m_extendVisibleMap.value(mainText)) {
800 QRect extendRect; // = option.rect.adjusted(option.rect.width() - 28 , 22 , -12 , -10 );
801 QRect extendBkgRect; // = option.rect.adjusted(option.rect.width() - 32 , 18 , -8 , -6 );
802 if (m_listView->verticalScrollBar()->isVisible()) {
803 extendRect = option.rect.adjusted(option.rect.width() - 28, 22, -12, -10);
804 extendBkgRect = option.rect.adjusted(option.rect.width() - 32, 18, -8, -6);
805 } else {
806 extendRect = option.rect.adjusted(option.rect.width() - 28 - 16, 22, -12 - 16, -10);
807 extendBkgRect = option.rect.adjusted(option.rect.width() - 32 - 16, 18, -8 - 16, -6);
808 }
809 if (extendPress && extendBkgRect.contains(m_listView->mousePoint()))
810 painter->setBrush(ThemeController::mixColor(ThemeController::getCustomColorFromDT("button-active"), ThemeController::getCustomColorFromDT("brighttext-active"), 0.2));
811 else if (extendBkgRect.contains(m_listView->mousePoint())) {
812 if (m_extendIconMap.value(mainText).name().contains("ukui-down.symbolic"))
813 QToolTip::showText(QCursor::pos(), tr("Expand"), qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
814 else
815 QToolTip::showText(QCursor::pos(), tr("Fold"), qobject_cast<QWidget *>(const_cast<QWidget *>(option.widget)));
816 painter->setBrush(ThemeController::mixColor(ThemeController::getCustomColorFromDT("button-active"), ThemeController::getCustomColorFromDT("brighttext-active"), 0.05));
817 }
818 if (!extendBkgRect.contains(m_listView->mousePoint())) {
819 QToolTip::showText(QCursor::pos(), "");
820 }
821 painter->drawRoundedRect(extendBkgRect, 6, 6);
822 painter->drawPixmap(extendRect, extendPixmap);
823 }
824 painter->restore();
825 }
826 }
827
828 QRect textRect = option.rect;
829 textRect = textRect.adjusted(16, 12, 0, 0);
830 QFont font;
831 font.setPointSize(ThemeController::systemFontSize());
832 painter->setFont(font);
833 painter->drawText(textRect, Qt::AlignVCenter, elidedText);
834 painter->restore();
835 break;
836 }
837 default:
838 break;
839 }
840 }
841
842 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
843 {
844 int flag = index.model()->data(index, Qt::UserRole).toInt();
845 QSize size;
846 switch (flag) {
847 case 2: // tagItem
848 size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_NavigationBatHeight) + 12);
849 break;
850 default:
851 size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_NavigationBatHeight) + Parmscontroller::parm(Parmscontroller::Parm::PM_NavigationBatInterval));
852 break;
853 }
854 size.setWidth(m_listView->width() - 16);
855 return size;
856 }
857
858 bool Delegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
859 {
860 if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::Enter) {
861 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
862 QRect addRect;
863 QRect extendRect;
864 QString mainText = index.data(Qt::DisplayRole).toString();
865 if (m_addList.contains(mainText) || m_extendList.contains(mainText)) {
866 if (m_addVisibleMap.value(mainText)) {
867 if (m_extendVisibleMap.value(mainText)) {
868 if (m_listView->verticalScrollBar()->isVisible()) {
869 extendRect = option.rect.adjusted(option.rect.width() - 32, 18, -8, -6);
870 addRect = option.rect.adjusted(option.rect.width() - 64, 18, -40, -6);
871 } else {
872 extendRect = option.rect.adjusted(option.rect.width() - 32 - 16, 18, -8 - 16, -6);
873 addRect = option.rect.adjusted(option.rect.width() - 64 - 16, 18, -40 - 16, -6);
874 }
875
876 if (extendRect.contains(mouseEvent->pos())) {
877 if (event->type() == QEvent::MouseButtonPress) {
878 extendPress = true;
879 emit extendClicked(mainText, index.row());
880 } else {
881 extendPress = false;
882 }
883 }
884 if (addRect.contains(mouseEvent->pos())) {
885 if (event->type() == QEvent::MouseButtonPress) {
886 addPress = true;
887 emit addClicked(mainText);
888 } else {
889 addPress = false;
890 }
891 }
892
893 } else {
894 if (m_listView->verticalScrollBar()->isVisible())
895 addRect = option.rect.adjusted(option.rect.width() - 32, 18, -8, -6);
896 else
897 addRect = option.rect.adjusted(option.rect.width() - 32 - 16, 18, -8 - 16, -6);
898 if (addRect.contains(mouseEvent->pos())) {
899 if (event->type() == QEvent::MouseButtonPress) {
900 addPress = true;
901 emit addClicked(mainText);
902 } else {
903 addPress = false;
904 }
905 }
906 }
907 } else {
908 if (m_extendVisibleMap.value(mainText)) {
909 if (m_listView->verticalScrollBar()->isVisible())
910 extendRect = option.rect.adjusted(option.rect.width() - 32, 18, -8, -6);
911 else
912 extendRect = option.rect.adjusted(option.rect.width() - 32 - 16, 18, -8 - 16, -6);
913 if (extendRect.contains(mouseEvent->pos())) {
914 if (event->type() == QEvent::MouseButtonPress) {
915 extendPress = true;
916 emit extendClicked(mainText, index.row());
917 } else {
918 extendPress = false;
919 }
920 }
921 }
922 }
923 }
924 } else {
925 addPress = false;
926 extendPress = false;
927 }
928 m_listView->viewport()->update();
929 return QStyledItemDelegate::editorEvent(event, model, option, index);
930 }
931
932 ListView::ListView(QWidget *parent)
933 : QListView(parent)
934 {
935 setMouseTracking(true);
936 installEventFilter(this);
937 }
938
939 QPoint ListView::mousePoint()
940 {
941 return m_mousePoint;
942 }
943
944 void ListView::mousePressEvent(QMouseEvent *event)
945 {
946 if (event->button() & Qt::RightButton)
947 return;
948 else if (event->button() & Qt::LeftButton)
949 QListView::mousePressEvent(event);
950 }
951
952 void ListView::mouseMoveEvent(QMouseEvent *e)
953 {
954 m_mousePoint = e->pos();
955 QListView::mouseMoveEvent(e);
956 }
957
958 void ListView::leaveEvent(QEvent *event)
959 {
960 // QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(event);
961 // m_mousePoint = mouseEvent->pos();
962
963 m_mousePoint = mapFromGlobal(QCursor::pos());
964 QListView::leaveEvent(event);
965 }
966
967 bool ListView::eventFilter(QObject *watched, QEvent *event)
968 {
969 if ( event->type() == QEvent::KeyPress)
970 {
971 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
972
973 // 处理Tab键导航
974 if (keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Backtab || keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down)
975 {
976
977 QModelIndex currentIndex = this->currentIndex();
978 int rowCount = model()->rowCount();
979 if (rowCount == 0)
980 {
981 return true;
982 }
983
984 int newRow;
985 if (currentIndex.isValid())
986 {
987 if (keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Down) {
988 newRow = (currentIndex.row() + 1) % rowCount;
989 } else {
990 newRow = (currentIndex.row() - 1 + rowCount) % rowCount;
991 }
992 } else {
993 newRow = 0;
994 }
995
996 int attempts = 0;
997 while (attempts < rowCount)
998 {
999 QModelIndex candidateIndex = model()->index(newRow, 0);
1000
1001 if (candidateIndex.isValid() && (model()->flags(candidateIndex) & Qt::ItemIsEnabled) && (model()->flags(candidateIndex) & Qt::ItemIsSelectable))
1002 {
1003 QVariant itemTypeData = model()->data(candidateIndex, Qt::UserRole);
1004 if (itemTypeData.isValid() && itemTypeData.toInt() == 2) { // TagItem
1005
1006 if (keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Down) {
1007 newRow = (newRow + 1) % rowCount;
1008 } else {
1009 newRow = (newRow - 1 + rowCount) % rowCount;
1010 }
1011 attempts++;
1012 continue;
1013 }
1014 if(!isRowHidden(candidateIndex.row()))
1015 {
1016 setCurrentIndex(candidateIndex);
1017 scrollTo(candidateIndex);
1018 emit clicked(candidateIndex);
1019 return true;
1020 }
1021 }
1022
1023 if ((keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Down))
1024 {
1025 newRow = (newRow + 1) % rowCount;
1026 }
1027 else
1028 {
1029 newRow = (newRow - 1 + rowCount) % rowCount;
1030 }
1031 attempts++;
1032 }
1033 return true;
1034 }
1035 }
1036 if(event->type() == QEvent::FocusIn)
1037 {
1038 if(this->currentIndex().row() == -1 )
1039 {
1040 if (model() && model()->rowCount() > 0)
1041 {
1042 for (int row = 0; row < model()->rowCount(); ++row)
1043 {
1044 QModelIndex candidateIndex = model()->index(row, 0);
1045
1046 if (candidateIndex.isValid() && (model()->flags(candidateIndex) & Qt::ItemIsEnabled) && (model()->flags(candidateIndex) & Qt::ItemIsSelectable))
1047 {
1048 QVariant itemTypeData = model()->data(candidateIndex, Qt::UserRole);
1049 if (!itemTypeData.isValid() || itemTypeData.toInt() != 2 && !isRowHidden(candidateIndex.row()))
1050 {
1051 setCurrentIndex(candidateIndex);
1052 emit clicked(candidateIndex);
1053 break;
1054 }
1055 }
1056 }
1057 }
1058 }
1059 }
1060
1061 return QObject::eventFilter(watched, event);
1062 }
1063
1064 }
1065
1066 #include "knavigationbar.moc";
1067 #include "moc_knavigationbar.cpp";