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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
*/
#include "mappingtreeresultmodel.h"
#include "smtreeitem.h"
#include "smglobals.h"
MappingTreeResultModel::MappingTreeResultModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent) {
mSourceModel = static_cast<MappingTreeModel*>(SmGlobals::instance()->model("MappingTree"));
}
Qt::ItemFlags MappingTreeResultModel::flags(const QModelIndex &index) const {
Q_UNUSED(index);
return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
}
QVariant MappingTreeResultModel::data(const QModelIndex &index, int role) const{
if(!index.isValid()){
return QVariant();
}
SmTreeItem *item = itemAt(index);
if(role == NameRole){
return item->data(Name);
}
if(role == MappingIdRole){
return item->data(MappingId);
}
if(role == DescIdRole){
return item->data(DescId);
}
if(role == ParentIdRole){
return item->data(ParentId);
}
return SmTreeModel::data(index, role);
}
bool MappingTreeResultModel::setData(const QModelIndex &index, const QVariant &value, int role){
SmTreeItem *item = itemAt(index);
if(role == NameRole){
item->setData(Name, value);
return true;
}
if(role == MappingIdRole){
item->setData(MappingId, value);
return true;
}
if(role == ParentIdRole){
item->setData(ParentId, value);
return true;
}
if(role == DescIdRole){
item->setData(DescId, value);
return true;
}
return SmTreeModel::setData(index, value, role);
}
void MappingTreeResultModel::addItem(const MappingData &data){
QList<int> pPath = data.parents;
if(!mCurrentData.contains(data)){
mCurrentData << data;
}
QList<int> curPath;
std::reverse(pPath.begin(), pPath.end());
MappingTreeModel *mapModel = qobject_cast<MappingTreeModel*>(SmGlobals::instance()->model("MappingTree"));
QModelIndex curIdx = QModelIndex();
int curId = -1;
beginResetModel();
while(!pPath.isEmpty()){
curId = pPath.last();
curPath << curId;
pPath.removeLast();
QModelIndex lastIdx = curIdx;
curIdx = find(curId, MappingId, curIdx);
if(!curIdx.isValid()){
QModelIndex sourceIdx = mapModel->indexFromParentPath(curPath, true);
if(!sourceIdx.isValid()){
return; //should never happen!
}
SmTreeItem *pItem;
if(!lastIdx.isValid()){
pItem = root();
}else{
pItem = static_cast<SmTreeItem*>(lastIdx.internalPointer());
}
int row = pItem->childCount();
const QString curName = sourceIdx.data(MappingTreeModel::NameRole).toString();
for(int i = 0; i < pItem->childCount(); ++i){
if(pItem->child(i)->data(Name).toString() > curName){
row = i;
}
}
QVariantList data;
data << curName << sourceIdx.data(MappingTreeModel::MappingIdRole) << sourceIdx.data(MappingTreeModel::MappingParentIdRole) << sourceIdx.data(MappingTreeModel::DescIdRole);
SmTreeItem *newItem = new SmTreeItem(data, pItem);
pItem->insertChild(row, newItem);
curIdx = createIndex(row, 0, newItem);
}
}
endResetModel();
}
void MappingTreeResultModel::removeItem(const QModelIndex &idx){
if(!idx.isValid()){
return;
}
SmTreeItem *curItem = static_cast<SmTreeItem*>(idx.internalPointer());
MappingData rmData = mSourceModel->mappingDataFromItem(curItem);
beginResetModel();
int row = curItem->row();
SmTreeItem *parent = curItem->parent();
parent->removeChild(row);
int count = mCurrentData.removeAll(rmData);
int toRemove = -1;
if(count == 0){
for(int i = 0; i < mCurrentData.count(); ++i){
MappingData cur = mCurrentData.at(i);
if(cur.parents.contains(rmData.mappingId)){
toRemove = i;
break;
}
}
}
if(toRemove > -1 && toRemove < mCurrentData.count()){
mCurrentData.removeAt(toRemove);
}
endResetModel();
}
QList<QVariant> MappingTreeResultModel::getMappings(SmTreeItem *start) const{
QList<QVariant> retval;
for(int i = 0; i < start->childCount(); ++i){
SmTreeItem *childItem = start->child(i);
if(childItem->childCount()){
retval.append(getMappings(childItem));
}else{
retval << childItem->data(MappingId);
}
}
return retval;
}
QList<QVariant> MappingTreeResultModel::columnValues(int column) const {
return columnValuesRecursive(root(), column);
}
void MappingTreeResultModel::clearData(){
setRoot(new SmTreeItem(NumFields));
mCurrentData.clear();
}
int MappingTreeResultModel::hasChild(SmTreeItem *item, const QVariant &name, int column) const{
for(int i = 0; i < item->childCount(); ++i){
if(item->child(i)->data(column) == name){
return i;
}
}
return -1;
}
QList<QVariant> MappingTreeResultModel::columnValuesRecursive(SmTreeItem *parent, int column) const {
QList<QVariant> retval;
if(!parent->childCount()){
return retval;
}
for(int i = 0; i < parent->childCount(); ++i){
SmTreeItem *child = parent->child(i);
QVariant value = child->data(column);
if(value.canConvert(QVariant::Int) && (value.toInt() != -1)){
retval << value;
}
retval << columnValuesRecursive(child, column);
}
return retval;
}
|