-
Notifications
You must be signed in to change notification settings - Fork 4
/
projectmodel.cpp
executable file
·213 lines (182 loc) · 6.08 KB
/
projectmodel.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "projectmodel.h"
#include<QDebug>
#include<QDir>
ProjectModel::ProjectModel( QObject *parent)
: QAbstractItemModel(parent)
{
rootItem = new TreeNode(new Project("Project"), PROJECT_NODE);
//setupModelData(data.split('\n'), rootItem);
}
ProjectModel::~ProjectModel()
{
}
void ProjectModel::addProject(Project * project)
{
this->beginInsertRows(QModelIndex(),rootItem->childCount(),rootItem->childCount());
TreeNode * node = new TreeNode(project,PROJECT_NODE,rootItem);
rootItem->appendChild(node);
this->endInsertRows();
this->beginInsertRows(this->index(this->currentProject,0),0,1);
node->appendChild(new TreeNode(new ProjectItem({"Files"}),MIDDLE_NODE,node));
node->appendChild(new TreeNode(new ProjectItem({"Tools"}),MIDDLE_NODE,node));
this->endInsertRows();
this->projects.append(project);
this->currentProject = this->projects.size()-1;
}
void ProjectModel::closeProject(int row)
{
this->beginRemoveRows(QModelIndex(),row,row);
rootItem->removeChild(row);
this->endRemoveRows();
this->projects.removeAt(row);
this->currentProject = this->projects.size()-1;
}
int ProjectModel::addFile(ProjectItem * file,QString suffix){
if(this->currentProject == -1) return -1;
TreeNode * node = rootItem->child(this->currentProject)->child(0);
QModelIndex node_index = this->index(0,0,this->index(this->currentProject,0));
// if(node->childCount() >0){
// this->beginRemoveRows(this->index(0,0,node_index),0,0);
// node->removeChild(this->currentProject);
// this->endRemoveRows();
// }
for(int i=0;i<node->childCount();i++)
{
TreeNode *child = node->child(i);
if(child->data(0).toString().endsWith(suffix))
{
this->beginRemoveRows(node_index,i,i);
node->removeChild(i);
this->endRemoveRows();
break;
}
}
this->beginInsertRows(node_index,node->childCount(),node->childCount());
node->appendChild(new TreeNode(file,FILE_NODE,node));
this->endInsertRows();
return 0;
}
int ProjectModel::addTool(ProjectItem * tool){
if(this->currentProject == -1) return -1;
TreeNode * node = rootItem->child(this->currentProject)->child(1);
QModelIndex node_index = this->index(1,0,this->index(this->currentProject,0));
// if(node->childCount() >0) {
// this->beginRemoveRows(this->index(0,0,node_index),0,0);
// node->removeChild(this->currentProject);
// this->endRemoveRows();
// }
for(int i=0;i<node->childCount();i++)
{
TreeNode *child = node->child(i);
if(child->data(1).toString().compare(tool->get(1).toString()) == 0)
{
this->beginRemoveRows(node_index,i,i);
node->removeChild(i);
this->endRemoveRows();
break;
}
}
this->beginInsertRows(node_index,node->childCount(),node->childCount());
node->appendChild(new TreeNode(tool,TOOL_NODE,node));
this->endInsertRows();
return 0;
}
int ProjectModel::removeTool(ProjectItem * tool)
{
if(this->currentProject == -1) return -1;
TreeNode * node = rootItem->child(this->currentProject)->child(1);
QModelIndex node_index = this->index(1,0,this->index(this->currentProject,0));
for(int i=0;i<node->childCount();i++)
{
if(node->child(i)->data(1) == tool->get(1))
{
this->beginRemoveRows(node_index,i,i);
node->removeChild(i);
this->endRemoveRows();
break;
}
}
return 0;
}
TreeNode *ProjectModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeNode *item = static_cast<TreeNode*>(index.internalPointer());
if (item)
return item;
}
return rootItem;
}
QString ProjectModel::getSelectTreeNodeName(const QModelIndex &index) const
{
TreeNode * node = this->getItem(index);
return node->data(0).toString();
}
int ProjectModel::getSelectTreeNodeType(const QModelIndex &index) const
{
TreeNode * node = this->getItem(index);
return node->getType();
}
QVariant ProjectModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
QModelIndex ProjectModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeNode *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeNode*>(parent.internalPointer());
TreeNode *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
return QModelIndex();
}
QModelIndex ProjectModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeNode *childItem = static_cast<TreeNode*>(index.internalPointer());
TreeNode *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int ProjectModel::rowCount(const QModelIndex &parent) const
{
TreeNode *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeNode*>(parent.internalPointer());
return parentItem->childCount();
}
Qt::ItemFlags ProjectModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
int ProjectModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeNode*>(parent.internalPointer())->columnCount();
return rootItem->columnCount();
// FIXME: Implement me!
}
QVariant ProjectModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
TreeNode *item = static_cast<TreeNode*>(index.internalPointer());
return item->data(index.column());
}