MainWindow.cpp 21.5 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2 3
#include "MainWindow.h"
#include "ui_MainWindow.h"

Thiago Santini's avatar
Thiago Santini committed
4 5
#include <QSysInfo>

Thiago Santini's avatar
Thiago Santini committed
6
// TODO: automatically detect suitable window positions on first init
7

Thiago Santini's avatar
Thiago Santini committed
8 9 10 11 12 13
void MainWindow::createExtraMenus()
{
    ui->menuBar->addAction("References");
    ui->menuBar->addAction("About");
}

14 15 16 17 18 19 20
MainWindow::MainWindow(QWidget* parent)
    : ERWidget("EyeRecToo", parent)
    , lEyeWidget()
    , rEyeWidget()
    , fieldWidget()
    , synchronizer()
    , ui(new Ui::MainWindow)
Thiago Santini's avatar
Thiago Santini committed
21
{
22
    ui->setupUi(this);
Thiago Santini's avatar
Thiago Santini committed
23 24

    createExtraMenus();
25
    connect(ui->menuBar, SIGNAL(triggered(QAction*)), this, SLOT(menuOption(QAction*)));
Thiago Santini's avatar
Thiago Santini committed
26

27 28
    settings = new QSettings(gCfgDir + "/" + "EyeRecToo.ini", QSettings::IniFormat);
    cfg.load(settings);
Thiago Santini's avatar
Thiago Santini committed
29

30
    ui->statusBar->showMessage(QString("This is version %1").arg(VERSION));
Thiago Santini's avatar
Thiago Santini committed
31 32 33
    setWindowIcon(QIcon(":/icons/EyeRecToo.png"));

    if (!cfg.workingDirectory.isEmpty())
34 35
        setWorkingDirectory(cfg.workingDirectory);
    ui->pwd->setText(QDir::currentPath());
Thiago Santini's avatar
Thiago Santini committed
36

37
    ui->blinker->hide();
Thiago Santini's avatar
Thiago Santini committed
38

39 40 41 42
    logWidget = new LogWidget("Log Widget");
    logWidget->setDefaults(true, { 480, 240 });
    setupWidget(logWidget, settings, ui->log);
    gLogWidget = logWidget;
Thiago Santini's avatar
Thiago Santini committed
43

Thiago Santini's avatar
Thiago Santini committed
44 45 46 47 48 49
    /*
     * WARNING: DO NOT REMOVE THIS CALL to QCameraInfo::availableCameras()
     * Logically, its meaningless, but it guarantees that DirectShow will work
     * properly by CoInitializing it in the main thread.
     */
    volatile QList<QCameraInfo> tmp = QCameraInfo::availableCameras();
50
    Q_UNUSED(tmp);
51 52 53
    checkDelimitiers<EyeData>();
    checkDelimitiers<FieldData>();
    checkDelimitiers<DataTuple>();
Thiago Santini's avatar
Thiago Santini committed
54

55
    gPerformanceMonitor.setFrameDrop(true);
Thiago Santini's avatar
Thiago Santini committed
56 57 58 59

    /*
     * Asynchronous elements
     */
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    lEyeWidget = new CameraWidget("Left Eye Widget", ImageProcessor::Eye);
    lEyeWidget->setDefaults(true, { 320, 240 });
    lEyeWidget->setWindowIcon(QIcon(":/icons/lEyeWidget.png"));
    setupWidget(lEyeWidget, settings, ui->leftEyeCam);
    QThread::msleep(200);
    rEyeWidget = new CameraWidget("Right Eye Widget", ImageProcessor::Eye);
    rEyeWidget->setDefaults(true, { 320, 240 });
    rEyeWidget->setWindowIcon(QIcon(":/icons/rEyeWidget.png"));
    setupWidget(rEyeWidget, settings, ui->rightEyeCam);
    QThread::msleep(200);
    fieldWidget = new CameraWidget("Field Widget", ImageProcessor::Field);
    fieldWidget->setDefaults(true, { 854, 480 });
    fieldWidget->setWindowIcon(QIcon(":/icons/fieldWidget.png"));
    setupWidget(fieldWidget, settings, ui->fieldCam);

    audioRecorder = new AudioRecorder();
Thiago Santini's avatar
Thiago Santini committed
76

Thiago Santini's avatar
Thiago Santini committed
77 78 79 80
    /*
     * Synchronizer
     */
    synchronizer = new Synchronizer();
81 82 83 84
    connect(lEyeWidget, SIGNAL(newData(EyeData)),
        synchronizer, SLOT(newLeftEyeData(EyeData)), Qt::QueuedConnection);
    connect(rEyeWidget, SIGNAL(newData(EyeData)),
        synchronizer, SLOT(newRightEyeData(EyeData)));
Thiago Santini's avatar
Thiago Santini committed
85
    connect(fieldWidget, SIGNAL(newData(FieldData)),
86
        synchronizer, SLOT(newFieldData(FieldData)));
Thiago Santini's avatar
Thiago Santini committed
87 88 89 90

    /*
     * Synchronous elements
     */
91 92 93
    gazeEstimationWidget = new GazeEstimationWidget("Gaze Estimation Widget");
    gazeEstimationWidget->setDefaults(false);
    setupWidget(gazeEstimationWidget, settings, ui->gazeEstimation);
Thiago Santini's avatar
Thiago Santini committed
94
    connect(synchronizer, SIGNAL(newData(DataTuple)),
95 96 97
        gazeEstimationWidget, SIGNAL(inDataTuple(DataTuple)));
    connect(fieldWidget, SIGNAL(newClick(Timestamp, QPoint, QSize)),
        gazeEstimationWidget, SIGNAL(newClick(Timestamp, QPoint, QSize)));
Thiago Santini's avatar
Thiago Santini committed
98 99

    connect(gazeEstimationWidget, SIGNAL(outDataTuple(DataTuple)),
100
        fieldWidget, SLOT(preview(DataTuple)));
Thiago Santini's avatar
Thiago Santini committed
101 102 103 104 105

    journalThread = new QThread();
    journalThread->setObjectName("Journal");
    journalThread->start();
    journalThread->setPriority(QThread::NormalPriority);
106
    journal = new DataRecorderThread("Journal", DataTuple::header());
Thiago Santini's avatar
Thiago Santini committed
107 108 109 110 111 112
    journal->moveToThread(journalThread);
    QMetaObject::invokeMethod(journal, "create");

    networkStream = new NetworkStream();

    networkStream->start(2002);
113
    connect(gazeEstimationWidget, SIGNAL(outDataTuple(DataTuple)), networkStream, SLOT(push(DataTuple)));
Thiago Santini's avatar
Thiago Santini committed
114

115 116 117
    performanceMonitorWidget = new PerformanceMonitorWidget("Performance Monitor Widget");
    performanceMonitorWidget->setDefaults(false);
    setupWidget(performanceMonitorWidget, settings, ui->performanceMonitor);
118

Thiago Santini's avatar
Thiago Santini committed
119 120
    // GUI to Widgets signals
    connect(this, SIGNAL(stopRecording()),
121
        audioRecorder, SLOT(stopRecording()));
Thiago Santini's avatar
Thiago Santini committed
122
    connect(this, SIGNAL(startRecording()),
123 124 125 126 127 128 129
        audioRecorder, SLOT(startRecording()));
    connect(this, SIGNAL(startRecording()),
        lEyeWidget, SLOT(startRecording()));
    connect(this, SIGNAL(stopRecording()),
        lEyeWidget, SLOT(stopRecording()));
    connect(this, SIGNAL(startRecording()),
        rEyeWidget, SLOT(startRecording()));
Thiago Santini's avatar
Thiago Santini committed
130
    connect(this, SIGNAL(stopRecording()),
131
        rEyeWidget, SLOT(stopRecording()));
Thiago Santini's avatar
Thiago Santini committed
132
    connect(this, SIGNAL(startRecording()),
133
        fieldWidget, SLOT(startRecording()));
Thiago Santini's avatar
Thiago Santini committed
134
    connect(this, SIGNAL(stopRecording()),
135
        fieldWidget, SLOT(stopRecording()));
Thiago Santini's avatar
Thiago Santini committed
136
    connect(this, SIGNAL(startRecording()),
137
        gazeEstimationWidget, SLOT(startRecording()));
Thiago Santini's avatar
Thiago Santini committed
138
    connect(this, SIGNAL(stopRecording()),
139 140 141 142 143
        gazeEstimationWidget, SLOT(stopRecording()));
    connect(this, SIGNAL(startRecording()),
        journal, SIGNAL(startRecording()));
    connect(this, SIGNAL(stopRecording()),
        journal, SIGNAL(stopRecording()));
Thiago Santini's avatar
Thiago Santini committed
144

145 146
    loadSoundEffect("rec-start.wav", gExeDir, recStartSound);
    loadSoundEffect("rec-stop.wav", gExeDir, recStopSound);
147

148
    setupWidget(this, settings);
149

150
    /***************************************************************************
Thiago Santini's avatar
Thiago Santini committed
151 152 153
	 *  Commands
	 **************************************************************************/

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    // Calibration
    connect(&commandManager, SIGNAL(toggleCalibration()),
        gazeEstimationWidget, SLOT(toggleCalibration()));
    connect(&commandManager, SIGNAL(toggleMarkerCollection()),
        gazeEstimationWidget, SLOT(toggleMarkerCollection()));
    connect(&commandManager, SIGNAL(toggleRemoteCalibration()),
        gazeEstimationWidget, SLOT(toggleRemoteCalibration()));

    // Recording
    connect(&commandManager, SIGNAL(toggleRecording()),
        ui->recordingToggle, SLOT(click()));
    connect(&commandManager, SIGNAL(toggleRemoteRecording()),
        this, SLOT(toggleRemoteRecording()));

    // Additionals
    connect(&commandManager, SIGNAL(freezeCameraImages()),
        this, SLOT(freezeCameraImages()));
    connect(&commandManager, SIGNAL(unfreezeCameraImages()),
        this, SLOT(unfreezeCameraImages()));
173
    connect(&commandManager, SIGNAL(togglePreview()),
174
        this, SLOT(togglePreview()));
Thiago Santini's avatar
Thiago Santini committed
175 176 177 178 179 180 181
}

