mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
settings improvements and refactoring
This commit is contained in:
@@ -50,6 +50,7 @@ void saveMdem();
|
|||||||
void updateMdemInfo(std::string filename = "", bool isChanged = true);
|
void updateMdemInfo(std::string filename = "", bool isChanged = true);
|
||||||
QMainWindow *initMdemListWindow();
|
QMainWindow *initMdemListWindow();
|
||||||
|
|
||||||
|
#define SETTING_MEMORYBASE "defaultMemorybase"
|
||||||
#define SETTING_TIMEZONE "timezone"
|
#define SETTING_TIMEZONE "timezone"
|
||||||
#define SETTING_CHARACTER_WRAP "characterWrap"
|
#define SETTING_CHARACTER_WRAP "characterWrap"
|
||||||
#define SETTING_NOT_REMEMBERED "notRemembered"
|
#define SETTING_NOT_REMEMBERED "notRemembered"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <filesystem>
|
||||||
#include <qabstractbutton.h>
|
#include <qabstractbutton.h>
|
||||||
#include <qapplication.h>
|
#include <qapplication.h>
|
||||||
#include <qboxlayout.h>
|
#include <qboxlayout.h>
|
||||||
@@ -78,14 +79,14 @@
|
|||||||
// Memorybase info.
|
// Memorybase info.
|
||||||
// @Improve: set the default directory for convenience. It should be configured by user
|
// @Improve: set the default directory for convenience. It should be configured by user
|
||||||
// or set to "".
|
// or set to "".
|
||||||
std::string currentPath = "/home/jorenchik/Code/mdemory/memorybase/";
|
QString currentPath = "";
|
||||||
std::string currentMdem = "";
|
std::string currentMdem = "";
|
||||||
QFileSystemModel *model;
|
QFileSystemModel *model;
|
||||||
QTreeView *mdemList;
|
QTreeView *mdemList;
|
||||||
QLabel *membaseLabel;
|
QLabel *membaseLabel;
|
||||||
|
|
||||||
std::map<std::string, MdemBuffer*> buffers;
|
std::map<std::string, MdemBuffer*> buffers;
|
||||||
MdemBuffer *currentMdemBuffer;
|
MdemBuffer *currentMdemBuffer = nullptr;
|
||||||
|
|
||||||
// Mdem scroll list.
|
// Mdem scroll list.
|
||||||
std::vector<Mdem*> mdems = std::vector<Mdem*>();
|
std::vector<Mdem*> mdems = std::vector<Mdem*>();
|
||||||
@@ -646,7 +647,7 @@ void reloadMdem(std::string path) {
|
|||||||
SwitchPage(0);
|
SwitchPage(0);
|
||||||
updateMdemInfo(filename, false);
|
updateMdemInfo(filename, false);
|
||||||
} else {
|
} 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();
|
hideQuestionElements();
|
||||||
@@ -654,7 +655,21 @@ void reloadMdem(std::string path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void pickDirectory(QString directory) {
|
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.
|
// Update tree view.
|
||||||
if (directory.length() <= 0) {
|
if (directory.length() <= 0) {
|
||||||
@@ -668,7 +683,7 @@ void pickDirectory(QString directory) {
|
|||||||
membaseLabel->setText(QString::fromStdString(
|
membaseLabel->setText(QString::fromStdString(
|
||||||
std::format(
|
std::format(
|
||||||
"memorybase: {}",
|
"memorybase: {}",
|
||||||
getFilename(currentPath)
|
getFilename(currentPath.toStdString())
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
@@ -933,13 +948,17 @@ QMainWindow *initMdemListWindow() {
|
|||||||
practice,
|
practice,
|
||||||
&QToolButton::clicked,
|
&QToolButton::clicked,
|
||||||
[cbAlgorithm]() {
|
[cbAlgorithm]() {
|
||||||
|
if (currentMdemBuffer) {
|
||||||
trainWindow->show();
|
trainWindow->show();
|
||||||
trainWindow->resize(600, 300);
|
trainWindow->resize(600, 300);
|
||||||
initiatePractice(
|
initiatePractice(
|
||||||
currentMdemBuffer,
|
currentMdemBuffer,
|
||||||
static_cast<PracticeAlgorithm>(cbAlgorithm->currentData().toInt())
|
static_cast<PracticeAlgorithm>(
|
||||||
|
cbAlgorithm->currentData().toInt()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1046,8 +1065,9 @@ QMainWindow *initMdemListWindow() {
|
|||||||
window->setCentralWidget(hSplitter);
|
window->setCentralWidget(hSplitter);
|
||||||
window->show();
|
window->show();
|
||||||
|
|
||||||
if (currentPath.length() > 0) {
|
currentPath = settings->value(SETTING_MEMORYBASE).toString();
|
||||||
pickDirectory(QString::fromStdString(currentPath));
|
if (currentPath.size() > 0) {
|
||||||
|
pickDirectory(currentPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
QShortcut* shortcutSaveFile = new QShortcut(QKeySequence("Ctrl+S"), window);
|
QShortcut* shortcutSaveFile = new QShortcut(QKeySequence("Ctrl+S"), window);
|
||||||
@@ -1055,5 +1075,10 @@ QMainWindow *initMdemListWindow() {
|
|||||||
saveMdem();
|
saveMdem();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QShortcut* shortcutSettings = new QShortcut(QKeySequence("Ctrl+C"), window);
|
||||||
|
QObject::connect(shortcutSettings, &QShortcut::activated, [settingsWindow]() {
|
||||||
|
settingsWindow->show();
|
||||||
|
});
|
||||||
|
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,14 @@
|
|||||||
#include <qlayoutitem.h>
|
#include <qlayoutitem.h>
|
||||||
#include <qmainwindow.h>
|
#include <qmainwindow.h>
|
||||||
#include <qnamespace.h>
|
#include <qnamespace.h>
|
||||||
|
#include <qobject.h>
|
||||||
#include <qobjectdefs.h>
|
#include <qobjectdefs.h>
|
||||||
#include <qsettings.h>
|
#include <qsettings.h>
|
||||||
#include <qsizepolicy.h>
|
#include <qsizepolicy.h>
|
||||||
|
#include <qspinbox.h>
|
||||||
#include <qt/QtWidgets/qwidget.h>
|
#include <qt/QtWidgets/qwidget.h>
|
||||||
#include <qtoolbutton.h>
|
#include <qtoolbutton.h>
|
||||||
|
#include <qvalidator.h>
|
||||||
#include <qwidget.h>
|
#include <qwidget.h>
|
||||||
#include <qwindow.h>
|
#include <qwindow.h>
|
||||||
#include <qwindowdefs.h>
|
#include <qwindowdefs.h>
|
||||||
@@ -65,10 +68,40 @@ QWidget *initSettings () {
|
|||||||
QString settingsFile = configDir + "/mdem.ini";
|
QString settingsFile = configDir + "/mdem.ini";
|
||||||
settings = new QSettings(settingsFile, QSettings::IniFormat);
|
settings = new QSettings(settingsFile, QSettings::IniFormat);
|
||||||
auto* settingsWindow = new QWidget;
|
auto* settingsWindow = new QWidget;
|
||||||
|
|
||||||
settingsWindow->setWindowTitle("Settings");
|
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 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;
|
auto characterWrap = new QSpinBox;
|
||||||
characterWrap->setRange(30, 150);
|
characterWrap->setRange(30, 150);
|
||||||
formLayout->addRow("Character wrap in code gen [30-150]:", characterWrap);
|
formLayout->addRow("Character wrap in code gen [30-150]:", characterWrap);
|
||||||
@@ -93,24 +126,103 @@ QWidget *initSettings () {
|
|||||||
easy->setRange(0, 100);
|
easy->setRange(0, 100);
|
||||||
formLayout->addRow("Easy:", easy);
|
formLayout->addRow("Easy:", easy);
|
||||||
|
|
||||||
auto* btnSaveSettings = new QPushButton("Save");
|
auto btnLayout = new QHBoxLayout;
|
||||||
auto* mainLayout = new QVBoxLayout;
|
auto btnSaveSettings = new QPushButton("Save");
|
||||||
|
auto btnLoad = new QPushButton("Load");
|
||||||
|
auto mainLayout = new QVBoxLayout;
|
||||||
|
btnLayout->addWidget(btnSaveSettings);
|
||||||
|
btnLayout->addWidget(btnLoad);
|
||||||
|
|
||||||
// @Improve: validate setting values
|
// @Improve: validate setting values
|
||||||
|
auto setSettingInputs = [
|
||||||
|
mbaseInput,
|
||||||
|
characterWrap,
|
||||||
|
timezone,
|
||||||
|
notRemembered,
|
||||||
|
hard,
|
||||||
|
medium,
|
||||||
|
easy
|
||||||
|
]() {
|
||||||
|
mbaseInput->setText(settings->value(SETTING_MEMORYBASE).toString());
|
||||||
characterWrap->setValue(settings->value(SETTING_CHARACTER_WRAP).toInt());
|
characterWrap->setValue(settings->value(SETTING_CHARACTER_WRAP).toInt());
|
||||||
timezone->setValue(settings->value(SETTING_TIMEZONE).toInt());
|
timezone->setValue(settings->value(SETTING_TIMEZONE).toInt());
|
||||||
notRemembered->setValue(settings->value(SETTING_NOT_REMEMBERED).toDouble());
|
notRemembered->setValue(settings->value(SETTING_NOT_REMEMBERED).toDouble());
|
||||||
hard->setValue(settings->value(SETTING_HARD).toDouble());
|
hard->setValue(settings->value(SETTING_HARD).toDouble());
|
||||||
medium->setValue(settings->value(SETTING_MEDIUM).toDouble());
|
medium->setValue(settings->value(SETTING_MEDIUM).toDouble());
|
||||||
easy->setValue(settings->value(SETTING_EASY).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_CHARACTER_WRAP, characterWrap->value());
|
||||||
settings->setValue(SETTING_TIMEZONE, timezone->value());
|
settings->setValue(SETTING_TIMEZONE, timezone->value());
|
||||||
settings->setValue(SETTING_NOT_REMEMBERED, notRemembered->value());
|
settings->setValue(SETTING_NOT_REMEMBERED, notRemembered->value());
|
||||||
settings->setValue(SETTING_HARD, hard->value());
|
settings->setValue(SETTING_HARD, hard->value());
|
||||||
settings->setValue(SETTING_MEDIUM, medium->value());
|
settings->setValue(SETTING_MEDIUM, medium->value());
|
||||||
settings->setValue(SETTING_EASY, easy->value());
|
settings->setValue(SETTING_EASY, easy->value());
|
||||||
|
updateSettingsLabel(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto loadSettings = [setSettingInputs, updateSettingsLabel]() {
|
||||||
|
settings->sync();
|
||||||
|
setSettingInputs();
|
||||||
|
updateSettingsLabel(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
@@ -120,17 +232,32 @@ QWidget *initSettings () {
|
|||||||
saveSettings();
|
saveSettings();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
QObject::connect(
|
||||||
|
btnLoad,
|
||||||
|
&QPushButton::clicked,
|
||||||
|
[loadSettings]() {
|
||||||
|
loadSettings();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
loadSettings();
|
||||||
|
|
||||||
QShortcut* shortcutSave = new QShortcut(QKeySequence("Ctrl+S"), settingsWindow);
|
QShortcut* shortcutSave = new QShortcut(QKeySequence("Ctrl+S"), settingsWindow);
|
||||||
QObject::connect(shortcutSave, &QShortcut::activated, [saveSettings]() {
|
QObject::connect(shortcutSave, &QShortcut::activated, [saveSettings]() {
|
||||||
saveSettings();
|
saveSettings();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
auto spacer = new QSpacerItem(
|
||||||
|
50,
|
||||||
|
50,
|
||||||
|
QSizePolicy::Expanding,
|
||||||
|
QSizePolicy::Expanding
|
||||||
|
);
|
||||||
|
|
||||||
|
mainLayout->addWidget(top);
|
||||||
mainLayout->addLayout(formLayout);
|
mainLayout->addLayout(formLayout);
|
||||||
mainLayout->addWidget(btnSaveSettings);
|
mainLayout->addItem(spacer);
|
||||||
|
mainLayout->addLayout(btnLayout);
|
||||||
|
|
||||||
settingsWindow->setLayout(mainLayout);
|
settingsWindow->setLayout(mainLayout);
|
||||||
return settingsWindow;
|
return settingsWindow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -264,9 +264,6 @@ void releaseItem(QStandardItem** item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void releaseAllItems() {
|
void releaseAllItems() {
|
||||||
std::cout <<
|
|
||||||
std::format("Starting release, currently in the pool: {}", itemPool.size()) <<
|
|
||||||
std::endl;
|
|
||||||
auto releaseAllInModel = [](QStandardItemModel *model) {
|
auto releaseAllInModel = [](QStandardItemModel *model) {
|
||||||
auto itemCount = model->rowCount();
|
auto itemCount = model->rowCount();
|
||||||
for (size_t i = 0; i < itemCount; ++i) {
|
for (size_t i = 0; i < itemCount; ++i) {
|
||||||
@@ -281,7 +278,6 @@ void releaseAllItems() {
|
|||||||
for (auto groupView: groupViews) {
|
for (auto groupView: groupViews) {
|
||||||
releaseAllInModel(&groupView->itemModel);
|
releaseAllInModel(&groupView->itemModel);
|
||||||
}
|
}
|
||||||
std::cout << std::format("All items released, currently in the pool: {}", itemPool.size()) << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void hideQuestionElements() {
|
void hideQuestionElements() {
|
||||||
|
|||||||
Reference in New Issue
Block a user