1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "localtreemodel.h"
6
7#include "localtreeitem.h"
8#include "dap/protocol.h"
9
10#include <QStringList>
11
12using namespace DEBUG_NAMESPACE;
13using namespace dap;
14LocalTreeModel::LocalTreeModel(QObject *parent)
15 : QAbstractItemModel(parent)
16{
17 rootItem = new LocalTreeItem(this);
18}
19
20LocalTreeModel::~LocalTreeModel()
21{
22 delete rootItem;
23}
24
25void LocalTreeModel::setHeaders(const QList<QString> &headers)
26{
27 this->headers = headers;
28}
29
30int LocalTreeModel::columnCount(const QModelIndex &parent) const
31{
32 if (parent.isValid())
33 return static_cast<LocalTreeItem*>(parent.internalPointer())->columnCount();
34 return headers.count();
35}
36
37QVariant LocalTreeModel::data(const QModelIndex &index, int role) const
38{
39 if (!index.isValid())
40 return QVariant();
41
42 if (role != Qt::DisplayRole)
43 return QVariant();
44
45 LocalTreeItem *item = static_cast<LocalTreeItem*>(index.internalPointer());
46
47 return item->data(index.column());
48}
49
50Qt::ItemFlags LocalTreeModel::flags(const QModelIndex &index) const
51{
52 if (!index.isValid())
53 return Qt::NoItemFlags;
54
55 return QAbstractItemModel::flags(index);
56}
57
58QVariant LocalTreeModel::headerData(int section, Qt::Orientation orientation,
59 int role) const
60{
61 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
62 return headers.at(section);
63
64 return QVariant();
65}
66
67QModelIndex LocalTreeModel::index(int row, int column, const QModelIndex &parent) const
68{
69 if (!hasIndex(row, column, parent))
70 return QModelIndex();
71
72 LocalTreeItem *parentItem;
73
74 if (!parent.isValid())
75 parentItem = rootItem;
76 else
77 parentItem = static_cast<LocalTreeItem*>(parent.internalPointer());
78
79 LocalTreeItem *childItem = parentItem->child(row);
80 if (childItem)
81 return createIndex(row, column, childItem);
82 else
83 return QModelIndex();
84}
85
86QModelIndex LocalTreeModel::parent(const QModelIndex &index) const
87{
88 if (!index.isValid())
89 return QModelIndex();
90
91 LocalTreeItem *childItem = static_cast<LocalTreeItem*>(index.internalPointer());
92 LocalTreeItem *parentItem = childItem->getParentItem();
93
94 if (!parentItem || parentItem == rootItem)
95 return QModelIndex();
96
97 return createIndex(parentItem->row(), 0, parentItem);
98}
99
100int LocalTreeModel::rowCount(const QModelIndex &parent) const
101{
102 LocalTreeItem *parentItem;
103 if (parent.column() > 0)
104 return 0;
105
106 if (!parent.isValid())
107 parentItem = rootItem;
108 else
109 parentItem = static_cast<LocalTreeItem*>(parent.internalPointer());
110
111 return parentItem->childCount();
112}
113
114void LocalTreeModel::appendItem(LocalTreeItem* parent, IVariables &vars)
115{
116 if (parent) {
117 for (auto var : vars) {
118 LocalTreeItem *item = new LocalTreeItem(this, parent);
119 item->setVariable(var.var);
120 parent->appendChild(item);
121
122 if (var.children.size() > 0) {
123 appendItem(item, var.children);
124 }
125 }
126 }
127};
128
129void LocalTreeModel::setDatas(IVariables &datas)
130{
131 clear();
132
133 appendItem(rootItem, datas);
134}
135
136void LocalTreeModel::clear()
137{
138 if (rootItem) {
139 beginRemoveRows(rootItem->index(), 0, rootItem->childCount() - 1);
140 rootItem->removeChildren();
141 endRemoveRows();
142 }
143}
144
145QModelIndex LocalTreeModel::indexForItem(const LocalTreeItem *item) const
146{
147 if (!item || item == rootItem) {
148 return QModelIndex();
149 }
150
151 LocalTreeItem *p = item->parent();
152 if (!p)
153 return QModelIndex();
154
155 auto mitem = const_cast<LocalTreeItem *>(item);
156 int row = p->indexOf(mitem);
157 return createIndex(row, 0, mitem);
158}
159