MainWindow::~MainWindow()
{
    delete ui;
}

182
void MainWindow::closeEvent(QCloseEvent* event)
Thiago Santini's avatar
Thiago Santini committed
183
{
184 185 186 187 188 189 190
    if (ui->recordingToggle->isChecked()) {
        ui->recordingToggle->setChecked(false);
        on_recordingToggle_clicked();
        // should be equivalent, but just in case click() is connected with a
        // queued connection in the future :-)
        // ui->recordingToggle->click();
    }
Thiago Santini's avatar
Thiago Santini committed
191

192 193 194 195 196 197 198 199 200 201 202
    cfg.workingDirectory = QDir::currentPath();
    if (settings) {
        cfg.save(settings);
        save(settings);
        logWidget->save(settings);
        lEyeWidget->save(settings);
        rEyeWidget->save(settings);
        fieldWidget->save(settings);
        gazeEstimationWidget->save(settings);
        performanceMonitorWidget->save(settings);
    }
Thiago Santini's avatar
Thiago Santini committed
203

204
    qInfo() << "Closing Left Eye Widget...";
205
    if (lEyeWidget) {
Thiago Santini's avatar
Thiago Santini committed
206 207
        lEyeWidget->close();
        lEyeWidget->deleteLater();
Thiago Santini's avatar
Thiago Santini committed
208
        lEyeWidget = nullptr;
Thiago Santini's avatar
Thiago Santini committed
209
    }
210
    qInfo() << "Closing Right Eye Widget...";
211
    if (rEyeWidget) {
Thiago Santini's avatar
Thiago Santini committed
212 213
        rEyeWidget->close();
        rEyeWidget->deleteLater();
Thiago Santini's avatar
Thiago Santini committed
214
        rEyeWidget = nullptr;
Thiago Santini's avatar
Thiago Santini committed
215
    }
216
    qInfo() << "Closing Field Widget...";
217
    if (fieldWidget) {
Thiago Santini's avatar
Thiago Santini committed
218 219
        fieldWidget->close();
        fieldWidget->deleteLater();
Thiago Santini's avatar
Thiago Santini committed
220
        fieldWidget = nullptr;
221
    }
Thiago Santini's avatar
Thiago Santini committed
222

223
    qInfo() << "Closing Gaze Estimation Widget...";
Thiago Santini's avatar
Thiago Santini committed
224 225 226 227 228 229 230 231 232
    if (gazeEstimationWidget) {
        gazeEstimationWidget->close();
        gazeEstimationWidget->deleteLater();
    }

    qInfo() << "Stoping network stream...";
    if (networkStream)
        networkStream->deleteLater();

233
    gPerformanceMonitor.report();
234 235 236 237 238

    qInfo() << "Closing Performance Monitor Widget...";
    if (performanceMonitorWidget) {
        performanceMonitorWidget->close();
        performanceMonitorWidget->deleteLater();
Thiago Santini's avatar
Thiago Santini committed
239
        performanceMonitorWidget = nullptr;
240 241
    }

242
    qInfo() << "Closing Log Widget...";
Thiago Santini's avatar
Thiago Santini committed
243
    if (logWidget) {
Thiago Santini's avatar
Thiago Santini committed
244
        gLogWidget = nullptr;
245
        logWidget->close();
Thiago Santini's avatar
Thiago Santini committed
246
        logWidget->deleteLater();
Thiago Santini's avatar
Thiago Santini committed
247
        logWidget = nullptr;
Thiago Santini's avatar
Thiago Santini committed
248 249
    }

250 251 252
    qInfo() << "Stopping journal and synchronizer";
    if (journal) {
        journal->deleteLater();
Thiago Santini's avatar
Thiago Santini committed
253
        journal = nullptr;
254
    }
Thiago Santini's avatar
Thiago Santini committed
255

256 257 258 259
    if (audioRecorder) {
        audioRecorder->deleteLater();
        audioRecorder = nullptr;
    }
260 261 262

    if (synchronizer) {
        synchronizer->deleteLater();
Thiago Santini's avatar
Thiago Santini committed
263
        synchronizer = nullptr;
264 265 266
    }

    if (settings) {
Thiago Santini's avatar
Thiago Santini committed
267
        settings->deleteLater();
Thiago Santini's avatar
Thiago Santini committed
268
        settings = nullptr;
269
    }
Thiago Santini's avatar
Thiago Santini committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

    event->accept();
}

