-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogVideoPlayer.cpp
891 lines (666 loc) · 29.8 KB
/
DialogVideoPlayer.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
#include "DialogVideoPlayer.h"
#include "ui_DialogVideoPlayer.h"
#include <time.h>
#include <sys/timeb.h>
DialogVideoPlayer::DialogVideoPlayer(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogVideoPlayer) {
ui->setupUi(this);
connect(ui->buttonOpen, SIGNAL(clicked()), this, SLOT(openMoviePress()));
connect(ui->buttonPlay, SIGNAL(clicked()), this, SLOT(playButtonPress()));
connect(ui->buttonImage, SIGNAL(clicked()), this, SLOT(openImageFilePress()));
connect(ui->checkBoxSmooth, SIGNAL(clicked(bool)), this, SLOT(settingChanged()));
connect(ui->rbFragment, SIGNAL(clicked(bool)), this, SLOT(settingChanged()));
connect(ui->rbSegment, SIGNAL(clicked(bool)), this, SLOT(settingChanged()));
connect(ui->rbWholeFile, SIGNAL(clicked(bool)), this, SLOT(settingChanged()));
connect(ui->checkBoxLoop, SIGNAL(clicked(bool)), this, SLOT(settingChanged()));
connect(ui->checkBoxMovie, SIGNAL(clicked(bool)), this, SLOT(changeMovieMode()));
connect(ui->spinBoxFragment, SIGNAL(valueChanged(int)), this, SLOT(spinBoxFragmentChanged(int)));
connect(ui->spinBoxSegment, SIGNAL(valueChanged(int)), SLOT(spinBoxSegmentChanged(int)));
connect(ui->spinBoxIndex, SIGNAL(valueChanged(int)), SLOT(spinBoxIndexChanged(int)));
connect(ui->spinBoxTimeOffset, SIGNAL(valueChanged(int)), SLOT(spinBoxTimeOffsetChanged(int)));
connect(ui->sliderDotSize, SIGNAL(valueChanged(int)), SLOT(sliderDotSizeChanged()));
// Set up ui controls
ui->checkBoxMovie->setEnabled(false);
ui->buttonPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
// Set up the main visualization view
ui->visualizationView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->visualizationView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
visualizationScene = new QGraphicsScene(this);
visualizationScene->setSceneRect(-10000,-10000,20000,20000); // to allow centering.....
ui->visualizationView->setScene(visualizationScene);
// Set up the media player (for videos)
visualizationVideoItem = new QGraphicsVideoItem();
visualizationVideoItem->setAspectRatioMode(Qt::KeepAspectRatio);
mediaPlayer = new QMediaPlayer(this);
mediaPlayer->setNotifyInterval(100);
mediaPlayer->setVideoOutput(visualizationVideoItem);
visualizationScene->addItem(visualizationVideoItem);
// Set up screen layer (mirrors screen size)
screenLayerItem = new QGraphicsRectItem();
visualizationScene->addItem(screenLayerItem);
//Set up the background image Pixmap
backgroundImageItem = new QGraphicsPixmapItem();
visualizationScene->addItem(backgroundImageItem);
// Set up the pixmap (to draw the view location)
visualizationPixmapItem = new QGraphicsPixmapItem();
visualizationScene->addItem(visualizationPixmapItem);
// Media player connections
connect(mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError()));
// Set up the timeline
ui->timeLineView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->timeLineView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
timeLineScene = new QGraphicsScene(this);
ui->timeLineView->setScene(timeLineScene);
// Set up timeline pixmap
timeLinePixmapItem = new QGraphicsPixmapItem();
timeLineScene->addItem(timeLinePixmapItem);
// Set up the fixations view
ui->fixationsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->fixationsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
fixationsScene = new QGraphicsScene(this);
ui->fixationsView->setScene(fixationsScene);
// Set up timeline pixmap
fixationsPixmapItem = new QGraphicsPixmapItem();
fixationsScene->addItem(fixationsPixmapItem);
// Set up line indicating time on time line
timeLineTimeItem = new QGraphicsLineItem();
timeLineTimeItem->setPen(QPen(Qt::black, 1, Qt::DashLine));
timeLineTimeItem->setPos(0, 0);
timeLineScene->addItem(timeLineTimeItem);
// Set up pens
smoothPens = new QPen[2];
roughPens = new QPen[4];
whitePen = QPen(Qt::white, 3);
smoothPens[0] = Consts::smoothPenX;
smoothPens[1] = Consts::smoothPenY;
roughPens[0] = Consts::roughPenLeftX;
roughPens[1] = Consts::roughPenRightX;
roughPens[2] = Consts::roughPenLeftY;
roughPens[3] = Consts::roughPenRightY;
}
DialogVideoPlayer::~DialogVideoPlayer() {
playing = false;
mediaPlayer->stop();
delete[] smoothPens;
delete[] roughPens;
delete fixationsPixmapItem;
delete fixationsScene;
delete timeLineTimeItem;
delete timeLinePixmapItem;
delete timeLineScene;
delete visualizationPixmapItem;
delete visualizationVideoItem;
delete visualizationScene;
delete mediaPlayer;
delete ui;
}
void DialogVideoPlayer::loadData(GrafixParticipant* participant, mat &p_roughM_in, mat &p_smoothM_in, mat &p_fixAllM_in, mat &p_segmentsM_in) {
_participant = participant;
p_roughM = &p_roughM_in;
p_smoothM = &p_smoothM_in;
p_fixAllM = &p_fixAllM_in;
segmentsM = p_segmentsM_in;
if (segmentsM.n_rows == 0) {
segmentsM.zeros(1,3);
segmentsM(0,0) = 0;
segmentsM(0,1) = 0;
segmentsM(0,2) = p_roughM->n_rows - 1;
}
// order the segments
uvec indices = sort_index(segmentsM.cols(1,1));
this->segmentsM = segmentsM.rows(indices);
secsFragment = this->_participant->GetProject()->GetProjectSetting(Consts::SETTING_SECS_FRAGMENT, Consts::ACTIVE_CONFIGURATION()).toInt();
hz = this->_participant->GetProject()->GetProjectSetting(Consts::SETTING_HZ, Consts::ACTIVE_CONFIGURATION()).toInt();
samplesPerFragment = secsFragment * hz;
expWidth = this->_participant->GetProject()->GetProjectSetting(Consts::SETTING_EXP_WIDTH, Consts::ACTIVE_CONFIGURATION()).toInt();
expHeight = this->_participant->GetProject()->GetProjectSetting(Consts::SETTING_EXP_HEIGHT, Consts::ACTIVE_CONFIGURATION()).toInt();
if (p_smoothM->is_empty()) {
settingPlaySmooth = false;
ui->checkBoxSmooth->setChecked(false);
ui->checkBoxSmooth->setEnabled(false);
} else {
ui->checkBoxSmooth->setChecked(true);
}
firstSampleMS = (*p_roughM).at(0, 0); //this is the ms of when the player first started
currentIndex = 0;
currentFragment = -1;
currentSegment = 0;
settingChanged();
stopPlaying();
// Load media from settings
QString moviePath = _participant->GetProjectSetting(Consts::SETTING_MOVIE_PATH).toString();
loadMovie(moviePath);
QByteArray imagesPaths = _participant->GetProjectSetting(Consts::SETTING_IMAGE_PATHS).toByteArray();
pathsImages = QJsonDocument::fromJson(imagesPaths).object().toVariantMap();
qDebug() << pathsImages;
updatePlaybackState(0, true);
qDebug() << " finish loading ";
dotSizePercentage = 5;
ui->sliderDotSize->blockSignals(true);
ui->sliderDotSize->setValue(dotSizePercentage * 10);
ui->sliderDotSize->blockSignals(false);
resizeDisplay();
}
void DialogVideoPlayer::resizeEvent (QResizeEvent *event) {
QDialog::resizeEvent(event);
qDebug() << " resize event ";
repaintAll();
}
void DialogVideoPlayer::mousePressEvent(QMouseEvent *mouseEvent) {
bool leftButton = mouseEvent->buttons() & Qt::LeftButton;
QRect timeLineRect = ui->timeLineView->geometry();
bool onTimeLine = timeLineRect.contains(mouseEvent->pos());
if (leftButton && onTimeLine) {
// Set position
double samples_over_time_line = (double)samplesPerFragment * (double)(mouseEvent->pos().x() - timeLineRect.x()) / (double)timeLineRect.width();
int current_position = (currentFragment * samplesPerFragment) + (int)floor(samples_over_time_line);
if (current_position > 0 && current_position < (int)p_roughM->n_rows) {
updatePlaybackState(current_position, true);
}
return;
}
QRect fixationsRect = ui->fixationsView->geometry();
bool onFixations = fixationsRect.contains(mouseEvent->pos());
if (leftButton && onFixations) {
// Set position to beginning of fixation (if on one)
double samples_over_fixations = (double)samplesPerFragment * (double)(mouseEvent->pos().x() - fixationsRect.x()) / (double)fixationsRect.width();
int selected_position = (currentFragment * samplesPerFragment) + (int)floor(samples_over_fixations);
for (uword i = 0; i < p_fixAllM->n_rows ; i++) {
bool fixation_starts_before_selected = p_fixAllM->at(i, FIXCOL_START) <= selected_position;
bool fixation_ends_after_selected = p_fixAllM->at(i, FIXCOL_END) >= selected_position;
//go to start of fixation
if (fixation_starts_before_selected && fixation_ends_after_selected) {
updatePlaybackState(p_fixAllM->at(i, FIXCOL_START), true);
}
}
}
}
void DialogVideoPlayer::resizeDisplay() {
// Calculate resize ratios for resizing
vid_width = visualizationVideoItem->nativeSize().width();
vid_height = visualizationVideoItem->nativeSize().height();
if (vid_width <= 0) { vid_width = expWidth; }
if (vid_height <= 0) { vid_height = expHeight; }
visualizationVideoItem->setSize(QSizeF(vid_width, vid_height));
qDebug() << "exp width: " << expWidth << " exp height: " << expHeight;
qDebug() << "vid width: " << vid_width << " vid height: " << vid_height;
double ratioW = expWidth / vid_width;
double ratioH = expHeight / vid_height;
// smaller ratio will ensure that the image fits in the view
if (ratioW > ratioH) {
//data is wider than video
qDebug() << "data wider than video";
//make heights the same,
display_height = vid_height;
display_width = expWidth * vid_height / expHeight;
vid_offset_y = 0;
vid_offset_x = (display_width - vid_width ) / 2;
} else {
//data is taller than data (or the same)
//make widths the same
display_width = vid_width;
display_height = expHeight * vid_width / expWidth;
vid_offset_x = 0;
vid_offset_y = (display_height - vid_height) / 2;
}
qDebug() << "disp width: " << display_width << " disp height: " << display_height;
qDebug() << "offsetts: " << vid_offset_x << " " << vid_offset_y;
screenLayerItem->setRect(0, 0, display_width, display_height);
ui->visualizationView->fitInView(screenLayerItem, Qt::KeepAspectRatio);
visualizationVideoItem->setPos(vid_offset_x, vid_offset_y);
//ui->visualizationView->centerOn(display_width / 2, display_height / 2);
qDebug() << "resizing: " << ui->visualizationView->size();
qDebug() << "screenLayerItem: " << screenLayerItem->rect();
qDebug() << "trasform: " << ui->visualizationView->transform();
timeLineScene->setSceneRect(ui->timeLineView->rect());
actualDotSize = vid_width * dotSizePercentage / 100;
}
void DialogVideoPlayer::spinBoxSegmentChanged(int value) {
int target_index = qBound(0, value, (int)segmentsM.n_rows);
if (target_index != value) {
ui->spinBoxSegment->setValue(target_index);
}
if (target_index == 0) {
updatePlaybackState(0, true);
} else if (target_index - 1 < (int)segmentsM.n_rows) {
updatePlaybackState(segmentsM(FIXCOL_START,target_index - 1), true);
}
}
void DialogVideoPlayer::spinBoxIndexChanged(int value) {
int target_index = qBound(0, value, (int)p_roughM->n_rows - 1);
if (target_index != value) {
ui->spinBoxIndex->setValue(target_index);
}
updatePlaybackState(target_index, true);
}
void DialogVideoPlayer::spinBoxFragmentChanged(int value) {
int target_index = qBound(0, value * samplesPerFragment, (int)p_roughM->n_rows - 1);
updatePlaybackState(target_index, true);
}
void DialogVideoPlayer::spinBoxTimeOffsetChanged(int value) {
movieOffsetMS = value;
repaintAll();
}
void DialogVideoPlayer::closeEvent(QCloseEvent *event) {
mediaPlayer->stop();
playing = false;
QDialog::closeEvent(event);
}
void DialogVideoPlayer::openMoviePress() {
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"), QDir::homePath());
if (!fileName.isEmpty() && loadMovie(fileName)) {
qDebug() << "setting " << fileName;
_participant->SetProjectSetting(Consts::SETTING_MOVIE_PATH, fileName);
}
}
bool DialogVideoPlayer::loadMovie(QString path) {
qDebug() << "loading " << path;
QFileInfo check_file(path);
// check if file exists and if yes: Is it really a file and no directory?
if (!check_file.exists() || !check_file.isFile()) {
return false;
}
mediaPlayer->setMedia(QUrl::fromLocalFile(path));
// Wait to load...
while(mediaPlayer->mediaStatus() != QMediaPlayer::LoadedMedia && mediaPlayer->mediaStatus() != QMediaPlayer::InvalidMedia) {
qApp->processEvents();
}
if (mediaPlayer->mediaStatus() != QMediaPlayer::InvalidMedia) {
resizeDisplay();
ui->checkBoxMovie->setEnabled(true);
ui->checkBoxMovie->setChecked(true);
return true;
} else {
QMessageBox messageBox;
messageBox.critical(0, "Error", "An error has occured when loading this movie!");
messageBox.setFixedSize(500, 200);
}
return false;
}
void DialogVideoPlayer::playButtonPress() {
if (playing) {
stopPlaying();
} else {
startPlaying();
}
}
void DialogVideoPlayer::stopPlaying() {
qDebug() << "Stop Playing";
playing = false;
ui->buttonPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
}
void DialogVideoPlayer::startPlaying() {
qDebug() << "Start Playing";
resizeDisplay();
playing = true;
ui->buttonPlay->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
if (ui->checkBoxMovie->isChecked()) {
if (mediaPlayer->mediaStatus() == QMediaPlayer::InvalidMedia) {
ui->checkBoxMovie->setChecked(false);
ui->checkBoxMovie->setEnabled(false);
stopPlaying();
return;
}
while(mediaPlayer->mediaStatus() == QMediaPlayer::LoadingMedia) {
qApp->processEvents();
}
mediaPlayer->setPosition(currentTimeMS + movieOffsetMS - firstSampleMS);
mediaPlayer->play();
while(mediaPlayer->state() == QMediaPlayer::PlayingState && playing) {
// Get position in ms with offset since start of movie and map to data
double current_movie_time = (double)(mediaPlayer->position()) + firstSampleMS - movieOffsetMS;
updatePlaybackStateTime(current_movie_time);
qApp->processEvents();
}
stopPlaying();
mediaPlayer->pause();
} else {
clockStartTime = getMilliCount();
playStartSampleMS = p_roughM->at(qMax(0, currentIndex), 0 );
while(playing) {
double current_movie_time = playStartSampleMS + getMilliCount() - clockStartTime;
updatePlaybackStateTime(current_movie_time);
qApp->processEvents();
}
stopPlaying();
}
}
int DialogVideoPlayer::getMilliCount() {
timeb tb;
ftime(&tb);
int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
return nCount;
}
void DialogVideoPlayer::repaintAll() {
resizeDisplay();
paintTimeLine();
paintFixations();
paintCurrentTimeLineLineFrame();
paintCurrentVisualizationFrame();
}
void DialogVideoPlayer::updatePlaybackStateTime(double newTimeMS) {
currentTimeMS = newTimeMS;
//Search for current index
int last_index = (*p_roughM).n_rows - 1;
int previousIndex = qMax(currentIndex, 0);
if (p_roughM->at(previousIndex, 0) > currentTimeMS ) {
previousIndex = 0;
}
int current_index = -1;
for(int search_index = previousIndex; search_index <= last_index; search_index++) {
double searched_time = (*p_roughM).at(search_index, 0);
if (searched_time >= currentTimeMS) {
//found latest frame - paint
current_index = search_index;
break;
}
}
updatePlaybackState(current_index, false);
}
void DialogVideoPlayer::updatePlaybackState(int index, bool resetTimeSource) {
bool trigger_fragment_mode = settingPlayMode == PlayModeFragment &&
(index >= ((currentFragment + 1) * samplesPerFragment) || index == -1);
if (trigger_fragment_mode) {
if (settingLoop) {
updatePlaybackState(currentFragment * samplesPerFragment, true);
} else {
stopPlaying();
}
return;
}
bool trigger_whole_file_mode = settingPlayMode == PlayModeWholeFile &&
(index >= (int)p_roughM->n_rows || index == -1);
if (trigger_whole_file_mode) {
if (settingLoop) {
updatePlaybackState(0, true);
} else {
stopPlaying();
}
return;
}
bool segment_in_bounds = currentSegment > 0 && currentSegment - 1 < (int)segmentsM.n_rows;
bool trigger_segment_mode = segment_in_bounds && settingPlayMode == PlayModeSegment &&
(index >= segmentsM(currentSegment - 1, SEGCOL_END) || index == -1);
if (trigger_segment_mode) {
if (settingLoop) {
updatePlaybackState(segmentsM(currentSegment - 1, SEGCOL_START), true);
} else {
stopPlaying();
}
return;
}
// Display index
currentIndex = index;
currentTimeMS = p_roughM->at(qMax(0, currentIndex), 0 );
// Display time since start
int milliseconds = currentTimeMS - firstSampleMS;
int minutes = floor(milliseconds / 60000);
milliseconds -= minutes * 60000;
int seconds = floor(milliseconds / 1000);
milliseconds -= seconds * 1000;
ui->labelTime->setText(QString("Time (m:s:ms): %1:%2:%3").arg(minutes).arg(seconds).arg(milliseconds));
ui->spinBoxIndex->blockSignals(true);
ui->spinBoxIndex->setValue(currentIndex);
ui->spinBoxIndex->blockSignals(false);
// Calculate current fragment
int current_fragment = currentIndex > -1 ? (int)floor((double)currentIndex / samplesPerFragment) : -1;
if (current_fragment != currentFragment) {
currentFragment = current_fragment;
paintTimeLine();
paintFixations();
}
ui->spinBoxFragment->blockSignals(true);
ui->spinBoxFragment->setValue(currentFragment);
ui->spinBoxFragment->blockSignals(false);
// Calculate current segment
int current_segment = 0; // 0 means at start of file
for (uword i = 0; i < segmentsM.n_rows -1; ++i) {
if (currentIndex >= segmentsM(i, SEGCOL_START)) {
current_segment = i;
}
}
if (currentSegment != current_segment + 1) {
currentSegment = current_segment + 1;
if (!ui->checkBoxMovie->isChecked()) {
paintBackgroundImage();
}
}
ui->spinBoxSegment->blockSignals(true);
ui->spinBoxSegment->setValue(currentSegment);
ui->spinBoxSegment->blockSignals(false);
paintCurrentVisualizationFrame();
paintCurrentTimeLineLineFrame();
if (resetTimeSource) {
if (ui->checkBoxMovie->isChecked()) {
mediaPlayer->setPosition(currentTimeMS + movieOffsetMS - firstSampleMS);
} else {
clockStartTime = getMilliCount();
playStartSampleMS = p_roughM->at(qMax(0, currentIndex), 0 );
}
}
}
void DialogVideoPlayer::paintCurrentVisualizationFrame() {
bool show_fixation_numbers = true;
QPixmap pixmap(display_width, display_height);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
QPen myPen(QColor(0,0,0,127), 1, Qt::SolidLine);
myPen.setCapStyle(Qt::RoundCap);
painter.setPen(myPen);
if (show_fixation_numbers && p_fixAllM->n_rows > 0) {
QFont serifFont("Times", 64, QFont::Bold);
painter.setFont(serifFont);
uvec fixIndex = arma::find((*p_fixAllM).col(FIXCOL_START) <= currentIndex);
mat aux = (*p_fixAllM).rows(fixIndex);
fixIndex = arma::find(aux.col(FIXCOL_END) >= currentIndex);
if (fixIndex.n_rows != 0) {
painter.drawText( QRect(0, 0, display_width, display_height), Qt::AlignCenter, QString::number(fixIndex(0)) );
}
}
mat *matrixToUse = settingPlaySmooth ? p_smoothM : p_roughM;
if (matrixToUse->is_empty()) return;
QPen *pensToUse = settingPlaySmooth ? smoothPens : roughPens;
int nPensToUse = settingPlaySmooth ? 1 : 2;
double width_multi = settingPlaySmooth ? (double)display_width / (double)expWidth : display_width;
double height_multi = settingPlaySmooth ? (double)display_height / (double)expHeight : display_height;
for(int i = 0; i < nPensToUse; ++i) {
myPen.setColor(pensToUse[i * 2].color());
// Pupil width
if (p_roughM->n_cols >= 8 && p_roughM->at(currentIndex, 6) > 0 && p_roughM->at(currentIndex, 7) > 0) {
double width = (p_roughM->at(currentIndex, 6) + p_roughM->at(currentIndex, 7)) / 6;
int penWidth = (int)floor(width * actualDotSize);
myPen.setWidth(penWidth);
} else {
myPen.setWidth(actualDotSize);
}
uword index = 2 * (i + 1);
int x = matrixToUse->at(currentIndex, index) * width_multi;
int y = matrixToUse->at(currentIndex, index + 1) * height_multi;
painter.setPen(myPen);
painter.drawPoint(x,y);
painter.setPen(whitePen);
painter.drawPoint(x,y);
}
painter.end();
visualizationPixmapItem->setPixmap(pixmap);
}
void DialogVideoPlayer::paintCurrentTimeLineLineFrame() {
// Get how far in fragment currentIndex is
int index_in_fragment = currentIndex - (currentFragment * samplesPerFragment);
double pos_in_fragment = (double)index_in_fragment / (double)samplesPerFragment;
qreal x = pos_in_fragment * ui->timeLineView->width();
qreal y = ui->timeLineView->height();
timeLineTimeItem->setLine(x, 0, x, y);
}
void DialogVideoPlayer::paintNoFrameFound() {
QPixmap pixmap(display_width, display_height);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
QFont serifFont("Times", 12, QFont::Bold);
painter.setFont(serifFont);
painter.drawText( QPoint( 5, 20 ), "No valid data found!" );
painter.end();
visualizationPixmapItem->setPixmap(pixmap);
// Clear time line
QPixmap timeLinePixmap = QPixmap(ui->timeLineView->width(), ui->timeLineView->height());
timeLinePixmap.fill(Qt::white);
timeLinePixmapItem->setPixmap(timeLinePixmap);
}
void DialogVideoPlayer::paintTimeLine() {
QPixmap timeLinePixmap = QPixmap(ui->timeLineView->width(), ui->timeLineView->height());
timeLinePixmap.fill(Qt::white);
QPainter painter(&timeLinePixmap);
double width = timeLinePixmap.width();
double height = settingPlaySmooth ? timeLinePixmap.height() / (double)expWidth : timeLinePixmap.height();
mat *matrixToUse = settingPlaySmooth ? p_smoothM : p_roughM;
QPen *pensToUse = settingPlaySmooth ? smoothPens : roughPens;
int nPensToUse = settingPlaySmooth ? 2 : 4;
// Get starting index
uword startIndex = currentFragment * samplesPerFragment;
// Sanity check for number of rows
if (startIndex >= matrixToUse->n_rows) return;
// Get draw limit
uword limit = qMin(matrixToUse->n_rows - 1, startIndex + samplesPerFragment) - startIndex;
for (uword i = 0; i < limit; ++i) {
uword index = i + startIndex;
double x = (double)i * width / (double)samplesPerFragment;
for(int j = 0; j < nPensToUse; ++j) {
uword col = (uword)j + 2;
if (matrixToUse->at(index, col) != -1) {
painter.setPen(pensToUse[j]);
painter.drawPoint(x, matrixToUse->at(index, col) * height);
}
}
}
painter.end();
timeLinePixmapItem->setPixmap(timeLinePixmap);
}
void DialogVideoPlayer::paintFixations() {
QPixmap fixationsPixmap = QPixmap(ui->fixationsView->width(), ui->fixationsView->height());
fixationsPixmap.fill(Qt::white);
QPainter painter(&fixationsPixmap);
double positionMultiplier = (double)ui->fixationsView->width() / (double)samplesPerFragment;
int counter = 0;
QFont font = painter.font() ;
font.setPointSize ( 8 );
painter.setFont(font);
std::stringstream number;
number.str("");
double posStart = 0;
double posEnd = 0;
uword start_index = currentFragment * samplesPerFragment;
uword end_index = start_index + samplesPerFragment;
int auxIndex = 0;
if (p_fixAllM->n_rows > 0) {
uvec fixIndex = arma::find(p_fixAllM->col(FIXCOL_END) >= start_index);
if (!fixIndex.is_empty()) {
auxIndex = fixIndex(0);
} else {
auxIndex = p_fixAllM->n_rows ;
}
}
for (uword i = auxIndex; i < p_fixAllM->n_rows ; i++) {
bool fixation_starts_after_fragment_start = p_fixAllM->at(i, FIXCOL_START) >= start_index;
bool fixation_starts_before_fragment_end = p_fixAllM->at(i, FIXCOL_START) <= end_index;
bool fixation_starts_before_fragment_start = p_fixAllM->at(i, FIXCOL_START) <= start_index;
bool fixation_ends_after_fragment_start = p_fixAllM->at(i, FIXCOL_END) >= start_index;
if ((fixation_starts_after_fragment_start && fixation_starts_before_fragment_end) ||
(fixation_starts_before_fragment_start && fixation_ends_after_fragment_start)) {
// If it's a fixation that didn't end in the previous segment
if (fixation_starts_before_fragment_start && fixation_ends_after_fragment_start )
posStart = -1;
else
posStart = (p_fixAllM->at(i, FIXCOL_START) - start_index ) * positionMultiplier;
posEnd = ((p_fixAllM->at(i, FIXCOL_END) - start_index ) * positionMultiplier) - posStart;
if (p_fixAllM->at(i, FIXCOL_SMOOTH_PURSUIT) == Consts::SMOOTHP_YES) {
painter.setBrush(QBrush("#ffbc00"));
} else {
painter.setBrush(QBrush("#1ac500"));
}
painter.drawRect(QRect(posStart, 12, posEnd, 16));
number.str("");
number << counter;
painter.drawText( QPoint(posStart, 9), number.str().c_str() );
counter ++; // Next fixation
} else if (p_fixAllM->at(i, FIXCOL_END) >= end_index) {
break;
}
}
painter.end();
fixationsPixmapItem->setPixmap(fixationsPixmap);
}
void DialogVideoPlayer::paintBackgroundImage(){
if (currentSegment <= 0 || currentSegment > (int)segmentsM.n_rows || segmentsM.is_empty()) {
clearBackgroundImage();
return;
}
QString id = QString((int)segmentsM(currentSegment - 1, SEGCOL_ID));
qDebug() << "paitning: " << id;
// Find the correct id
if (pathsImages.contains(id)) {
QString path = pathsImages.value(id).toString();
qDebug() << "Paitning at path " << path;
QPixmap pixmap = QPixmap(path);
QSize size(display_width , display_height);
pixmap = pixmap.scaled(size);
backgroundImageItem->setPixmap(pixmap);
} else {
clearBackgroundImage();
}
}
void DialogVideoPlayer::clearBackgroundImage() {
//clear the background
QPixmap clear = QPixmap(expWidth, expHeight);
clear.fill(Qt::transparent);
backgroundImageItem->setPixmap(clear);
}
void DialogVideoPlayer::loadImages(){
}
void DialogVideoPlayer::openImageFilePress(){
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),tr("Files (*.*)"));
QFileInfo check_file(fileName);
if (!fileName.isEmpty() && check_file.exists() && check_file.isFile()){
QString number = QString((int)segmentsM(currentSegment - 1, SEGCOL_ID));
pathsImages[number] = fileName;
_participant->SetProjectSetting(Consts::SETTING_IMAGE_PATHS, QJsonDocument::fromVariant(pathsImages).toJson());
paintBackgroundImage();
}
}
void DialogVideoPlayer::changeMovieMode() {
stopPlaying();
if (!ui->checkBoxMovie->isChecked()) {
paintBackgroundImage();
ui->labelTimeOffset->setEnabled(false);
ui->spinBoxTimeOffset->setEnabled(false);
} else {
clearBackgroundImage();
ui->labelTimeOffset->setEnabled(true);
ui->spinBoxTimeOffset->setEnabled(true);
}
}
void DialogVideoPlayer::settingChanged() {
if (ui->rbFragment->isChecked()) {
settingPlayMode = DialogVideoPlayer::PlayModeFragment;
} else if (ui->rbSegment->isChecked()) {
settingPlayMode = DialogVideoPlayer::PlayModeSegment;
} else if (ui->rbWholeFile->isChecked()) {
settingPlayMode = DialogVideoPlayer::PlayModeWholeFile;
}
if (settingPlaySmooth != ui->checkBoxSmooth->isChecked()) {
settingPlaySmooth = ui->checkBoxSmooth->isChecked();
paintTimeLine();
}
settingLoop = ui->checkBoxLoop->isChecked();
}
void DialogVideoPlayer::sliderDotSizeChanged() {
dotSizePercentage = (double)ui->sliderDotSize->value() / (double)10;
resizeDisplay();
repaintAll();
}
void DialogVideoPlayer::handleError() {
mediaPlayer->stop();
ui->checkBoxMovie->setChecked(false);
ui->checkBoxMovie->setEnabled(false);
settingChanged();
}