Debian Codegraph @

kysdk-qtwidgets/src/karrowtabbar.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 "karrowtabbar.h"
24 #include "kborderlessbutton.h"
25 #include "knativetabbar.h"
26 #include "themeController.h"
27 #include <QBoxLayout>
28 #include <QDebug>
29 #include <QProxyStyle>
30
31 namespace kdk
32 {
33 class Q_DECL_HIDDEN ArrowTabBarStyle : public QProxyStyle
34 {
35 Q_OBJECT
36
37 public:
38 ArrowTabBarStyle(QStyle *style = nullptr);
39
40 QRect subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const override;
41 };
42
43 class Q_DECL_HIDDEN KArrowTabBarPrivate : public QObject, public ThemeController
44 {
45 Q_OBJECT
46 Q_DECLARE_PUBLIC(KArrowTabBar)
47
48 public:
49 KArrowTabBarPrivate(KArrowTabBar *parent);
50
51 void currentChangeUpdateBtnStatus(int index);
52
53 void updateBtnVisible();
54
55 private:
56 KArrowTabBar *q_ptr;
57 KBorderlessButton *m_pLeftButton;
58 KBorderlessButton *m_pRightButton;
59 KNativeTabbar *m_pTabBar;
60 QHBoxLayout *m_pHLayout;
61 };
62
63 KArrowTabBar::KArrowTabBar(QWidget *parent)
64 : QWidget(parent)
65 , d_ptr(new KArrowTabBarPrivate(this))
66 {
67 Q_D(KArrowTabBar);
68 connect(this, &KArrowTabBar::sizeChange, this, [=] {
69 d->updateBtnVisible();
70 });
71 }
72
73 KNativeTabbar *KArrowTabBar::tabbar()
74 {
75 Q_D(KArrowTabBar);
76 return d->m_pTabBar;
77 }
78
79 void KArrowTabBar::resizeEvent(QResizeEvent *event)
80 {
81 Q_D(KArrowTabBar);
82 QWidget::resizeEvent(event);
83 emit sizeChange();
84 }
85
86 KArrowTabBarPrivate::KArrowTabBarPrivate(KArrowTabBar *parent)
87 : q_ptr(parent)
88 {
89 Q_Q(KArrowTabBar);
90 m_pHLayout = new QHBoxLayout(q);
91 m_pTabBar = new KNativeTabbar();
92 ArrowTabBarStyle *style = new ArrowTabBarStyle();
93 m_pTabBar->setStyle(style);
94 m_pLeftButton = new KBorderlessButton();
95 m_pLeftButton->setIcon(QIcon::fromTheme("go-previous.symbolic"));
96 m_pRightButton = new KBorderlessButton();
97 m_pRightButton->setIcon(QIcon::fromTheme("go-next.symbolic"));
98
99 m_pHLayout->addWidget(m_pLeftButton);
100 m_pHLayout->addWidget(m_pTabBar);
101 m_pHLayout->addWidget(m_pRightButton);
102
103 connect(m_pTabBar, &KNativeTabbar::currentChanged, this, [=](int index) {
104 currentChangeUpdateBtnStatus(index);
105 });
106
107 connect(m_pLeftButton, &KBorderlessButton::clicked, this, [=] {
108 m_pTabBar->leftScrollTabs();
109 });
110 connect(m_pRightButton, &KBorderlessButton::clicked, this, [=] {
111 m_pTabBar->rightScrollTabs();
112 });
113 connect(m_pTabBar, &KNativeTabbar::leftBStatus, q, [=](bool flag) {
114 m_pLeftButton->setEnabled(flag);
115 });
116 connect(m_pTabBar, &KNativeTabbar::rightBStatus, q, [=](bool flag) {
117 m_pRightButton->setEnabled(flag);
118 });
119 }
120
121 void KArrowTabBarPrivate::currentChangeUpdateBtnStatus(int index)
122 {
123 Q_Q(KArrowTabBar);
124 if (index == 0) {
125 m_pLeftButton->setEnabled(false);
126 if (!m_pRightButton->isEnabled())
127 m_pRightButton->setEnabled(true);
128 } else if (index == m_pTabBar->count() - 1) {
129 m_pRightButton->setEnabled(false);
130 if (!m_pLeftButton->isEnabled())
131 m_pLeftButton->setEnabled(true);
132 } else {
133 if (!m_pLeftButton->isEnabled())
134 m_pLeftButton->setEnabled(true);
135 if (!m_pRightButton->isEnabled())
136 m_pRightButton->setEnabled(true);
137 }
138 }
139
140 void KArrowTabBarPrivate::updateBtnVisible()
141 {
142 Q_Q(KArrowTabBar);
143 int wid = 0;
144 for (int i = 0; i < q->tabbar()->count(); i++)
145 wid += q->tabbar()->tabRect(i).width();
146 if (q->rect().width() < wid) {
147 if (m_pTabBar->usesScrollButtons()) {
148 m_pLeftButton->setVisible(true);
149 m_pRightButton->setVisible(true);
150 } else {
151 m_pLeftButton->setVisible(false);
152 m_pRightButton->setVisible(false);
153 }
154 } else {
155 m_pLeftButton->setVisible(false);
156 m_pRightButton->setVisible(false);
157 }
158 }
159
160 ArrowTabBarStyle::ArrowTabBarStyle(QStyle *style)
161 {
162 }
163
164 QRect ArrowTabBarStyle::subElementRect(QStyle::SubElement element, const QStyleOption *option, const QWidget *widget) const
165 {
166 QRect rc;
167 switch (element) {
168 case QStyle::SE_TabBarScrollLeftButton:
169 rc = option->rect;
170 rc.setWidth(0);
171 rc.setHeight(0);
172 break;
173 case QStyle::SE_TabBarScrollRightButton:
174 rc = option->rect;
175 rc.setWidth(0);
176 rc.setHeight(0);
177 break;
178 default:
179 break;
180 }
181 return rc;
182 }
183
184 }
185
186 #include "karrowtabbar.moc"
187 #include "moc_karrowtabbar.cpp"