void MainWindow::setWorkingDirectory(QString dir)
{
    previousPwd = QDir::currentPath();
    QDir::setCurrent(dir);
    ui->pwd->setText(dir);
    qInfo() << "PWD set to" << dir;
}

void MainWindow::on_changePwdButton_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
285 286 287
        QDir::currentPath(),
        QFileDialog::ShowDirsOnly
            | QFileDialog::DontResolveSymlinks);
Thiago Santini's avatar
Thiago Santini committed
288 289 290 291 292 293 294 295 296 297 298 299

    if (!dir.isEmpty()) {
        setWorkingDirectory(dir);
    }
}

void MainWindow::setSubjectName(QString newSubjectName)
{
    QRegExp re("[a-zA-Z0-9-_]*");
    Q_ASSERT(re.isValid());

    if (!re.exactMatch(newSubjectName)) {
Thiago Santini's avatar
Thiago Santini committed
300
        QMessageBox::warning(nullptr,
301 302 303 304
            "Invalid subject name.",
            QString("Invalid name: \"%1\".\n\nSubject names may contain only letters, numbers, dashes (-), and underscores (_).\nIn regex terms: %2").arg(newSubjectName, re.pattern()),
            QMessageBox::Ok,
            QMessageBox::NoButton);
Thiago Santini's avatar
Thiago Santini committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318
        return;
    }

    ui->subject->setText(newSubjectName);
    if (newSubjectName.isEmpty())
        ui->changeSubjectButton->setText("Set");
    else
        ui->changeSubjectButton->setText("Change");

    qInfo() << "Subject set to" << newSubjectName;
}

