-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.cpp
More file actions
290 lines (260 loc) · 7.81 KB
/
loader.cpp
File metadata and controls
290 lines (260 loc) · 7.81 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "loader.h"
#include <QBuffer>
#include <QFileInfo>
#include <QImage>
#include <QImageReader>
#include <QPixmap>
#include <QRegularExpression>
bool compareNumbers(const QString& v1, const QString& v2)
{
static QRegularExpression r("(\\D+|\\d+)");
auto match1 = r.globalMatch(v1);
auto match2 = r.globalMatch(v2);
bool ok1, ok2;
while (match1.hasNext() && match2.hasNext()) {
QString str1 = match1.next().captured(1);
QString str2 = match2.next().captured(1);
if (str1 == str2) {
continue;
}
int num1 = str1.toInt(&ok1);
int num2 = str2.toInt(&ok2);
if (ok1 && ok2 && (num1 != num2)) {
return (num1 < num2);
}
else {
return (str1 < str2);
}
}
return v1 < v2;
}
Loader::Loader(QObject* parent)
: QThread { parent }
{
connect(this, &QThread::finished, this, [=] {
if (queue.count() > 0) {
start();
}
});
}
Loader::~Loader()
{
if (nullptr != sevenZip) {
sevenZip->close();
delete sevenZip;
}
if (nullptr != zip) {
zip->close();
delete zip;
}
}
QStringList Loader::getEntries(KArchive* zip, const KArchiveDirectory* dir)
{
QStringList list;
auto entries = dir->entries();
auto base = dir->name();
if (base == "/") {
base = "";
}
else {
base += "/";
}
for (int i = 0; i < entries.size(); ++i) {
auto fName = entries[i];
const KArchiveEntry* entry = dir->entry(fName);
if (entry->isDirectory()) {
KArchiveDirectory* newDir = (KArchiveDirectory*)entry;
list.append(getEntries(zip, newDir));
}
else {
auto fullPath = QString("%1%2").arg(base).arg(entry->name());
list.append(fullPath);
// qDebug() << "getEntry: " << fName << "isDir: " << entry->isDirectory() << "fullPath: " << fullPath;
}
}
return list;
}
void Loader::load7zip(const QString& path)
{
sevenZip = new K7Zip(path);
sevenZip->open(QIODeviceBase::ReadOnly);
auto dir = sevenZip->directory();
QStringList tempList = getEntries(sevenZip, dir);
std::sort(tempList.begin(), tempList.end(), compareNumbers);
QStringList fileList;
// This loop populates the model with items and stores the filename (to load the image later)
// and size (to keep the scrollbar the same size and in the same position while loading images)
// the images are loaded on demand on Loader::run().
for (int i = 0; i < tempList.size(); ++i) {
auto fName = tempList[i];
auto file = dir->file(fName);
if (nullptr != file) {
auto item = new QStandardItem;
item->setText(fName);
auto device = file->createDevice();
QImageReader reader(device);
if (reader.canRead()) {
QSize s = reader.size();
item->setData(fName, Qt::DisplayRole);
item->setData(s, Qt::SizeHintRole);
fileList.append(fName);
// qDebug() << "file " << fName;
emit itemLoaded(item);
}
device->close();
device->deleteLater();
}
}
emit fileListLoaded(fileList);
}
void Loader::loadZip(const QString& path)
{
zip = new QuaZip(fileName);
zip->open(QuaZip::mdUnzip);
QStringList fileList;
QStringList tempList = zip->getFileNameList(); //.filter(re);
std::sort(tempList.begin(), tempList.end(), compareNumbers);
QuaZipFile file(zip);
while (tempList.size() > 0) {
auto fName = tempList.takeFirst();
zip->setCurrentFile(fName);
file.open(QuaZipFile::ReadOnly);
QImageReader reader(&file);
if (reader.canRead()) {
auto item = new QStandardItem;
item->setText(fName);
QSize s = reader.size();
if (!s.isValid()) {
QImage img = QImage::fromData(file.readAll());
s = img.size();
qDebug() << "size2" << s;
}
item->setData(s, Qt::SizeHintRole);
fileList.append(fName);
emit itemLoaded(item);
}
file.close();
}
emit fileListLoaded(fileList);
}
// Using KArchive
void Loader::setFile(const QString& path)
{
if (nullptr != sevenZip) {
sevenZip->close();
delete sevenZip;
sevenZip = nullptr;
}
if (nullptr != zip) {
zip->close();
delete zip;
zip = nullptr;
}
emit fileListLoaded(QStringList());
fileName = path;
QFileInfo info(path);
if (!info.exists()) {
return;
}
if (info.suffix().toLower() == "zip" || info.suffix().toLower() == "cbz") {
qDebug() << "QuaZip: " << path;
loadZip(path);
}
else if (info.suffix().toLower() == "7z") {
qDebug() << "KArchive: " << path;
load7zip(path);
}
else {
return;
}
}
// Using Quazip
// void Loader::setFile(QString path)
// {
// if (nullptr != zip) {
// zip->close();
// zip->~QuaZip();
// }
// fileName = path;
// zip = new QuaZip(fileName);
// zip->open(QuaZip::mdUnzip);
// // QRegularExpression re("(\\.bmp|\\.png|\\.jpg|\\.jpeg|\\.webp)$", QRegularExpression::PatternOption::CaseInsensitiveOption);
// QStringList fileList;
// QStringList tempList = zip->getFileNameList(); //.filter(re);
// std::sort(tempList.begin(), tempList.end(), compareNumbers);
// QuaZipFile file(zip);
// while (tempList.size() > 0) {
// auto fName = tempList.takeFirst();
// auto item = new QStandardItem;
// item->setText(fName);
// zip->setCurrentFile(fName);
// file.open(QuaZipFile::ReadOnly);
// QSize s;
// bool canRead = false;
// if (true) {
// QImageReader reader(&file);
// s = reader.size();
// // reader.
// canRead = reader.canRead();
// }
// if (canRead) {
// qDebug() << "size1" << s;
// if (!s.isValid()) {
// QImage img = QImage::fromData(file.readAll());
// s = img.size();
// qDebug() << "size2" << s;
// }
// item->setData(s, Qt::SizeHintRole);
// fileList.append(fName);
// emit itemLoaded(item);
// }
// qDebug() << "c1";
// file.close();
// qDebug() << "c2";
// }
// emit fileListLoaded(fileList);
// zip->close();
// }
void Loader::addToQueue(const QModelIndex& index)
{
if (!queue.contains(index)) {
if (queue.size() > 5) {
queue.prepend(index);
}
else {
queue.append(index);
}
}
start();
}
void Loader::run()
{
if ((nullptr == sevenZip && nullptr == zip) || queue.count() == 0) {
return;
}
// zip->open(QuaZip::mdUnzip);
if (nullptr != zip) {
QuaZipFile file(zip);
while (queue.size() > 0) {
auto index = queue.takeFirst();
QString fName(index.data(Qt::DisplayRole).toString());
zip->setCurrentFile(fName);
file.open(QuaZipFile::ReadOnly);
auto pix = QPixmap::fromImage(QImage::fromData(file.readAll()));
file.close();
emit pixmapLoaded(index, pix);
}
}
else if (nullptr != sevenZip) {
while (queue.size() > 0) {
auto index = queue.takeFirst();
QString fName(index.data(Qt::DisplayRole).toString());
auto file = sevenZip->directory()->file(fName);
if (nullptr != file) {
auto pix = QPixmap::fromImage(QImage::fromData(file->data()));
emit pixmapLoaded(index, pix);
}
}
}
// zip->close();
}