-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartDirUtils.cpp
66 lines (59 loc) · 2.26 KB
/
SmartDirUtils.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
//
// Created by septemberhx on 2020/6/5.
//
#include "SmartDirUtils.h"
#include <QDir>
#include <QDateTime>
#include <QMimeType>
#include <QIcon>
#include <QFileIconProvider>
#include <QDebug>
#include <QPixmap>
QCache<QString, QPixmap> SmartDirUtils::pixmapCache(100);
QMimeDatabase SmartDirUtils::db;
QFileInfoList SmartDirUtils::fileInfoList(const QStringList& pathList) {
QFileInfoList infoList;
for (const QString& dirPath : pathList) {
const QFileInfo dirInfo(dirPath);
if (dirInfo.exists() && dirInfo.isDir()) {
const QDir& dir(dirPath);
infoList << dir.entryInfoList(QDir::Files);
}
}
qSort(infoList.begin(), infoList.end(), [](const QFileInfo& info1, const QFileInfo& info2) {
return info1.lastModified() > info2.lastModified();
});
return infoList;
}
const QPixmap& SmartDirUtils::getFileIcon(const QFileInfo &fileInfo, int w, int h) {
// I don't know why, but QPixmapCache will be clearly each time
// So I use QCache instead
QString key = QString("%1_%2").arg(fileInfo.absoluteFilePath(), QString::number(fileInfo.lastModified().toMSecsSinceEpoch()));
if (!pixmapCache.contains(key)) {
QMimeType mime = db.mimeTypeForFile(fileInfo);
if (mime.name().startsWith("image")) {
auto *pixmap = new QPixmap(fileInfo.absoluteFilePath());
*pixmap = pixmap->scaled(w, h, Qt::KeepAspectRatio);
pixmapCache.insert(key, pixmap);
} else {
QIcon icon = QFileIconProvider().icon(fileInfo);
auto *pixmap = new QPixmap(icon.pixmap(w, h));
pixmapCache.insert(key, pixmap);
}
}
return *pixmapCache[key];
}
const QPixmap &SmartDirUtils::getSimpleIcon(const QFileInfo &fileInfo, int w, int h) {
QString key = QString("%1_%2").arg(fileInfo.absoluteFilePath(), QString::number(fileInfo.lastModified().toMSecsSinceEpoch()));
if (!pixmapCache.contains(key)) {
QIcon icon = QFileIconProvider().icon(fileInfo);
auto *pixmap = new QPixmap(icon.pixmap(w, h));
QMimeType mime = db.mimeTypeForFile(fileInfo);
if (!mime.name().startsWith("image")) {
pixmapCache.insert(key, pixmap);
}
return *pixmap;
} else {
return *pixmapCache[key];
}
}