FlashExam: add images display capability

Some bug fixing left to do with QGraphicsView, but it's minor changes.
This commit is contained in:
Nicolas Mailloux 2024-11-28 12:05:41 -05:00
parent 9ee4510fa8
commit a0a87cffd7
3 changed files with 51 additions and 25 deletions

View file

@ -28,6 +28,7 @@ flashExam::flashExam(QWidget *parent)
ui->backBtn->setIcon(QIcon(":/resources/arrow-left.png")); ui->backBtn->setIcon(QIcon(":/resources/arrow-left.png"));
ui->randomizeCheckBox->click(); ui->randomizeCheckBox->click();
graphicsScene = new QGraphicsScene(this);
setupCardsList(); setupCardsList();
} }
@ -50,15 +51,15 @@ void flashExam::setupCardsList() {
void flashExam::on_startBtn_clicked() void flashExam::on_startBtn_clicked()
{ {
QString currentItem = ui->listWidget->currentItem()->text(); 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 {
QString cardsList = "/mnt/onboard/onboard/.flashexam/" + currentItem; QString cardsList = "/mnt/onboard/onboard/.flashexam/" + listName;
QString answersList = "/mnt/onboard/onboard/.flashexam/" + currentItem + ".answers"; QString answersList = "/mnt/onboard/onboard/.flashexam/" + listName + ".answers";
if(QFile::exists(answersList)) { if(QFile::exists(answersList)) {
log("Setting up cards list '" + currentItem + "'", className); log("Setting up cards list '" + listName + "'", className);
initCardsList(cardsList, answersList); initCardsList(cardsList, answersList);
} }
else { else {
@ -71,20 +72,7 @@ void flashExam::initCardsList(QString cardsList, QString answersList) {
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();
cardsTotal = cardsStringList.count() + 1;
int it = 1;
for (auto& i : cardsStringList) {
i.prepend(QString::number(it) + " ");
it += 1;
}
it = 1;
for (auto& i : answersStringList) {
i.prepend(QString::number(it) + " ");
it += 1;
}
cardsTotal = it;
displayCard(false); displayCard(false);
ui->stackedWidget->setCurrentIndex(1); ui->stackedWidget->setCurrentIndex(1);
} }
@ -101,8 +89,7 @@ void flashExam::on_revealBtn_clicked()
displayCard(true); displayCard(true);
} }
else { else {
QString answerText = answersStringList.at(currentCardNumber); QString answerText = displayImage(answersStringList.at(currentCardNumber));
answerText.remove(0, 2);
ui->textBrowser->setText(answerText); ui->textBrowser->setText(answerText);
answerShown = true; answerShown = true;
ui->revealBtn->setText("Hide answer"); ui->revealBtn->setText("Hide answer");
@ -116,15 +103,12 @@ void flashExam::on_nextBtn_clicked()
} }
void flashExam::displayCard(bool existingCardNumber) { void flashExam::displayCard(bool existingCardNumber) {
QString cardText;
if(!existingCardNumber) { if(!existingCardNumber) {
if(randomize) { if(randomize) {
currentCardNumber = QRandomGenerator::global()->bounded(cardsTotal - 1); currentCardNumber = QRandomGenerator::global()->bounded(cardsTotal - 1);
} }
} }
cardText = cardsStringList.at(currentCardNumber); QString cardText = displayImage(cardsStringList.at(currentCardNumber));
ui->graphicsView->hide();
cardText.remove(0, 2);
ui->cardNumberLabel->setText("Card " + QString::number(currentCardNumber + 1)); ui->cardNumberLabel->setText("Card " + QString::number(currentCardNumber + 1));
ui->textBrowser->setText(cardText); ui->textBrowser->setText(cardText);
@ -132,3 +116,34 @@ void flashExam::displayCard(bool existingCardNumber) {
answerShown = false; answerShown = false;
} }
QString flashExam::displayImage(QString cardText) {
ui->textBrowser->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
ui->graphicsView->hide();
QRegularExpression imageRegex("IMG='([^']+)'");
QRegularExpressionMatch match = imageRegex.match(cardText);
if (match.hasMatch()) {
QString imagePath = match.captured(1); // Captured group 1 is the value of IMG
log("Displaying image '" + imagePath + "'", className);
ui->graphicsView->items().clear();
graphicsScene->clear();
QPixmap pixmap("/mnt/onboard/onboard/.flashexam/resources/" + listName + "/" + imagePath);
graphicsScene->addPixmap(pixmap);
ui->graphicsView->setScene(graphicsScene);
// Shrinking scene if item is smaller than previous one
QRectF rect = graphicsScene->itemsBoundingRect();
graphicsScene->setSceneRect(rect);
ui->graphicsView->fitInView(graphicsScene->sceneRect(), Qt::KeepAspectRatio);
ui->textBrowser->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
ui->graphicsView->show();
}
else {
log("IMG key not found", className);
}
QRegularExpression removeRegex("IMG='[^']+'\\s*");
cardText.remove(removeRegex);
return cardText;
}

View file

@ -2,6 +2,7 @@
#define FLASHEXAM_H #define FLASHEXAM_H
#include <QWidget> #include <QWidget>
#include <QGraphicsScene>
namespace Ui { namespace Ui {
class flashExam; class flashExam;
@ -20,6 +21,7 @@ private:
Ui::flashExam *ui; Ui::flashExam *ui;
int cardsTotal; int cardsTotal;
int currentCardNumber; int currentCardNumber;
QString listName;
bool randomize; bool randomize;
bool answerShown = false; bool answerShown = false;
QStringList cardsStringList; QStringList cardsStringList;
@ -27,6 +29,8 @@ private:
void setupCardsList(); void setupCardsList();
void initCardsList(QString cardsList, QString answersList); void initCardsList(QString cardsList, QString answersList);
void displayCard(bool existingCardNumber); void displayCard(bool existingCardNumber);
QString displayImage(QString cardText);
QGraphicsScene * graphicsScene;
signals: signals:
void showToast(QString messageToDisplay); void showToast(QString messageToDisplay);

View file

@ -237,7 +237,14 @@
<widget class="QGraphicsView" name="graphicsView"/> <widget class="QGraphicsView" name="graphicsView"/>
</item> </item>
<item row="3" column="0"> <item row="3" column="0">
<widget class="QTextBrowser" name="textBrowser"/> <widget class="QTextBrowser" name="textBrowser">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item> </item>
<item row="5" column="0"> <item row="5" column="0">
<widget class="QPushButton" name="revealBtn"> <widget class="QPushButton" name="revealBtn">