diff --git a/src/cpp/qtapp/main.cpp b/src/cpp/qtapp/main.cpp index ed8e449..1eb5a7a 100644 --- a/src/cpp/qtapp/main.cpp +++ b/src/cpp/qtapp/main.cpp @@ -2,14 +2,17 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include +#include #include #include #include @@ -35,7 +38,6 @@ #include #include #include -#include #include "api.h" #include "parser.h" @@ -87,12 +89,12 @@ public: wFront->setProperty("first", "true"); wMdem->setStyleSheet(QString( "QWidget#%1 > QWidget {" - "border-right: 1px solid gray;" - "border-bottom: 1px solid gray;" - "border-left: 1px solid gray;" + "border-right: 1px solid gray;" + "border-bottom: 1px solid gray;" + "border-left: 1px solid gray;" "} " "QWidget#%1 > QWidget[first=\"true\"] {" - "border-top: 1px solid gray;" + "border-top: 1px solid gray;" "}" ).arg(id)); @@ -140,7 +142,6 @@ public: } }; - QString workingPath = "/home/jorenchik/Code/mdemory/memorybase"; std::string currentPath = ""; QList mdems = QList(); @@ -337,6 +338,52 @@ void SwitchPage(int pageIdx) { std::regex lastPathElementExp = std::regex("(.+\\/)*(.+)"); +struct ErrorView { + QWidget box; + QVBoxLayout layout; + QLabel label; +}; +std::vector errorPool; +std::vector errorViews; + +#define ERROR_POOL_CHUNK 50 + +ErrorView *makeErrorView() { + auto errorView = new ErrorView; + errorView->box.setObjectName("error-box"); + errorView->box.setLayout(&errorView->layout); + errorView->box.setMinimumHeight(30); + errorView->box.setStyleSheet( + "QWidget#error-box {" + "border: 1px solid red;" + "}" + ); + errorView->layout.addWidget(&errorView->label); + return errorView; +}; + +ErrorView* acquireError() { + if (errorPool.size() <= 0) { + for (int i = 0; i < ERROR_POOL_CHUNK; ++i) { + auto errorView = makeErrorView(); + errorPool.push_back(errorView); + hMdemScroll->addWidget(&errorView->box); + errorView->box.hide(); + } + } + auto item = errorPool.back(); + errorPool.pop_back(); + errorViews.push_back(item); + std::cout << std::format("Acquired, current pool size: {}\n", errorPool.size()); + return item; +} + +void releaseError(ErrorView** item) { + errorPool.push_back(*item); + (*item) = nullptr; + std::cout << std::format("Released, current pool size: {}\n", errorPool.size()); +} + void loadMdem() { auto file = std::ifstream(currentPath); std::string content; @@ -349,7 +396,23 @@ void loadMdem() { 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(); + errorViews.pop_back(); + errorView->box.hide(); + releaseError(&errorView); + } + if (res.error == "") { + time_t trainedAt; if (res.value.lastTrainedAt == 0) { trainedAt = 0; @@ -360,13 +423,27 @@ void loadMdem() { questions = res.value.questions; makePages(); SwitchPage(0); - std::smatch matches; - auto filename = std::regex_search(currentPath, matches, lastPathElementExp); - deckListLabel->setText( - QString::fromStdString(std::format("mdem: {}", matches[2].str())) - ); } else { std::cout << std::format("Compilation error: {}", res.error) << std::endl; + + // Show errors. + hMdemScroll->removeItem(mdemSpacer); + auto errorView = acquireError(); + errorView->label.setText( + QString::fromStdString( + std::format( + "Error while transpiling {}: {} ({}:{})", + filename.str(), + res.error, + res.row, + res.column + ) + ) + ); + errorView->box.show(); + hMdemScroll->addWidget(&errorView->box); + + hMdemScroll->addItem(mdemSpacer); for (auto mdem: mdems) { if (mdem->wMdem->isVisible()) { mdem->wMdem->hide(); diff --git a/src/cpp/qtapp/trainWindow.cpp b/src/cpp/qtapp/trainWindow.cpp index 77cbf89..34f6d15 100644 --- a/src/cpp/qtapp/trainWindow.cpp +++ b/src/cpp/qtapp/trainWindow.cpp @@ -244,7 +244,6 @@ std::vector itemPool; #define ITEM_POOL_CHUNK 200 - QStandardItem* acquireItem() { if (itemPool.size() <= 0) { for (int i = 0; i < ITEM_POOL_CHUNK; ++i) {