FlashExam: more fixes

This commit is contained in:
Nicolas Mailloux 2024-11-29 10:18:12 -05:00
parent a0a87cffd7
commit 77e3adfe9b
3 changed files with 160 additions and 99 deletions

View file

@ -26,6 +26,7 @@ flashExam::flashExam(QWidget *parent)
ui->textBrowser->setStyleSheet("font-size: 12pt");
ui->closeBtn->setIcon(QIcon(":/resources/close.png"));
ui->backBtn->setIcon(QIcon(":/resources/arrow-left.png"));
ui->nonRedundantRandomizationCheckBox->setDisabled(true);
ui->randomizeCheckBox->click();
graphicsScene = new QGraphicsScene(this);
@ -44,18 +45,21 @@ void flashExam::on_closeBtn_clicked()
void flashExam::setupCardsList() {
QDir dir("/mnt/onboard/onboard/.flashexam");
ui->listWidget->clear();
for (const QString &filename : dir.entryList(QDir::Files)) {
if(!filename.contains(".answers")) {
ui->listWidget->addItem(filename);
}
}
}
void flashExam::on_startBtn_clicked()
{
listName = ui->listWidget->currentItem()->text();
if(ui->listWidget->selectedItems().isEmpty()) {
emit showToast("You must select a cards list");
}
else {
listName = ui->listWidget->currentItem()->text();
QString cardsList = "/mnt/onboard/onboard/.flashexam/" + listName;
QString answersList = "/mnt/onboard/onboard/.flashexam/" + listName + ".answers";
if(QFile::exists(answersList)) {
@ -69,9 +73,14 @@ void flashExam::on_startBtn_clicked()
}
void flashExam::initCardsList(QString cardsList, QString answersList) {
cardsStringList.clear();
answersStringList.clear();
cardsStringList = readFile(cardsList).split(QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n"), QString::SkipEmptyParts);
answersStringList = readFile(answersList).split(QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n"), QString::SkipEmptyParts);
randomize = ui->randomizeCheckBox->isChecked();
nonRedundantRandomization = ui->nonRedundantRandomizationCheckBox->isChecked();
cardsAlreadyShown.clear();
ui->nonRedundantRandomizationCheckBox->setChecked(false);
cardsTotal = cardsStringList.count() + 1;
displayCard(false);
ui->stackedWidget->setCurrentIndex(1);
@ -79,6 +88,8 @@ void flashExam::initCardsList(QString cardsList, QString answersList) {
void flashExam::on_backBtn_clicked()
{
this->setDisabled(false);
setupCardsList();
ui->stackedWidget->setCurrentIndex(0);
}
@ -105,7 +116,29 @@ void flashExam::on_nextBtn_clicked()
void flashExam::displayCard(bool existingCardNumber) {
if(!existingCardNumber) {
if(randomize) {
while(true) {
currentCardNumber = QRandomGenerator::global()->bounded(cardsTotal - 1);
if(nonRedundantRandomization) {
if(!cardsAlreadyShown.contains(currentCardNumber)) {
cardsAlreadyShown.append(currentCardNumber);
break;
}
else {
if(cardsAlreadyShown.count() != cardsTotal - 1) {
log("cardsAlreadyShown already contains random card number chosen, choosing another one", className);
}
else {
emit showToast("No more cards to display");
QTimer::singleShot(5000, this, SLOT(on_backBtn_clicked()));
this->setDisabled(true);
break;
}
}
}
else {
break;
}
}
}
}
QString cardText = displayImage(cardsStringList.at(currentCardNumber));
@ -123,12 +156,13 @@ QString flashExam::displayImage(QString cardText) {
QRegularExpressionMatch match = imageRegex.match(cardText);
if(match.hasMatch()) {
QString imagePath = match.captured(1); // Captured group 1 is the value of IMG
QString imageFile = match.captured(1); // Captured group 1 is the value of IMG
QString imagePath = "/mnt/onboard/onboard/.flashexam/resources/" + imageFile;
log("Displaying image '" + imagePath + "'", className);
if(QFile::exists(imagePath)) {
ui->graphicsView->items().clear();
graphicsScene->clear();
QPixmap pixmap("/mnt/onboard/onboard/.flashexam/resources/" + listName + "/" + imagePath);
QPixmap pixmap(imagePath);
graphicsScene->addPixmap(pixmap);
ui->graphicsView->setScene(graphicsScene);
// Shrinking scene if item is smaller than previous one
@ -139,7 +173,8 @@ QString flashExam::displayImage(QString cardText) {
ui->graphicsView->show();
}
else {
log("IMG key not found", className);
log("Image does not exist", className);
}
}
QRegularExpression removeRegex("IMG='[^']+'\\s*");
@ -147,3 +182,14 @@ QString flashExam::displayImage(QString cardText) {
return cardText;
}
void flashExam::on_randomizeCheckBox_toggled(bool checked)
{
if(checked) {
ui->nonRedundantRandomizationCheckBox->setDisabled(false);
}
else {
ui->nonRedundantRandomizationCheckBox->setDisabled(true);
}
}

View file

@ -23,6 +23,8 @@ private:
int currentCardNumber;
QString listName;
bool randomize;
bool nonRedundantRandomization;
QList<int> cardsAlreadyShown;
bool answerShown = false;
QStringList cardsStringList;
QStringList answersStringList;
@ -40,6 +42,7 @@ private slots:
void on_backBtn_clicked();
void on_revealBtn_clicked();
void on_nextBtn_clicked();
void on_randomizeCheckBox_toggled(bool checked);
};
#endif // FLASHEXAM_H

View file

@ -35,21 +35,6 @@
</property>
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="0">
<widget class="QListWidget" name="listWidget"/>
</item>
<item row="6" column="0">
<widget class="QPushButton" name="startBtn">
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="Line" name="line">
<property name="frameShadow">
@ -75,6 +60,21 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QListWidget" name="listWidget"/>
</item>
<item row="7" column="0">
<widget class="QPushButton" name="startBtn">
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_2">
<property name="bottomMargin">
@ -142,7 +142,7 @@
</item>
</layout>
</item>
<item row="5" column="0">
<item row="6" column="0">
<widget class="Line" name="line_2">
<property name="frameShadow">
<enum>QFrame::Shadow::Plain</enum>
@ -152,6 +152,18 @@
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QCheckBox" name="nonRedundantRandomizationCheckBox">
<property name="font">
<font>
<family>U001</family>
</font>
</property>
<property name="text">
<string>Non-redundant randomized mode</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
@ -172,45 +184,16 @@
</property>
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_4">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QPushButton" name="backBtn">
<property name="text">
<string/>
<item row="3" column="0">
<widget class="QTextBrowser" name="textBrowser">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="cardNumberLabel">
<property name="font">
<font>
<family>U001</family>
</font>
</property>
<property name="text">
<string>Card number</string>
</property>
</widget>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="7" column="0">
<widget class="QPushButton" name="nextBtn">
<property name="font">
@ -233,28 +216,58 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QGraphicsView" name="graphicsView"/>
</item>
<item row="3" column="0">
<widget class="QTextBrowser" name="textBrowser">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_4">
<property name="bottomMargin">
<number>0</number>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="revealBtn">
<item row="0" column="2">
<widget class="QLabel" name="cardNumberLabel">
<property name="font">
<font>
<bold>true</bold>
<family>U001</family>
</font>
</property>
<property name="text">
<string>Show answer</string>
<string>Card number</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="backBtn">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QGraphicsView" name="graphicsView"/>
</item>
<item row="1" column="0">
<widget class="Line" name="line_5">
<property name="frameShadow">
<enum>QFrame::Shadow::Plain</enum>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
</widget>
</item>
@ -271,16 +284,15 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="Line" name="line_5">
<property name="frameShadow">
<enum>QFrame::Shadow::Plain</enum>
<item row="5" column="0">
<widget class="QPushButton" name="revealBtn">
<property name="font">
<font>
<bold>true</bold>
</font>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
<property name="text">
<string>Show answer</string>
</property>
</widget>
</item>