fixed a bug with edit & saving an mdem

This commit is contained in:
jorenchik
2024-10-17 21:11:17 +03:00
parent c099c3c0f1
commit f1591b73c8

View File

@@ -86,7 +86,7 @@ struct Page {
// @Debug: set the default directory for convenience. It should be configured by user
// or set to "".
std::string currentPath = "/home/jorenchik/Code/mdemory/memorybase";
std::string currentPath = "/home/jorenchik/Code/mdemory/memorybase/";
std::string currentMdem = "";
/*std::string currentPath = "";*/
QFileSystemModel *model;
@@ -97,6 +97,7 @@ QTreeView *mdemList;
QLabel *deckListLabel;
QVBoxLayout *hMdemScroll;
QList<Mdem*> mdems = QList<Mdem*>();
time_t trainedAt = 0;
std::vector<Question*> questions = std::vector<Question*>();
QSpacerItem *mdemSpacer;
std::vector<ErrorView*> errorPool;
@@ -140,8 +141,17 @@ void showBacklabels(Mdem *mdem) {
}
}
std::string outputMdem(std::vector<Question*> questions) {
std::string outputMdem(std::vector<Question*> questions, time_t time = 0) {
std::stringstream ss;
if (time > 0) {
std::tm* tm = std::localtime(&time);
char buffer[100];
std::strftime(buffer, sizeof(buffer), "%d.%m.%Y %H\\:%M", tm);
auto time = std::string(buffer);
ss << time << std::endl << std::endl;
}
for (auto question: questions) {
std::string cooldownPart;
if (question->Cooldown != 0) {
@@ -571,13 +581,14 @@ void reloadMdem() {
}
if (res.error == "") {
time_t trainedAt;
if (res.value.lastTrainedAt == 0) {
trainedAt = 0;
} else {
// TODO: time_zone option
trainedAt = res.value.lastTrainedAt + 3600 * 2;
}
std::cout << std::format("Last trained at: {}", trainedAt) << std::endl;
questions = res.value.questions;
makePages();
SwitchPage(0);
@@ -647,8 +658,19 @@ void setupEditorSave(bool checked) {
"There are no questions in your input."
);
} else if (res.value.questions.size() == 1) {
setupMdem(editMdem, res.value.questions[0]);
showBacklabels(editMdem);
auto oldQuestion = editMdem->question;
editMdem->question = res.value.questions[0];
for (int i = 0; i < questions.size(); ++i) {
if (questions[i] == oldQuestion) {
questions.erase(questions.begin() + i);
delete questions[i];
questions[i] = editMdem->question;
break;
}
}
/*setupMdem(editMdem, res.value.questions[0]);*/
/*showBacklabels(editMdem);*/
editorWindow->hide();
} else {
QMessageBox::information(
nullptr,
@@ -671,6 +693,7 @@ void setupEditorSave(bool checked) {
);
makePages();
SwitchPage(0);
editorWindow->hide();
}
}
}
@@ -814,12 +837,13 @@ int main(int argc, char *argv[]) {
QObject::connect(add, &QToolButton::clicked, []() {
editMdem = nullptr;
editorWindow->show();
editor->setText("");
});
QObject::connect(load, &QToolButton::clicked, &reloadMdem);
QObject::connect(save, &QToolButton::clicked, []() {
auto filename = getFilename(currentMdem);
std::ofstream out("generated.mdem");
out << outputMdem(questions);;
std::ofstream out(currentMdem);
out << outputMdem(questions, trainedAt);
});
QObject::connect(
practice,