Debian Codegraph @

kysdk-qtwidgets/src/ksettinggroup.cpp

raw
1 /*
2 *
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: Sisheng He <[email protected]>
20 *
21 */
22 #include "ksettinggroup.h"
23 #include <QApplication>
24 namespace kdk
25 {
26
27 class KSettingGroupPrivate : public QObject
28 {
29 Q_OBJECT
30 Q_DECLARE_PUBLIC(KSettingGroup)
31 public:
32 KSettingGroupPrivate(KSettingGroup *parent);
33
34 private:
35 KSettingGroup *q_ptr;
36
37 QVBoxLayout *m_pMainLayout;
38 KBaseFrame::BorderRadiusStyle m_lastRadiusStyle = KBaseFrame::BorderRadiusStyle::Bottom;
39 KBaseFrame::BorderRadiusStyle m_OneRadiusStyle = KBaseFrame::BorderRadiusStyle::Around;
40 QList<KBaseFrame *> m_showItemList;
41 QList<KBaseFrame *> m_allItemList;
42
43 private:
44 /**
45 * 递归添加显示出来的 KBaseFrame
46 */
47 void updateShowItemList(KBaseFrame *frame);
48 /**
49 * 递归获取所有 KBaseFrame
50 */
51 void updateAllItemList(KBaseFrame *frame);
52 };
53
54 KSettingGroupPrivate::KSettingGroupPrivate(KSettingGroup *parent)
55 : q_ptr(parent)
56 {
57 Q_Q(KSettingGroup);
58 m_pMainLayout = new QVBoxLayout(q);
59 m_pMainLayout->setSpacing(1);
60 m_pMainLayout->setContentsMargins(0, 0, 0, 0);
61 q->setLayout(m_pMainLayout);
62 qApp->installEventFilter(q);
63 q->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
64 }
65
66 void KSettingGroupPrivate::updateAllItemList(KBaseFrame *frame)
67 {
68 if (frame->isContainer() && frame->frameShape() == QFrame::NoFrame && frame->layout()) {
69 for (int i = 0; i < frame->layout()->count(); i++) {
70 QLayoutItem *it = frame->layout()->itemAt(i);
71 KBaseFrame *item = qobject_cast<KBaseFrame *>(it->widget());
72 if (!item) {
73 continue;
74 }
75 updateAllItemList(item);
76 }
77 } else {
78 m_allItemList.append(frame);
79 return;
80 }
81 }
82
83 void KSettingGroupPrivate::updateShowItemList(KBaseFrame *frame)
84 {
85 Q_Q(KSettingGroup);
86 if (frame->isContainer() && frame->frameShape() == QFrame::NoFrame && frame->layout()) {
87 for (int i = 0; i < frame->layout()->count(); i++) {
88 QLayoutItem *it = frame->layout()->itemAt(i);
89 KBaseFrame *item = qobject_cast<KBaseFrame *>(it->widget());
90 if (!item) {
91 continue;
92 }
93 updateShowItemList(item);
94 }
95 } else {
96 if (frame->isVisibleTo(q)) {
97 m_showItemList.append(frame);
98 }
99 return;
100 }
101 }
102
103 KSettingGroup::KSettingGroup(QWidget *parent, Shape shape, BorderRadiusStyle style)
104 : KBaseFrame(parent, style, false)
105 , d_ptr(new KSettingGroupPrivate(this))
106 {
107 Q_D(KSettingGroup);
108 setFrameShape(shape);
109 }
110
111 KSettingGroup::~KSettingGroup()
112 {
113 }
114
115 void KSettingGroup::addWidget(QFrame *item, bool autoUpdateShape, bool visible)
116 {
117 Q_D(KSettingGroup);
118 d->m_pMainLayout->addWidget(item, autoUpdateShape);
119 item->setVisible(visible);
120 if (autoUpdateShape) {
121 updateShape();
122 }
123 }
124
125 void KSettingGroup::insertWidget(int index, QFrame *item, bool autoUpdateShape, bool visible)
126 {
127 Q_D(KSettingGroup);
128 d->m_pMainLayout->insertWidget(index, item);
129 item->setVisible(visible);
130 if (autoUpdateShape) {
131 updateShape();
132 }
133 }
134
135 void KSettingGroup::removeWidget(QWidget *w, bool autoUpdateShape)
136 {
137 Q_D(KSettingGroup);
138 d->m_pMainLayout->removeWidget(w);
139 if (autoUpdateShape) {
140 updateShape();
141 }
142 }
143
144 void KSettingGroup::updateShape()
145 {
146 Q_D(KSettingGroup);
147 d->m_showItemList.clear();
148 for (int i = 0; i < d->m_pMainLayout->count(); i++) {
149 QLayoutItem *it = d->m_pMainLayout->itemAt(i);
150 KBaseFrame *item = qobject_cast<KBaseFrame *>(it->widget());
151 if (!item) {
152 continue;
153 }
154 d->updateShowItemList(item);
155 }
156
157 for (int i = 0; i < d->m_showItemList.size(); i++) {
158 KBaseFrame *item = d->m_showItemList.at(i);
159 if (d->m_showItemList.size() == 1) {
160 item->setRadiusType(d->m_OneRadiusStyle);
161 } else if (i == 0) {
162 item->setRadiusType(BorderRadiusStyle::Top);
163 } else if (i == d->m_showItemList.size() - 1) {
164 item->setRadiusType(d->m_lastRadiusStyle);
165 } else {
166 item->setRadiusType(BorderRadiusStyle::None);
167 }
168 }
169 }
170
171 QVBoxLayout *KSettingGroup::layout()
172 {
173 Q_D(KSettingGroup);
174 return d->m_pMainLayout;
175 }
176
177 void KSettingGroup::setLastRadiusStyle(BorderRadiusStyle style)
178 {
179 Q_D(KSettingGroup);
180 d->m_lastRadiusStyle = style;
181 }
182
183 void KSettingGroup::setOneRadiusStyle(BorderRadiusStyle style)
184 {
185 Q_D(KSettingGroup);
186 d->m_OneRadiusStyle = style;
187 }
188
189 bool KSettingGroup::eventFilter(QObject *watched, QEvent *event)
190 {
191 Q_D(KSettingGroup);
192 if (event->type() == QEvent::Hide || event->type() == QEvent::Show) {
193 d->m_allItemList.clear();
194 for (int i = 0; i < d->m_pMainLayout->count(); i++) {
195 QLayoutItem *it = d->m_pMainLayout->itemAt(i);
196 KBaseFrame *item = qobject_cast<KBaseFrame *>(it->widget());
197 d->updateAllItemList(item);
198 }
199 for (int i = 0; i < d->m_allItemList.size(); i++) {
200 if (d->m_allItemList.at(i) == watched) {
201 updateShape();
202 }
203 }
204 }
205 return KBaseFrame::eventFilter(watched, event);
206 }
207
208 int KSettingGroup::showItemsCount()
209 {
210 Q_D(KSettingGroup);
211 d->m_showItemList.clear();
212 for (int i = 0; i < d->m_pMainLayout->count(); i++) {
213 QLayoutItem *it = d->m_pMainLayout->itemAt(i);
214 KBaseFrame *item = qobject_cast<KBaseFrame *>(it->widget());
215 if (!item) {
216 continue;
217 }
218 d->updateShowItemList(item);
219 }
220 return d->m_showItemList.size();
221 }
222
223 void KSettingGroup::removeAndDeleteAllWidget()
224 {
225 Q_D(KSettingGroup);
226 while (d->m_pMainLayout->count() > 0) {
227 QLayoutItem *it = d->m_pMainLayout->itemAt(0);
228 KBaseFrame *item = qobject_cast<KBaseFrame *>(it->widget());
229 if (!item) {
230 continue;
231 }
232 d->m_pMainLayout->removeWidget(item);
233 item->deleteLater();
234 item = nullptr;
235 }
236 return;
237 }
238
239 }
240
241 #include "ksettinggroup.moc"
242 #include "moc_ksettinggroup.cpp"