1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "localtreeitem.h"
6#include "localtreemodel.h"
7
8#include <QStringList>
9#include <qabstractitemmodel.h>
10
11LocalTreeItem::LocalTreeItem(LocalTreeModel *_model, LocalTreeItem *parent)
12 : model(_model)
13{
14 parentItem = parent;
15}
16
17LocalTreeItem::~LocalTreeItem()
18{
19 qDeleteAll(children);
20}
21
22void LocalTreeItem::appendChild(LocalTreeItem *item)
23{
24 children.append(item);
25}
26
27void LocalTreeItem::setVariable(dap::Variable &variable)
28{
29 itemVariable = variable;
30}
31
32LocalTreeItem *LocalTreeItem::child(int row)
33{
34 return children.value(row);
35}
36
37int LocalTreeItem::childCount() const
38{
39 return children.count();
40}
41
42int LocalTreeItem::columnCount() const
43{
44 return KReference + 1;
45}
46
47QVariant LocalTreeItem::data(int column) const
48{
49 QVariant var;
50 switch (column) {
51 case ColumnType::kName:
52 {
53 QString name = itemVariable.name.c_str();
54 var = name;
55 break;
56 }
57 case ColumnType::KVaule:
58 {
59 QString value = itemVariable.value.c_str();
60 var = value;
61 break;
62 }
63 case ColumnType::KReference:
64 if (itemVariable.memoryReference) {
65 QString value = itemVariable.memoryReference.value().c_str();
66 var = value;
67 break;
68 }
69 }
70 return var;
71}
72
73LocalTreeItem *LocalTreeItem::getParentItem()
74{
75 return parentItem;
76}
77
78void LocalTreeItem::removeChildren()
79{
80 if (childCount() == 0)
81 return;
82 if (model) {
83 clear();
84 }
85}
86
87LocalTreeItem *LocalTreeItem::parent() const
88{
89 return parentItem;
90}
91
92int LocalTreeItem::indexOf(const LocalTreeItem *item) const
93{
94 auto it = std::find(begin(), end(), item);
95 return it == end() ? -1 : static_cast<int>(it - begin());
96}
97
98QModelIndex LocalTreeItem::index() const
99{
100 if (!model)
101 return QModelIndex();
102 return model->indexForItem(this);
103}
104
105void LocalTreeItem::clear()
106{
107 while (childCount() != 0) {
108 LocalTreeItem *item = children.takeLast();
109 item->model = nullptr;
110 item->parentItem = nullptr;
111 delete item;
112 item = nullptr;
113 }
114}
115
116int LocalTreeItem::row() const
117{
118 if (parentItem)
119 return parentItem->children.indexOf(const_cast<LocalTreeItem*>(this));
120
121 return 0;
122}
123