-
Notifications
You must be signed in to change notification settings - Fork 0
/
filezillausermodel.cpp
130 lines (106 loc) · 2.89 KB
/
filezillausermodel.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
#include "filezillausermodel.h"
FilezillaUserModel::FilezillaUserModel(std::vector<FilezillaUser> *users, QObject *parent) :
QAbstractItemModel(parent),
pUsers(users)
{
}
int FilezillaUserModel::rowCount(const QModelIndex &parent) const
{
if(!parent.isValid())
{
return pUsers->size();
}
else if(parent.internalId() == 0)
{
return (*pUsers)[parent.row()].directories.size();
}
return 0;
}
int FilezillaUserModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
QModelIndex FilezillaUserModel::index(int row, int column, const QModelIndex &parent) const
{
if(!parent.isValid())
{
return createIndex(row, 0);
}
if(column > 0)
{
return QModelIndex();
}
return createIndex(row, column, parent.row()+1);
}
QModelIndex FilezillaUserModel::parent(const QModelIndex &child) const
{
if(!child.isValid())
{
return QModelIndex();
}
if(child.internalPointer() != 0)
{
return createIndex(child.internalId()-1, 0);
}
return QModelIndex();
}
QVariant FilezillaUserModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid()) return QVariant();
if(static_cast<unsigned int>(index.row()) > pUsers->size()) return QVariant();
if(role != Qt::DisplayRole) return QVariant();
if(index.internalId() != 0)
{
int parentIndex = index.internalId() - 1;
if(index.row() < (*pUsers)[parentIndex].directories.size())
{
return (*pUsers)[parentIndex].directories[index.row()].dir;
}
return QVariant();
}
return (*pUsers)[index.row()].username;
}
QVariant FilezillaUserModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
return QVariant("Users");
}
return QVariant();
}
Qt::ItemFlags FilezillaUserModel::flags(const QModelIndex &index) const
{
if(index.internalId() == 0)
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
return Qt::ItemIsEnabled;
}
void FilezillaUserModel::pushBack(const FilezillaUser &toAdd)
{
beginInsertRows(QModelIndex(), pUsers->size(), pUsers->size());
pUsers->push_back(toAdd);
endInsertRows();
/*QModelIndex parent = createIndex(pUsers->size()-1, 0, static_cast<void*>(&(*pUsers)[pUsers->size()-1]));
beginInsertRows(parent, 0, toAdd.directories.size()-1);
endInsertRows();*/
}
void FilezillaUserModel::clear()
{
if(pUsers->size() == 0)
return;
beginRemoveRows(QModelIndex(), 0, pUsers->size()-1);
pUsers->clear();
endRemoveRows();
}
void FilezillaUserModel::removeAt(int index)
{
beginRemoveRows(QModelIndex(), index, index);
std::vector<FilezillaUser>::iterator iter = pUsers->begin();
for(int i=0; i<index; i++)
{
iter++;
}
pUsers->erase(iter);
endRemoveRows();
}