From b2bbb2b24dd7df7f4a59259dc77684730a3b18c6 Mon Sep 17 00:00:00 2001 From: jorenchik Date: Sun, 6 Oct 2024 14:23:13 +0300 Subject: [PATCH] directory choosing improved --- src/cpp/qtapp/main.cpp | 106 ++++++++++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 38 deletions(-) diff --git a/src/cpp/qtapp/main.cpp b/src/cpp/qtapp/main.cpp index 36a5e30..a9578a5 100644 --- a/src/cpp/qtapp/main.cpp +++ b/src/cpp/qtapp/main.cpp @@ -75,9 +75,14 @@ struct Page { int end; }; -QString workingPath = "/home/jorenchik/Code/mdemory/memorybase"; +// @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 currentMdem = ""; +/*std::string currentPath = "";*/ QFileSystemModel *model; +QLabel *mdemLabel; +QTreeView *mdemList; // Mdem list QLabel *deckListLabel; @@ -391,25 +396,34 @@ void releaseError(ErrorView** item) { std::cout << std::format("Released, current pool size: {}\n", errorPool.size()); } -void loadMdem() { - auto file = std::ifstream(currentPath); +void reloadMdem() { + auto file = std::ifstream(currentMdem); std::string content; + + for (auto mdem: mdems) { + mdem->wMdem.hide(); + } + for (auto question: questions) { + delete question; + } + questions.clear(); + + std::smatch matches; + auto filenameMatched = std::regex_search(currentMdem, matches, lastPathElementExp); + auto filename = matches[2]; + if (filename.str().length() > 0) { + deckListLabel->setText( + QString::fromStdString(std::format("mdem: {}", filename.str())) + ); + } else { + deckListLabel->setText(""); + } + if (file) { std::stringstream buffer; buffer << file.rdbuf(); content = buffer.str(); auto res = transpile(content, true); - for (auto question: questions) { - delete question; - } - questions.clear(); - - std::smatch matches; - auto filenameMatched = std::regex_search(currentPath, matches, lastPathElementExp); - auto filename = matches[2]; - deckListLabel->setText( - QString::fromStdString(std::format("mdem: {}", filename.str())) - ); while (errorViews.size() > 0) { auto errorView = errorViews.back(); @@ -419,7 +433,6 @@ void loadMdem() { } if (res.error == "") { - time_t trainedAt; if (res.value.lastTrainedAt == 0) { trainedAt = 0; @@ -449,19 +462,42 @@ void loadMdem() { ); errorView->box.show(); hMdemScroll->addWidget(&errorView->box); - hMdemScroll->addItem(mdemSpacer); - for (auto mdem: mdems) { - if (mdem->wMdem.isVisible()) { - mdem->wMdem.hide(); - } - } } } else { std::cout << std::format("Could not open the file: {}", currentPath) << std::endl; } } +void pickDirectory(QString directory) { + currentPath = directory.toStdString(); + + // Update tree view. + if (directory.length() <= 0) { + mdemLabel->setText(directory); + return; + } + mdemList->setRootIndex(model->setRootPath(directory)); + std::smatch matches; + + // Update label. + auto filenameMatched = std::regex_search( + currentPath, + matches, + lastPathElementExp + ); + auto filename = matches[2]; + mdemLabel->setText(QString::fromStdString( + std::format( + "memorybase: {}", + filename.str() + ) + )); + + currentMdem = ""; + reloadMdem(); +} + int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow window; @@ -479,11 +515,8 @@ int main(int argc, char *argv[]) { fileDialog->connect( fileDialog, &QFileDialog::fileSelected, - [](QString directory - ) { - workingPath = directory; - model->setRootPath(workingPath); - }); + pickDirectory + ); }); menuBar->addMenu(menu); @@ -496,12 +529,9 @@ int main(int argc, char *argv[]) { QVBoxLayout *leftLayout = new QVBoxLayout(); QWidget *leftTop = new QWidget(); QVBoxLayout *vLeftTop = new QVBoxLayout(); - QLabel *mdemLabel = new QLabel("mdems"); - QTreeView *mdemList = new QTreeView(); + mdemList = new QTreeView(); + mdemLabel = new QLabel(); model = new QFileSystemModel(); - if (workingPath.length() > 0) { - model->setRootPath(workingPath); - } mdemLabel->setStyleSheet( "font-size: 17px;" "font-weight: 400;" @@ -521,15 +551,11 @@ int main(int argc, char *argv[]) { &QTreeView::doubleClicked, [](const QModelIndex &index) { auto fileInfo = model->fileInfo(index); - currentPath = fileInfo.filePath().toStdString(); - loadMdem(); + currentMdem = fileInfo.filePath().toStdString(); + reloadMdem(); } ); - QModelIndex rootIndex = model->index( - "/home/jorenchik/Code/mdemory/memorybase" - ); - mdemList->setRootIndex(rootIndex); for (int col = 1; col < model->columnCount(); ++col) { mdemList->hideColumn(col); } @@ -565,7 +591,7 @@ int main(int argc, char *argv[]) { // Buttons load->setText("Load"); practice->setText("Practice"); - QObject::connect(load, &QToolButton::clicked, &loadMdem); + QObject::connect(load, &QToolButton::clicked, &reloadMdem); // Mdems QScrollArea *mdemScroll = new QScrollArea(); @@ -661,5 +687,9 @@ int main(int argc, char *argv[]) { window.setCentralWidget(hSplitter); window.show(); + if (currentPath.length() > 0) { + pickDirectory(QString::fromStdString(currentPath)); + } + return app.exec(); }