void MainWindow::on_changeSubjectButton_clicked()
{
319
    QString newSubjectName = QInputDialog::getText(this, "Set subject", "Subject name:", QLineEdit::Normal, QString(), nullptr, Qt::CustomizeWindowHint);
Thiago Santini's avatar
Thiago Santini committed
320 321 322 323 324 325 326 327
    setSubjectName(newSubjectName);
}

bool MainWindow::setupSubjectDirectory()
{
    QString subject = ui->subject->text();

    if (subject.isEmpty()) {
328
        QString tmpSubjectName = QString::number(QDateTime::currentMSecsSinceEpoch() / 1000);
Thiago Santini's avatar
Thiago Santini committed
329
        QMessageBox msgBox(this);
330 331 332
        QPushButton* continueButton = msgBox.addButton("Start Anyway", QMessageBox::ActionRole);
        QPushButton* addButton = msgBox.addButton("Add Subject", QMessageBox::ActionRole);
        QPushButton* cancelButton = msgBox.addButton("Cancel", QMessageBox::ActionRole);
Thiago Santini's avatar
Thiago Santini committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
        msgBox.setText(QString("Currently there is no test subject.\nIf record is started, subject will be set to %1.\t").arg(tmpSubjectName));
        msgBox.exec();
        if (msgBox.clickedButton() == continueButton)
            setSubjectName(tmpSubjectName);
        else if (msgBox.clickedButton() == addButton)
            on_changeSubjectButton_clicked();
        else if (msgBox.clickedButton() == cancelButton)
            return false;

        if (ui->subject->text().isEmpty()) // smartass user still entered an empty string, show him who's the boss
            setSubjectName(tmpSubjectName);
    }

    QString path = QDir::currentPath() + "/" + ui->subject->text();

    unsigned int recording = 0;
349
    if (QDir(path).exists()) {
Thiago Santini's avatar
Thiago Santini committed
350 351 352 353
        QDirIterator it(path);
        while (it.hasNext()) {
            QRegExp re("[0-9]*");
            QString subDir = it.next();
354
            subDir = subDir.mid(path.size() + 1);
Thiago Santini's avatar
Thiago Santini committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
            if (re.exactMatch(subDir)) {
                unsigned int subDirRecId = subDir.toInt();
                if (subDirRecId > recording)
                    recording = subDirRecId;
            }
        }
    }
    recording++;

    path += "/" + QString::number(recording);

    previousPwd = QDir().currentPath();
    QDir().mkpath(path);
    setWorkingDirectory(path);

    return true;
}

