-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldapobjectmodel.cpp
More file actions
181 lines (149 loc) · 5.7 KB
/
Copy pathldapobjectmodel.cpp
File metadata and controls
181 lines (149 loc) · 5.7 KB
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
#include "common.h"
#include "ldapobjectmodel.h"
LdapObjectModel::LdapObjectModel(QObject *parent)
: QAbstractItemModel(parent)
{
}
LdapObjectModel::~LdapObjectModel()
{
qDebug() << "LdapObjectModel::~LdapObjectModel";
qDeleteAll(connections.begin(), connections.end());
connections.clear();
}
QVariant LdapObjectModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return QVariant();
}
QModelIndex LdapObjectModel::index(int row, int column, const QModelIndex &parent) const
{
LdapObject *childObject;
if (!hasIndex(row, column, parent)) {
// qDebug() << "LdapObjectModel::index: not hasIndex for" << row << column;
return QModelIndex();
}
if (!parent.isValid()) {
childObject = connections.at(row);
// qDebug() << "LdapObjectModel::index: not valid";
} else {
LdapObject *parentObject = static_cast<LdapObject*>(parent.internalPointer());
childObject = parentObject->child(row);
}
if (childObject) {
// qDebug() << "LdapObjectModel::index: createIndex for object" << row << column << childObject->name();
return createIndex(row, column, childObject);
}
else
return QModelIndex();
}
QModelIndex LdapObjectModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
LdapObject *childObject = static_cast<LdapObject*>(index.internalPointer());
LdapObject *const parentObject = childObject->parent();
if (parentObject == nullptr)
return QModelIndex();
// qDebug() << "LdapObjectModel::parent: createIndex for object" << parentObject->row() << 0;
return createIndex(parentObject->row(), 0, parentObject);
}
int LdapObjectModel::rowCount(const QModelIndex &parent) const
{
LdapObject *parentObject;
int rows = 0;
if (!parent.isValid()) {
rows = connections.size();
// qDebug() << "LdapObjectModel::rowCount: for connections" << rows;
} else {
parentObject = static_cast<LdapObject*>(parent.internalPointer());
rows = parentObject->childCount();
// qDebug() << "LdapObjectModel::rowCount: for object" << parent.row() << parent.column() << "with" << rows;
}
return rows;
}
int LdapObjectModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
// qDebug() << "LdapObjectModel::columnCount: parent is valid" << parent.row() << parent.column();
// qDebug() << "LdapObjectModel::columnCount: " << static_cast<LdapObject*>(parent.internalPointer())->columnCount();
return static_cast<LdapObject*>(parent.internalPointer())->columnCount();
}
if (!connections.empty()) {
// qDebug() << "LdapObjectModel::columnCount: connections is not empty";
return 1;
}
// qDebug() << "LdapObjectModel::columnCount: 0";
return 0;
}
bool LdapObjectModel::hasChildren(const QModelIndex &parent) const
{
LdapObject *parentObject;
if (!parent.isValid()) {
// qDebug() << "LdapObjectModel::hasChildren parent is not valid";
return !connections.empty();
} else {
// qDebug() << "LdapObjectModel::hasChildren parent is valid" << parent.row() << parent.column();
parentObject = static_cast<LdapObject*>(parent.internalPointer());
}
// qDebug() << "LdapObjectModel::hasChildren parent child count" << parentObject->childCount();
return parentObject->childCount() > 0;
}
bool LdapObjectModel::canFetchMore(const QModelIndex &parent) const
{
bool canFetch = false;
LdapObject *parentObject = nullptr;
if (parent.isValid())
parentObject = static_cast<LdapObject*>(parent.internalPointer());
if (parentObject == nullptr) {
// qDebug() << "LdapObjectModel::canFetchMore: for connections";
foreach (const LdapConnection *connection, connections)
{
if (connection->canFetchRoot()) {
canFetch = true;
break;
}
}
} else {
// qDebug() << "LdapObjectModel::canFetchMore: for objects with parent" << parent.row() << parent.column() << parentObject->name();
canFetch = parentObject->canFetch();
}
// qDebug() << "LdapObjectModel::canFetchMore: is " << (canFetch ? "true" : "false");
return canFetch;
}
void LdapObjectModel::fetchMore(const QModelIndex &parent)
{
if (!parent.isValid()) {
// qDebug() << "LdapObjectModel::fetchMore: for not valid";
foreach (LdapConnection *connection, connections)
{
if (connection->canFetchRoot()) {
// qDebug() << "LdapObjectModel::fetchMore: for connection" << connection->name();
connection->fetchRoot();
}
}
} else {
LdapObject *parentObject = static_cast<LdapObject*>(parent.internalPointer());
if (parentObject->canFetch()) {
// qDebug() << "LdapObjectModel::fetchMore: for object" << parent.row() << parent.column() << parentObject->name();
parentObject->fetch();
}
}
}
QVariant LdapObjectModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
// qDebug() << "LdapObjectModel::data index is not valid";
return QVariant();
}
if (role != Qt::DisplayRole) {
// qDebug() << "LdapObjectModel::data for not DisplayRole";
return QVariant();
}
LdapObject *object = static_cast<LdapObject*>(index.internalPointer());
// qDebug() << "LdapObjectModel::data return NameAttr for ObjectType" << index.row() << index.column() << object->type();
return object->data(NameAttr);
}
void LdapObjectModel::addConnector(Connector &connector)
{
qDebug() << "LdapObjectModel::addConnector";
connections.append(new LdapConnection(connector));
}