-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmywindows.cpp
372 lines (324 loc) · 11.1 KB
/
mywindows.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "mywindows.h"
#include <QFileIconProvider>
myWindows::myWindows(QWidget *parent) : QWidget(parent) {
// Getting size of the screen
QList<QScreen *> screenObj = QGuiApplication::screens();
screen = screenObj.at(0);
int sizeCol;
screenH = screen->geometry().height();
screenW = screen->geometry().width();
sizeCol = 150;
sizePreviewH = 512;
sizePreviewW = 512;
int minPrev = screenH - sizeCol;
if (minPrev < sizePreviewH) {
sizePreviewH = minPrev;
sizePreviewW = minPrev;
}
isShiftOn = false;
// Globale layout
// _____Vlayout______
// | |
// | ___HLayout___ |
// | | | |
// | | preview | |
// | | | |
// | | file info| |
// | |____________| |
// | |
// | column view |
// |________________|
//
layoutGlobal = new QVBoxLayout;
layoutGlobal->setAlignment(Qt::AlignCenter);
// Gloval preview
preview = new imagePreview(this);
// Preview file part
layoutPreview = new QHBoxLayout;
lab = new QLabel("image ici");
lab->setMaximumHeight(sizePreviewH - 110);
imDef = QPixmap(":/images/test.png");
lab->setPixmap(imDef);
info = new fileInfo;
// Might prevent freeze
QFileIconProvider *fileIconProvider = new QFileIconProvider();
fileIconProvider->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
// Column view part
model = new QFileSystemModel(this);
model->setRootPath(QDir::rootPath());
model->setResolveSymlinks(true);
model->setReadOnly(false);
model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs |
QDir::System);
model->setIconProvider(fileIconProvider);
// Loading preferences
loadSettings();
columnView = new QColumnView(this);
columnView->setMinimumHeight(sizeCol);
columnView->setModel(model);
// tree->setRootIndex(model->index(QDir::currentPath()));
columnView->setCurrentIndex(model->index(lastPath));
// columnView->setRootIndex());
QItemSelectionModel *itSel = columnView->selectionModel();
// Adding rename
QPushButton *rename = new QPushButton("Rename");
// All the thread deletion part
QThreadPool::globalInstance()->setMaxThreadCount(1);
toDelete = new QList<QString>;
QHBoxLayout *spin_and_delete = new QHBoxLayout;
spin_and_delete->setAlignment(Qt::AlignLeft);
deleteStatus = new QLabel("");
spinBox = new QSpinBox();
spinBox->setMaximum(5);
spinBox->setMinimum(0);
spinBox->setValue(MAX_DEPTH);
// Keyboard
// global space shortcut
shortcutSpace = new QShortcut(QKeySequence(Qt::Key_Space), this);
shortcutSpace->setContext(Qt::ApplicationShortcut);
// global enter shortcut
shortcutEnter = new QShortcut(QKeySequence(Qt::Key_Return), this);
// shortcutEnter->setContext(Qt::ApplicationShortcut);
// Global Supr Shortcut
shortcutDel = new QShortcut(QKeySequence(Qt::Key_Delete), this);
shortcutDel->setContext(Qt::ApplicationShortcut);
// Qconnect
QObject::connect(shortcutSpace, SIGNAL(activated()), this,
SLOT(keyboardEvent()));
QObject::connect(shortcutEnter, SIGNAL(activated()), this,
SLOT(keyboardEnter()));
QObject::connect(shortcutDel, SIGNAL(activated()), this, SLOT(keyboardDel()));
// Listen to qColumnView click
// Selection of a file
QObject::connect(itSel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
this, SLOT(clickedNew(QModelIndex, QModelIndex)));
QObject::connect(rename, SIGNAL(clicked()), this, SLOT(rename()));
QObject::connect(spinBox, SIGNAL(valueChanged(int)), this,
SLOT(depthChanged(int)));
//
spin_and_delete->addWidget(new QLabel("Preview depth"));
spin_and_delete->addWidget(spinBox);
spin_and_delete->addWidget(deleteStatus);
// Adding
layoutPreview->addWidget(lab);
layoutPreview->addWidget(info);
layoutGlobal->addLayout(layoutPreview);
layoutGlobal->addWidget(columnView);
layoutGlobal->addWidget(rename);
layoutGlobal->addLayout(spin_and_delete);
// Get event even if not in front
eater = new KeyPressEater(this);
preview->installEventFilter(eater);
this->setLayout(layoutGlobal);
this->resize(1024, 900);
this->show();
}
// Update variable last*Path AND if shift is on remember all selected files
void myWindows::updatePath(QModelIndex index) {
lastFilePath = model->filePath(index);
QFileInfo infoFile(lastFilePath);
lastPath = infoFile.canonicalPath();
if (isShiftOn) {
shiftList.append(lastFilePath);
} else {
shiftList.clear();
shiftList.append(lastFilePath);
}
}
// The actionb called by the column view when the user do something
void myWindows::clickedNew(QModelIndex index, QModelIndex) {
updatePath(index);
QString fileName = model->fileName(index);
QFileInfo infoFile(lastFilePath);
QString ext = fileName.split(".")
.back(); // We could use here QFileInfo::completeSuffix()
int SIZE_NAME_MAX = 50;
if (fileName.length() > SIZE_NAME_MAX) {
info->setName(fileName.mid(0, SIZE_NAME_MAX));
} else {
info->setName(fileName);
}
if (ext.length() == 1 || ext.length() >= 5) {
info->setType("Not a standard file");
} else {
info->setType(ext.toLower());
}
info->setSize(int(model->size(index)));
info->setResolution(0, 0);
// If it's an image we update the previews and the informations
QString lowExt = ext.toLower();
if (infoFile.isFile() && isImage(lowExt)) {
updateImage();
} else if (infoFile.isDir()) {
// If there is an image inside we try to show it
bool found = parseFolderAndUpdate(lastFilePath, MAX_DEPTH);
// else we show the default image if no file is an image
if (!found) {
lab->setPixmap(imDef);
}
} else {
// else we show the default image
lab->setPixmap(imDef);
}
}
bool myWindows::parseFolderAndUpdate(QString path, int depth) {
if (depth < 0) {
return false;
}
QDir dir = QDir(path);
dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
QString lowExt;
QFileInfoList list = dir.entryInfoList();
// qDebug() << depth << path << list.size();
bool found = false;
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
lowExt = fileInfo.suffix().toLower();
if (fileInfo.isFile() && isImage(lowExt)) {
updateImage(fileInfo.absoluteFilePath());
found = true;
} else if (fileInfo.isDir() && depth > 0) {
// qDebug() << "Deeper" << fileInfo.absoluteFilePath();
found = parseFolderAndUpdate(fileInfo.absoluteFilePath(), depth - 1);
}
if (found) {
break;
}
}
return found;
}
bool myWindows::isImage(QString suffix) {
QString lowSuffix = suffix.toLower();
return (lowSuffix == "jpg" || lowSuffix == "jpeg" || lowSuffix == "png" || lowSuffix == "webp");
}
void myWindows::updateImage() { updateImage(lastFilePath); }
void myWindows::updateImage(QString image) {
lastImagePath = QString(image); // For later in case of fullscreen
QPixmap imtmp(image);
if (imtmp.isNull()) { // in the case someone give a bad extension (png instead
// of jpg)...
imtmp = imDef;
}
QPixmap imtmp2 = imtmp.scaledToHeight(sizePreviewH, Qt::SmoothTransformation);
if (imtmp2.width() > sizePreviewW) {
lab->setPixmap(imtmp2.copy(0, 0, sizePreviewW, sizePreviewH));
} else {
lab->setPixmap(imtmp2);
}
info->setResolution(imtmp.width(), imtmp.height());
preview->updateImage(imtmp);
}
// Function to watch the global shortcut SPACE that is for showing preview
void myWindows::keyboardEvent() {
// qDebug() << "SPACE ";
if (preview->showing) {
preview->hidePreview();
} else {
if (lastImagePath != nullptr) {
preview->showImage(lastImagePath);
preview->activateWindow();
}
}
}
// Function to watch the global shortcut SPACE that is for opening the file with
// default app
void myWindows::keyboardEnter() {
// qDebug() << "ENTER ";
QDesktopServices::openUrl(QUrl::fromLocalFile(lastFilePath));
}
// Debug funtion to show all keyboard event
void myWindows::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Shift) {
// qDebug() << "Key Shift";
isShiftOn = true;
}
}
void myWindows::rename() {
bool ok;
QString text = QInputDialog::getText(this, tr("Renamming"), tr("base name:"),
QLineEdit::Normal, "", &ok);
int num = 0;
if (ok) {
for (int var = 0; var < shiftList.length(); ++var) {
_rename(shiftList.at(var), text, &num);
}
}
}
void myWindows::depthChanged(int newValue) { MAX_DEPTH = newValue; }
void myWindows::_rename(QString path, QString newName, int *num) {
QFileInfo tmp(path);
if (tmp.isFile()) {
QFile file(path);
QString newConstructedName =
tmp.canonicalPath() + QDir::separator() + newName;
// If the name if something XXX-01 else 01
if (newName != "") {
newConstructedName += "-";
}
if (*num >= 10) {
if (*num < 100) {
newConstructedName += QString("0");
}
} else {
newConstructedName += QString("00");
}
newConstructedName += QString::number(*num);
// if the file had an extension we keep it else nothing
// prev.jpg -> XXX-01.jpg
if (tmp.completeSuffix() != "") {
newConstructedName += "." + tmp.completeSuffix();
}
file.rename(newConstructedName);
*num = *num + 1;
} else if (tmp.isDir()) {
// If we have a dir we get folders and files inside and try to rename them
QDir fold(path);
QStringList elmts =
fold.entryList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
for (int var = 0; var < elmts.length(); ++var) {
_rename(path + QDir::separator() + elmts.at(var), newName, num);
}
}
}
void myWindows::keyReleaseEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Shift) {
// qDebug() <<"You Release Key " <<event->text();
isShiftOn = false;
}
}
void myWindows::loadSettings() {
QSettings settings("IntCorpLightAssociation", "FileViewer");
lastPath = settings.value("lastPath").toString();
MAX_DEPTH = settings.value("depthMax").toInt();
}
void myWindows::saveSettings() {
QSettings settings("IntCorpLightAssociation", "FileViewer");
settings.setValue("lastPath", lastPath);
settings.setValue("depthMax", MAX_DEPTH);
}
int myWindows::canDelete() {
QMessageBox box;
box.setText("Selected files/folders will be eternally deleted !!");
box.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
box.setWindowFlags(Qt::WindowStaysOnTopHint);
box.setDefaultButton(QMessageBox::Ok);
return box.exec();
}
void myWindows::keyboardDel() {
// If oldDelete i.e rm -r, ask for confirmation else move to bin
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
int ret = canDelete();
#else
// New way, we can "delete" all the time
int ret = QMessageBox::Ok;
#endif
if (ret == QMessageBox::Ok) {
deletetask *task = new deletetask(shiftList, deleteStatus, toDelete);
QThreadPool::globalInstance()->start(task);
}
}
// To add coloration to folders/files
// http://stackoverflow.com/questions/1397484/custom-text-color-for-certain-indexes-in-qtreeview
// When the app is closed we saved what is necessary to save
void myWindows::closeEvent(QCloseEvent *) { saveSettings(); }
myWindows::~myWindows() { delete eater; }