-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbnailmanager.cpp
113 lines (93 loc) · 2.83 KB
/
thumbnailmanager.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
#include "thumbnailmanager.h"
ThumbnailManager::ThumbnailManager(QWidget* mainWidget)
{
makeThumbnailDirectory();
this->mainWidget = mainWidget;
const int DEFAULT_WIDTH = 250;
const int DEFAULT_HEIGHT = 250;
defaultSize = QSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
maximumSize = QSize(DEFAULT_WIDTH * 2, DEFAULT_HEIGHT * 2);
loadStored();
loadDefault();
}
void ThumbnailManager::makeThumbnailDirectory()
{
QDir dir;
dir.mkdir("thumbnails");
}
void ThumbnailManager::loadStored()
{
Database* database = Database::getInstance();
QList<FileTuple> files = database->getAllFiles();
ProgressDialog popup(mainWidget, "Loading thumbnails...", files.size());
// If file has a thumbnail, cache it.
for (int i = 0; i < files.size(); ++i) {
FileTuple file = files[i];
popup.setValue(i);
QString thumbnailPath = file.getThumbnail();
if (!thumbnailPath.isEmpty()) {
QIcon thumbnail(thumbnailPath);
cached.insert(file.getId(), thumbnail);
}
}
popup.setValueToMaximum();
}
void ThumbnailManager::loadDefault()
{
// Icons made by Nysttren.
defaultImage = QIcon("default icons/image.png");
defaultVideo = QIcon("default icons/video.png");
defaultFile = QIcon("default icons/file.png");
defaultFolder = QIcon("default icons/folder.png");
}
QSize ThumbnailManager::getDefaultSize()
{
return defaultSize;
}
QIcon ThumbnailManager::getThumbnail(const FileTuple& file)
{
bool hasThumbnail = !file.getThumbnail().isEmpty();
if (hasThumbnail) {
return cached.value(file.getId());
}
else {
QString type = file.getType();
if (type == "image") {
return defaultImage;
}
else if (type == "video") {
return defaultVideo;
}
else if (type == "folder") {
return defaultFolder;
}
else {
return defaultFile;
}
}
}
void ThumbnailManager::changeThumbnail(const FileTuple& file, const QString& sourcePath)
{
QString oldPath = file.getThumbnail();
deleteThumbnail(oldPath, file.getId());
generateAndSaveThumbnail(sourcePath, file.getId());
}
void ThumbnailManager::deleteThumbnail(const QString& path, const int& fileId)
{
QFile file(path);
if (file.exists()) {
file.remove();
}
cached.remove(fileId);
}
void ThumbnailManager::generateAndSaveThumbnail(const QString& sourcePath, const int& fileId)
{
QString newPath = "thumbnails/" + QString::number(fileId) + ".jpg";
QImage image;
image.load(sourcePath);
image = image.scaled(maximumSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
image.save(newPath);
cached.insert(fileId, QIcon(QPixmap::fromImage(image)));
Database* database = Database::getInstance();
database->setThumbnail(newPath, fileId);
}