From f1591b73c8c373e939772331983f53eeb96607da Mon Sep 17 00:00:00 2001 From: jorenchik Date: Thu, 17 Oct 2024 21:11:17 +0300 Subject: [PATCH] fixed a bug with edit & saving an mdem --- src/qtapp/main.cpp | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/qtapp/main.cpp b/src/qtapp/main.cpp index 4fc281a..81cf5f3 100644 --- a/src/qtapp/main.cpp +++ b/src/qtapp/main.cpp @@ -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 mdems = QList(); +time_t trainedAt = 0; std::vector questions = std::vector(); QSpacerItem *mdemSpacer; std::vector errorPool; @@ -140,8 +141,17 @@ void showBacklabels(Mdem *mdem) { } } -std::string outputMdem(std::vector questions) { +std::string outputMdem(std::vector 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,