directory choosing improved

This commit is contained in:
jorenchik
2024-10-06 14:23:13 +03:00
parent 9d327aed1b
commit b2bbb2b24d

View File

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