-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
6112 lines (4931 loc) · 166 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
// 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 "MultiWatcher.h"
#include "drawingObjects.h"
#include "haar.h"
//#include <QThread>
#include <QtConcurrent/QtConcurrent>
#include <QFutureWatcher>
#include <QFuture>
#include <QMessageBox>
// end QConcurent stuff
#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 <QTransform>
#include <QtGlobal>
#include <QMatrix4x4>
#include <regex>
#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;
Q_DECLARE_METATYPE(ACTIONS::action);
#define CONNECTCAST(class,fun,args) static_cast<void(class::*)args>(&class::fun)
/*
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) : basCamera(new BaslerCamera()) ,
m_settings(new SettingsValues()),
// m_experimentalData(new ExperimentalData()),
QMainWindow(parent),
ui(new Ui::MainWindow)
{
#ifdef DEBUGPRINT
qDebug() << "Function Name: " << Q_FUNC_INFO;
#endif
//m_CurrentQImage = nullptr;
m_pStageXY = nullptr;
m_pStageZ = nullptr;
//m_pFocusCUDA = nullptr;
ui->setupUi(this);
// to send these data types over signals slots mechanism
qRegisterMetaType<cv::Mat>("cv::Mat");
qRegisterMetaType<QVector<QString> >("QVector<QString>");
qRegisterMetaType<ACTIONS::action>("ACTIONS::action");
qRegisterMetaType<FIDUCIAL::position>("FIDUCIAL::position");
qRegisterMetaType<QTextCursor>("QTextCursor");
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->setCameras(QString("21799625"), QString("21799596"));
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;
m_settingsDlg.create(m_settings.data());
// get settings from serialised file
deSerialiseSettings();
// set display to point to image container
ui->display_image->setImageContainerPointer(&m_ImagesContainer);
ui->actionReference_fiducial_marks->setEnabled(!m_settings->bLockFiducial);
ui->display_image->updatePaddTarget(m_settings->bPaddTargetImage, m_settings->numWaveletLevels);
setUpSlotsSignals();
// set up stages
//emit m_pMicroscopeStages.getAvailablePorts();
//emit m_pMicroscopeStages.assignPorts();
}
/**
*
* 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_pWorkerThreadXY->quit();
m_pWorkerThreadXY->wait();
m_pWorkerThreadZ->quit();
m_pWorkerThreadZ->wait();
/*
m_pWorkerThreadCUDA->quit();
m_pWorkerThreadCUDA->wait();
*/
if (m_pWorkerThreadXY != nullptr)
delete m_pWorkerThreadXY;
if (m_pWorkerThreadZ != nullptr)
delete m_pWorkerThreadZ;
/*
if (m_pWorkerThreadCUDA != nullptr)
delete m_pWorkerThreadCUDA;
if (m_pStageXY != nullptr)
delete m_pStageXY;
if (m_pStageZ != nullptr)
delete m_pStageZ;*/
/*
if (m_pFocusCUDA != nullptr)
delete m_pFocusCUDA;
*/
}
void MainWindow::serialiseSettings(bool fromBackup)
{
QFile settingsFile;
QString XMLfilename;
if (fromBackup)
settingsFile.setFileName("targetter_settings_BACKUP.dat");
else
settingsFile.setFileName("targetter_settings.dat");
if (settingsFile.open(QIODevice::WriteOnly))
{
QDataStream out(&settingsFile); // we will serialize the data into the file
m_settings->serialize(out);
settingsFile.close();
}
/*
if (fromBackup)
XMLfilename = "targeter_project_BACKUP.xml";
else
XMLfilename = m_settings->s_project_FilenamePrefix + m_settings->s_project_Barcode + ".xml";
XMLWriter projectWriter(this);
projectWriter.OpenFile(m_settings->s_project_Directory, XMLfilename);
// only add to settings node
projectWriter.writeProjectSettings(m_settings.data());
*/
}
void MainWindow::saveBackup(bool bLoad)
{
if (bLoad)
deSerialiseSettings(true);
else
serialiseSettings(true);
}
void MainWindow::deSerialiseSettings(bool fromBackup)
{
QFile settingsFile;
QString XMLfilename;
if (fromBackup)
settingsFile.setFileName("targetter_settings_BACKUP.dat");
else
settingsFile.setFileName("targetter_settings.dat");
if (settingsFile.open(QIODevice::ReadOnly))
{
QDataStream in(&settingsFile); // we will serialize the data into the file
m_settings->deserialize(in);
registerFiducialMarks();
settingsFile.close();
}
else
m_settings->initialize();
/*
if (fromBackup || m_settings->s_project_FilenamePrefix == "" && m_settings->s_project_Barcode == "")
XMLfilename = "targeter_project_BACKUP.xml";
else
XMLfilename = m_settings->s_project_FilenamePrefix + m_settings->s_project_Barcode + ".xml";
// now get project properties
QMap<QString, QVariant> map;
XMLWriter projectWriter(this);
projectWriter.OpenFile(m_settings->s_project_Directory, XMLfilename);
projectWriter.readProjectInformation(XMLfilename, m_settings.data());
*/
// update dialog
m_settingsDlg.initControls();
}
/*
emit createTransformationMatrix(QVector3D(3,3,3), QVector3D(6,3,3), QVector3D(3, 6,6)); // test matrix
*/
void MainWindow::registerFiducialMarks()
{
if (m_settingsDlg.checkFiducialsOrder(m_settings->fiducial_marks_stage, m_settings->fiducial_marks_image, false))
{
// create QRect of fiducial positions in image
QVector3D stageTL = m_settings->fiducial_marks_stage[FIDUCIAL::position::topleft_microscope];
QVector3D stageTR = m_settings->fiducial_marks_stage[FIDUCIAL::position::topright_microscope];
QVector3D stageBL = m_settings->fiducial_marks_stage[FIDUCIAL::position::bottomleft_microscope];
QVector3D stageBR = m_settings->fiducial_marks_stage[FIDUCIAL::position::bottomright_microscope];
QPointF stageImageTL = m_settings->fiducial_marks_image[FIDUCIAL::position::topleft_microscope];
QPointF stageImageTR = m_settings->fiducial_marks_image[FIDUCIAL::position::topright_microscope];
QPointF stageImageBL = m_settings->fiducial_marks_image[FIDUCIAL::position::bottomleft_microscope];
QPointF stageImageBR = m_settings->fiducial_marks_image[FIDUCIAL::position::bottomright_microscope];
// adjust stage position by offsetting fiducial position in image in microns
m_settings->fiducial_marks_stageAndImage[FIDUCIAL::position::topleft_microscope] = HelperFunctions::addPointToVector3D(stageTL, stageImageTL*m_settings->mmPerPixel);
m_settings->fiducial_marks_stageAndImage[FIDUCIAL::position::topright_microscope] = HelperFunctions::addPointToVector3D(stageTR, stageImageTR*m_settings->mmPerPixel);
m_settings->fiducial_marks_stageAndImage[FIDUCIAL::position::bottomleft_microscope] = HelperFunctions::addPointToVector3D(stageBL, stageImageBL*m_settings->mmPerPixel);
m_settings->fiducial_marks_stageAndImage[FIDUCIAL::position::bottomright_microscope] = HelperFunctions::addPointToVector3D(stageBR, stageImageBR*m_settings->mmPerPixel);
QTransform trans;
QPolygonF imageFiducials;
imageFiducials.append(m_settings->fiducial_marks_image[FIDUCIAL::position::topleft_overview]);
imageFiducials.append(m_settings->fiducial_marks_image[FIDUCIAL::position::topright_overview]);
imageFiducials.append(m_settings->fiducial_marks_image[FIDUCIAL::position::bottomleft_overview]);
imageFiducials.append(m_settings->fiducial_marks_image[FIDUCIAL::position::bottomright_overview]);
QPolygonF stageFiducials;
stageFiducials.append(stageTL.toPointF());
stageFiducials.append(stageTR.toPointF());
stageFiducials.append(stageBL.toPointF());
stageFiducials.append(stageBR.toPointF());
// if possible to get transformation matrix
if (QTransform::quadToQuad(imageFiducials, stageFiducials, m_settings->m_coordinateTransform))
LOGCONSOLE("coordinate transform successfully registered", CONSOLECOLOURS::Information);
else
LOGCONSOLE("unable to calculate coordinate transform", CONSOLECOLOURS::Information);
// now create Fiducials to Stage transformation matrix
// position of fiducial mark in stage coordinates relative to microscope image
// create transformation matrix that maps the position of objects in stage/image pixel space to fiducial marks (unit axes) space
createTransformationMatrix(m_settings->fiducial_marks_stageAndImage[FIDUCIAL::position::topleft_microscope],
m_settings->fiducial_marks_stageAndImage[FIDUCIAL::position::topright_microscope],
m_settings->fiducial_marks_stageAndImage[FIDUCIAL::position::bottomleft_microscope]);
}
else
LOGCONSOLE("please ensure that fiducial marks correctly registered", CONSOLECOLOURS::Information);
}
void MainWindow::logFeedback(int score, QString name, QString email, QString institute, QString desc)
{
XMLWriter logWriter(this);
logWriter.openXMLFile("feedback.xml", QIODevice::ReadWrite);
logWriter.appendFeedback(score, name, email, institute, desc);
}
void MainWindow::setUpSlotsSignals()
{
connect(basCamera.data(), &BaslerCamera::processedImage, this, &MainWindow::updateVideoImage);
connect(basCamera.data(), &BaslerCamera::LOGCONSOLE, this, CONNECTCAST(MainWindow, LOGCONSOLE, (QString, CONSOLECOLOURS::colour)));
connect(ui->ImageThumbList, &QListWidget::customContextMenuRequested, this, &MainWindow::ProvideContextMenu);
connect(ui->ImageThumbList, &QListWidget::itemClicked, this, &MainWindow::onThumbImageClick);
// slots for communication from printqlabel class
connect(ui->display_image, &PaintQLabel::disablePanButton, this, &MainWindow::disablePanButton);
connect(ui->display_image, &PaintQLabel::setTargetArea, this, &MainWindow::setTargetArea);
connect(ui->display_image, &PaintQLabel::unsetDrawingButtons, this, &MainWindow::unsetDrawingButtons);
connect(ui->display_image, &PaintQLabel::StatusBarMessage, this, &MainWindow::StatusBarMessage);
connect(ui->display_image, &PaintQLabel::addFiducialMark, this, &MainWindow::addFiducialMark);
connect(ui->display_image, &PaintQLabel::moveObjective, this, &MainWindow::moveObjective);
connect(ui->display_image, &PaintQLabel::setSamplingDistance, this, &MainWindow::setSamplingDistance);
// slot to get values back from modeless dialog
connect(&m_settingsDlg, &SettingsDialog::sendSettings, this, &MainWindow::receiveSettings);
connect(&m_settingsDlg, &SettingsDialog::logFeedback, this, &MainWindow::logFeedback);
connect(&m_settingsDlg, &SettingsDialog::SETFIDUCIALPOSITION, ui->display_image, &PaintQLabel::setFiducialPosition);
connect(&m_settingsDlg, &SettingsDialog::getCOMPORTS, this, &MainWindow::getAvailablePorts);
// logging from Image Processing functions run in separate thread
connect(&m_ImageProcessing, &ImageProcessing::LOGCONSOLE, this, CONNECTCAST(MainWindow, LOGCONSOLE, (QString, CONSOLECOLOURS::colour)));
// XY STAGE SLOTS ///////////////////////////////
m_pStageXY = new StageControlXY;
m_pWorkerThreadXY = new QThread;
// make stage control object a worker thread object
m_pStageXY->moveToThread(m_pWorkerThreadXY);
// Marzhauser stage commands called from settings dialog
connect(&m_settingsDlg, &SettingsDialog::MoveAbsoluteXY, m_pStageXY, &StageControlXY::MoveAbsolute);
connect(&m_settingsDlg, &SettingsDialog::MoveRelativeXY, m_pStageXY, &StageControlXY::MoveRelative);
connect(&m_settingsDlg, &SettingsDialog::SendCommandXY, m_pStageXY, &StageControlXY::SendCommand);
connect(&m_settingsDlg, &SettingsDialog::SetVelocityXY, m_pStageXY, &StageControlXY::SetVelocity);
connect(&m_settingsDlg, &SettingsDialog::CalibrateXY, m_pStageXY, &StageControlXY::Calibrate);
connect(&m_settingsDlg, &SettingsDialog::MeasureRangeXY, m_pStageXY, &StageControlXY::MeasureRange);
connect(&m_settingsDlg, &SettingsDialog::ConnectXY, m_pStageXY, &StageControlXY::Connect);
connect(&m_settingsDlg, &SettingsDialog::DisconnectXY, m_pStageXY, &StageControlXY::Disconnect);
connect(&m_settingsDlg, &SettingsDialog::AbortXY, m_pStageXY, &StageControlXY::Abort);
connect(&m_settingsDlg, &SettingsDialog::SetJoystickXY, m_pStageXY, &StageControlXY::SetJoystick);
connect(&m_settingsDlg, &SettingsDialog::LOGCONSOLE, this, CONNECTCAST(MainWindow, LOGCONSOLE, (QString, CONSOLECOLOURS::colour)));
connect(&m_settingsDlg, &SettingsDialog::LockFiducial, this, &MainWindow::enableFiducial);
connect(&m_settingsDlg, &SettingsDialog::saveBackup, this, &MainWindow::saveBackup);
connect(&m_settingsDlg, &SettingsDialog::moveToFiducialFromOverview, this, &MainWindow::moveObjective);
connect(&m_settingsDlg, &SettingsDialog::createTransformationMatrix, this, &MainWindow::createTransformationMatrix);
connect(&m_settingsDlg, &SettingsDialog::setGrid, ui->display_image, &PaintQLabel::setGrid);
connect(&m_settingsDlg, &SettingsDialog::updatePaddTarget, ui->display_image, &PaintQLabel::updatePaddTarget);
// Marzhauser stage commands called from main program
connect(this, &MainWindow::MoveAbsoluteXY, m_pStageXY, &StageControlXY::MoveAbsolute);
connect(this, &MainWindow::MoveRelativeXY, m_pStageXY, &StageControlXY::MoveRelative);
connect(this, &MainWindow::SendCommandXY, m_pStageXY, &StageControlXY::SendCommand);
connect(this, &MainWindow::SetVelocityXY, m_pStageXY, &StageControlXY::SetVelocity);
connect(this, &MainWindow::CalibrateXY, m_pStageXY, &StageControlXY::Calibrate);
connect(this, &MainWindow::MeasureRangeXY, m_pStageXY, &StageControlXY::MeasureRange);
connect(this, &MainWindow::ConnectXY, m_pStageXY, &StageControlXY::Connect);
connect(this, &MainWindow::DisconnectXY, m_pStageXY, &StageControlXY::Disconnect);
connect(this, &MainWindow::AbortXY, m_pStageXY, &StageControlXY::Abort);
connect(this, &MainWindow::connectToPortXY, m_pStageXY, &StageControlXY::assignPort);
connect(this, &MainWindow::getPositionXY, m_pStageXY, CONNECTCAST(StageControlXY, updatePositions, (bool)));
connect(this, &MainWindow::reportCOMPORTS, &m_settingsDlg, &SettingsDialog::updateCOMPORTS);
#ifdef _HAVE_IMAGEMAGICK
connect(&m_imageReadWrite, &ImageReadWrite::LOGCONSOLE, this, CONNECTCAST(MainWindow, LOGCONSOLE, (QString, CONSOLECOLOURS::colour)));
#endif
connect(&m_Timer, &QTimer::timeout, this, &MainWindow::logDot);
connect(m_pStageXY, &StageControlXY::LOGCONSOLE, this, CONNECTCAST(MainWindow, LOGCONSOLE, (QString, CONSOLECOLOURS::colour)));
connect(m_pStageXY, &StageControlXY::ACTIONCOMPLETED, this, &MainWindow::StageMovementCompleted);
connect(m_pStageXY, &StageControlXY::UPDATEPOSITIONS, this, &MainWindow::updatePositionXY);
connect(m_pStageXY, &StageControlXY::CONNECTEDTOPORT, this, &MainWindow::StageConnectedXY);
connect(m_pStageXY, &StageControlXY::STAGEMOVED, this, &MainWindow::stageMovedXY);
// clean up
connect(m_pWorkerThreadXY, &QThread::finished, m_pStageXY, &StageControlXY::deleteLater);
m_pWorkerThreadXY->start();
// Z STAGE SLOTS ///////////////////////////////
m_pStageZ = new StageControlZ;
m_pWorkerThreadZ = new QThread;
// make stage control object a worker thread object
m_pStageZ->moveToThread(m_pWorkerThreadZ);
// signal slots
connect(&m_settingsDlg, &SettingsDialog::MoveAbsoluteZ, m_pStageZ, &StageControlZ::moveAbsolute);
connect(&m_settingsDlg, &SettingsDialog::MoveRelativeZ, m_pStageZ, &StageControlZ::MoveRelative);
connect(&m_settingsDlg, &SettingsDialog::SendCommandZ, m_pStageZ, &StageControlZ::SendCommand);
connect(&m_settingsDlg, &SettingsDialog::ConnectZ, m_pStageZ, &StageControlZ::Connect);
connect(&m_settingsDlg, &SettingsDialog::DisconnectZ, m_pStageZ, &StageControlZ::Disconnect);
connect(&m_settingsDlg, &SettingsDialog::SetKeypadZ, m_pStageZ, &StageControlZ::SetKeypad);
connect(&m_settingsDlg, &SettingsDialog::stopMotionZ, m_pStageZ, &StageControlZ::stopMotion);
connect(this, &MainWindow::MoveAbsoluteZ, m_pStageZ, &StageControlZ::moveAbsolute);
connect(m_pStageZ, &StageControlZ::LOGCONSOLE, this, CONNECTCAST(MainWindow, LOGCONSOLE, (QString, CONSOLECOLOURS::colour)));
connect(m_pStageZ, &StageControlZ::ACTIONCOMPLETED, this, &MainWindow::StageMovementCompleted);
connect(m_pStageZ, &StageControlZ::UPDATEPOSITION, this, &MainWindow::updatePositionZ);
connect(m_pStageZ, &StageControlZ::STAGEMOVED, this, &MainWindow::stageMovedZ);
connect(this, &MainWindow::getPositionZ, m_pStageZ, CONNECTCAST(StageControlZ, updatePosition, (bool)));
// clean up
connect(m_pWorkerThreadZ, &QThread::finished, m_pStageZ, &StageControlZ::deleteLater);
m_pWorkerThreadZ->start();
// XY STAGE SLOTS ///////////////////////////////
/*
m_pFocusCUDA = new focusingCUDA;
m_pWorkerThreadCUDA = new QThread;
// make stage control object a worker thread object
m_pFocusCUDA->moveToThread(m_pWorkerThreadCUDA);
*/
//connect(this, &MainWindow::getFocusCUDA, &m_FocusCUDA, &focusingCUDA::getFocusScore);
//connect(&m_FocusCUDA, &focusingCUDA::setFOCUS, this, &MainWindow::addFocusValue);
//m_pWorkerThreadCUDA->start();
// detect com ports
getAvailablePorts();
}
void MainWindow::updatePositionXY(bool bFid, double x, double y)
{
// update position
m_settings->m_stage_position_XY_X = x;
m_settings->m_stage_position_XY_Y = y;
// updates position label
m_settingsDlg.updatePositions();
if (bFid)
emit getPositionZ(bFid);
}
void MainWindow::updatePositionZ(bool bFid, double z)
{
// update position
m_settings->m_stage_position_Z = z;
// updates position label
m_settingsDlg.updatePosition();
if(bFid)
{
// then this has to be called
m_settingsDlg.setFiducial();
consoleLog("Fiducial Mark added: " + QString::number(m_settings->m_image_position_XY_X) + "," +
QString::number(m_settings->m_image_position_XY_Y) + "," +
QString::number(m_settings->m_stage_position_XY_X) + "," +
QString::number(m_settings->m_stage_position_XY_Y) + "," +
QString::number(m_settings->m_stage_position_Z),
CONSOLECOLOURS::colour::Information);
}
}
// called from drawing label
void MainWindow::addFiducialMark(FIDUCIAL::position pos, QPoint p)
{
m_settings->m_image_position_XY_X = p.x();
m_settings->m_image_position_XY_Y = p.y();
m_settings->fiducialPos = pos;
m_settingsDlg.updateFiducialComboIndex();
// get stage position from Stage manager then add fiducial (in case of manual movements)
emit getPositionXY(true);
}
void MainWindow::assignPortZ(QVector<QString> AvailablePorts, QString excludePort)
{
for (int i = 0; i < AvailablePorts.length(); i++)
{
if (AvailablePorts[i] == excludePort)
continue;
if (m_pStageZ->connectToPort(AvailablePorts[i]))
{
StageConnectedZ(AvailablePorts[i]);
break;
}
}
}
void MainWindow::StageConnectedXY(QString port)
{
// update dialog label
m_settingsDlg.updateCONNECTPORT(true, port);
// connect to available Z
assignPortZ(m_AvailablePorts, port);
}
void MainWindow::StageConnectedZ(QString port)
{
// update dialog label
m_settingsDlg.updateCONNECTPORT(false, port);
}
void MainWindow::getAvailablePorts()
{
TCHAR lpTargetPath[5000]; // buffer to store the path of the COMPORTS
DWORD test;
bool gotPort = 0; // in case the port is not found
QVector<QString> ports;
for (int i = 0; i < 255; i++) // checking ports from COM0 to COM255
{
QString ComName = "COM" + QString::number(i); // converting to COM0, COM1, COM2
test = QueryDosDevice((LPWSTR)ComName.utf16(), (LPWSTR)lpTargetPath, 5000);
// Test the return value and error if any
if (test != 0) //QueryDosDevice returns zero if it didn't find an object
{
m_AvailablePorts.push_back(ComName); // add to the ComboBox
ports.push_back(ComName);
gotPort = 1; // found port
}
if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) //in case buffer got filled
{
lpTargetPath[10000]; // in case the buffer got filled, increase size of the buffer.
continue;
}
}
if (!gotPort) // if not port
m_AvailablePorts.push_back(QString("No Active Ports Found")); // to display error message incase no ports found
emit reportCOMPORTS(ports);
emit connectToPortXY(ports, "");
consoleLog("COM ports scanned");
}
void MainWindow::enableFiducial(bool bEnable)
{
ui->actionReference_fiducial_marks->setEnabled(!bEnable);
if (!bEnable)
ui->display_image->setDrawingMode(drawingMode::none);
}
void MainWindow::StageMovementCompleted(QString message)
{
consoleLog(message, CONSOLECOLOURS::colour::Information);
QApplication::restoreOverrideCursor();
}
void MainWindow::shutDownInstruments()
{
basCamera->detachCameras();
}
void MainWindow::addTargeterImage(QExplicitlySharedDataPointer<targeterImage> tim, QAction* pAction)
{
addCVImage(tim, nullptr, true);
LogProcessing(false); // stop timer from showing dots in console
if (pAction != nullptr)
pAction->setChecked(false);
}
void MainWindow::addMatImage(cv::Mat img, QString imagename, imageType::imageType type, QAction* pAction)
{
addCVImage(img, imagename, true, type, true);
LogProcessing(false); // stop timer from showing dots in console
if (pAction != nullptr)
pAction->setChecked(false);
}
bool MainWindow::updateQTImage(cv::Mat img, QString imagename, QAction* pAction)
{
// update last cv image trigger show image on paint control
QExplicitlySharedDataPointer<targeterImage> pTar = m_ImagesContainer.getLastImagePtr();
if (pTar == nullptr)
{
addCVImage(img, 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 != nullptr)
return pAction->isChecked();
else
return false;
}
/**
*
* 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);
*/
}
void MainWindow::logDot() {
/*
//QString msg = "<font color=\"green\"><span>. </span></font>";
QTextCursor c = ui->consoleDisplay->textCursor();
c.movePosition(QTextCursor::End);
ui->consoleDisplay->appendPlainText(". ");
//QMetaObject::invokeMethod(ui->consoleDisplay, "appendHtml", Qt::QueuedConnection, Q_ARG(QString, msg));
*/
ui->consoleDisplay->moveCursor(QTextCursor::End);
ui->consoleDisplay->textCursor().insertHtml(". ");
ui->consoleDisplay->moveCursor(QTextCursor::End);
};
/**
*
* 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
}
QString MainWindow::getSaveFilename(QExplicitlySharedDataPointer<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)