-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
executable file
·1406 lines (1171 loc) · 49.2 KB
/
mainwindow.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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "thumbnail.h"
#include "dialogcloudsetup.h"
#include "emaildialog.h"
#include <QDebug>
#include <QFileDialog>
#include <QGraphicsItem>
#include <QPrintPreviewDialog>
#include <QMessageBox>
#include <QDirModel>
#include <QInputDialog>
#include <QDesktopServices>
#include <QUrl>
#include <QtConcurrent/QtConcurrent>
#include <QBitmap>
#include <qts3.h>
#ifndef DEFAULT_DIR
// Expected to be a subdirectory under the home directory
#define DEFAULT_DIR "Pictures"
#endif
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
fileModel(NULL),
fileSelection(NULL),
fileThumbnail(NULL),
changeEnable(false),
loadImagesDisabled(false),
previewWindow(new PreviewWindow(this)),
adminMode(false),
adminPassword(""),
dirName(""),
deselectInProcess(false)
{
// Start the timer so we can keep track of execution times.
// qDebug() << __FILE__ << __FUNCTION__ << "Starting mainwindow timer" << timer1.restart();
// Initialize so we can access the settings
settings = new QSettings(QString("SantaShip"),QString("SantaShip"));
// qDebug() << __FILE__ << __FUNCTION__ << settings->fileName();
// Start the ui engine
ui->setupUi(this);
// Put the build date time and version into the labelVersion
setVersionLabel(0,0);
// Setup the right click actions that we will add to the buttons
actionDeletePictures = new QAction(tr("Delete Selected"),this);
connect(actionDeletePictures, SIGNAL(triggered()), this, SLOT(OnDeletePictures()));
actionArchivePictures = new QAction(tr("Archive Selected"),this);
connect(actionArchivePictures, SIGNAL(triggered()), this, SLOT(OnArchive()));
signalMapperPrinterRemove = new QSignalMapper(this);
connect(signalMapperPrinterRemove, SIGNAL(mapped(int)), this, SLOT(OnPrinterRemove(int)));
signalMapperPrinterSettings = new QSignalMapper(this);
connect(signalMapperPrinterSettings, SIGNAL(mapped(int)), this, SLOT(OnPrinterSettings(int)));
// Setup a list of what files we can process
QStringList filterList;
filterList << "*.jpg";
filterList << "*.bmp";
filterList << "*.png";
filterList << "*.gif";
filterList << "*.jpeg";
filterList << "*.pbm";
filterList << "*.tiff";
// Define the thumbnail provider
thumbnailTimer = new QTimer();
fileThumbnail = new QFileThumbnailProvider();
// Connect the thumbnail timer to force a reload if not events for that amount of time
connect(thumbnailTimer, SIGNAL(timeout()), this, SLOT(OnThumbnailTimeout()));
// Initialize the model of the file system
fileModel = new QFileSystemModel(this);
fileModel->setNameFilters(filterList);
fileModel->setFilter(QDir::Files);
fileModel->setNameFilterDisables(false);
fileModel->sort(3);
// Start sending directory updates to the various windows
connect(fileModel, SIGNAL(directoryLoaded(QString)), this, SLOT(OnDirLoaded(QString)));
// connect(fileModel, SIGNAL(layoutAboutToBeChanged()), this, SLOT(genIconsStart()));
// connect(fileModel, SIGNAL(layoutChanged()), this, SLOT(genIconsDone()));
// connect(fileModel, SIGNAL(directoryLoaded(QString)), previewWindow, SLOT(OnDirLoaded(QString)));
// Initialize the list view to display the file system model
// qDebug() << __FILE__ << __FUNCTION__ << "Setup List View:" << timer1.restart();
// ui->listView->setAcceptDrops(false);
// ui->listView->setDragEnabled(false);
// ui->listView->setDragDropMode(QAbstractItemView::NoDragDrop);
ui->listView->addAction(actionDeletePictures);
ui->listView->addAction(actionArchivePictures);
ui->listView->setContextMenuPolicy(Qt::ActionsContextMenu);
ui->listView->setIconSize(QSize(250,150));
// Setup the class to track selections on the list view
fileSelection = new QItemSelectionModel(fileModel);
connect(fileSelection, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(OnSelectionChanged(QItemSelection,QItemSelection)));
// Setup the graphics scene to paint the images in
graphicsScene = new QGraphicsScene(this);
// Setup the mapper to map the printer buttons as they are added
signalMapperPrint = new QSignalMapper(this);
connect(signalMapperPrint, SIGNAL(mapped(int)), this, SLOT(OnPrint(int)));
// Setup the mapper to map the layout buttons as they are added
signalMapperLayout = new QSignalMapper(this);
connect(signalMapperLayout, SIGNAL(mapped(QWidget*)), this, SLOT(OnLayout(QWidget*)));
// Setup the Layout buttons.
loadLayouts();
// Setup the overlay location and scale combo's
ui->comboBoxOverlayLocation->addItem("Bottom Right",OVERLAY_BOTTOMRIGHT);
ui->comboBoxOverlayLocation->addItem("Bottom Left",OVERLAY_BOTTOMLEFT);
ui->comboBoxOverlayLocation->addItem("Top Left",OVERLAY_TOPLEFT);
ui->comboBoxOverlayLocation->addItem("Top Right",OVERLAY_TOPRIGHT);
ui->comboBoxOverlayLocation->addItem("Whole Image",OVERLAY_WHOLEIMAGE);
// ToDo Make sure we resize the images when the graphicsView is resized.
connect(ui->splitter, SIGNAL(splitterMoved(int,int)), this, SLOT(OnResize()));
// Load the settings
// qDebug() << __FILE__ << __FUNCTION__ << "readSettings:" << timer1.restart();
readSettings();
// Select the first layout as default
OnLayout(imageLayoutList.first());
// Start the sync with the cloud
//cloudSync.start();
cloudSyncTimer = new QTimer();
connect(cloudSyncTimer, SIGNAL(timeout()), this, SLOT(OnCloudSyncTimeout()));
cloudSyncTimer->start(1*60*1000);
}
MainWindow::~MainWindow()
{
delete previewWindow;
delete signalMapperLayout;
delete signalMapperPrint;
delete graphicsScene;
delete fileSelection;
delete fileModel;
delete fileThumbnail;
delete ui;
delete thumbnailTimer;
delete cloudSyncTimer;
delete settings;
}
/*
* Normal Event driven slots.
*/
void MainWindow::OnDir()
{
QString directory = QFileDialog::getExistingDirectory(this,tr("Open Directory"),fileModel->rootPath());
// qDebug() << __FILE__ << __FUNCTION__ << directory;
fileModel->setRootPath(directory);
OnOverlay();
}
void MainWindow::OnSmaller()
{
QSize size = ui->listView->iconSize();
size.setWidth(size.width() / 2);
size.setHeight(size.width() * 3 / 5);
ui->listView->setIconSize(size);
ui->pushButtonBigger->setEnabled(isAdminMode());
if (size.width() <= 125) {
ui->pushButtonSmaller->setEnabled(false);
}
}
void MainWindow::OnBigger()
{
QSize size = ui->listView->iconSize();
size.setWidth(size.width() * 2);
size.setHeight(size.width() * 3 / 5);
ui->listView->setIconSize(size);
ui->pushButtonSmaller->setEnabled(isAdminMode());
if (size.width() >= 500) {
ui->pushButtonBigger->setEnabled(false);
}
}
void MainWindow::OnThumbnailTimeout()
{
// qDebug() << __FILE__ << __FUNCTION__ << "Thumbnails timedout";
// Toggle the existance of a .touch directory to force folder refresh
QString touchDir = fileModel->rootPath() + "/.touch";
QDir dir(touchDir);
if (!dir.exists())
{
// Touch folder doesn't exist so create it
dir.mkpath(touchDir);
}
else
{
// Touch folder exist so remove it
dir.rmdir(touchDir);
}
}
void MainWindow::OnCloudSyncTimeout()
{
QDir filesDir(cloudSync.data.filesDirName);
QDir emailDir(cloudSync.data.emailDirName);
// First process any pending images to send to the cloud
int fileCnt = filesDir.entryList(QDir::Files).size();
int emailCnt = emailDir.entryList(QDir::Files).size();
setVersionLabel(fileCnt,emailCnt);
// Use the global thread pool
if (!cloudSync.data.working && (fileCnt || emailCnt)) {
cloudSync.data.working = true;
QFuture<void> result = QtConcurrent::run(cloudSyncWork, &cloudSync.data);
}
}
void MainWindow::OnCrop()
{
LoadImages(graphicsScene, fileSelection->selectedIndexes(), imageLayoutCurr);
OnResize();
}
void MainWindow::OnOverlay(QString text)
{
Q_UNUSED(text);
OnOverlay();
}
void MainWindow::OnOverlay()
{
if (ui->comboBoxOverlay->currentIndex()) {
overlayPixmap = new QPixmap(dirName + "/.Overlays/" + ui->comboBoxOverlay->currentText());
if (overlayPixmap) {
QBitmap overlayMask = overlayPixmap->createHeuristicMask();
if (ui->checkBoxOverlayMask->isChecked())
overlayPixmap->setMask(overlayMask);
}
} else {
if (overlayPixmap) {
delete overlayPixmap;
overlayPixmap = NULL;
}
}
LoadImages(graphicsScene, fileSelection->selectedIndexes(), imageLayoutCurr);
OnResize();
}
void MainWindow::restartThumbnailTimer()
{
// Set timeout to notify app to reload thus refresh thumbnails
thumbnailTimer->setSingleShot(true);
thumbnailTimer->start(5000);
}
void MainWindow::OnDeletePictures()
{
int imageIndex;
// Disable LoadImages
loadImagesDisabled = true;
// Get list of selected files
QModelIndexList indexList = fileSelection->selectedIndexes();
// Display warning message box
QMessageBox msgBox(QMessageBox::Question,"Delete Selected Files", "Are you sure?", QMessageBox::Ok | QMessageBox::Cancel, this);
int result = msgBox.exec();
if (result == QMessageBox::Ok) {
//qDebug() << __FILE__ << __FUNCTION__ << "Delete files";
// remove the files
for (imageIndex = 0; imageIndex < indexList.length(); imageIndex++) {
fileModel->remove(indexList.at(imageIndex));
}
}
// Clear the current selection
fileSelection->clear();
// Re-enable LoadImages
loadImagesDisabled = false;
// Update the display
LoadImages(graphicsScene, fileSelection->selectedIndexes(), imageLayoutCurr);
OnResize();
}
void MainWindow::LoadImages(QGraphicsScene* graphicsScene, QModelIndexList indexList, QImageLayoutButton* imageLayoutCurr)
{
int imageIndex,layoutIndex,firstImageIndex,imageFlags;
bool imageLandscape,layoutLandscape;
double imageAspect,layoutAspect;
// qDebug() << __FILE__ << __FUNCTION__ << this->imageLayoutCurr->text();
if (loadImagesDisabled) return;
// QModelIndexList indexList = fileSelection->selectedIndexes();
// Empty the scene
graphicsScene->clear();
// Draw a bounding rectangle for the page
graphicsScene->addRect(imageLayoutCurr->rect,QPen(QColor(0,0,0)));
// Figure out the first image to use and justify to latest selections
firstImageIndex = indexList.count() - imageLayoutCurr->getImageCnt();
if (firstImageIndex < 0) firstImageIndex = 0;
// For Each image location in the layout place an image.
// Repeat the image if there are more locations than selected images.
if (indexList.length()) for (imageIndex = firstImageIndex, layoutIndex = 0; layoutIndex < imageLayoutCurr->getImageCnt(); imageIndex++, layoutIndex++) {
// Check if we are trying to display more images than selected
// and loop back to the first image.
if (imageIndex >= indexList.length()) imageIndex = firstImageIndex;
// Get the layout rectangle and flags of where we want the image
QRectF layoutRect = imageLayoutCurr->getImageRect(layoutIndex);
imageFlags = imageLayoutCurr->getImageFlags(layoutIndex);
layoutAspect = layoutRect.width() / layoutRect.height();
// Figure out layout position orientation
if (layoutAspect >= 1.0) {
// Image Layout position is landscape
layoutLandscape = true;
} else {
// Image Layout position is portrait
layoutLandscape = false;
}
// Load the image into a pixmap
QPixmap pixmap(fileModel->fileInfo(indexList.at(imageIndex)).absoluteFilePath());
if (pixmap.isNull()) continue;
imageAspect = pixmap.width() / pixmap.height();
// Figure out image orientation
if (imageAspect >= 1.0) {
// Image is landscape
imageLandscape = true;
} else {
// Image is portrait
imageLandscape = false;
}
// Local variables
qreal x,y;
qreal r1,r2;
qreal rotation = 0.0;
// Move/Scale the image to the appropriate location
if (imageLandscape != layoutLandscape) {
// Different orientations so we need to rotate the image to fit the layout
rotation = 90;
QTransform trans;
trans.rotate(rotation);
pixmap = pixmap.transformed(trans);
}
qreal newX1 = 0.0;
qreal newY1 = 0.0;
qreal newX2 = pixmap.width();
qreal newY2 = pixmap.height();
x = layoutRect.left();
y = layoutRect.top();
r1 = (qreal) layoutRect.width()/(qreal) pixmap.width();
r2 = (qreal) layoutRect.height()/(qreal) pixmap.height();
if ((imageFlags & QImageLayoutButton::CROP_IMAGE) || ui->checkBoxCrop->isChecked()) {
if (r1 < r2) {
r1 = r2;
}
newX1 = ((pixmap.width() * r1) - layoutRect.width()) / (2 * r1);
newY1 = ((pixmap.height() * r1) - layoutRect.height()) / (2 * r1);
newX2 -= 2 * newX1;
newY2 -= 2 * newY1;
x += newX1 * r1;
y += newY1 * r1;
} else {
if (r1 > r2) {
r1 = r2;
}
}
x += (layoutRect.width() - pixmap.width() * r1) / 2.0;
y += (layoutRect.height() - pixmap.height() * r1) / 2.0;
// Put the pixmap on the display
QGraphicsPixmapItem *item = graphicsScene->addPixmap(pixmap.copy(newX1, newY1, newX2, newY2));
item->setScale(r1);
item->setPos(x,y);
// Draw a bounding rectangle
graphicsScene->addRect(layoutRect,QPen(QColor(0,0,0)));
// Add any specified overlay
if (overlayPixmap) {
qreal overlayScale = ui->spinBoxOverlayScale->value() / 100.0;
r1 = (qreal) layoutRect.width()/(qreal) overlayPixmap->width()*overlayScale;
r2 = (qreal) layoutRect.height()/(qreal) overlayPixmap->height()*overlayScale;
if (r1 > r2)
r1 = r2;
item = graphicsScene->addPixmap(*overlayPixmap);
item->setScale(r1);
x = layoutRect.left();
y = layoutRect.top();
if (overlayScale != 1.0) {
switch(ui->comboBoxOverlayLocation->currentData().toInt()) {
case OVERLAY_TOPLEFT:
x += layoutRect.width() * overlayScale / 2;
y += layoutRect.height() * overlayScale / 2;
break;
case OVERLAY_TOPRIGHT:
x += layoutRect.width() - layoutRect.width() * overlayScale / 2 - overlayPixmap->width() * r1;
y += layoutRect.height() * overlayScale / 2;
break;
case OVERLAY_BOTTOMLEFT:
x += layoutRect.width() * overlayScale / 2;
y += layoutRect.height() - layoutRect.height() * overlayScale / 2 - overlayPixmap->height() * r1;
break;
case OVERLAY_BOTTOMRIGHT:
x += layoutRect.width() - layoutRect.width() * overlayScale / 2 - overlayPixmap->width() * r1;
y += layoutRect.height() - layoutRect.height() * overlayScale / 2 - overlayPixmap->height() * r1;
break;
case OVERLAY_WHOLEIMAGE:
// No changes
break;
}
}
item->setPos(x,y);
}
}
qreal border = 0;
graphicsScene->setSceneRect(imageLayoutCurr->rect.left() - border, imageLayoutCurr->rect.top() - border, imageLayoutCurr->rect.width() + 2 * border, imageLayoutCurr->rect.height() + 2 * border);
}
void MainWindow::OnResize()
{
// qDebug() << __FILE__ << __FUNCTION__;
// Make sure the correct portion of the graphicsScene is visible in the graphicsView.
ui->graphicsView->setScene(graphicsScene);
ui->graphicsView->fitInView(graphicsScene->sceneRect(),Qt::KeepAspectRatio);
ui->graphicsView->ensureVisible(graphicsScene->sceneRect());
}
void MainWindow::OnSelectionChanged(QItemSelection selected,QItemSelection deselected)
{
Q_UNUSED (selected);
Q_UNUSED (deselected);
// qDebug() << __FILE__ << __FUNCTION__ << "selected" << selected.indexes();
// qDebug() << __FILE__ << __FUNCTION__ << "deselected" << deselected.indexes();
LoadImages(graphicsScene, fileSelection->selectedIndexes(), imageLayoutCurr);
OnResize();
}
void MainWindow::OnLayout(QWidget *widget)
{
QImageLayoutButton *imageLayout;
// Set the current layout
imageLayoutCurr = (QImageLayoutButton*) widget;
// enable all layouts except the selected one
int i;
for (i = 0; i < imageLayoutList.length(); i++) {
imageLayout = imageLayoutList.at(i);
imageLayout->setEnabled(imageLayout != imageLayoutCurr);
}
LoadImages(graphicsScene, fileSelection->selectedIndexes(), imageLayoutCurr);
OnResize();
int index;
for (index = 0 ; index < printerList.size() ; index++) {
QPrinter *printer = printerList.at(index);
// qDebug() << "printer" << printer->printerName();
// qDebug() << " paperSize" << printer->paperSize(QPrinter::Inch);
// qDebug() << " imageLayout.rect" << imageLayoutCurr->rect;
QSizeF paperSize = printer->paperSize(QPrinter::Inch);
if ((paperSize.width() * 1000 == imageLayoutCurr->rect.width() &&
paperSize.height() * 1000 == imageLayoutCurr->rect.height()) ||
(paperSize.width() * 1000 == imageLayoutCurr->rect.height() &&
paperSize.height() * 1000 == imageLayoutCurr->rect.width())) {
// qDebug() << " enabled";
printButtonList.at(index)->setEnabled(true);
} else {
// qDebug() << " disabled";
printButtonList.at(index)->setEnabled(false);
}
}
}
void MainWindow::AddPrinter(QPrinter *printer)
{
printerList.append(printer);
QPushButton *button = new QPushButton(printer->printerName());
QAction *actionPrinterRemove = new QAction(tr("Remove Printer"),this);
QAction *actionPrinterSettings = new QAction(tr("Printer Settings"),this);
button->addAction(actionPrinterRemove);
button->addAction(actionPrinterSettings);
button->setContextMenuPolicy(Qt::ActionsContextMenu);
printButtonList.append(button);
ui->verticalLayoutPrintButtons->addWidget(button);
signalMapperPrint->setMapping(button,printButtonList.length() - 1);
connect(button, SIGNAL(clicked()), signalMapperPrint, SLOT(map()));
signalMapperPrinterRemove->setMapping(actionPrinterRemove,printButtonList.length() - 1);
connect(actionPrinterRemove, SIGNAL(triggered()), signalMapperPrinterRemove, SLOT(map()));
signalMapperPrinterSettings->setMapping(actionPrinterSettings,printButtonList.length() - 1);
connect(actionPrinterSettings, SIGNAL(triggered()), signalMapperPrinterSettings, SLOT(map()));
// Do an on layout so printer buttons are enabled accordingly
//OnLayout(imageLayoutCurr);
}
void MainWindow::OnPrinterRemove(int index)
{
// Only do it if we are in admin mode
if (!isAdminMode()) return;
// Set the current button
QPushButton *button = printButtonList.at(index);
// qDebug() << __FILE__ << __FUNCTION__ << button->text();
// Cleanup and remove the button
ui->verticalLayoutPrintButtons->removeWidget(button);
disconnect(button, SIGNAL(clicked()));
button->hide();
while(button->actions().length()) {
QAction *action = button->actions().last();
disconnect(action, SIGNAL(triggered()));
button->removeAction(action);
delete action;
}
// ToDo delete the button causing SIGABRT
// delete button;
// Just replace the entry in the list with NULL so other printer indexes are still correct
printButtonList.replace(index, NULL);
// Also cleanup the printer object
QPrinter *printer = printerList.at(index);
delete printer;
// Just replace the entry in the list with NULL so other printer indexes are still correct
printerList.replace(index, NULL);
}
void MainWindow::OnPrinterSettings(int index)
{
// Set the current button
// QPushButton *button = printButtonList.at(index);
// qDebug() << __FILE__ << __FUNCTION__ << button->text();
// ToDo: Dialog only flashed and doesn't let you change anything
QPrintDialog printDialog(printerList.at(index), this);
printDialog.exec();
}
void MainWindow::OnArchive()
{
int imageIndex;
// Check for and if necessary create an archive directory
QString archiveDir = fileModel->rootPath() + "/.Archive";
QDir dir(archiveDir);
if (!dir.exists())
{
// Archive folder doesn't exist so create it
dir.mkpath(archiveDir);
}
// Now point to the current folder
dir.setPath(fileModel->rootPath());
// Disable LoadImages
loadImagesDisabled = true;
// Get list of selected files
QModelIndexList indexList = fileSelection->selectedIndexes();
// Display warning message box
QMessageBox msgBox(QMessageBox::Question,"Archive All/Selected Files", "Are you sure?", QMessageBox::Ok | QMessageBox::Cancel, this);
int result = msgBox.exec();
if (result == QMessageBox::Ok) {
//qDebug() << __FILE__ << __FUNCTION__ << "Archive files";
QString fileName;
if (indexList.length() == 0) {
// Nothing is selected move all of them
QModelIndex index = fileModel->index(dirName);
int numRows = fileModel->rowCount(index);
for (int row = 0; row < numRows; row++) {
fileName = fileModel->fileName(fileModel->index(row,0,index));
if (!dir.rename(fileName, ".Archive/" + fileName)) {
qDebug() << "dir.rename failed!";
}
}
} else {
// Move selected files
for (imageIndex = 0; imageIndex < indexList.length(); imageIndex++) {
fileName = fileModel->fileName(indexList.at(imageIndex));
if (!dir.rename(fileName, ".Archive/" + fileName)) {
qDebug() << "dir.rename failed!";
}
}
}
}
// Clear the current selection
fileSelection->clear();
// Re-enable LoadImages
loadImagesDisabled = false;
// Update the display
LoadImages(graphicsScene, fileSelection->selectedIndexes(), imageLayoutCurr);
OnResize();
}
void MainWindow::OnEMail()
{
QString emailAddress = ui->lineEditEmail->text();
// Get list of selected files
QModelIndexList indexList = fileSelection->selectedIndexes();
QStringList fileNames;
for (int imageIndex = 0; imageIndex < indexList.length(); imageIndex++) {
fileNames.append(fileModel->rootPath() + "/" + fileModel->fileName(indexList.at(imageIndex)));
}
// Use EMail Dialog box
EmailDialog emailDialog;
emailDialog.setEmailAddress(emailAddress);
emailDialog.setFileNames(fileNames);
int response = emailDialog.exec();
emailAddress = emailDialog.emailAddress();
fileNames = emailDialog.fileNames();
if (response == QDialog::Accepted &&
!emailAddress.isEmpty() &&
emailAddress.contains("@") &&
fileSelection->hasSelection()) {
// Only do E-Mail/Web if address entered and pictures are selected
qDebug() << __FILE__ << __FUNCTION__ << "Send E-Mail to " << emailAddress;
// Create a file containing the email address
QString emailID = QUuid::createUuid().toString().mid(1);
emailID.chop(1);
QFile emailFile(cloudSync.data.emailDirName + "/" + emailID + ".eml");
emailFile.open(QIODevice::Append);
QTextStream emailStream (&emailFile);
emailStream << emailAddress << "\n";
qDebug() << "emailID" << emailID;
// Add the picture ID's and copy the pictures to the staging folder
foreach(QString srcFileName, fileNames) {
QFileInfo srcFileInfo(srcFileName);
QString destFileName = QUuid::createUuid().toString().mid(1);
destFileName.chop(1);
destFileName.append(srcFileInfo.fileName());
// destFileName.append(".");
// destFileName.append(srcFileInfo.suffix());
qDebug() << "Copy file" << srcFileName << "to" << destFileName;
QFile::copy(srcFileName, cloudSync.data.filesDirName + "/" + destFileName);
emailStream << destFileName << "\n";
}
emailFile.close();
}
// If Reset is checked clear the settings
if (ui->checkBoxReset->checkState() == Qt::Checked) {
OnDefaults();
}
// Force a status update and if any pictures / email pending start a sync
OnCloudSyncTimeout();
}
void MainWindow::OnPrint(int index)
{
QPrinter *printer = printerList.at(index);
printer->setCopyCount(ui->spinBoxCopies->value());
if (printer && fileSelection->hasSelection()) {
// qDebug() << __FILE__ << __FUNCTION__ << printer->printerName();
// Do the printing here
if (graphicsScene->sceneRect().width() > graphicsScene->sceneRect().height()) {
printer->setOrientation(QPrinter::Landscape);
} else {
printer->setOrientation(QPrinter::Portrait);
}
if (ui->checkBoxPreview->checkState() == Qt::Checked) {
// Do a preview if checked
QPrintPreviewDialog printPreview(printer);
connect(&printPreview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(paintRequested(QPrinter*)));
printPreview.exec();
disconnect(this, SLOT(paintRequested(QPrinter*)));
} else {
// Else do a print
QPrintDialog printDialog(printer, this);
if (printDialog.exec() == QDialog::Accepted) {
paintRequested(printer);
}
}
}
OnEMail();
// As we called OnEMail we don't need to do this...
/*
* // If Reset is checked clear the settings
* if (ui->checkBoxReset->checkState() == Qt::Checked) {
* OnDefaults();
* }
*/
}
void MainWindow::paintRequested(QPrinter *printer)
{
QPainter painter(printer);
graphicsScene->render(&painter);
}
void MainWindow::OnDirLoaded(QString dir)
{
// qDebug() << __FILE__ << __FUNCTION__ << dir << dirName << timer1.restart();
if (dir == dirName)
{
fileModel->setIconProvider(fileThumbnail);
ui->listView->setModel(fileModel);
ui->listView->setRootIndex(fileModel->index(dirName));
ui->listView->setSelectionModel(fileSelection);
}
#if 0
// fileModel->sort(3);:"
ui->listView->scrollToBottom();
if (fileSelection->selectedIndexes().length() == 0) {
// Currently no Items are selected so select the latest
// qDebug() << __FILE__ << __FUNCTION__ << "No files selected so autoselect the last one?";
// ui->listView->sel
}
#endif
// qDebug() << __FILE__ << __FUNCTION__ << "Models Loaded" << timer1.restart();
loadPreviewWindowContents(dir);
// qDebug() << __FILE__ << __FUNCTION__ << "Preview Loaded" << timer1.restart();
}
void MainWindow::loadPreviewWindowContents(QString dir)
{
// qDebug() << __FILE__ << __FUNCTION__ << dir;
if (previewWindow && previewWindow->isVisible()) {
QModelIndex index = fileModel->index(dir);
int numRows = fileModel->rowCount(index);
// Figure out the start of the last n that will be displayed
int startRow = numRows - previewWindow->imageLayoutCurr->getImageCnt();
if (startRow < 0) startRow = 0;
// Create a list of them so we can use the LoadImages method
QModelIndexList indexList;
for (int row = startRow; row < numRows; row++) {
indexList.append(fileModel->index(row,0,index));
}
// Load the images onto the preview window.
LoadImages(previewWindow->graphicsScene, indexList, previewWindow->imageLayoutCurr);
previewWindow->OnResize();
}
}
void MainWindow::setVersionLabel(int pics, int emails)
{
QString version;
QTextStream(&version)
<< "Santa Ship Ver 3.0 " << __DATE__ << " " << __TIME__
<< " To sync Pictures " << pics << " & Emails " << emails;
// Load the version and build date / time
//version = QString("SantaShip Ver 3.0 ") + QString(__DATE__) + QString(" ") + QString(__TIME__);
// Add status from cloudsync
//version += QString(" CloudSync")
ui->labelVersion->setText(version);
}
void MainWindow::OnDefaults()
{
// Set the layout back to default (first entry)
OnLayout(imageLayoutList.first());
// Set the overlay back to defaults
ui->comboBoxOverlay->setCurrentIndex(1);
ui->comboBoxOverlayLocation->setCurrentIndex(0);
ui->spinBoxOverlayScale->setValue(25);
ui->checkBoxOverlayMask->setChecked(true);
OnOverlay();
// Set the E-Mail back
ui->lineEditEmail->clear();
// Set Crop
ui->checkBoxCrop->setChecked(true);
// Clear the preview option
ui->checkBoxPreview->setChecked(false);
// Set the reset on print
ui->checkBoxReset->setChecked(true);
// Set the copies count back to 1
ui->spinBoxCopies->setValue(1);
// Clear the current selection
fileSelection->clear();
// Update the display
LoadImages(graphicsScene, fileSelection->selectedIndexes(), imageLayoutCurr);
OnResize();
}
/*
* Automatically connected action driven slots
*/
void MainWindow::on_actionPreview_Window_triggered(bool checked)
{
// qDebug() << __FILE__ << __FUNCTION__;
if (checked) {
// Show the window
previewWindow->show();
} else {
// Hide the window don't destroy it we might want it again
previewWindow->hide();
}
}
void MainWindow::on_actionAdd_Printer_triggered(bool checked)
{
Q_UNUSED(checked);
// qDebug() << __FILE__ << __FUNCTION__;
QPrinter *printer = new QPrinter();
QPrintDialog printDialog(printer, this);
// qDebug() << __FILE__ << __FUNCTION__ << "First call";
if (printDialog.exec() == QDialog::Accepted) {
// qDebug() << __FILE__ << __FUNCTION__ << "adding" << printer->printerName() << printer->resolution();
AddPrinter(printer);
}
// qDebug() << __FILE__ << __FUNCTION__ << "Second call";
// if (printDialog.exec() == QDialog::Accepted) {
// qDebug() << __FILE__ << __FUNCTION__ << "adding" << printer->printerName() << printer->resolution();
// AddPrinter(printer);
// }
}
void MainWindow::on_actionFull_Screen_triggered(bool checked)
{
//qDebug() << __FILE__ << __FUNCTION__;
if (!checked) {
showNormal();
previewWindow->showNormal();
} else {
showFullScreen();
previewWindow->showFullScreen();
}
}
void MainWindow::on_actionSave_Settings_triggered(bool checked)
{
Q_UNUSED (checked);
writeSettings();
}
void MainWindow::on_actionChange_Enable_triggered(bool checked)
{
Q_UNUSED (checked);
QAction *actClicked = (QAction*) this->sender();
// qDebug() << __FILE__ << __FUNCTION__ << "Change Enabled" << checked;
if (adminMode)
{
// We are currently in adminMode so we need to get out.
adminMode = false;
}
else
{
// We are not in adminMode so we need to get in.
bool ok = false;
QString passwd = QInputDialog::getText(this, QString("Please enter admin password"), QString("Password"), QLineEdit::Password, QString(), &ok);
if (ok && passwd == adminPassword)
{
// User hit ok and passwd matches
adminMode = true;
}
else
{
adminMode = false;
}
}
actClicked->setChecked(adminMode);
// Depending on adminMode enable / disable buttons and menu items.
ui->actionAdd_Printer->setEnabled(adminMode);
ui->actionFull_Screen->setEnabled(adminMode);
ui->actionPreview_Window->setEnabled(adminMode);
ui->actionSave_Settings->setEnabled(adminMode);
ui->pushButtonDir->setEnabled(adminMode);
ui->actionCloud_Access->setEnabled(adminMode);
// Also enable / disable the size change buttons.
QSize size = ui->listView->iconSize();
if (size.width() > 125 )
{
ui->pushButtonSmaller->setEnabled(adminMode);
}
else
{
ui->pushButtonSmaller->setEnabled(false);
}
if (size.width() < 500)
{
ui->pushButtonBigger->setEnabled(adminMode);
}
else
{
ui->pushButtonBigger->setEnabled(false);
}
}
void MainWindow::on_actionCloud_Access_triggered(bool checked)
{
Q_UNUSED (checked);
DialogCloudSetup cloudSetup;
cloudSetup.setS3Access(cloudSync.data.S3Access);
cloudSetup.setS3Secret(cloudSync.data.S3Secret);
cloudSetup.setS3Bucket(cloudSync.data.S3Bucket);