void MainWindow::on_recordingToggle_clicked()
{
    if (ui->recordingToggle->isChecked()) {
        if (!setupSubjectDirectory()) {
            ui->recordingToggle->setChecked(false);
            return;
379 380 381 382
        }
        storeMetaDataHead();
        QMetaObject::invokeMethod(performanceMonitorWidget, "on_resetCounters_clicked");
        qInfo() << "Record starting (Subject:" << ui->subject->text() << ")";
Thiago Santini's avatar
Thiago Santini committed
383 384 385 386 387
        ui->changeSubjectButton->setEnabled(false);
        ui->changePwdButton->setEnabled(false);
        emit startRecording();
        ui->recordingToggle->setText("Finish");
        connect(gazeEstimationWidget, SIGNAL(outDataTuple(DataTuple)),
388
            journal, SIGNAL(newData(DataTuple)));
Thiago Santini's avatar
Thiago Santini committed
389 390
        QTimer::singleShot(500, this, SLOT(effectiveRecordingStart())); // TODO: right now we wait a predefined amount of time; ideally, we should wait for an ack from everyone involved
        ui->recordingToggle->setEnabled(false);
Thiago Santini's avatar
Thiago Santini committed
391
        recStartSound.play();
Thiago Santini's avatar
Thiago Santini committed
392 393 394 395
    } else {
        qInfo() << "Record stopped (Subject:" << ui->subject->text() << ")";
        emit stopRecording();
        disconnect(gazeEstimationWidget, SIGNAL(outDataTuple(DataTuple)),
396 397 398
            journal, SIGNAL(newData(DataTuple)));
        storeMetaDataTail();
        killTimer(elapsedTimeUpdateTimer);
Thiago Santini's avatar
Thiago Santini committed
399 400 401 402 403 404 405
        elapsedTime.invalidate();
        ui->elapsedTime->setText("00:00:00");
        ui->recordingToggle->setText("Start");
        ui->blinker->hide();
        setWorkingDirectory(previousPwd);
        ui->changeSubjectButton->setEnabled(true);
        ui->changePwdButton->setEnabled(true);
Thiago Santini's avatar
Thiago Santini committed
406
        recStopSound.play();
407 408
        gPerformanceMonitor.report();
    }
Thiago Santini's avatar
Thiago Santini committed
409 410 411 412 413 414 415 416 417 418 419 420
}
void MainWindow::effectiveRecordingStart()
{
    elapsedTime.restart();
    elapsedTimeUpdateTimer = startTimer(500);
    ui->recordingToggle->setEnabled(true);
    qInfo() << "Record started.";
}

void MainWindow::timerEvent(QTimerEvent* event)
{
    if (event->timerId() == elapsedTimeUpdateTimer) {
421
        ui->elapsedTime->setText(QDateTime::fromTime_t((elapsedTime.elapsed() / 1000 + 0.5), Qt::UTC).toString("hh:mm:ss"));
Thiago Santini's avatar
Thiago Santini committed
422 423 424 425 426 427 428 429

        if (ui->blinker->isVisible())
            ui->blinker->hide();
        else
            ui->blinker->show();
    }
}

430
void MainWindow::widgetButtonReact(QMainWindow* window, bool checked)
Thiago Santini's avatar
Thiago Santini committed
431 432 433 434
{
    if (!window)
        return;

435
    if (checked) {
Thiago Santini's avatar
Thiago Santini committed
436 437
        window->show();
        window->raise();
438
        window->activateWindow();
439 440 441
        window->setFocus();
        if (window->isMinimized())
            window->showNormal();
Thiago Santini's avatar
Thiago Santini committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
    } else
        window->hide();
}

void MainWindow::on_leftEyeCam_clicked()
{
    widgetButtonReact(lEyeWidget, ui->leftEyeCam->isChecked());
}

void MainWindow::on_rightEyeCam_clicked()
{
    widgetButtonReact(rEyeWidget, ui->rightEyeCam->isChecked());
}

void MainWindow::on_fieldCam_clicked()
{
    widgetButtonReact(fieldWidget, ui->fieldCam->isChecked());
}

void MainWindow::on_gazeEstimation_clicked()
{
    widgetButtonReact(gazeEstimationWidget, ui->gazeEstimation->isChecked());
}

Thiago Santini's avatar
Thiago Santini committed
466 467 468 469 470
void MainWindow::on_log_clicked()
{
    widgetButtonReact(logWidget, ui->log->isChecked());
}

471 472 473 474 475
void MainWindow::on_performanceMonitor_clicked()
{
    widgetButtonReact(performanceMonitorWidget, ui->performanceMonitor->isChecked());
}

476
void MainWindow::freezeCameraImages()
Thiago Santini's avatar
Thiago Santini committed
477
{
478 479
    disconnect(gazeEstimationWidget, SIGNAL(outDataTuple(DataTuple)),
        fieldWidget, SLOT(preview(DataTuple)));
Thiago Santini's avatar
Thiago Santini committed
480 481
}

482
void MainWindow::unfreezeCameraImages()
Thiago Santini's avatar
Thiago Santini committed
483
{
484 485
    connect(gazeEstimationWidget, SIGNAL(outDataTuple(DataTuple)),
        fieldWidget, SLOT(preview(DataTuple)));
Thiago Santini's avatar
Thiago Santini committed
486 487 488 489 490 491 492 493 494 495 496 497
}

void MainWindow::menuOption(QAction* action)
{
    if (action->text().toLower() == "references")
        showReferencesDialog();
    if (action->text().toLower() == "about")
        showAboutDialog();
}

void MainWindow::showReferencesDialog()
{
498 499 500 501 502 503 504 505 506
    ReferenceList::add("Santini et al.",
        "PuReST: Robust pupil tracking for real-time pervasive eye tracking",
        "ETRA", "2018b",
        "https://doi.org/10.1145/3204493.3204578");
    ReferenceList::add("Santini et al.",
        "PuRe: Robust pupil detection for real-time pervasive eye tracking",
        "CVIU", "2018a",
        "https://www.sciencedirect.com/science/article/pii/S1077314218300146");
    ReferenceList::add("Santini et al.",
Thiago Santini's avatar
Thiago Santini committed
507 508
        "EyeRecToo: Open-source Software for Real-time Pervasive Head-mounted Eye Tracking",
        "VISAPP", "2017a",
509 510
        "http://www.scitepress.org/DigitalLibrary/PublicationsDetail.aspx?ID=gLeoir7PxnI=&t=1");
    ReferenceList::add("Santini et al.",
Thiago Santini's avatar
Thiago Santini committed
511 512
        "CalibMe: Fast and Unsupervised Eye Tracker Calibration for Gaze-Based Pervasive Human-Computer Interaction",
        "CHI", "2017b",
513 514
        "http://dl.acm.org/citation.cfm?id=3025453.3025950");
    ReferenceList::add("Fuhl et al.",
Thiago Santini's avatar
Thiago Santini committed
515 516
        "ElSe: Ellipse Selection for Robust Pupil Detection in Real-World Environments",
        "ETRA", "2016",
517 518
        "http://dl.acm.org/citation.cfm?id=2857505");
    ReferenceList::add("Fuhl et al.",
Thiago Santini's avatar
Thiago Santini committed
519 520
        "ExCuSe: Robust Pupil Detection in Real-World Scenarios",
        "LNCS", "2015",
521
        "https://link.springer.com/chapter/10.1007/978-3-319-23192-1_4");
Thiago Santini's avatar
Thiago Santini committed
522
#ifdef STARBURST
523
    ReferenceList::add("Li et al.",
Thiago Santini's avatar
Thiago Santini committed
524 525
        "Starburst: A Hybrid Algorithm for Video-based Eye Tracking Combining Feature-based and Model-based Approaches",
        "CVPR", "2005",
526
        "http://ieeexplore.ieee.org/abstract/document/1565386/");
Thiago Santini's avatar
Thiago Santini committed
527 528
#endif
#ifdef SWIRSKI
529
    ReferenceList::add("Swirski et al.",
Thiago Santini's avatar
Thiago Santini committed
530 531
        "Robust real-time pupil tracking in highly off-axis images",
        "ETRA", "2012",
532
        "http://dl.acm.org/citation.cfm?id=2168585");
Thiago Santini's avatar
Thiago Santini committed
533
#endif
534
    ReferenceList::add("Garrido-Jurado et al.",
Thiago Santini's avatar
Thiago Santini committed
535 536
        "Automatic generation and detection of highly reliable fiducial markers under occlusion",
        "Pattern Recognition", "2014",
537 538
        "http://dl.acm.org/citation.cfm?id=2589359");
    ReferenceList::add("Bradski et al.",
Thiago Santini's avatar
Thiago Santini committed
539 540
        "OpenCV",
        "Dr. Dobb’s Journal of Software Tools", "2000",
541 542
        "http://www.drdobbs.com/open-source/the-opencv-library/184404319");
    ReferenceList::add("Qt Project",
Thiago Santini's avatar
Thiago Santini committed
543 544
        "Qt Framework",
        "Online", "2017",
545
        "http://www.qt.io");
Thiago Santini's avatar
Thiago Santini committed
546 547 548 549 550 551 552 553

    QString msg("EyeRecToo utilizes methods developed by multiple people. ");
    msg.append("This section provides a list of these methods so you can easily cite the ones you use :-)<br><br><br>");
    QMessageBox::information(this, "References", msg.append(ReferenceList::text()), QMessageBox::NoButton);
}

