mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
directory choosing improved
This commit is contained in:
@@ -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;
|
||||
if (file) {
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
content = buffer.str();
|
||||
auto res = transpile(content, true);
|
||||
|
||||
for (auto mdem: mdems) {
|
||||
mdem->wMdem.hide();
|
||||
}
|
||||
for (auto question: questions) {
|
||||
delete question;
|
||||
}
|
||||
questions.clear();
|
||||
|
||||
std::smatch matches;
|
||||
auto filenameMatched = std::regex_search(currentPath, matches, lastPathElementExp);
|
||||
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);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user