-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainWindow.cpp
More file actions
436 lines (385 loc) · 14 KB
/
mainWindow.cpp
File metadata and controls
436 lines (385 loc) · 14 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// -*- coding: utf-8 -*-
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
#include "mainWindow.h"
#include <QApplication>
#include <QLayout>
#include <QDirIterator>
#include <QDebug>
#include <QSettings>
#include <QMessageBox>
#include <QImageReader>
#include <QThreadPool>
#include <QMenu>
#include <QDir>
#include <QGraphicsOpacityEffect>
#include <QPainter>
#include <QTimer>
#include <QDebug>
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
#include <QScreen>
#endif
#include <cstdlib>
#include <random>
#include <time.h>
#include "sleepyTime.h"
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
// This loads the slide show as a background thread. The main window
// will display an image while this is loading. Otherwise, it looks
// like the program is not working while the data is getting loaded.
// The flag "_ready" is set to true when this thread is complete.
//
class LoadSlideShow : public QRunnable {
MainWindow *_win;
public:
LoadSlideShow(MainWindow *win) {
_win = win;
}
void run() override {
if (!_win->loadImagesFromDirectoryName(_win->_directory.toString())) {
QMessageBox box;
box.setText("No Images found");
box.exec();
exit(0);
}
// randomize list
if (_win->_randomMode.toBool()) {
std::random_device rng;
std::mt19937 urng( rng() );
std::shuffle(_win->_names.begin()
,_win->_names.end(), urng);
}
_win->_ready = true; // indicate we're done
}
};
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
MainWindow::MainWindow(QStringList args, QWidget *parent)
: QMainWindow(parent)
{
// read settings from ini file
QSettings settings("PhotoViewer.ini",QSettings::IniFormat);
_directory = settings.value("Directory","./photos").value<QString>();
_sleepMode = settings.value("SleepMode",_sleepMode).value<bool>();
_secondsToShowImage = settings.value("DisplayTime"
,_secondsToShowImage).value<int>();
_displayFileName = settings.value("DisplayFileName"
,_displayFileName).value<bool>();
_hideCursor = settings.value("HideCursor",_hideCursor).value<bool>();
_randomMode = settings.value("Random",_randomMode).value<bool>();
_fullscreen = settings.value("Fullscreen",_fullscreen).value<bool>();
_sqlite = settings.value("SqLite","").toString();
// check for existance of sqlite database
if (_sqlite != "" ) {
QFileInfo f(_sqlite);
if (!f.exists()) {
QMessageBox box;
box.setText(QString("SqLite file %1 Not Found")
.arg(_sqlite));
box.exec();
exit(0);
}
_usesqlite = openDatabase(_sqlite);
}
// override settings with command line options
processCommandLine();
// hide the mouse cursor
if (_hideCursor.toBool())
QApplication::setOverrideCursor(Qt::BlankCursor);
// allow drag-drops
setAcceptDrops(true);
// check for directory existance
if (!_usesqlite) {
QFileInfo f(_directory.toString());
if (!f.exists()) {
QMessageBox box;
box.setText(QString("Photo Directory %1 Not Found")
.arg(_directory.toString()));
box.exec();
exit(0);
}
}
// Set the size of the screen
setStyleSheet("QMainWindow {background: 'black';}");
if (!_fullscreen.toBool()) {
//
// hmmm... if you don't do this, Qt creates a very
// small initial window
//
QScreen* screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
resize(screenGeometry.width() * 0.75
,screenGeometry.height() * 0.75);
}
// create labels (labels hold images)
for (int i = 0; i < _numLabels; ++i) {
_label[i] = new MyLabel(this);
_label[i]->_displayFileName = _displayFileName.toBool();
_label[i]->setSizePolicy(QSizePolicy::Ignored,
QSizePolicy::Ignored);
_label[i]->setScaledContents(true);
}
// set up window
setScreenSize();
// load an initial image onto both labels while we
// wait for the list to be created
{
QString path = QDir::currentPath();
path += "/images/hello.png";
QFileInfo fileInfo(path);
if (fileInfo.exists()) {
for (int i = 0; i < _numLabels; ++i) {
_currentLabel = i;
loadImage(path);
}
}
}
// Load the initial slide show in a Thread. QThreadPool automatically
// deletes the thread when it's finished
if (!_usesqlite) {
QThreadPool::globalInstance()->start(new LoadSlideShow(this));
} else {
_ready = true;
}
_imagetimer = new QTimer;
connect(_imagetimer,&QTimer::timeout, this,
[=]() {
if (_ready) { // thread complete
showImage(); // display next image
}
});
_imagetimer->start(_secondsToShowImage.toInt() * 1000);
show();
_geometry = saveGeometry();
_opTimer = new QTimer(this);
connect(_opTimer,&QTimer::timeout, this,
[=]() {
this->setOpacity();
});
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
void MainWindow::nextImage(void)
{
#if 1
if (_usesqlite) {
QString next = _rrList.getNext();
if (next == "") return; // not enough in list
loadImage(next);
_imagetimer->start();
return;
}
#endif
#if 0
if (_usesqlite) {
showImage();
_imagetimer->start();
return;
}
#endif
if (_names.size() == 0 ) return; // single image display
++_lastN;
if (_lastN == _names.size()) {
_lastN = 0;
}
_currentN = _lastN;
showImage();
_imagetimer->start();
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
void MainWindow::prevImage(void)
{
if (_usesqlite) {
QString prev = _rrList.getPrevious();
if (prev == "") return; // not enough in rrlist
loadImage(prev);
_imagetimer->start();
return;
}
if (_names.size() == 0 ) return; // single image display
int newone = _lastN - 1;
if (newone < 0) newone = _names.size()-1;
_currentN = newone;
showImage();
_imagetimer->start();
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
bool MainWindow::loadImagesFromDirectoryName(const QString &dirName)
{
QDirIterator it(dirName, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString name = it.next();
QFileInfo info(name);
if (!info.isDir()) {
QImageReader qImageReader(name);
if (qImageReader.canRead()) {
_names.push_back(name);
}
}
}
return _names.size() > 0 ? true : false;
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
void MainWindow::showImage(void)
{
if (_pause) return;
if (!_usesqlite) {
_lastN = _currentN;
_currentN++;
if (_currentN >= _names.size()) {
// end of list reached, re-sort list with new random call
std::random_device rng;
std::mt19937 urng( rng() );
std::shuffle(_names.begin(), _names.end(), urng);
_currentN = _lastN = 0;
}
}
// move to next label (labels hold the images)
_currentLabel = 1 - _currentLabel;
// start out new image with 0 opacity and fade in
_label[_currentLabel]->setOpacity(0);
if (_usesqlite) {
QString name = (const char *)getImage();// load from database
if (loadImage(name)) {
_rrList.addItem(name);
}
} else {
loadImage(_names[_currentN]);
}
_opacity = 1.0;
_opTimer->start(100);
if (_sleepMode.toBool()) {
Sleep(_secondsToShowImage.toInt() * 1000 / 2);
}
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
bool MainWindow::loadImage( const QString &fileName)
{
MyLabel *label = _label[_currentLabel];
QImage image;
if (!image.load(fileName)) {
label->_text = QString("Cant load %1").arg(fileName);
return false;
}
//
// donn, Oct 10, 2022 - always load the file name
// into the _text variable since we can now toggle the
// display using the mouse pop up
//
int orientation = getOrientation(fileName,_datetime);
// qDebug() << "Orientation" << orientation << _datetime;
_date = _datetime.left(10);
{
QFileInfo info(fileName);
// just display the last directory + the file name
QStringList list = fileName.split('/');
int sz = list.size();
if (sz > 2) {
if (label->_displayDirectory) {
label->_text = list[sz-2] + "/" + list[sz-1];
} else {
label->_text = list[sz-1];
}
} else {
label->_text = fileName;
}
if (_date.length() > 0) {
label->_text += " | " + _date;
}
}
// rotate image if needed
QTransform rot;
switch(orientation) {
case 3: // 180 flip
rot.rotate(180);
break;
case 6: // 90 rot
rot.rotate(90);
break;
case 8: // 270 rot
rot.rotate(-90);
break;
default:
break;
}
//
// Perform the Qt smoothing algorithm and apply
// the pixmap to the label.
//
// This really is only necessary for images that have not been pre-scaled
// to the screen size
//
if (image.width() > width()) {
label->setPixmap(QPixmap::fromImage(image)
.transformed(rot).scaled(QSize(width(),height())
,Qt::KeepAspectRatio,
Qt::SmoothTransformation));
} else {
label->setPixmap(QPixmap::fromImage(image).transformed(rot));
}
resizeLabel(label);
return true;
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
void MainWindow::resizeLabel(MyLabel *label)
{
if (label != nullptr) {
#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
float h = label->pixmap().height();
float w = label->pixmap().width();
#else
auto pm=label->pixmap(Qt::ReturnByValue);
float h = pm.height();
float w = pm.width();
#endif
float scrWidth = width();
float scrHeight = height();
if (h < 0.01f ||
w < 0.01f ||
scrHeight < 0.01f ||
scrWidth < 0.01f) return; // prevent divide by zero
float scaleH = scrHeight / h;
float scaleW = scrWidth / w;
float scale = scaleH;
if (scaleW < scaleH) {
scale = scaleW;
}
label->resize(w * scale,h * scale);
float spaceLeftW = (scrWidth - w * scale) / 2.f;
float spaceLeftH = (scrHeight - h * scale) / 2.f;
label->move(spaceLeftW,spaceLeftH);
}
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
void MainWindow::setScreenSize(void)
{
if (_fullscreen.toBool()) {
QScreen* screen = QGuiApplication::primaryScreen();
if (screen) {
QRect screenGeometry = screen->geometry();
resize(screenGeometry.width()
,screenGeometry.height());
}
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
move(0,0);
} else {
setWindowFlags(windowFlags() & ~Qt::FramelessWindowHint);
}
show();
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
MainWindow::~MainWindow()
{
for (int i = 0; i < _numLabels; ++i) {
delete _label[i];
}
delete _imagetimer;
}
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
void MainWindow::setOpacity(void)
{
if (_opacity <= 0.0) { // stop fading
_opTimer->stop();
} else {
_label[1-_currentLabel]->setOpacity(_opacity); // fade out last
_label[_currentLabel]->setOpacity(1-_opacity); // fade in current
}
_opacity -= 0.1;
}