settings improvements and refactoring

This commit is contained in:
jorenchik
2024-10-26 23:23:27 +03:00
parent cf50188f66
commit f6989e7e10
4 changed files with 178 additions and 29 deletions

View File

@@ -4,6 +4,7 @@
#include <fstream>
#include <iostream>
#include <map>
#include <filesystem>
#include <qabstractbutton.h>
#include <qapplication.h>
#include <qboxlayout.h>
@@ -78,14 +79,14 @@
// Memorybase info.
// @Improve: set the default directory for convenience. It should be configured by user
// or set to "".
std::string currentPath = "/home/jorenchik/Code/mdemory/memorybase/";
QString currentPath = "";
std::string currentMdem = "";
QFileSystemModel *model;
QTreeView *mdemList;
QLabel *membaseLabel;
std::map<std::string, MdemBuffer*> buffers;
MdemBuffer *currentMdemBuffer;
MdemBuffer *currentMdemBuffer = nullptr;
// Mdem scroll list.
std::vector<Mdem*> mdems = std::vector<Mdem*>();
@@ -646,7 +647,7 @@ void reloadMdem(std::string path) {
SwitchPage(0);
updateMdemInfo(filename, false);
} else {
std::cout << std::format("Could not open the file: {}", currentPath) << std::endl;
std::cout << std::format("Could not open the file: {}", currentPath.toStdString()) << std::endl;
}
hideQuestionElements();
@@ -654,7 +655,21 @@ void reloadMdem(std::string path) {
}
void pickDirectory(QString directory) {
currentPath = directory.toStdString();
auto path = std::filesystem::path(directory.toStdString());
if (
!std::filesystem::exists(directory.toStdString()) ||
!std::filesystem::is_directory(directory.toStdString())
) {
QMessageBox::information(
nullptr,
"Error",
"The directory that is specified as the default memorybase does not exist."
);
return;
}
currentPath = directory;
// Update tree view.
if (directory.length() <= 0) {
@@ -668,7 +683,7 @@ void pickDirectory(QString directory) {
membaseLabel->setText(QString::fromStdString(
std::format(
"memorybase: {}",
getFilename(currentPath)
getFilename(currentPath.toStdString())
)
));
@@ -933,12 +948,16 @@ QMainWindow *initMdemListWindow() {
practice,
&QToolButton::clicked,
[cbAlgorithm]() {
trainWindow->show();
trainWindow->resize(600, 300);
initiatePractice(
currentMdemBuffer,
static_cast<PracticeAlgorithm>(cbAlgorithm->currentData().toInt())
);
if (currentMdemBuffer) {
trainWindow->show();
trainWindow->resize(600, 300);
initiatePractice(
currentMdemBuffer,
static_cast<PracticeAlgorithm>(
cbAlgorithm->currentData().toInt()
)
);
}
}
);
}
@@ -1046,8 +1065,9 @@ QMainWindow *initMdemListWindow() {
window->setCentralWidget(hSplitter);
window->show();
if (currentPath.length() > 0) {
pickDirectory(QString::fromStdString(currentPath));
currentPath = settings->value(SETTING_MEMORYBASE).toString();
if (currentPath.size() > 0) {
pickDirectory(currentPath);
}
QShortcut* shortcutSaveFile = new QShortcut(QKeySequence("Ctrl+S"), window);
@@ -1055,5 +1075,10 @@ QMainWindow *initMdemListWindow() {
saveMdem();
});
QShortcut* shortcutSettings = new QShortcut(QKeySequence("Ctrl+C"), window);
QObject::connect(shortcutSettings, &QShortcut::activated, [settingsWindow]() {
settingsWindow->show();
});
return window;
}