| 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 "knavigationtrail.h" |
| 24 |
#include "kborderlessbutton.h" |
| 25 |
#include "kcrumb.h" |
| 26 |
#include "themeController.h" |
| 27 |
#include <QBoxLayout> |
| 28 |
#include <QDebug> |
| 29 |
|
| 30 |
namespace kdk |
| 31 |
{ |
| 32 |
|
| 33 |
class Q_DECL_HIDDEN KNavigationTrailPrivate : public QObject, public ThemeController |
| 34 |
{ |
| 35 |
Q_OBJECT |
| 36 |
Q_DECLARE_PUBLIC(KNavigationTrail) |
| 37 |
|
| 38 |
public: |
| 39 |
KNavigationTrailPrivate(KNavigationTrail *parent); |
| 40 |
|
| 41 |
void updateLayout(); |
| 42 |
|
| 43 |
private: |
| 44 |
KNavigationTrail *q_ptr; |
| 45 |
KBorderlessButton *m_pLeftButton; |
| 46 |
KBorderlessButton *m_pRightButton; |
| 47 |
KCrumb *m_pCrumb; |
| 48 |
QHBoxLayout *m_pHLayout; |
| 49 |
ButtonPosition m_position; |
| 50 |
}; |
| 51 |
|
| 52 |
KNavigationTrail::KNavigationTrail(QWidget *parent) |
| 53 |
: QWidget(parent) |
| 54 |
, d_ptr(new KNavigationTrailPrivate(this)) |
| 55 |
{ |
| 56 |
Q_D(KNavigationTrail); |
| 57 |
} |
| 58 |
|
| 59 |
KCrumb *KNavigationTrail::crumb() |
| 60 |
{ |
| 61 |
Q_D(KNavigationTrail); |
| 62 |
return d->m_pCrumb; |
| 63 |
} |
| 64 |
|
| 65 |
void KNavigationTrail::setPosition(ButtonPosition type) |
| 66 |
{ |
| 67 |
Q_D(KNavigationTrail); |
| 68 |
d->m_position = type; |
| 69 |
d->updateLayout(); |
| 70 |
} |
| 71 |
|
| 72 |
void KNavigationTrail::resizeEvent(QResizeEvent *event) |
| 73 |
{ |
| 74 |
Q_D(KNavigationTrail); |
| 75 |
QWidget::resizeEvent(event); |
| 76 |
emit sizeChange(); |
| 77 |
} |
| 78 |
|
| 79 |
KNavigationTrailPrivate::KNavigationTrailPrivate(KNavigationTrail *parent) |
| 80 |
: q_ptr(parent) |
| 81 |
{ |
| 82 |
Q_Q(KNavigationTrail); |
| 83 |
m_position = ButtonPosition::LeftOrRight; |
| 84 |
m_pHLayout = new QHBoxLayout(q); |
| 85 |
m_pCrumb = new KCrumb(); |
| 86 |
m_pLeftButton = new KBorderlessButton(); |
| 87 |
m_pLeftButton->setIcon(QIcon::fromTheme("go-previous.symbolic")); |
| 88 |
m_pRightButton = new KBorderlessButton(); |
| 89 |
m_pRightButton->setIcon(QIcon::fromTheme("go-next.symbolic")); |
| 90 |
|
| 91 |
m_pHLayout->addWidget(m_pLeftButton); |
| 92 |
m_pHLayout->addWidget(m_pRightButton); |
| 93 |
m_pHLayout->addWidget(m_pCrumb); |
| 94 |
|
| 95 |
connect(m_pCrumb, &KCrumb::currentChanged, this, [=](int index) { |
| 96 |
if (index == 0) { |
| 97 |
m_pLeftButton->setEnabled(false); |
| 98 |
if (!m_pRightButton->isEnabled()) |
| 99 |
m_pRightButton->setEnabled(true); |
| 100 |
} else if (index == m_pCrumb->count() - 1) { |
| 101 |
m_pRightButton->setEnabled(false); |
| 102 |
if (!m_pLeftButton->isEnabled()) |
| 103 |
m_pLeftButton->setEnabled(true); |
| 104 |
} else { |
| 105 |
if (!m_pLeftButton->isEnabled()) |
| 106 |
m_pLeftButton->setEnabled(true); |
| 107 |
if (!m_pRightButton->isEnabled()) |
| 108 |
m_pRightButton->setEnabled(true); |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
connect(m_pLeftButton, &KBorderlessButton::clicked, this, [=] { |
| 113 |
// m_pCrumb->leftScroll(); |
| 114 |
m_pCrumb->setCurrentIndex(m_pCrumb->currentIndex() - 1); |
| 115 |
}); |
| 116 |
connect(m_pRightButton, &KBorderlessButton::clicked, this, [=] { |
| 117 |
// m_pCrumb->rightScroll(); |
| 118 |
m_pCrumb->setCurrentIndex(m_pCrumb->currentIndex() + 1); |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
void KNavigationTrailPrivate::updateLayout() |
| 123 |
{ |
| 124 |
Q_Q(KNavigationTrail); |
| 125 |
QLayoutItem *child; |
| 126 |
while ((child = m_pHLayout->takeAt(0)) != 0) { |
| 127 |
// 删除Stretch(弹簧)等布局 |
| 128 |
if (child->spacerItem()) { |
| 129 |
m_pHLayout->removeItem(child); |
| 130 |
continue; |
| 131 |
} |
| 132 |
// 删除布局 |
| 133 |
m_pHLayout->removeWidget(child->widget()); |
| 134 |
child->widget()->setParent(nullptr); |
| 135 |
delete child; |
| 136 |
child = nullptr; |
| 137 |
} |
| 138 |
|
| 139 |
switch (m_position) { |
| 140 |
case ButtonPosition::LeftPosition: |
| 141 |
m_pHLayout->addWidget(m_pLeftButton); |
| 142 |
m_pHLayout->addWidget(m_pRightButton); |
| 143 |
m_pHLayout->addWidget(m_pCrumb); |
| 144 |
break; |
| 145 |
case ButtonPosition::LeftOrRight: |
| 146 |
m_pHLayout->addWidget(m_pLeftButton); |
| 147 |
m_pHLayout->addWidget(m_pCrumb); |
| 148 |
m_pHLayout->addWidget(m_pRightButton); |
| 149 |
break; |
| 150 |
case ButtonPosition::RightPosition: |
| 151 |
m_pHLayout->addWidget(m_pCrumb); |
| 152 |
m_pHLayout->addWidget(m_pLeftButton); |
| 153 |
m_pHLayout->addWidget(m_pRightButton); |
| 154 |
break; |
| 155 |
default: |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
#include "knavigationtrail.moc" |
| 163 |
#include "moc_knavigationtrail.cpp" |