-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
510 lines (439 loc) · 14.5 KB
/
mainwindow.cpp
File metadata and controls
510 lines (439 loc) · 14.5 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "settings_dialog.h"
#include <iostream>
#include <QKeyEvent>
#include <QTimer>
#include <QMovie>
#include <QScrollBar>
#include <time.h>
/* OPEN ISSUES:
* > if settings file not found, program crashes. Should create one by default
* > if settings file doesn't have a blank line at the end file, program crashes.
* > It gets confusing to retrive the total number of files/imgs in a directory.
* confused this->imgDir->dir->count() several times with this->imgDir->fileList.count()
* the first one is incorrect as it gives the total number of files in the directory
* wether they are imgs or not.
*/
/* Flow
* main (not this file) creates Main window and CALLS(must) Initload
* execution order:
* MainWindow(constructor)
* initLoad()
* settings.(load)
* loadDir() // loads index.txt
* setImg()
*
*/
/* How to add a new parameter:
* 1) create define str in program_settings.h
* 2) create Ui elements in settings_diaglog.ui
* 3) in settings_dialog.cpp:
* -modify loadSettings to set the current values in the UI when shown
* -modify on_button_accepted()
* to update the paramater value modified by the user.
* IMPORTANT: this will only update the settings structure and file,
* no instant changes in the running application will be made.
* 4) in mainwindow.cpp:
* -modify on_ConfigureButton_triggered() to do the proper actions
* for the updated values to take effect.
*/
/* Feature: Random Order.
* Images are displayed randomly, but an image is not shown twice unless a full cycle is done.
* MainWindow has a vector called randIndex.
* It is initialized using resetRandIndex() wich construct the vector with values
* from 0 to total elements in the fileList.
* Randomly takes one when getRandIndex() is called.
* resentRandIndex() is called and initialized at loadDir(), which also happen to reload the list whenever
* a dir is changed.
* Also if getRandIndex() retrives all elements, it resets the vector.
* /
/* The constructor initializes a lot of important stuff,
* maybe this should be somewhere else? */
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
st = new Settings_dialog(this);
this->timer = new QTimer(this);
/* connect the signal/timer for the slide show feature */
connect(timer,SIGNAL(timeout()),this, SLOT(on_nextButton_clicked()));
st->program_settings = &this->program_settings;
// this 'show' is to refresh the values of ui->scrollArea->width/height,
// critical when showing the first image.
this->show();
hideMenu = false;
scaleImg = true;
loadIndex = true;
hasCustomImg = false;
imgdir.dirExist = false;
showDebug = false;
//ui->imgLabel = new ClickableLabel(ui->scrollAreaWidgetContents);
time_t timer;
time(&timer);
qsrand(timer);
ui->imgLabel->parentScrollArea = ui->scrollArea;
ui->imgLabel->parentMainWindow = this;
ui->imgLabel->testLabel = ui->testLabel;
ui->debugTextViewer->hide();
ui->testLabel->hide();
this->coutdebug();
}
void MainWindow::coutdebug()
{
if(!showDebug)
{
dbg.str("");
return;
}
if(dbg.str().size() > 0)
ui->debugTextViewer->append(dbg.str().c_str());
dbg.str("");
}
void MainWindow::initialLoad()
{
this->statusBar()->hide();
slideShowActive = 0;
int x_win_offset;
int y_win_offset;
execution_path.replace("\\","/",Qt::CaseSensitive);
program_settings.path = execution_path.toStdString();
program_settings.path.append("/settings");
dbg << program_settings.path << std::endl;
program_settings.load();
program_settings.dump();
this->resize(1900,1050);
x_win_offset = program_settings.retrive_as_int(X_WIN_OFFSET);
y_win_offset = program_settings.retrive_as_int(Y_WIN_OFFSET);
this->move(x_win_offset, y_win_offset);
this->random = program_settings.retrive_as_int(RANDOM_SETT);
this->imgdir.orderBy = program_settings.retrive_as_int(ORDER_BY_SETT);
this->showDebug = program_settings.retrive_as_int(SHOW_DEBUG);
if(hasCustomImg)
{
ui->imgLabel->setText(customImg);
this->program_settings.modify(DIR1_SETT,QFileInfo(customImg).absolutePath().toStdString());
}
if(showDebug)
{
ui->debugTextViewer->show();
ui->testLabel->show();
}
loadDir();
coutdebug();
}
void MainWindow::resetRandIndex()
{
randIndex.clear();
for (int var = 0; var < this->imgdir.fileList.count(); ++var)
{
randIndex.push_back(var);
dbg << "add:"<< var<< std::endl;
}
coutdebug();
}
int MainWindow::getRandIndex()
{
int pos,index;
if(1 == randIndex.count())
{
resetRandIndex();
return 0;
}
int randN = qrand();
pos = randN%randIndex.count();
dbg << "randN: " << randN << std::endl;
dbg << "randPos: " << pos << std::endl;
index = randIndex.at(pos);
dbg << "randIndex: " << index << std::endl;
randIndex.remove(pos);
coutdebug();
return index;
}
void MainWindow::changeSideBar()
{
QLayoutItem *tmp = this->ui->horizontalMainLayout->layout()->takeAt(0);
this->ui->horizontalMainLayout->layout()->addItem(tmp);
tmp = this->ui->scrollAreaWidgetContents->layout()->takeAt(0);
this->ui->scrollAreaWidgetContents->layout()->addItem(tmp);
}
void MainWindow::loadDir()
{
imgdir.fileList.clear();
imgdir.load(program_settings.retrive(DIR1_SETT).c_str());
/* Resolve Index */
if(hasCustomImg && imgdir.dirExist)
{
QString tmp, name = QFileInfo(customImg).fileName();
for (index = 0; index < imgdir.fileList.count(); index++)
{
tmp = QFileInfo(imgdir.fileList.at(index)).fileName();
if(name == tmp)
break;
}
dbg << "found that index is: " << index << std::endl;
}
else if(loadIndex)
{
index = imgdir.loadIndex(imgdir.buildPath(
program_settings.retrive(DIR1_SETT),INDEX_FILE_NAME).c_str());
// Bug when contents change and index doesn't match,
// crash if index > total imgs
if(index > imgdir.fileList.count())
index = 0;
}
else
{
index = 0;
}
resetRandIndex();
this->setImg();
coutdebug();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_ConfigureButton_triggered()
{
dbg << "configure" << std::endl;
st->loadSettings(this->program_settings);
st->exec();
if(st->dirModified)
{
this->hasCustomImg = false;
st->dirModified = false;
this->imgdir.orderBy = st->ui->orderByComboBox->currentIndex();
loadDir();
}
this->random = program_settings.retrive_as_int(RANDOM_SETT);
this->showDebug = program_settings.retrive_as_int(SHOW_DEBUG);
if(showDebug)
{
ui->debugTextViewer->show();
ui->testLabel->show();
}
else
{
ui->debugTextViewer->hide();
ui->testLabel->hide();
}
coutdebug();
}
void MainWindow::on_Configure2_triggered()
{
st2.show();
}
void MainWindow::adjustScrollArea()
{
/* ***** FEATURE: Center the image in the scroll area ****
*
* Hard to explain, but there is a relation between the size of the image,
* tha area it can display, and the scroll bar size.
* think about this:
* if the image is height 1080, and the window size is 1000(actually the scroll area),
* then the scroll bar will almost fill the bar area,
* the space the bar doesn't cover will represent the 80 remaining pixels
* it cant show, then to move the bar to the middle you need to move it to 40.
*/
const QPixmap *imgtemp = ui->imgLabel->pixmap();
if(!imgtemp)
return;
ui->scrollArea->horizontalScrollBar()->setMaximum(imgtemp->width() - ui->scrollArea->width());
ui->scrollArea->verticalScrollBar()->setMaximum(imgtemp->width() - ui->scrollArea->width());
// ui->scrollArea->horizontalScrollBar()->setMinimum(0);
// ui->scrollArea->verticalScrollBar()->setMinimum(0);
ui->scrollArea->verticalScrollBar()->setValue(
(int)(imgtemp->height() - ui->scrollArea->height())/2);
ui->scrollArea->horizontalScrollBar()->setValue(
(int)(imgtemp->width() - ui->scrollArea->width())/2);
// dbg << "scroll area height: "<< ui->scrollArea->height() << std::endl;
// dbg << "scroll area width: "<< ui->scrollArea->width() << std::endl;
coutdebug();
}
void MainWindow::setImg()
{
if(!this->imgdir.dirExist || this->imgdir.fileList.length() == 0)
{
QPixmap empty;
ui->imgLabel->setPixmap(empty);
if(!this->imgdir.dirExist)
ui->imgLabel->setText("Path is invalid, is not a directory?");
else
ui->imgLabel->setText("Couldn't find any image in the directory");
return;
}
// in case index is outbounds, should not happen
if(index > imgdir.fileList.count())
return;
QString temp = imgdir.dir.absoluteFilePath(imgdir.fileList.value(index));
dbg << imgdir.fileList.value(index).toStdString().c_str() << std::endl;
dbg << temp.toStdString().c_str() << std::endl;
dbg << index << std::endl;
QPixmap imgtemp;
imgtemp.load(temp.toStdString().c_str());
if(scaleImg)
{
if(imgtemp.height() > program_settings.retrive_as_int(MAX_HEIGHT_SETT))
imgtemp = imgtemp.scaledToHeight(
program_settings.retrive_as_int(MAX_HEIGHT_SETT),
Qt::SmoothTransformation);
if(imgtemp.width() > program_settings.retrive_as_int(MAX_WIDTH_SETT))
imgtemp = imgtemp.scaledToWidth(
program_settings.retrive_as_int(MAX_WIDTH_SETT),
Qt::SmoothTransformation);
}
if(temp.endsWith(".gif",Qt::CaseInsensitive) ||
temp.endsWith(".webm",Qt::CaseInsensitive)) // not yet support for webm
{
QMovie *movie = new QMovie(temp.toStdString().c_str());
ui->imgLabel->setMovie(movie);
movie->start();
dbg << "movie" << std::endl;
}else
{
ui->imgLabel->setPixmap(imgtemp);
}
adjustScrollArea();
imgdir.saveIndex(imgdir.buildPath(
program_settings.retrive(DIR1_SETT),"i.txt").c_str(),
index);
updateFileName();
coutdebug();
}
void MainWindow::nextImg()
{
index++;
if(index > imgdir.fileList.count() - 1)
index = 0;
this->scaleImg = true;
if(random)
index = getRandIndex();
setImg();
}
void MainWindow::prevImg()
{
index--;
if(index < 0)
index = imgdir.fileList.count() - 1;
this->scaleImg = true;
if(random)
index = getRandIndex();
setImg();
}
void MainWindow::on_nextButton_clicked()
{
nextImg();
}
void MainWindow::on_prevButton_clicked()
{
prevImg();
}
void MainWindow::keyPressEvent(QKeyEvent * qkey)
{
QString file1,file2;
switch(qkey->key())
{
case Qt::Key_Right:
case Qt::Key_D:
nextImg();
break;
case Qt::Key_Left:
case Qt::Key_A:
prevImg();
break;
case Qt::Key_S:
file1 = imgdir.buildPath(
program_settings.retrive(DIR1_SETT),
imgdir.fileList.at(index).toStdString()).c_str();
file2 = imgdir.buildPath(
program_settings.retrive(DIR2_SETT),
imgdir.fileList.at(index).toStdString()).c_str();
dbg << file1.toStdString() << std::endl;
dbg << file2.toStdString() << std::endl;
imgdir.copyImg(file1,file2);
this->setWindowTitle(this->windowTitle().append(" (COPIED)"));
break;
case Qt::Key_W:
file1 = imgdir.buildPath(
program_settings.retrive(DIR1_SETT),
imgdir.fileList.at(index).toStdString()).c_str();
file2 = imgdir.buildPath(
program_settings.retrive(DIR2_SETT),
imgdir.fileList.at(index).toStdString()).c_str();
dbg << file1.toStdString() << std::endl;
dbg << file2.toStdString() << std::endl;
imgdir.moveImg(file1,file2);
imgdir.fileList.removeAt(index--);
nextImg();
break;
case Qt::Key_Space:
on_slideShowStart_clicked();
break;
default:
break;
}
coutdebug();
}
void MainWindow::on_slideShowStart_clicked()
{
slideShowActive = !slideShowActive;
if(slideShowActive)
{
this->ui->slideShowStart->setText("Slide Stop");
int time = jarr::Attribute::to_number(
program_settings.retrive(TIME_SETT));
timer->start(time*1000);
}
else
{
this->ui->slideShowStart->setText("Slide Start");
timer->stop();
}
coutdebug();
}
void MainWindow::updateFileName()
{
std::stringstream new_title;
new_title << imgdir.fileList.at(index).toStdString()
<< " ( " << jarr::Attribute::to_string(index).c_str()
<< " / " << imgdir.fileList.count() << " )";
this->ui->menuFilename->setTitle(new_title.str().c_str());
this->setWindowTitle(new_title.str().c_str());
coutdebug();
}
void MainWindow::on_hideMenuButton_clicked()
{
hideMenu = !hideMenu;
if(hideMenu)
{
this->ui->hideMenuButton->setText("Show Menu");
this->ui->menuBar->hide();
}
else
{
this->ui->hideMenuButton->setText("Hide Menu");
this->menuBar()->show();
}
coutdebug();
}
void MainWindow::on_imgLabel_clicked()
{
dbg << "click img" << std::endl;
this->scaleImg = !this->scaleImg;
setImg();
coutdebug();
}
void MainWindow::on_ChangeSideBar_clicked()
{
changeSideBar();
}
void MainWindow::resizeEvent(QResizeEvent *)
{
QPixmap *imgtemp = (QPixmap*)ui->imgLabel->pixmap();
if(!imgtemp)
return;
ui->imgLabel->setPixmap(*imgtemp);
adjustScrollArea();
}