-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow-current.cpp
4291 lines (3474 loc) · 106 KB
/
mainwindow-current.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
// Targeter - target identification software for EUCALL workpackage 6
// Licensed under the GPL License. See LICENSE file in the project root for full license information.
// Copyright(C) 2017 David Watts
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "FocusStack.h"
#include "opencvtoqt.h"
#include "globals.h"
#include "Settings.h"
#include "settingsdialog.h"
#include "findtargets.h"
#include "imageprocessing.h"
#include "HelperFunctions.h"
#include "imagesContainer.h"
#include "targeterimage.h"
#include "BaslerCamera.h"
//#include <QThread>
#include <QtConcurrent/QtConcurrent>
#include <QFuture>
#include <QFutureWatcher>
// end Qconcurent stuff
#include <QTimer>
#include <QFile>
#include <QFileDialog>
#include <QStringList>
#include <QDebug>
#include <QPainter>
#include <QWheelEvent>
#include <QSettings>
#include <QListWidgetItem>
#include <QBitmap>
#include <cmath>
#include <QScrollBar>
#include <QPlainTextEdit>
#include "opencv2/core.hpp"
#include "opencv/highgui.h"
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/video.hpp"
using namespace cv;
using namespace std;
using namespace QtConcurrent;
/*
QIcon targeterIcon;
try
{
targeterIcon.addFile(":icons/24/targeter.png", QSize(24, 24));
QIcon deleteIcon = QIcon::fromTheme("edit-delete");
ui->actionProcessFocusStack->setIcon(targeterIcon);
ui->actionDelete_Selected_Images->setIcon(deleteIcon);
}
catch( cv::Exception& e )
{
const char* err_msg = e.what();
std::cout << "exception caught: " << err_msg << std::endl;
}
*/
/**
*
* class constructor: sets up slots, initializes variables, gets serialised settings values
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::MainWindow
* Qualifier : QMainWindow(parent), ui(new Ui::MainWindow)
* @param QWidget * parent
* @return
* Access public
*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
//m_CurrentQImage = NULL;
this->m_scale = 1;
ui->setupUi(this);
// to send these data types over signals slots mechanism
qRegisterMetaType<cv::Mat>("cv::Mat");
qRegisterMetaType<QVector<QString> >("QVector<QString>");
ui->action_Open->setIcon(this->style()->standardIcon(QStyle::SP_DialogOpenButton));
ui->action_Save->setIcon(this->style()->standardIcon(QStyle::SP_DialogSaveButton));
ui->action_New->setIcon(this->style()->standardIcon(QStyle::SP_FileDialogNewFolder));
ui->ImageThumbList->setStyleSheet("QVectorWidget { background-color: "+QApplication::palette().color(QPalette::Window).name()+";}");
//{background-color:red; border: 1px solid red }
QPalette palette = ui->ImageThumbList->palette();
palette.setColor(ui->ImageThumbList->backgroundRole(), ui->display_image->palette().color(QWidget::backgroundRole()));
palette.setColor(ui->ImageThumbList->foregroundRole(), ui->display_image->palette().color(QWidget::backgroundRole()));
ui->ImageThumbList->setAutoFillBackground(true);
ui->ImageThumbList->setPalette(palette);
qRegisterMetaType<CONSOLECOLOURS::colour>("CONSOLECOLOURS::colour");
basCamera = make_unique<BaslerCamera>();
m_settings = make_unique<SettingsValues>();
basCamera->setCameras(QString("21799625"), QString("21799596"));
connect(basCamera.get(), SIGNAL(processedImage(cv::Mat)), this, SLOT(updateQTImage(cv::Mat)));
ui->ImageThumbList->setContextMenuPolicy(Qt::CustomContextMenu);
// set up context menu
m_thumb_context_menu.toolTipsVisible();
m_thumb_context_menu.addAction(ui->actionSet_as_Target);
m_thumb_context_menu.addAction(ui->actionSet_as_find_targets_image);
m_thumb_context_menu.addAction(ui->actionDeleteImage);
// set up scrolling text window with 2 lines
ui->consoleDisplay->ensureCursorVisible();
ui->consoleDisplay->setReadOnly(true);
//QFontMetrics m(m_textEdit->font());
//int RowHeight = m.lineSpacing();
//ui->consoleDisplay->setStyleSheet("QPlainTextEdit:focus{max-height: 400px}");
QPalette p = ui->consoleDisplay->palette();
p.setColor(QPalette::Base, QColor(80, 80, 80));
p.setColor(QPalette::Text, Qt::white);
ui->consoleDisplay->setPalette(p);
//ui->consoleDisplay->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
ui->consoleDisplay->setFocusPolicy(Qt::ClickFocus);
//m_editScrollBar->setSingleStep(RowHeight);
// add to toolbar
//ui->toolBarEdit->addWidget(m_textEdit);
//m_textEdit->resize(m_textEdit->width(), 3 * RowHeight);
//ui->toolBarEdit->resize(m_textEdit->width(), 4 * RowHeight);
//m_textEdit->setMaximumHeight(200 * RowHeight);
consoleLog("Welcome to", CONSOLECOLOURS::colour::Information);
consoleLog("Targeter", true, true, CONSOLECOLOURS::colour::Information);
fh_addFileListToMenu();
m_bContextMenuAction = false;
// get settings from serialised file
deSerialiseSettings();
m_settingsDlg.create(m_settings.get());
QString XMLfilename = m_settings->s_project_FilenamePrefix + m_settings->s_project_Barcode + ".xml";
xmlWriter.openForWrite(XMLfilename);
// set display to point to image container
ui->display_image->setImageContainerPointer(&m_ImagesContainer);
setUpSlotsSignals();
// set up stages
//emit m_pMicroscopeStages.getAvailablePorts();
//emit m_pMicroscopeStages.assignPorts();
}
void MainWindow::logFeedback(int score, QString name, QString email, QString institute, QString desc)
{
XMLWriter logWriter;
connect(&logWriter, SIGNAL(LOGCONSOLE(QString, CONSOLECOLOURS::colour)), this, SLOT(LOGCONSOLE(QString, CONSOLECOLOURS::colour)));
logWriter.openForAppend("feedback.xml");
logWriter.appendFeedback(score, name, email, institute, desc);
}
void MainWindow::setUpSlotsSignals()
{
m_pMicroscopeStages = new StageManager;
m_pWorkerThread = new QThread;
// handle thumbnail clicks
connect(ui->ImageThumbList, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ProvideContextMenu(const QPoint &)));
connect(ui->ImageThumbList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onThumbImageClick(QListWidgetItem*)));
// slots for communication from printqlabel class
connect(ui->display_image, SIGNAL(disablePanButton()), this, SLOT(disablePanButton()));
connect(ui->display_image, SIGNAL(setTargetArea(drawingShape)), this, SLOT(setTargetArea(drawingShape)));
connect(ui->display_image, SIGNAL(unsetDrawingButtons()), this, SLOT(unsetDrawingButtons()));
connect(ui->display_image, SIGNAL(StatusBarMessage(QString)), this, SLOT(StatusBarMessage(QString)));
connect(ui->display_image, SIGNAL(addFiducialMark(QPoint)), this, SLOT(addFiducialMark(QPoint)));
connect(ui->display_image, SIGNAL(moveObjective(QPoint)), this, SLOT(moveObjective(QPoint)));
// slot to get values back from modeless dialog
connect(&m_settingsDlg, SIGNAL(sendSettings()), this, SLOT(receiveSettings()));
connect(&m_settingsDlg, SIGNAL(logFeedback(int, QString, QString, QString, QString)), this, SLOT(logFeedback(int, QString, QString, QString, QString)));
// logging from Image Processing functions run in separate thread
connect(&m_ImageProcessing, SIGNAL(LOGCONSOLE(QString, CONSOLECOLOURS::colour)), this, SLOT(LOGCONSOLE(QString, CONSOLECOLOURS::colour)));
// make stage control object a worker thread object
m_pMicroscopeStages->moveToThread(m_pWorkerThread);
// Marzhauser stage commands called from settings dialog
connect(&m_settingsDlg, SIGNAL(MoveAbsoluteXY(double, double)), m_pMicroscopeStages, SLOT(MoveAbsoluteXY(double, double)));
connect(&m_settingsDlg, SIGNAL(MoveRelativeXY(double, double)), m_pMicroscopeStages, SLOT(MoveRelativeXY(double, double)));
connect(&m_settingsDlg, SIGNAL(SendCommandXY(QString)), m_pMicroscopeStages, SLOT(SendCommandXY(QString)));
connect(&m_settingsDlg, SIGNAL(SetVelocityXY(double)), m_pMicroscopeStages, SLOT(SetVelocityXY(double)));
connect(&m_settingsDlg, SIGNAL(CalibrateXY()), m_pMicroscopeStages, SLOT(CalibrateXY()));
connect(&m_settingsDlg, SIGNAL(MeasureRangeXY()), m_pMicroscopeStages, SLOT(MeasureRangeXY()));
connect(&m_settingsDlg, SIGNAL(ConnectXY()), m_pMicroscopeStages, SLOT(ConnectXY()));
connect(&m_settingsDlg, SIGNAL(DisconnectXY()), m_pMicroscopeStages, SLOT(DisconnectXY()));
connect(&m_settingsDlg, SIGNAL(AbortXY()), m_pMicroscopeStages, SLOT(AbortXY()));
connect(&m_settingsDlg, SIGNAL(SetJoyStickXY(bool)), m_pMicroscopeStages, SLOT(SetJoystickXY(bool)));
connect(&m_settingsDlg, SIGNAL(LOGCONSOLE(QString, CONSOLECOLOURS::colour)), this, SLOT(LOGCONSOLE(QString, CONSOLECOLOURS::colour)));
connect(m_settingsDlg.getStagePanel(), SIGNAL(mouseDownPoint(double, double)), m_pMicroscopeStages, SLOT(goXY(double, double)));
connect(&m_settingsDlg, SIGNAL(getCOMPORTS()), m_pMicroscopeStages, SLOT(getAvailablePorts()));
connect(m_pMicroscopeStages, SIGNAL(reportCOMPORTS(QVector<QString>)), &m_settingsDlg, SLOT(updateCOMPORTS(QVector<QString>)));
// Marzhauser stage commands called from main program
connect(this, SIGNAL(MoveAbsoluteXY(double, double)), m_pMicroscopeStages, SLOT(MoveAbsoluteXY(double, double)));
connect(this, SIGNAL(MoveRelativeXY(double, double)), m_pMicroscopeStages, SLOT(MoveRelativeXY(double, double)));
connect(this, SIGNAL(SendCommandXY(QString)), m_pMicroscopeStages, SLOT(SendCommandXY(QString)));
connect(this, SIGNAL(SetVelocityXY(double)), m_pMicroscopeStages, SLOT(SetVelocityXY(double)));
connect(this, SIGNAL(CalibrateXY()), m_pMicroscopeStages, SLOT(CalibrateXY()));
connect(this, SIGNAL(MeasureRangeXY()), m_pMicroscopeStages, SLOT(MeasureRangeXY()));
connect(this, SIGNAL(ConnectXY()), m_pMicroscopeStages, SLOT(ConnectXY()));
connect(this, SIGNAL(DisconnectXY()), m_pMicroscopeStages, SLOT(DisconnectXY()));
connect(this, SIGNAL(AbortXY()), m_pMicroscopeStages, SLOT(AbortXY()));
// connect(&m_settingsDlg, SIGNAL(serialiseSettings(SettingsValues)), this, SLOT(serialiseSettings(SettingsValues)));
connect(m_pMicroscopeStages, SIGNAL(LOGCONSOLE(QString, CONSOLECOLOURS::colour)), this, SLOT(LOGCONSOLE(QString, CONSOLECOLOURS::colour)));
connect(m_pMicroscopeStages, SIGNAL(ACTIONCOMPLETED(QString)), this, SLOT(StageMovementCompleted(QString)));
connect(m_pMicroscopeStages, SIGNAL(STAGECONNECTED(bool, QString)), this, SLOT(StageConnected(bool, QString)));
connect(&xmlWriter, SIGNAL(LOGCONSOLE(QString, CONSOLECOLOURS::colour)), this, SLOT(LOGCONSOLE(QString, CONSOLECOLOURS::colour)));
// clean up
connect(m_pWorkerThread, SIGNAL(finished()), m_pMicroscopeStages, SLOT(deleteLater()));
m_pWorkerThread->start();
}
void MainWindow::StageConnected(bool isXY, QString COMPORT)
{
m_settingsDlg.updateCONNECTPORT(isXY, COMPORT);
}
void MainWindow::StageMovementCompleted(QString message)
{
consoleLog(message, CONSOLECOLOURS::colour::Information);
QApplication::restoreOverrideCursor();
}
/**
*
* class destructor, serializes settings values
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::~MainWindow
* Qualifier
* @return
* Access public
*/
MainWindow::~MainWindow()
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
serialiseSettings();
delete ui;
m_ImagesContainer.freeImages();
m_pWorkerThread->quit();
m_pWorkerThread->wait();
}
void MainWindow::addTargeterImage(targeterImage tim, QAction* pAction)
{
addCVImage(tim, NULL, true);
if (pAction != NULL)
pAction->setChecked(false);
}
void MainWindow::addMatImage(cv::Mat img, QString imagename, imageType::imageType type, QAction* pAction)
{
addCVImage(img, QUuid::QUuid(), imagename, true, type, true);
if (pAction != NULL)
pAction->setChecked(false);
}
bool MainWindow::updateQTImage(cv::Mat img, QString imagename, imageType::imageType type, QAction* pAction)
{
// update last cv image trigger show image on paint control
targeterImage* pTar = m_ImagesContainer.getLastImagePtr();
if (pTar == NULL)
{
addCVImage(img.clone(), imagename);
pTar = m_ImagesContainer.getLastImagePtr();
}
else
pTar->addImage(img);
// sets the last image as the one to be displayed
ui->display_image->setImageIndex(m_ImagesContainer.getNumImages()-1);
if (pAction != NULL)
return pAction->isChecked();
else
return false;
}
void MainWindow::serialiseSettings()
{
QFile file("targetter_settings.dat");
if (file.open(QIODevice::WriteOnly))
{
QDataStream out(&file); // we will serialize the data into the file
m_settings->serialize(out);
file.close();
}
}
void MainWindow::deSerialiseSettings()
{
QFile file("targetter_settings.dat");
if (file.open(QIODevice::ReadOnly)) {
QDataStream in(&file); // we will serialize the data into the file
m_settings->deserialize(in);
}
else
{
m_settings->initialize();
}
file.close();
}
/**
*
* writes debug information into application console
*
* @author David Watts
* @since 2017/04/03
*
* FullName MainWindow::consoleLog
* Qualifier
* @param QString strText
* @param QMessageBox::Icon icn
* @return void
* Access public
*/
void MainWindow::consoleLog(QString strText, CONSOLECOLOURS::colour icn)
{
consoleLog(strText, true, true, icn);
}
void MainWindow::consoleLog(QString strText, bool newline, bool moveToEnd, CONSOLECOLOURS::colour icn)
{
QString msg = "<p>";
//QPalette p = ui->consoleDisplay->palette();
if (icn == CONSOLECOLOURS::Warning)
msg = "<font color=\"yellow\">";
if (icn == CONSOLECOLOURS::Data)
msg = "<font color=\"yellow\">";
else if (icn == CONSOLECOLOURS::Question)
msg = "<font color=\"green\">";
else if (icn == CONSOLECOLOURS::Critical)
msg = "<font color=\"red\">";
else if (icn == CONSOLECOLOURS::Information)
msg = "<font color=\"white\">";
else
msg = "<font color=\"white\">";
msg += strText;
msg += "</font></p>";
QMetaObject::invokeMethod(ui->consoleDisplay, "appendHtml", Qt::QueuedConnection, Q_ARG(QString, msg));
//ui->consoleDisplay->appendHtml(msg); // output string
/*
QTextCursor c = ui->consoleDisplay->textCursor();
if (moveToEnd)
c.movePosition(QTextCursor::End);
else
c.movePosition(QTextCursor::Start);
ui->consoleDisplay->setTextCursor(c);
*/
}
/**
*
* writes debug information into application console
*
* @author David Watts
* @since 2017/04/03
*
* FullName MainWindow::consoleLog
* Qualifier
* @param imageType::imageType type
* @param QMessageBox::Icon icn
* @return void
* Access public
*/
void MainWindow::consoleLog(imageType::imageType type, CONSOLECOLOURS::colour icn)
{
QString msg("A image of type");
if (type == imageType::mask)
msg += " mask ";
else if (type == imageType::target)
msg += " target ";
else if (type == imageType::test)
msg += " test ";
else if (type == imageType::roi)
msg += " region of interest ";
else if (type == imageType::cclabels)
msg += " connected components ";
else if (type == imageType::any)
msg += " valid image ";
else
msg += " different to this one ";
msg += "is required for this function";
consoleLog(msg, icn);
}
/**
*
* makes context menu for image thumbnails
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::ProvideContextMenu
* Qualifier
* @param const QPoint & pos
* @return void
* Access private
*/
void MainWindow::ProvideContextMenu(const QPoint &pos)
{
QPoint item = ui->ImageThumbList->mapToGlobal(pos);
//QWidget *widget = static_cast<QListWidgetItem*>(childAt(pos));
m_bContextMenuAction = true;
QAction* rightClickItem = m_thumb_context_menu.exec(item);
m_bContextMenuAction = false;
}
/**
*
* Manages recent file list
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::fh_addFileListToMenu
* Qualifier
* @return void
* Access public
*/
void MainWindow::fh_addFileListToMenu()
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
// create actions for recent files
for (int i = 0; i < MaxRecentFiles; ++i) {
recentFileActs[i] = new QAction(this);
recentFileActs[i]->setVisible(false);
connect(recentFileActs[i], SIGNAL(triggered()), this, SLOT(openRecentFile()));
}
QList<QMenu*> menus = menuBar()->findChildren<QMenu*>();
separatorAct = menus[0]->addSeparator();
//add action to menu bar
for (int i = 0; i < MaxRecentFiles; ++i)
menus[0]->addAction(recentFileActs[i]);
menus[0]->addSeparator();
fh_updateRecentFileActions();
}
/**
*
* Sets current file for recent file list
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::fh_setCurrentFile
* Qualifier
* @param const QString & fileName
* @return void
* Access public
*/
void MainWindow::fh_setCurrentFile(const QString &fileName)
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
curFile = fileName;
setWindowFilePath(curFile);
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
files.removeAll(fileName);
files.prepend(fileName);
while (files.size() > MaxRecentFiles)
files.removeLast();
settings.setValue("recentFileList", files);
fh_updateRecentFileActions();
}
/**
*
* Update actions for recent file list
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::fh_updateRecentFileActions
* Qualifier
* @return void
* Access public
*/
void MainWindow::fh_updateRecentFileActions()
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
QSettings settings;
QStringList files = settings.value("recentFileList").toStringList();
int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
for (int i = 0; i < numRecentFiles; ++i) {
QString text = tr("&%1 %2").arg(i + 1).arg(fh_strippedName(files[i]));
recentFileActs[i]->setText(text);
recentFileActs[i]->setData(files[i]);
recentFileActs[i]->setVisible(true);
}
for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
recentFileActs[j]->setVisible(false);
separatorAct->setVisible(numRecentFiles > 0);
}
/**
*
* gets just file name from full filenamepath
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::fh_strippedName
* Qualifier
* @param const QString & fullFileName
* @return QT_NAMESPACE::QString
* Access public
*/
QString MainWindow::fh_strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
void MainWindow::paintEvent(QPaintEvent* event)
{
#ifdef DEBUGPRINT
#ifdef ONMOUSEPAINTDEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
#endif
}
/*
void MainWindow::wheelEvent ( QWheelEvent * event )
{
this->m_scale += (event->delta()/120); //or use any other step for zooming
std::cout << "exception caught: " << this->m_scale << std::endl;
}
*/
QString MainWindow::getSaveFilename(targeterImage im, int& number, bool bIsCompleteImage)
{
if (im.filepathname == "") // create filename
return m_settings->s_project_Directory + m_settings->s_project_FilenamePrefix + "_" + QString::number(number++) + "_" + im.getUID().toString() + ".png";
else
{
QString filename = QString::fromStdString(im.filepathname);
QFileInfo check_file(filename);
// if it exists already and is not a subimage then number the filename
if (check_file.exists() && check_file.isFile() && bIsCompleteImage)
filename.insert(filename.lastIndexOf('.'), QString::number(number++));
return filename; // use existing filename
}
}
cv::Mat MainWindow::getDrawnImage(int index)
{
return cv::Mat();
}
/**
*
* Saves current image to file
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::SaveImage
* Qualifier
* @return void
* Access public
*/
void MainWindow::SaveImage()
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
std::vector<int> checked = getCheckedImages();
if (ui->ImageThumbList->count() > 0 && checked.size() > 0)
{
QFileDialog dialog;
QString filename, filepath = "";
int fileNumber = 0;
int ct = 0;
QVector<drawingShape> shapes;
// get current active image check if it is checked to save then check for sub-images
foreach(int i, checked)
{
targeterImage tim = m_ImagesContainer.getImageAt(i);
cv::Mat im = tim.getImage();
// does image already have a filename
if (i == m_currentImageDisplayedIndex) // current image
shapes = ui->display_image->getTargetImage(false, true, i);
// if first image
filename = getSaveFilename(tim, fileNumber, shapes.length() == 0);
// first file ask user if they want to modify file name (or path)
if (ct == 0)
{
// ask user to modify filename
filename = dialog.getSaveFileName(this, "Save Image File",
filename, tr("Image Files [*.jpg, *.jpeg, *.bmp, *.png , *.pgm, *.pbm, *.ppm *.tiff *.tif]"),
0, QFileDialog::DontUseNativeDialog);
// get directory
QFileInfo check_file(filename);
filepath = check_file.absolutePath();
}
else
{
// save image to dialog chosen path
if(filepath != "")
{
QFileInfo check_file(filename);
filename = filepath + "/" + check_file.fileName();
}
}
if(i == m_currentImageDisplayedIndex && shapes.length() > 0)
{
for (int j = 0; j < shapes.length(); j++)
{
// or save sub-image
QRect r = shapes[j].boundingBox;
cv::Rect cvr = cv::Rect(r.x(), r.y(), r.width(), r.height());
cv::Mat rectImage = im(cvr);
if (ct <= 0)
{
// save the image
imwrite(filename.toLocal8Bit().data(), rectImage);
}
else
{
QString file = filename;
QString sRect = "_(" + QString::number(shapes[j].boundingBox.left()) + "," + QString::number(shapes[j].boundingBox.top()) + ":" +
QString::number(shapes[j].boundingBox.right()) + "," + QString::number(shapes[j].boundingBox.bottom()) + ")";
file.insert(file.lastIndexOf('.'), sRect);
// save the image
imwrite(file.toLocal8Bit().data(), rectImage);
}
}
}
else
{
// save the image
imwrite(filename.toLocal8Bit().data(), im);
}
DBOUT("image written to file: " << filename.toLocal8Bit().data() << std::endl);
ct++;
}
if(ct == 0)
consoleLog("You must select the image(s) you wish to be written to file", CONSOLECOLOURS::Warning);
//cv::imwrite(fileName.toLocal8Bit().data(), *m_pCurrentImage );
}
}
/**
*
* Opens recent file from list
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::openRecentFile
* Qualifier
* @return void
* Access private
*/
void MainWindow::openRecentFile()
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
QAction *action = qobject_cast<QAction *>(sender());
if (action)
loadFile(action->data().toString());
}
/**
*
* loads image file
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::loadFile
* Qualifier
* @param const QString & fileName
* @return void
* Access public
*/
void MainWindow::loadFile(const QString &fileName)
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
addImage(fileName, imageType::display);
int i = m_ImagesContainer.getNumImages();
if(i>0)
{
try
{
m_currentImageDisplayedIndex = i - 1;
DisplayImage();
}
catch( cv::Exception& e )
{
const char* err_msg = e.what();
std::cout << "exception caught: " << err_msg << std::endl;
}
}
}
/**
*
* Displays message box depending on image type
*
* @author David Watts
* @since 2017/03/17
*
* FullName MainWindow::showMessage
* Qualifier
* @param imageType::imageType type
* @param QMessageBox::Icon icn
* @return void
* Access public
*/
void MainWindow::showMessage(imageType::imageType type, QMessageBox::Icon icn)
{
QString msg("A image of type");
if (type == imageType::mask)
msg += " mask ";
else if (type == imageType::target)
msg += " target ";
else if (type == imageType::test)
msg += " test ";
else if (type == imageType::roi)
msg += " region of interest ";
else if (type == imageType::cclabels)
msg += " connected components ";
else
msg += " different to this one ";
msg += "is required for this function";
showMessage(msg);
}
/**
*
* Displays message box
*
* @author David Watts
* @since 2017/03/17
*
* FullName MainWindow::showMessage
* Qualifier
* @param QString message
* @param QMessageBox::Icon icn
* @return void
* Access public
*/
void MainWindow::showMessage(QString message, QMessageBox::Icon icn)
{
QMessageBox mb;
QPixmap p(":icons/24/targeter.png");
mb.setIcon(icn);
mb.setWindowIcon(p);
mb.setWindowTitle(QString("Targeter Message"));
mb.setText(QString(message));
mb.exec();
}
/**
*
* Sets image histogram in settings dialog
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::setHistogramImage
* Qualifier
* @return void
* Access public
*/
void MainWindow::setHistogramImage()
{
// get histogram image
cv::Mat hIm = getHistogram();
if (hIm.rows > 0)
{
// convert to qt image
QImage qim = HelperFunctions::makeQImage(hIm);
m_settingsDlg.setImageGridDisplaySize(ui->display_image->getDisplaySize());
m_settingsDlg.setHistogramImage(qim);
}
}
/**
*
* Gets image histogram
*
* @author David Watts
* @since 2017/03/17
*
* FullName MainWindow::getHistogram
* Qualifier
* @return cv::Mat
* Access public
*/
cv::Mat MainWindow::getHistogram()
{
int histSize = 255;
float range[] = { 0, histSize };
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
cv::Mat hIm;
if (m_ImagesContainer.getNumImages() > 0)
{
cv::Mat& im = m_ImagesContainer.getImageAt(getValidImageIndex()).getImage();
if (!im.empty())
{
cv::Mat b_hist, gim;
if (im.channels() > 2)
{
cv::cvtColor(im, gim, cv::COLOR_BGR2GRAY);
calcHist(&gim, 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
}
else
calcHist(&im, 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
// get histogram image
hIm = HelperFunctions::displayHistogram(b_hist, histSize);
}
}
return hIm;
}
/**
*
* Creates targeter image and QImage for display from OpenCV image
*
* @author David Watts
* @since 2017/03/07
*
* FullName MainWindow::addCVImage
* Qualifier
* @param cv::Mat im
* @param QString imageName
* @param bool bRGBSwap
* @param imageType type
* @return void
* Access public
*/
void MainWindow::addCVImage(cv::Mat im, QUuid UID, QString imageName, bool bRGBSwap, imageType::imageType type, bool bDisplay, QString fileName)
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif