|
| 1 | +#include <QTimer> |
| 2 | +#include "mainwindow.h" |
| 3 | +#include "ui_mainwindow.h" |
| 4 | + |
| 5 | + |
| 6 | +MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), |
| 7 | + ui(new Ui::MainWindow), m_expInterval(0), m_durationInterval(0), |
| 8 | + m_trials(0), m_correctChars(0), m_totTrials(0), |
| 9 | + m_totCorrectChars(0) { |
| 10 | + |
| 11 | + m_randStr = new RandomString(3, "canadian-english-small"); |
| 12 | + totalLenTarget = 0; |
| 13 | + |
| 14 | + //initialize ui |
| 15 | + ui->setupUi(this); |
| 16 | + ui->displayLabel->hide(); |
| 17 | + ui->responseLabel->hide(); |
| 18 | + ui->nextButton->hide(); |
| 19 | + m_randStr->setStringLength(5); |
| 20 | +} |
| 21 | + |
| 22 | +MainWindow::~MainWindow() { |
| 23 | + delete m_randStr; |
| 24 | + delete ui; |
| 25 | +} |
| 26 | + |
| 27 | +void MainWindow::changeEvent(QEvent* e) { |
| 28 | + QMainWindow::changeEvent(e); |
| 29 | + switch (e->type()) { |
| 30 | + case QEvent::LanguageChange: |
| 31 | + ui->retranslateUi(this); |
| 32 | + break; |
| 33 | + default: |
| 34 | + break; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +void MainWindow::on_exposureSlider_valueChanged(int value) { |
| 39 | + ui->exposureLCD->display(value); |
| 40 | +} |
| 41 | + |
| 42 | +void MainWindow::on_lengthSlider_valueChanged(int value) { |
| 43 | + ui->lengthLCD->display(value); |
| 44 | +} |
| 45 | + |
| 46 | +void MainWindow::on_startButton_clicked() { |
| 47 | + //hide start button |
| 48 | + ui->startButton->hide(); |
| 49 | + |
| 50 | + //show game panel |
| 51 | + ui->displayLabel->show(); |
| 52 | + ui->targetString->show(); |
| 53 | + ui->responseLabel->show(); |
| 54 | + ui->responseString->show(); |
| 55 | + ui->nextButton->show(); |
| 56 | + |
| 57 | + //disable settings |
| 58 | + ui->groupBox->setEnabled(false); |
| 59 | + |
| 60 | + m_trials = 0; |
| 61 | + lenTarget = 0; |
| 62 | + |
| 63 | + //initialize the score system |
| 64 | + m_correctChars = 0; |
| 65 | + |
| 66 | + // start trials |
| 67 | + processTrial(); |
| 68 | +} |
| 69 | + |
| 70 | +//start |
| 71 | +void MainWindow::processTrial() { |
| 72 | + // get user defined values |
| 73 | + m_expInterval = ui->exposureSlider->value(); |
| 74 | + m_durationInterval = ui->durationSlider->value(); |
| 75 | + m_randStr->setStringLength(ui->lengthSlider->value()); |
| 76 | + |
| 77 | + //clear response text editor |
| 78 | + ui->responseString->setText(""); |
| 79 | + //display the random string |
| 80 | + QString string = m_randStr->generateString(); |
| 81 | + ui->targetString->setText(string); |
| 82 | + lenTarget += string.count(); |
| 83 | + totalLenTarget += string.count(); |
| 84 | + ui->responseString->setEnabled(false); |
| 85 | + ui->nextButton->setEnabled(false); |
| 86 | + //count the number of trials |
| 87 | + m_trials++; |
| 88 | + m_totTrials++; |
| 89 | + ui->nextButton->setText(QString("String %1").arg(m_trials)); |
| 90 | + //begin exposure |
| 91 | + QTimer::singleShot(m_expInterval, this, SLOT(timerDisplayRandStr())); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +void MainWindow::timerDisplayRandStr() { |
| 96 | + ui->targetString->setText(QString("")); |
| 97 | + //enable the response line editor and next button |
| 98 | + ui->responseString->setEnabled(true); |
| 99 | + ui->responseString->setFocus(); |
| 100 | + ui->nextButton->setEnabled(true); |
| 101 | + clicked = false; |
| 102 | + QTimer::singleShot(m_durationInterval, this, SLOT(on_nextButton_clicked())); |
| 103 | +} |
| 104 | + |
| 105 | +//end |
| 106 | + |
| 107 | +void MainWindow::on_responseString_returnPressed() { |
| 108 | + on_nextButton_clicked(); //Pressing return has same effect as clicking next. |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +void MainWindow::on_nextButton_clicked() { |
| 113 | + |
| 114 | + // prevents method from getting called twice, once from button and another from |
| 115 | + // timer |
| 116 | + if(clicked == false) |
| 117 | + clicked = true; |
| 118 | + else |
| 119 | + return; |
| 120 | + |
| 121 | + int goodChars(m_randStr->numCorrectChars(ui->responseString->text())); |
| 122 | + m_correctChars += goodChars; |
| 123 | + m_totCorrectChars += goodChars; |
| 124 | + ui->correctCharFraction->setText(QString("%1 / %2").arg(m_correctChars).arg(lenTarget)); |
| 125 | + ui->cumulativeFraction->setText(QString("%1 / %2").arg(m_totCorrectChars).arg(totalLenTarget)); |
| 126 | + |
| 127 | + if(m_trials == ui->trialsSpinBox->value()) { |
| 128 | + //hide game panel |
| 129 | + ui->startButton->show(); |
| 130 | + //show start button |
| 131 | + ui->displayLabel->hide(); |
| 132 | + ui->targetString->hide(); |
| 133 | + ui->responseLabel->hide(); |
| 134 | + ui->responseString->hide(); |
| 135 | + ui->nextButton->hide(); |
| 136 | + //Enble settings |
| 137 | + ui->groupBox->setEnabled(true); |
| 138 | + } |
| 139 | + else |
| 140 | + processTrial(); |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +void MainWindow::on_durationSlider_valueChanged(int value) { |
| 146 | + ui->durationLCD->display(value); |
| 147 | +} |
0 commit comments