-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageProvider.cpp
executable file
·263 lines (217 loc) · 6.18 KB
/
ImageProvider.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
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
/*
Copyright (c) 2017-2019 by Ilya Barykin
Released under the MIT License.
See the provided LICENSE.TXT file for details.
*/
#include "ImageProvider.h"
#include <QtConcurrent/QtConcurrent>
#include <vendor/verdigris/src/wobjectimpl.h>
ImageProvider::ImageProvider(Window *w, QString dir) : wnd(w) {
io = new PIO(this);
files = new QStringList;
imgLoader = new ImageLoader(this);
gifLoader = new GifLoader(this);
mutex = new QMutex;
connect(this, &ImageProvider::titleChanged, wnd, &Window::titleChanged, Qt::QueuedConnection);
QFile f("/home/ilya/.myQtProgs/photos_last.json");
if (f.open(QFile::ReadOnly)) {
lastPicture = QJsonDocument::fromJson(f.readAll()).object();
f.close();
}
if (dir == "") {
io->setNewCurrentDir();
} else {
io->setCurrentDir(dir);
loadDir();
}
}
void ImageProvider::keyPressEvent(int i) {
switch (i) {
case 16777234:
prev(1);
break;
case 16777236:
next(1);
break;
case 91 :
prev(100);
break;
case 93 :
next(100);
break;
case 16777223:
del();
break;
case 32 :
cop();
break;
case 69 :
exp();
break;
case 66 :
toggleBlur();
break;
case 44 :
loadNewDir(-1);
break;
case 46 :
loadNewDir(1);
break;
case 71 :
case 1055 :
goTo();
break;
default :
qDebug() << i;
break;
}
}
void ImageProvider::closeEvent() {
if (!files->empty()) {
lastPicture.insert(getDir(files->value(0)), current);
}
saveLastPicture();
}
void ImageProvider::saveLastPicture() const {
QFile f("/home/ilya/.myQtProgs/photos_last.json");
if (f.open(QIODevice::WriteOnly)) {
f.write(QJsonDocument(lastPicture).toJson());
f.close();
}
}
// Images loading/viewing
void ImageProvider::next(int i) {
if (!isLoading) {
current = current < files->size() - i ? current + i : 0;
loadImage();
}
}
void ImageProvider::prev(int i) {
if (!isLoading) {
current = current >= i ? current - i : files->size() - i;
current = current >= 0 ? current : files->size() - 1;
loadImage();
}
}
void ImageProvider::loadImage(bool rel) {
QString file = files->value(current);
if (!files->empty()) {
if (!rel && !files->empty()) {
lastPicture.insert(getDir(files->value(0)), current);
saveLastPicture();
}
gifLoader->stop();
if (file.endsWith(".gif")) {
gifLoader->loadImage(file);
} else {
if (!isLoading)
QtConcurrent::run(imgLoader, &ImageLoader::loadImage, file);
}
} else {
setText("");
QImage noImage;
setOriginal(noImage);
setScale(1);
setOriginalScale(1);
setScaled(noImage);
}
}
void ImageProvider::goTo() {
current = QInputDialog::getInt(wnd, "Go to", "Go to:", current, 0, files->size()) - 1;
loadImage();
}
void ImageProvider::toggleBlur() {
if (loaderType == 0) {
imgLoader->toggleBlur();
loadImage(true);
}
}
// IO
bool ImageProvider::isImg(QString s) {
return s.endsWith(".png") || s.endsWith(".jpg") || s.endsWith(".jpeg") || s.endsWith(".gif");
}
void ImageProvider::loadDir() {
files->clear();
QString c = io->getCurrentDir();
c = c.replace("\\", "/"); // \/ Dir-only
io->setCurrentDir(isImg(c) ? QStringRef(&c, 0, c.lastIndexOf('/')).toString() : (c.endsWith('/') ? c : c + '/'));
for (const QFileInfo &i : QDir(io->getCurrentDir()).entryInfoList({"*.png", "*.jpg", "*.jpeg", "*.gif"},
QDir::Files)) {
files->append(i.absolutePath() + '/' + i.fileName());
}
if (isImg(c)) {
c = c.remove(0, c.lastIndexOf('/') + 1); // File-name only
for (int i = 0; i < files->size(); i++)
if (files->value(i, "").endsWith(c))
current = i;
} else if (lastPicture.contains(getDir(io->getCurrentDir()))) {
current = lastPicture.value(getDir(io->getCurrentDir())).toInt(0);
current = current < files->size() ? current : 0;
} else {
current = 0;
}
if (io->getCurrentDir() != "")
loadImage();
}
void ImageProvider::del() {
io->del(files->value(current));
files->removeAt(current);
loadImage();
}
void ImageProvider::cop() {
io->cop(files->value(current));
files->removeAt(current);
loadImage();
}
void ImageProvider::exp() {
io->exp(files->value(current));
next(1);
}
// Setters
void ImageProvider::setText(QString t) {
this->text = t;
emit titleChanged(t);
}
void ImageProvider::setLoading(bool b) {
isLoading = b;
if (!isLoading)
wnd->resetCoord(),
wnd->update();
}
void ImageProvider::setOriginalScale(float f) {
wnd->original_scale = f;
}
void ImageProvider::setScale(float f) {
wnd->scale = f;
}
void ImageProvider::setOriginal(QImage &i) {
QMutexLocker m(mutex);
this->iOriginal = i;
wnd->update();
}
void ImageProvider::setScaled(QImage &i) {
QMutexLocker m(mutex);
this->iScaled = i;
wnd->update();
}
void ImageProvider::loadNewDir(int inc) {
if (isLoading)
return;
QString curr = io->getCurrentDir(), sDir = curr;
curr = sDir.remove(sDir.length() - 1, 1); // Delete last slash
sDir = sDir.remove(sDir.lastIndexOf('/'), 100); // Delete all before last slash
QFileInfoList entr = QDir(sDir).entryInfoList({"*"}, QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
int ind = 0;
for (int i = 0; i < entr.size(); i++) {
if (curr == entr[i].absoluteFilePath()) {
ind = i;
}
}
ind += inc;
ind = ind < 0 ? 0 : ind;
ind = ind > entr.length() - 1 ? entr.length() - 1 : ind;
sDir = entr[ind].absoluteFilePath();
io->setCurrentDir(sDir + '/');
loadDir();
}
W_OBJECT_IMPL(ImageProvider)