-
Notifications
You must be signed in to change notification settings - Fork 41
/
domitem.cpp
117 lines (83 loc) · 2.7 KB
/
domitem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "domitem.h"
#include <QTime>
DomItem::DomItem(int row, QString n, DomItem* parent) {
// Record the item's location within its parent.
rowNumber = row;
parentItem = parent;
// fill data
type = "string";
value = "";
name = n;
}
DomItem::DomItem() {}
DomItem::~DomItem() {
while (!childItems.isEmpty()) delete childItems.takeFirst();
}
DomItem* DomItem::parent() { return parentItem; }
DomItem* DomItem::child(int i) {
if ((!childItems.count()) <= i) return childItems.at(i);
return 0;
}
DomItem* DomItem::addChild(int i, DomItem* item) {
if (i == -1) i = childItems.size();
if (item == NULL) {
item = new DomItem(i, QObject::tr("NewItem") + " " + QString::number(i + 1),
this);
}
if (item->parent()->getType() == "array") //数组单独采用"Item"加空格的方式
{
if (item == NULL)
item = new DomItem(i, "Item " + QString::number(i + 1), this);
}
childItems.insert(i, item);
return item;
}
void DomItem::removeChild(int i) { childItems.removeAt(i); }
void DomItem::removeFromParent(int row) { parentItem->removeChild(row); }
int DomItem::childCount() const { return childItems.count(); }
int DomItem::row() const { return rowNumber; }
QString DomItem::getName() { return name; }
QString DomItem::getType() { return type; }
QString DomItem::getValue() { return value; }
void DomItem::setName(QString n) { name = n; }
void DomItem::setType(QString t) { type = t; }
void DomItem::setValue(QString v) { value = v; }
void DomItem::setData(QString n, QString t, QString v) {
name = n;
type = t;
value = v;
}
DomItem* DomItem::clone() {
// create new item
DomItem* newItem = new DomItem();
// fill the fields
newItem->rowNumber = this->rowNumber;
newItem->parentItem = this->parentItem;
newItem->type = this->type;
newItem->value = this->value;
newItem->name = this->name;
// copy children
for (int i = 0; i < childItems.count(); ++i) {
DomItem* item = childItems[i]->clone();
newItem->childItems.append(item);
item->parentItem = newItem;
}
return newItem;
}
bool DomItem::lessThan_key(const DomItem* item1, const DomItem* item2) {
return (item1->name < item2->name);
}
// 对key列执行排序
void DomItem::sort_key(Qt::SortOrder order) {
QList<DomItem*> _children; // 子节点
if (order == Qt::AscendingOrder) // 升序
{
std::sort(_children.begin(), _children.end(), lessThan_key);
} else // 降序
{
std::sort(_children.rbegin(), _children.rend(), lessThan_key);
}
}
void DomItem::sort(int column, Qt::SortOrder order) {
if (column == 0) sort_key(order);
}