void MainWindow::showAboutDialog()
{
554
    QString msg = QString("EyeRecToo v%1<br><br>").arg(VERSION);
Thiago Santini's avatar
Thiago Santini committed
555
    msg.append("Contact: <a href=\"mailto:thiago.santini@uni-tuebingen.de?Subject=[EyeRecToo] Contact\" target=\"_top\">thiago.santini@uni-tuebingen.de</a><br><br>");
556 557 558
    msg.append("Copyright &copy; 2018 Thiago Santini / University of Tübingen<br><br>");
    msg.append(QString("Build: %1 %2").arg(GIT_BRANCH).arg(GIT_COMMIT_HASH));
    QMessageBox::about(this, "About", msg);
Thiago Santini's avatar
Thiago Santini committed
559
}
560

561
void MainWindow::setupWidget(ERWidget* widget, QSettings* settings, QPushButton* button)
562
{
563 564 565
    // TODO: we might consider eventually moving each ERWidget settings to their own file
    widget->load(settings);
    widget->setup();
Thiago Santini's avatar
Thiago Santini committed
566

567
    // Sanitize position
568 569
    bool inScreen = false;
    for (int i = 0; i < QApplication::desktop()->screenCount(); i++)
570
        inScreen |= QApplication::desktop()->screenGeometry(i).contains(widget->pos(), true);
571
    if (!inScreen)
572
        widget->move(QApplication::desktop()->screenGeometry().topLeft());
573

574 575 576 577 578
    if (button) {
        button->setChecked(widget->isVisible());
        connect(widget, SIGNAL(closed(bool)),
            button, SLOT(setChecked(bool)));
    }
579

580 581 582 583
    connect(widget, SIGNAL(keyPress(QKeyEvent*)),
        &commandManager, SLOT(keyPress(QKeyEvent*)));
    connect(widget, SIGNAL(keyRelease(QKeyEvent*)),
        &commandManager, SLOT(keyRelease(QKeyEvent*)));
584
}
585

Thiago Santini's avatar
Thiago Santini committed
586
void MainWindow::toggleRemoteRecording()
587
{
588 589 590 591
    // If the subject name is empty, use the remote label
    if (!ui->recordingToggle->isChecked() && ui->subject->text().isEmpty())
        setSubjectName("remote");
    ui->recordingToggle->click();
592
}
593 594 595

void MainWindow::togglePreview()
{
596
    gFreezePreview = !gFreezePreview;
597
}
Thiago Santini's avatar
Thiago Santini committed
598 599 600

void MainWindow::storeMetaDataHead()
{
601 602 603 604 605 606
    QFile file(metaDataFile);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    QTextStream out(&file);

    QDateTime utc = QDateTime::currentDateTimeUtc();
607
    out << "start_utc" << Token::Delimiter << utc.toString(metaDateFormat) << Token::Newline;
608 609 610 611 612 613 614 615 616 617
    out << "start_timer" << Token::Delimiter << gTimer.elapsed() << Token::Newline;
    out << "version" << Token::Delimiter << VERSION << Token::Newline;
    out << "build" << Token::Delimiter << QString("%1 %2").arg(GIT_BRANCH).arg(GIT_COMMIT_HASH) << Token::Newline;

    QSysInfo system;
    (void)system; // MSVC is giving an unused variable warning so let's satisfy it...
    out << "OS" << Token::Delimiter << QString("%1 %2").arg(system.productType()).arg(system.productVersion()) << Token::Newline;
    out << "Host" << Token::Delimiter << system.machineHostName() << Token::Newline;

    file.close();
Thiago Santini's avatar
Thiago Santini committed
618 619 620 621
}

void MainWindow::storeMetaDataTail()
{
622 623 624 625
    QFile file(metaDataFile);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
        return;
    QTextStream out(&file);
Thiago Santini's avatar
Thiago Santini committed
626

627
    QDateTime utc = QDateTime::currentDateTimeUtc();
628
    out << "end_utc" << Token::Delimiter << utc.toString(metaDateFormat) << Token::Newline;
629
    out << "end_timer" << Token::Delimiter << gTimer.elapsed() << Token::Newline;
Thiago Santini's avatar
Thiago Santini committed
630

631
    file.close();
Thiago Santini's avatar
Thiago Santini committed
632
}