-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectpathlistmodel.cpp
120 lines (82 loc) · 2.06 KB
/
selectpathlistmodel.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
#include "selectpathlistmodel.h"
SelectPathListModel* SelectPathListModel::pSelectPathListModel = nullptr;
SelectPathListModel::SelectPathListModel(QObject *parent)
: QAbstractListModel(parent)
{
pSelectPathListModel = this;
}
int SelectPathListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_selectPath.size();
}
QVariant SelectPathListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
QJsonObject item = m_selectPath.at(index.row());
switch (role) {
case SEL_PATH_NAME:
return item.value("dirName").toString();
break;
case SHOW_ARROW:
return item.value("showArrow").toBool();
break;
default:
break;
}
return QVariant();
}
QHash<int,QByteArray> SelectPathListModel::roleNames() const
{
QHash<int,QByteArray> rolesHash;
rolesHash.insert(SEL_PATH_NAME, "dirName");
rolesHash.insert(SHOW_ARROW, "showArrow");
return rolesHash;
}
void SelectPathListModel::clearDirs()
{
m_selectPath.clear();
beginResetModel();
endResetModel();
}
void SelectPathListModel::addFolder(const QJsonObject& item)
{
m_selectPath.push_back(item);
beginResetModel();
endResetModel();
}
//删除最后一个节点
void SelectPathListModel::delFolder()
{
if (m_selectPath.empty())
{
return;
}
m_selectPath.pop_back();
beginResetModel();
endResetModel();
}
//删除当前节点之后的节点
void SelectPathListModel::delFolder(const QString& dirName)
{
bool bFind = false;
for (auto iter = m_selectPath.begin(); iter != m_selectPath.end();)
{
QString strName = iter->value("dirName").toString();
if (strName.compare(dirName) == 0)
{
bFind = true;
}
if(bFind && strName.compare(dirName) != 0)
{
iter = m_selectPath.erase(iter);
} else {
++iter;
}
}
//更新界面显示
beginResetModel();
endResetModel();
}