Debian Codegraph @

kysdk-qtwidgets/src/klistwidget.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 "klistwidget.h"
24 #include <QDebug>
25 namespace kdk
26 {
27
28 class KListWidgetPrivate : public QObject
29 {
30 Q_OBJECT
31 Q_DECLARE_PUBLIC(KListWidget)
32 public:
33 KListWidgetPrivate(KListWidget *parent);
34
35 private:
36 KListWidget *q_ptr;
37 QListWidgetItem *item;
38 QListWidgetItem *m_item;
39 };
40
41 KListWidgetPrivate::KListWidgetPrivate(KListWidget *parent)
42 : q_ptr(parent)
43 {
44 Q_Q(KListWidget);
45 setParent(parent);
46 }
47 KListWidget::KListWidget(QWidget *parent)
48 : d_ptr(new KListWidgetPrivate(this))
49 {
50 Q_D(KListWidget);
51 }
52 void KListWidget::AddItemWidget(KItemWidget *m_itemwidget)
53 {
54 Q_D(KListWidget);
55 if (!m_itemwidget)
56 return;
57 d->item = new QListWidgetItem(this);
58 d->item->setSizeHint(QSize(this->width(), 54));
59
60 this->setItemWidget(d->item, m_itemwidget);
61
62 // 建立连接
63 connect(this, &KListWidget::itemClicked, this, [=](QListWidgetItem *current) {
64 KItemWidget *curWidget = dynamic_cast<KItemWidget *>(this->itemWidget(current));
65 if (curWidget)
66 curWidget->SetInverse();
67 });
68 connect(this, &KListWidget::currentItemChanged, this, [=](QListWidgetItem *current, QListWidgetItem *previous) {
69 KItemWidget *curWidget = dynamic_cast<KItemWidget *>(this->itemWidget(current));
70 KItemWidget *preWidget = dynamic_cast<KItemWidget *>(this->itemWidget(previous));
71 if (curWidget && !preWidget) {
72 curWidget->CancelInverse();
73 } else if (curWidget) {
74 curWidget->SetInverse();
75 }
76 if (preWidget) {
77 preWidget->CancelInverse();
78 }
79 });
80 }
81
82 }
83 #include "klistwidget.moc"
84 #include "moc_klistwidget.cpp"