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

@@ -50,6 +50,7 @@ void saveMdem();
void updateMdemInfo(std::string filename = "", bool isChanged = true);
QMainWindow *initMdemListWindow();
#define SETTING_MEMORYBASE "defaultMemorybase"
#define SETTING_TIMEZONE "timezone"
#define SETTING_CHARACTER_WRAP "characterWrap"
#define SETTING_NOT_REMEMBERED "notRemembered"

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;
}

View File

@@ -7,11 +7,14 @@
#include <qlayoutitem.h>
#include <qmainwindow.h>
#include <qnamespace.h>
#include <qobject.h>
#include <qobjectdefs.h>
#include <qsettings.h>
#include <qsizepolicy.h>
#include <qspinbox.h>
#include <qt/QtWidgets/qwidget.h>
#include <qtoolbutton.h>
#include <qvalidator.h>
#include <qwidget.h>
#include <qwindow.h>
#include <qwindowdefs.h>
@@ -65,10 +68,40 @@ QWidget *initSettings () {
QString settingsFile = configDir + "/mdem.ini";
settings = new QSettings(settingsFile, QSettings::IniFormat);
auto* settingsWindow = new QWidget;
settingsWindow->setWindowTitle("Settings");
auto top = new QWidget;
auto hlTop = new QVBoxLayout;
top->setLayout(hlTop);
auto settingsLabel = new QLabel;
settingsLabel->setText("Settings");
settingsLabel->setStyleSheet(
"font-size: 20px;"
);
hlTop->addWidget(settingsLabel);
auto formLayout = new QFormLayout;
auto* mbaseInput = new QLineEdit;
auto* browseButton = new QPushButton("Browse");
auto* pathLayout = new QHBoxLayout;
pathLayout->addWidget(mbaseInput);
pathLayout->addWidget(browseButton);
formLayout->addRow("Default memorybase:", pathLayout);
QObject::connect(browseButton, &QPushButton::clicked, [mbaseInput]() {
QString dir = QFileDialog::getExistingDirectory(
nullptr,
"Select Config Directory",
mbaseInput->text()
);
if (!dir.isEmpty()) {
mbaseInput->setText(dir);
}
});
auto characterWrap = new QSpinBox;
characterWrap->setRange(30, 150);
formLayout->addRow("Character wrap in code gen [30-150]:", characterWrap);
@@ -93,24 +126,103 @@ QWidget *initSettings () {
easy->setRange(0, 100);
formLayout->addRow("Easy:", easy);
auto* btnSaveSettings = new QPushButton("Save");
auto* mainLayout = new QVBoxLayout;
auto btnLayout = new QHBoxLayout;
auto btnSaveSettings = new QPushButton("Save");
auto btnLoad = new QPushButton("Load");
auto mainLayout = new QVBoxLayout;
btnLayout->addWidget(btnSaveSettings);
btnLayout->addWidget(btnLoad);
// @Improve: validate setting values
characterWrap->setValue(settings->value(SETTING_CHARACTER_WRAP).toInt());
timezone->setValue(settings->value(SETTING_TIMEZONE).toInt());
notRemembered->setValue(settings->value(SETTING_NOT_REMEMBERED).toDouble());
hard->setValue(settings->value(SETTING_HARD).toDouble());
medium->setValue(settings->value(SETTING_MEDIUM).toDouble());
easy->setValue(settings->value(SETTING_EASY).toDouble());
auto setSettingInputs = [
mbaseInput,
characterWrap,
timezone,
notRemembered,
hard,
medium,
easy
]() {
mbaseInput->setText(settings->value(SETTING_MEMORYBASE).toString());
characterWrap->setValue(settings->value(SETTING_CHARACTER_WRAP).toInt());
timezone->setValue(settings->value(SETTING_TIMEZONE).toInt());
notRemembered->setValue(settings->value(SETTING_NOT_REMEMBERED).toDouble());
hard->setValue(settings->value(SETTING_HARD).toDouble());
medium->setValue(settings->value(SETTING_MEDIUM).toDouble());
easy->setValue(settings->value(SETTING_EASY).toDouble());
};
auto saveSettings = [characterWrap, timezone, notRemembered, hard, medium, easy]() {
auto updateSettingsLabel = [settingsLabel](bool isChanged) {
if (isChanged) {
settingsLabel->setText("Settings *");
} else {
settingsLabel->setText("Settings");
}
};
auto attachChangeListener = [settingsLabel, updateSettingsLabel](QObject *input, QString key) {
if (QLineEdit *lineInput = qobject_cast<QLineEdit*>(input)) {
QObject::connect(
lineInput,
&QLineEdit::textChanged,
[settingsLabel, key, updateSettingsLabel](QString value) {
if (settings->value(key).toString() != value) {
updateSettingsLabel(true);
}
});
} else if (QSpinBox *spinBox = qobject_cast<QSpinBox*>(input)) {
QObject::connect(
spinBox,
QOverload<int>::of(&QSpinBox::valueChanged),
[settingsLabel, key, updateSettingsLabel](int value) {
if (settings->value(key).toInt() != value) {
updateSettingsLabel(true);
}
});
} else if (QDoubleSpinBox *doubleSpinBox = qobject_cast<QDoubleSpinBox*>(input)) {
QObject::connect(
doubleSpinBox,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
[settingsLabel, key, updateSettingsLabel](double value) {
if (settings->value(key).toDouble() != value) {
updateSettingsLabel(true);
}
});
}
};
attachChangeListener(mbaseInput, SETTING_MEMORYBASE);
attachChangeListener(characterWrap, SETTING_CHARACTER_WRAP);
attachChangeListener(timezone, SETTING_TIMEZONE);
attachChangeListener(notRemembered, SETTING_NOT_REMEMBERED);
attachChangeListener(hard, SETTING_HARD);
attachChangeListener(medium, SETTING_MEDIUM);
attachChangeListener(easy, SETTING_EASY);
auto saveSettings = [
mbaseInput,
characterWrap,
timezone,
notRemembered,
hard,
medium,
easy,
updateSettingsLabel
]() {
settings->setValue(SETTING_MEMORYBASE, mbaseInput->text());
settings->setValue(SETTING_CHARACTER_WRAP, characterWrap->value());
settings->setValue(SETTING_TIMEZONE, timezone->value());
settings->setValue(SETTING_NOT_REMEMBERED, notRemembered->value());
settings->setValue(SETTING_HARD, hard->value());
settings->setValue(SETTING_MEDIUM, medium->value());
settings->setValue(SETTING_EASY, easy->value());
updateSettingsLabel(false);
};
auto loadSettings = [setSettingInputs, updateSettingsLabel]() {
settings->sync();
setSettingInputs();
updateSettingsLabel(false);
};
QObject::connect(
@@ -120,17 +232,32 @@ QWidget *initSettings () {
saveSettings();
}
);
QObject::connect(
btnLoad,
&QPushButton::clicked,
[loadSettings]() {
loadSettings();
}
);
loadSettings();
QShortcut* shortcutSave = new QShortcut(QKeySequence("Ctrl+S"), settingsWindow);
QObject::connect(shortcutSave, &QShortcut::activated, [saveSettings]() {
saveSettings();
});
auto spacer = new QSpacerItem(
50,
50,
QSizePolicy::Expanding,
QSizePolicy::Expanding
);
mainLayout->addWidget(top);
mainLayout->addLayout(formLayout);
mainLayout->addWidget(btnSaveSettings);
mainLayout->addItem(spacer);
mainLayout->addLayout(btnLayout);
settingsWindow->setLayout(mainLayout);
return settingsWindow;
}

View File

@@ -264,9 +264,6 @@ void releaseItem(QStandardItem** item) {
}
void releaseAllItems() {
std::cout <<
std::format("Starting release, currently in the pool: {}", itemPool.size()) <<
std::endl;
auto releaseAllInModel = [](QStandardItemModel *model) {
auto itemCount = model->rowCount();
for (size_t i = 0; i < itemCount; ++i) {
@@ -281,7 +278,6 @@ void releaseAllItems() {
for (auto groupView: groupViews) {
releaseAllInModel(&groupView->itemModel);
}
std::cout << std::format("All items released, currently in the pool: {}", itemPool.size()) << std::endl;
}
void hideQuestionElements() {