-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitemmodel.cpp
196 lines (158 loc) · 3.82 KB
/
itemmodel.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
#include "itemmodel.h"
/*
* TREE ITEM
*/
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent, int size, QString path, QString hash, TreeItemType type)
: data(data), parent(parent), size(size), path(path), hash(hash), type(type)
{}
TreeItem::~TreeItem()
{
qDeleteAll(children);
}
void TreeItem::appendChild(TreeItem *child)
{
this->children.append(child);
}
TreeItem *TreeItem::child(int row)
{
if (row < 0 || row >= children.size()) {
return nullptr;
}
return children.at(row);
}
int TreeItem::childCount() const
{
return children.size();
}
int TreeItem::columnCount() const
{
return data.size();
}
QVariant TreeItem::dataValue(int column) const
{
return data.at(column);
}
int TreeItem::row() const
{
if (parent) {
parent->children.indexOf(const_cast<TreeItem*>(this));
}
return 0;
}
TreeItem *TreeItem::parentItem()
{
return parent;
}
TreeItemType TreeItem::getType()
{
return type;
}
QString TreeItem::getPath()
{
return path;
}
QString TreeItem::getHash()
{
return hash;
}
int TreeItem::getSize()
{
return size;
}
/*
* ITEM MODEL
*/
ItemModel::ItemModel(TreeItem *root)
{
rootItem = root;
}
ItemModel::~ItemModel()
{
delete rootItem;
}
QModelIndex ItemModel::index(int row, int column, const QModelIndex &parent) const
{
if ( !hasIndex(row, column, parent) ) {
return QModelIndex();
}
TreeItem *parentItem;
if (!parent.isValid()) {
parentItem = rootItem;
} else {
parentItem = static_cast<TreeItem*>(parent.internalPointer());
}
TreeItem *childItem = parentItem->child(row);
if (childItem) {
return createIndex(row, column, childItem);
}
return QModelIndex();
}
QModelIndex ItemModel::parent(const QModelIndex &child) const
{
if (!child.isValid()) {
return QModelIndex();
}
TreeItem *childItem = static_cast<TreeItem*>(child.internalPointer());
TreeItem *parentItem = childItem->parentItem();
if (parentItem == rootItem) {
return QModelIndex();
}
return createIndex(parentItem->row(), 0, parentItem);
}
int ItemModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (parent.column() > 0) {
return 0;
}
if (!parent.isValid()) {
parentItem = rootItem;
} else {
parentItem = static_cast<TreeItem*>(parent.internalPointer());
}
return parentItem->childCount();
}
int ItemModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
}
return rootItem->columnCount();
}
QVariant ItemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if( role == Qt::DisplayRole) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->dataValue(index.column());
}
if ( role == Qt::DecorationRole && index.column() == 0) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
switch (item->getType()) {
case FOLDER:
return QIcon(":/tree/icons/folder.png");
case VIDEO:
return QIcon(":/tree/icons/clapperboard-play.png");
case IMAGE:
return QIcon(":/tree/icons/picture.png");
case OTHER:
return QIcon(":/tree/icons/document.png");
default:
break;
}
}
return QVariant();
}
Qt::ItemFlags ItemModel::flags(const QModelIndex &index) const
{
return QAbstractItemModel::flags(index);
}
QVariant ItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
return rootItem->dataValue(section);
}
return QVariant();
}