-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.cpp
139 lines (119 loc) · 3.47 KB
/
model.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
#include "model.h"
#include <QDebug>
Model::Model(QObject *parent)
: QAbstractListModel(parent)
{
}
int Model::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return vlist.size();
}
QVariant Model::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || vlist.size() <= 0)
return QVariant();
QVariantList temp = vlist.at(index.row());
switch (role) {
case pathRole:
return temp[0];
case sizeRole:
return temp[1];
case typeRole:
return temp[2];
case mimetypeRole:
return temp[3];
}
return QVariant();
}
//bool Model::setData(const QModelIndex &index, const QVariant &value, int role)
//{
// int indexrow = index.row();
// if (data(index, role) != value && vlist.size() > indexrow) {
// switch (role) {
// case fnameRole:
// vlist[indexrow][1] = value.toString();
// break;
// case debtRole:
// vlist[indexrow][2] = value.toString();
// break;
// case picRole:
// vlist[indexrow][3] = value.toString();
// break;
// }
// emit dataChanged(index, index, QVector<int>() << role);
// return true;
// }
// return false;
//}
Qt::ItemFlags Model::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable;
}
QHash<int, QByteArray> Model::roleNames() const
{
QHash<int, QByteArray> roles;
int column_number{0};
for (int i{pathRole}; i != ROLE_END; ++i, ++column_number) {
roles.insert(i, this->columns[column_number]);
}
return roles;
}
bool Model::insert(const QString& filename, const QString& size, const QString& type, const QString& mimetype, const QString& path, const QModelIndex &parent)
{
int rowcount = rowCount();
beginInsertRows(parent, rowcount, rowcount);
QVariantList temp;
temp.append(filename);
temp.append(size);
temp.append(type);
temp.append(mimetype);
temp.append(path);
vlist.push_back(temp);
endInsertRows();
return true;
}
bool Model::prepareAndInsert(QString filepath)
{
filepath.replace("file://", "");
QFileInfo info(filepath);
const QStringList splitedpath = filepath.split("/");
const QString mimetype = QMimeType(QMimeDatabase().mimeTypeForFile(info)).name();
const QStringList postfix{" B", " KB", " MB", " GB", " TB"};
QString filename = splitedpath[splitedpath.size() - 2] + "/" + splitedpath[splitedpath.size() - 1];
const size_t filenamesize = filename.size();
qint64 size{info.size()};
if (size == 0) {
return false;
}
if (filenamesize > 40) {
const int temp = filenamesize - (filenamesize % 40);
filename.remove(temp, filenamesize);
filename += "...";
}
qDebug() << filenamesize;
qDebug() << filename.size();
size_t i{0};
for (; i <= 4; ++i) {
if (size > 1024) {
size /= 1024;
}else {
break;
}
}
insert(filename, QString(QString::number(size) + postfix[i]), info.suffix(), mimetype, filepath);
return true;
}
QString Model::getFileInfo(int index) const
{
return vlist[index][4].toString();
}
bool Model::remove(int index, const QModelIndex& parent) {
beginRemoveRows(parent, index, index);
vlist.removeAt(index);
endRemoveRows();
return true;
}