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->textBrowser->setStyleSheet("font-size: 12pt");
ui->closeBtn->setIcon(QIcon(":/resources/close.png")); ui->closeBtn->setIcon(QIcon(":/resources/close.png"));
ui->backBtn->setIcon(QIcon(":/resources/arrow-left.png")); ui->backBtn->setIcon(QIcon(":/resources/arrow-left.png"));
ui->nonRedundantRandomizationCheckBox->setDisabled(true);
ui->randomizeCheckBox->click(); ui->randomizeCheckBox->click();
graphicsScene = new QGraphicsScene(this); graphicsScene = new QGraphicsScene(this);
@ -44,18 +45,21 @@ void flashExam::on_closeBtn_clicked()
void flashExam::setupCardsList() { void flashExam::setupCardsList() {
QDir dir("/mnt/onboard/onboard/.flashexam"); QDir dir("/mnt/onboard/onboard/.flashexam");
ui->listWidget->clear();
for (const QString &filename : dir.entryList(QDir::Files)) { for (const QString &filename : dir.entryList(QDir::Files)) {
ui->listWidget->addItem(filename); if(!filename.contains(".answers")) {
ui->listWidget->addItem(filename);
}
} }
} }
void flashExam::on_startBtn_clicked() void flashExam::on_startBtn_clicked()
{ {
listName = ui->listWidget->currentItem()->text();
if(ui->listWidget->selectedItems().isEmpty()) { if(ui->listWidget->selectedItems().isEmpty()) {
emit showToast("You must select a cards list"); emit showToast("You must select a cards list");
} }
else { else {
listName = ui->listWidget->currentItem()->text();
QString cardsList = "/mnt/onboard/onboard/.flashexam/" + listName; QString cardsList = "/mnt/onboard/onboard/.flashexam/" + listName;
QString answersList = "/mnt/onboard/onboard/.flashexam/" + listName + ".answers"; QString answersList = "/mnt/onboard/onboard/.flashexam/" + listName + ".answers";
if(QFile::exists(answersList)) { if(QFile::exists(answersList)) {
@ -69,9 +73,14 @@ void flashExam::on_startBtn_clicked()
} }
void flashExam::initCardsList(QString cardsList, QString answersList) { void flashExam::initCardsList(QString cardsList, QString answersList) {
cardsStringList.clear();
answersStringList.clear();
cardsStringList = readFile(cardsList).split(QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n"), QString::SkipEmptyParts); 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); answersStringList = readFile(answersList).split(QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n"), QString::SkipEmptyParts);
randomize = ui->randomizeCheckBox->isChecked(); randomize = ui->randomizeCheckBox->isChecked();
nonRedundantRandomization = ui->nonRedundantRandomizationCheckBox->isChecked();
cardsAlreadyShown.clear();
ui->nonRedundantRandomizationCheckBox->setChecked(false);
cardsTotal = cardsStringList.count() + 1; cardsTotal = cardsStringList.count() + 1;
displayCard(false); displayCard(false);
ui->stackedWidget->setCurrentIndex(1); ui->stackedWidget->setCurrentIndex(1);
@ -79,6 +88,8 @@ void flashExam::initCardsList(QString cardsList, QString answersList) {
void flashExam::on_backBtn_clicked() void flashExam::on_backBtn_clicked()
{ {
this->setDisabled(false);
setupCardsList();
ui->stackedWidget->setCurrentIndex(0); ui->stackedWidget->setCurrentIndex(0);
} }
@ -105,7 +116,29 @@ void flashExam::on_nextBtn_clicked()
void flashExam::displayCard(bool existingCardNumber) { void flashExam::displayCard(bool existingCardNumber) {
if(!existingCardNumber) { if(!existingCardNumber) {
if(randomize) { if(randomize) {
currentCardNumber = QRandomGenerator::global()->bounded(cardsTotal - 1); 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)); QString cardText = displayImage(cardsStringList.at(currentCardNumber));
@ -122,24 +155,26 @@ QString flashExam::displayImage(QString cardText) {
QRegularExpression imageRegex("IMG='([^']+)'"); QRegularExpression imageRegex("IMG='([^']+)'");
QRegularExpressionMatch match = imageRegex.match(cardText); QRegularExpressionMatch match = imageRegex.match(cardText);
if (match.hasMatch()) { 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); log("Displaying image '" + imagePath + "'", className);
if(QFile::exists(imagePath)) {
ui->graphicsView->items().clear(); ui->graphicsView->items().clear();
graphicsScene->clear(); graphicsScene->clear();
QPixmap pixmap("/mnt/onboard/onboard/.flashexam/resources/" + listName + "/" + imagePath); QPixmap pixmap(imagePath);
graphicsScene->addPixmap(pixmap); graphicsScene->addPixmap(pixmap);
ui->graphicsView->setScene(graphicsScene); ui->graphicsView->setScene(graphicsScene);
// Shrinking scene if item is smaller than previous one // Shrinking scene if item is smaller than previous one
QRectF rect = graphicsScene->itemsBoundingRect(); QRectF rect = graphicsScene->itemsBoundingRect();
graphicsScene->setSceneRect(rect); graphicsScene->setSceneRect(rect);
ui->graphicsView->fitInView(graphicsScene->sceneRect(), Qt::KeepAspectRatio); ui->graphicsView->fitInView(graphicsScene->sceneRect(), Qt::KeepAspectRatio);
ui->textBrowser->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum); ui->textBrowser->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
ui->graphicsView->show(); ui->graphicsView->show();
} }
else { else {
log("IMG key not found", className); log("Image does not exist", className);
}
} }
QRegularExpression removeRegex("IMG='[^']+'\\s*"); QRegularExpression removeRegex("IMG='[^']+'\\s*");
@ -147,3 +182,14 @@ QString flashExam::displayImage(QString cardText) {
return 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; int currentCardNumber;
QString listName; QString listName;
bool randomize; bool randomize;
bool nonRedundantRandomization;
QList<int> cardsAlreadyShown;
bool answerShown = false; bool answerShown = false;
QStringList cardsStringList; QStringList cardsStringList;
QStringList answersStringList; QStringList answersStringList;
@ -40,6 +42,7 @@ private slots:
void on_backBtn_clicked(); void on_backBtn_clicked();
void on_revealBtn_clicked(); void on_revealBtn_clicked();
void on_nextBtn_clicked(); void on_nextBtn_clicked();
void on_randomizeCheckBox_toggled(bool checked);
}; };
#endif // FLASHEXAM_H #endif // FLASHEXAM_H

View file

@ -35,21 +35,6 @@
</property> </property>
<item> <item>
<layout class="QGridLayout" name="gridLayout"> <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"> <item row="2" column="0">
<widget class="Line" name="line"> <widget class="Line" name="line">
<property name="frameShadow"> <property name="frameShadow">
@ -75,6 +60,21 @@
</property> </property>
</widget> </widget>
</item> </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"> <item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_2"> <layout class="QGridLayout" name="gridLayout_2">
<property name="bottomMargin"> <property name="bottomMargin">
@ -142,7 +142,7 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="5" column="0"> <item row="6" column="0">
<widget class="Line" name="line_2"> <widget class="Line" name="line_2">
<property name="frameShadow"> <property name="frameShadow">
<enum>QFrame::Shadow::Plain</enum> <enum>QFrame::Shadow::Plain</enum>
@ -152,6 +152,18 @@
</property> </property>
</widget> </widget>
</item> </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> </layout>
</item> </item>
</layout> </layout>
@ -172,44 +184,15 @@
</property> </property>
<item> <item>
<layout class="QGridLayout" name="gridLayout_3"> <layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0"> <item row="3" column="0">
<layout class="QGridLayout" name="gridLayout_4"> <widget class="QTextBrowser" name="textBrowser">
<property name="bottomMargin"> <property name="sizePolicy">
<number>0</number> <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property> </property>
<item row="0" column="0"> </widget>
<widget class="QPushButton" name="backBtn">
<property name="text">
<string/>
</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>
<item row="7" column="0"> <item row="7" column="0">
<widget class="QPushButton" name="nextBtn"> <widget class="QPushButton" name="nextBtn">
@ -233,28 +216,58 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_4">
<property name="bottomMargin">
<number>0</number>
</property>
<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="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"> <item row="2" column="0">
<widget class="QGraphicsView" name="graphicsView"/> <widget class="QGraphicsView" name="graphicsView"/>
</item> </item>
<item row="3" column="0"> <item row="1" column="0">
<widget class="QTextBrowser" name="textBrowser"> <widget class="Line" name="line_5">
<property name="sizePolicy"> <property name="frameShadow">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <enum>QFrame::Shadow::Plain</enum>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property> </property>
</widget> <property name="lineWidth">
</item> <number>2</number>
<item row="5" column="0">
<widget class="QPushButton" name="revealBtn">
<property name="font">
<font>
<bold>true</bold>
</font>
</property> </property>
<property name="text"> <property name="orientation">
<string>Show answer</string> <enum>Qt::Orientation::Horizontal</enum>
</property> </property>
</widget> </widget>
</item> </item>
@ -271,16 +284,15 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="5" column="0">
<widget class="Line" name="line_5"> <widget class="QPushButton" name="revealBtn">
<property name="frameShadow"> <property name="font">
<enum>QFrame::Shadow::Plain</enum> <font>
<bold>true</bold>
</font>
</property> </property>
<property name="lineWidth"> <property name="text">
<number>2</number> <string>Show answer</string>
</property>
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property> </property>
</widget> </widget>
</item> </item>