exit handling + comments

This commit is contained in:
jorenchik
2024-12-15 16:01:38 +02:00
parent 6a344fdb52
commit 0f0512bbbf
2 changed files with 259 additions and 96 deletions

View File

@@ -67,6 +67,7 @@ struct Toolbar {
void update(bool isChanged = false); void update(bool isChanged = false);
void saveMdem(); void saveMdem();
void saveMdem(MdemBuffer* mdemBuffer, std::string path);
void updateMdemInfo(std::string filename = "", bool isChanged = true); void updateMdemInfo(std::string filename = "", bool isChanged = true);
QMainWindow *initMdemListWindow(); QMainWindow *initMdemListWindow();
std::string outputMdem( std::string outputMdem(

View File

@@ -10,6 +10,7 @@
#include <QApplication> #include <QApplication>
#include <QMainWindow> #include <QMainWindow>
#include <QCloseEvent>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
@@ -50,7 +51,7 @@ std::string currentMdemPath = "";
QFileSystemModel *model; QFileSystemModel *model;
QTreeView *mdemList; QTreeView *mdemList;
std::map<std::string, MdemBuffer*> buffers; std::map<std::string, MdemBuffer*> buffers;
MdemBuffer *currentMdemBuffer; MdemBuffer *currentMdemBuffer = nullptr;
// Atmiņas kaartīšu saraksts. // Atmiņas kaartīšu saraksts.
std::vector<Mdem*> mdems = std::vector<Mdem*>(); std::vector<Mdem*> mdems = std::vector<Mdem*>();
@@ -71,6 +72,58 @@ QLabel *mdemLabel;
QLabel *lastPracticeLabel; QLabel *lastPracticeLabel;
QMainWindow *trainWindow; QMainWindow *trainWindow;
// Pielāgots lodziņa objekts, kas izveido ziņojuma
// lodziņu ar objektu pirms lietotne tiek aizvērta.
class CloseableMainWindow : public QMainWindow {
Q_OBJECT
public:
explicit CloseableMainWindow(QWidget *parent = nullptr)
: QMainWindow(parent) {
setWindowTitle("Exit Confirmation");
}
protected:
// Apstrādā aizveršanas notikumu.
void closeEvent(QCloseEvent *event) override {
QMessageBox msgBox;
// Pievieno tekstu un pogas.
msgBox.setWindowTitle("Darba beigšana");
msgBox.setText("Vai tiešām beigt darbu?");
auto *saveButton = msgBox.addButton(
"Saglabāt un beigt",
QMessageBox::AcceptRole
);
auto *exitButton = msgBox.addButton(
"Beigt bez saglabāšanas",
QMessageBox::DestructiveRole
);
auto *cancelButton = msgBox.addButton(
"Atcelt",
QMessageBox::RejectRole
);
msgBox.exec();
// Apstrādā izvēli.
if (msgBox.clickedButton() == saveButton) {
for (auto buffer: buffers) {
saveMdem(buffer.second, buffer.first);
}
QApplication::exit();
} else if (msgBox.clickedButton() == exitButton) {
QApplication::exit();
} else if (msgBox.clickedButton() == cancelButton) {
msgBox.close();
event->ignore();
} else {
// Ja izvēle nav izdarīta - izvēli atceļ.
event->ignore();
}
}
};
#include "mdemList.moc"
/* /*
* Atmiņas kartītei parāda visus slēptos elementus. * Atmiņas kartītei parāda visus slēptos elementus.
* */ * */
@@ -179,6 +232,10 @@ std::string outputMdem(
* */ * */
void makePages() { void makePages() {
pagination->pages.clear(); pagination->pages.clear();
if (!currentMdemBuffer) {
return;
}
auto len = currentMdemBuffer->questions.size(); auto len = currentMdemBuffer->questions.size();
perPage = settings->value(SETTING_PER_PAGE).toInt(); perPage = settings->value(SETTING_PER_PAGE).toInt();
auto pageAmount = len / perPage; auto pageAmount = len / perPage;
@@ -303,7 +360,9 @@ std::string getFilename(std::string path) {
* Atjauno atmiņas kartīšu faila augšējo informāciju. * Atjauno atmiņas kartīšu faila augšējo informāciju.
* */ * */
void updateMdemInfo(std::string filename, bool isChanged) { void updateMdemInfo(std::string filename, bool isChanged) {
if (currentMdemBuffer) {
currentMdemBuffer->isModified = isChanged; currentMdemBuffer->isModified = isChanged;
}
if (filename.length() > 0) { if (filename.length() > 0) {
std::stringstream ss; std::stringstream ss;
ss << std::format("Atmiņas kartīšu fails: {}", filename); ss << std::format("Atmiņas kartīšu fails: {}", filename);
@@ -313,7 +372,7 @@ void updateMdemInfo(std::string filename, bool isChanged) {
mdemLabel->setText(QString::fromStdString(ss.str())); mdemLabel->setText(QString::fromStdString(ss.str()));
ss.str(""); ss.str("");
if (currentMdemBuffer->trainedAt > 0) { if (currentMdemBuffer && currentMdemBuffer->trainedAt > 0) {
std::tm* tm = std::localtime(&currentMdemBuffer->trainedAt); std::tm* tm = std::localtime(&currentMdemBuffer->trainedAt);
char buffer[100]; char buffer[100];
std::strftime(buffer, sizeof(buffer), "%d.%m.%Y %H:%M", tm); std::strftime(buffer, sizeof(buffer), "%d.%m.%Y %H:%M", tm);
@@ -581,11 +640,15 @@ void switchPage(int pageIdx) {
createMdems(pageSlice); createMdems(pageSlice);
} }
/*
* Ielādē atmiņas kartītes un metadatus no norādītā faila ceļa.
* Ja eksistē bufferis, kam atbilst norādītais ceļš - ielādē
* no atmiņas.
*
* Ja tiek padots "", tad atmiņas kartīšu fails tiek atiestatīts.
* */
void reloadMdem(std::string path) { void reloadMdem(std::string path) {
if (path == "") { // Izņem bufferus, kas nav mainīti un nesakrīt ar tagadējo ceļu.
return;
}
auto toRemove = std::vector<std::string>(); auto toRemove = std::vector<std::string>();
for (auto it = buffers.begin(); it != buffers.end(); ++it) { for (auto it = buffers.begin(); it != buffers.end(); ++it) {
auto pair = *it; auto pair = *it;
@@ -600,23 +663,29 @@ void reloadMdem(std::string path) {
MdemBuffer *buffer; MdemBuffer *buffer;
auto filename = getFilename(path); auto filename = getFilename(path);
if (currentMdemPath.compare(path) == 0) { if (path == "") {
// Atiesta atmiņas kartīšu failus.
currentMdemBuffer = nullptr;
currentMdemPath = path; currentMdemPath = path;
} else if (currentMdemPath == path) {
// Pārlādē tagadējo failu - izdzēš esošo bufferi.
if (buffers.contains(path)) { if (buffers.contains(path)) {
buffers.erase(path); buffers.erase(path);
} }
} else if (!buffers.contains(path)) { } else if (!buffers.contains(path)) {
// Izveido bufferi, ja tāds neeksistē.
buffer = new MdemBuffer; buffer = new MdemBuffer;
buffers[path] = buffer; buffers[path] = buffer;
currentMdemBuffer = buffer; currentMdemBuffer = buffer;
currentMdemPath = path; currentMdemPath = path;
} else { } else {
// Bufferis eksistē, ielādē to no atmiņas.
buffer = buffers[path]; buffer = buffers[path];
currentMdemBuffer = buffer; currentMdemBuffer = buffer;
currentMdemPath = path;
makePages(); makePages();
switchPage(0); switchPage(0);
updateMdemInfo(getFilename(filename), buffer->isModified); updateMdemInfo(getFilename(filename), buffer->isModified);
currentMdemPath = path;
errorView->box.hide(); errorView->box.hide();
return; return;
} }
@@ -629,48 +698,58 @@ void reloadMdem(std::string path) {
for (auto mdem: mdems) { for (auto mdem: mdems) {
mdem->wMdem.hide(); mdem->wMdem.hide();
} }
if (currentMdemBuffer) {
for (auto question: currentMdemBuffer->questions) { for (auto question: currentMdemBuffer->questions) {
delete question; delete question;
} }
currentMdemBuffer->questions.clear(); currentMdemBuffer->questions.clear();
}
// TODO comment
if (currentMdemBuffer) {
if (file) { if (file) {
// Ielādē faila saturu buferī.
std::stringstream buffer; std::stringstream buffer;
buffer << file.rdbuf(); buffer << file.rdbuf();
content = buffer.str(); content = buffer.str();
currentMdemPath = path;
end = std::chrono::high_resolution_clock::now(); end = std::chrono::high_resolution_clock::now();
showTimes = settings->value(SETTING_SHOW_TIMES).toBool(); showTimes = settings->value(SETTING_SHOW_TIMES).toBool();
if (showTimes) { if (showTimes) {
std::cout << showTime("I/O laiks") << std::endl; std::cout << showTime("I/O laiks") << std::endl;
} }
debug = settings->value(SETTING_DEBUG).toBool(); debug = settings->value(SETTING_DEBUG).toBool();
auto res = transpile(content);
// Transpilē saturu.
auto res = transpile(content);
currentMdemBuffer->error = res.error.length() > 0; currentMdemBuffer->error = res.error.length() > 0;
if (res.error == "") { if (res.error == "") {
// Aizpilda pēdējo mācīšanās laiku.
if (res.value.lastTrainedAt == 0) { if (res.value.lastTrainedAt == 0) {
currentMdemBuffer->trainedAt = 0; currentMdemBuffer->trainedAt = 0;
} else { } else {
auto timezoneOffset = settings->value("timezone").toInt(); auto timezoneOffset = settings->value("timezone").toInt();
currentMdemBuffer->trainedAt = res.value.lastTrainedAt - 3600 * timezoneOffset; currentMdemBuffer->trainedAt = res.value.lastTrainedAt - 3600 * timezoneOffset;
} }
if (settings->value(SETTING_DEBUG).toBool()) { if (settings->value(SETTING_DEBUG).toBool()) {
std::cout << std::format("Pēdējās mācīšanās reize: {}", currentMdemBuffer->trainedAt) std::cout << std::format(
"Pēdējās mācīšanās reize: {}",
currentMdemBuffer->trainedAt
)
<< std::endl; << std::endl;
} }
// Uzstādā jautājumus.
currentMdemBuffer->questions = res.value.questions; currentMdemBuffer->questions = res.value.questions;
errorView->box.hide(); errorView->box.hide();
} else { } else {
// Apstrādā un izveido kļūdu.
currentMdemBuffer->trainedAt = 0; currentMdemBuffer->trainedAt = 0;
std::cout << std::format("Transpilācijas kļūda: {}", res.error) << std::endl;
for (auto question: res.value.questions) { for (auto question: res.value.questions) {
delete question; delete question;
} }
// Parāda kļūdu.
hMdemScroll->removeItem(mdemSpacer); hMdemScroll->removeItem(mdemSpacer);
errorView->label.setText( errorView->label.setText(
QString::fromStdString( QString::fromStdString(
@@ -684,17 +763,28 @@ void reloadMdem(std::string path) {
) )
); );
errorView->box.show(); errorView->box.show();
hMdemScroll->addWidget(&errorView->box); hMdemScroll->addWidget(&errorView->box);
hMdemScroll->addItem(mdemSpacer); hMdemScroll->addItem(mdemSpacer);
} }
// Uzstāda sarakstu.
makePages(); makePages();
switchPage(0); switchPage(0);
updateMdemInfo(filename, false); updateMdemInfo(filename, false);
} else { } else {
std::cout << std::format("Nevar atvert failu: {}", currentMbasePath.toStdString()) << std::endl; std::cout
<< std::format(
"Nevar atvert failu: {}",
currentMbasePath.toStdString()
)
<< std::endl;
}
} else {
updateMdemInfo("", false);
} }
// Visām kartītēm paslēp elementus un mācīšanās
// logā paslēp visus elementus.
for (auto mdem: mdems) { for (auto mdem: mdems) {
mdem->wBack.hide(); mdem->wBack.hide();
mdem->toggleVisibility.setText("Parādīt"); mdem->toggleVisibility.setText("Parādīt");
@@ -702,8 +792,12 @@ void reloadMdem(std::string path) {
hideQuestionElements(); hideQuestionElements();
} }
/*
* Atvēr direktoriju - atmiņas bāzi.
* */
void pickDirectory(QString directory) { void pickDirectory(QString directory) {
// Pārbauda eksistenci.
auto path = std::filesystem::path(directory.toStdString()); auto path = std::filesystem::path(directory.toStdString());
if ( if (
!std::filesystem::exists(directory.toStdString()) || !std::filesystem::exists(directory.toStdString()) ||
@@ -742,9 +836,33 @@ void pickDirectory(QString directory) {
currentMbasePath = ""; currentMbasePath = "";
return; return;
} }
currentMbasePath = directory; currentMbasePath = directory;
// Atbrīvo nevajadzīgos bufferus.
for (auto pair: buffers) {
bool isFileAChild;
// Pārbauda, vai bufferis ir izvēlētās direktorijas pēctacis.
try {
auto filePath = std::filesystem::absolute(pair.first).string();
auto dirPath = std::filesystem::absolute(currentMbasePath.toStdString()).string();
if (dirPath.back() != '/') {
dirPath += '/';
}
isFileAChild = filePath.rfind(dirPath, 0) == 0;
} catch (const std::filesystem::filesystem_error& e) {
isFileAChild = false;
}
// Ja bufferis nav pēctacis, to atbrīvo.
if (!isFileAChild) {
for (auto question: pair.second->questions) {
delete question;
}
pair.second->questions.clear();
buffers.erase(pair.first);
}
}
// Atjauno failu sarakstu. // Atjauno failu sarakstu.
if (directory.length() <= 0) { if (directory.length() <= 0) {
membaseLabel->setText(directory); membaseLabel->setText(directory);
@@ -761,15 +879,27 @@ void pickDirectory(QString directory) {
) )
)); ));
// Atiestata direktoriju.
reloadMdem(""); reloadMdem("");
} }
/*
* Saglabā kartīti(/-es) redigēšanai vai pievienošanai.
* */
void setupEditorSave() { void setupEditorSave() {
const QString title = editMdem ? "Rediģēšanas kļūda" : "Pievienošanas kļūda"; if (!currentMdemBuffer) {
return;
}
debug = settings->value(SETTING_DEBUG).toBool(); debug = settings->value(SETTING_DEBUG).toBool();
showTimes = settings->value(SETTING_SHOW_TIMES).toBool(); showTimes = settings->value(SETTING_SHOW_TIMES).toBool();
// Transpilē.
auto res = transpile(editor->text().toStdString()); auto res = transpile(editor->text().toStdString());
const QString title = editMdem ? "Rediģēšanas kļūda" : "Pievienošanas kļūda";
if (res.error.length() > 0) { if (res.error.length() > 0) {
// Apstrādā un parāda kļūdu.
currentMdemBuffer->trainedAt = 0; currentMdemBuffer->trainedAt = 0;
for (auto question: res.value.questions) { for (auto question: res.value.questions) {
delete question; delete question;
@@ -781,6 +911,7 @@ void setupEditorSave() {
); );
} else { } else {
if (editMdem) { if (editMdem) {
// Jautājuma redigēšana.
if (res.value.questions.size() <= 0) { if (res.value.questions.size() <= 0) {
QMessageBox::information( QMessageBox::information(
nullptr, nullptr,
@@ -792,9 +923,14 @@ void setupEditorSave() {
editMdem->question = res.value.questions[0]; editMdem->question = res.value.questions[0];
for (size_t i = 0; i < currentMdemBuffer->questions.size(); ++i) { for (size_t i = 0; i < currentMdemBuffer->questions.size(); ++i) {
if (currentMdemBuffer->questions[i] == oldQuestion) { if (currentMdemBuffer->questions[i] == oldQuestion) {
currentMdemBuffer->questions.erase(currentMdemBuffer->questions.begin() + i); currentMdemBuffer->questions.erase(
currentMdemBuffer->questions.begin() + i
);
delete oldQuestion; delete oldQuestion;
currentMdemBuffer->questions.insert(currentMdemBuffer->questions.begin() + i, res.value.questions[0]); currentMdemBuffer->questions.insert(
currentMdemBuffer->questions.begin() + i,
res.value.questions[0]
);
break; break;
} }
} }
@@ -810,6 +946,7 @@ void setupEditorSave() {
); );
} }
} else { } else {
// Jautājuma pievienošana.
if (res.value.questions.size() <= 0) { if (res.value.questions.size() <= 0) {
QMessageBox::information( QMessageBox::information(
nullptr, nullptr,
@@ -831,10 +968,25 @@ void setupEditorSave() {
} }
}; };
/*
* Saglabā tagadējo bufferi.
* */
void saveMdem() { void saveMdem() {
saveMdem(currentMdemBuffer, currentMdemPath);
}
/*
* Saglabā noteikto bufferi ar doto ceļu.
* Failā ieliek detraspilētās kartītes un datumu ar laiku.
* */
void saveMdem(MdemBuffer* buffer, std::string path) {
if (!buffer) {
return;
}
start = std::chrono::high_resolution_clock::now(); start = std::chrono::high_resolution_clock::now();
std::filesystem::path dir(currentMdemPath); // Izveido direktoriju, ja gadījuma tāda nav saglabājamam ceļam.
std::filesystem::path dir(path);
auto parentPath = dir.parent_path(); auto parentPath = dir.parent_path();
if (!parentPath.empty() && !std::filesystem::exists(parentPath)) { if (!parentPath.empty() && !std::filesystem::exists(parentPath)) {
if (!std::filesystem::create_directories(parentPath)) { if (!std::filesystem::create_directories(parentPath)) {
@@ -846,9 +998,7 @@ void saveMdem() {
} }
} }
auto filename = getFilename(currentMdemPath); // Iegūst parametrus, kas ietekmē detranspilēšanu.
std::ofstream out(currentMdemPath);
int wrap_width = 80; int wrap_width = 80;
if (settings->contains(SETTING_CHARACTER_WRAP)) { if (settings->contains(SETTING_CHARACTER_WRAP)) {
wrap_width = settings->value(SETTING_CHARACTER_WRAP).toInt(); wrap_width = settings->value(SETTING_CHARACTER_WRAP).toInt();
@@ -857,15 +1007,20 @@ void saveMdem() {
if (settings->contains(SETTING_TIMEZONE)) { if (settings->contains(SETTING_TIMEZONE)) {
timezoneOffset = settings->value(SETTING_TIMEZONE).toInt(); timezoneOffset = settings->value(SETTING_TIMEZONE).toInt();
} }
// Detranspilē un saglabā saturu failā.
auto filename = getFilename(path);
std::ofstream out(path);
out << outputMdem( out << outputMdem(
currentMdemBuffer->questions, buffer->questions,
currentMdemBuffer->trainedAt, buffer->trainedAt,
wrap_width, wrap_width,
timezoneOffset timezoneOffset
); );
updateMdemInfo(getFilename(currentMdemPath), false); // Indikators - kartīšu faila saturs nav mainīts pēc pēdējās saglabāšanas.
end = std::chrono::high_resolution_clock::now(); updateMdemInfo(getFilename(path), false);
end = std::chrono::high_resolution_clock::now();
showTimes = settings->value(SETTING_SHOW_TIMES).toBool(); showTimes = settings->value(SETTING_SHOW_TIMES).toBool();
if (showTimes) { if (showTimes) {
std::cout << showTime("Saglabāšanas laiks") << std::endl; std::cout << showTime("Saglabāšanas laiks") << std::endl;
@@ -876,11 +1031,13 @@ void saveMdem() {
* Inicializē visus atmiņas kartīšu saraksta elementus un izvēlni. * Inicializē visus atmiņas kartīšu saraksta elementus un izvēlni.
*/ */
QMainWindow *initMdemListWindow() { QMainWindow *initMdemListWindow() {
QMainWindow* window = new QMainWindow;
pagination = new Pagination;
auto *toolbar = new Toolbar();
// Saistīti logi. // Pamata objekti
auto window = new CloseableMainWindow;
auto *toolbar = new Toolbar();
pagination = new Pagination;
// Saistītie logi.
auto *settingsWindow = initSettings(); auto *settingsWindow = initSettings();
trainWindow = initTrainWindow(); trainWindow = initTrainWindow();
trainWindow->resize(800, 600); trainWindow->resize(800, 600);
@@ -906,9 +1063,12 @@ QMainWindow *initMdemListWindow() {
QMenuBar *menuBar = new QMenuBar; QMenuBar *menuBar = new QMenuBar;
QFileDialog *fileDialog = new QFileDialog; QFileDialog *fileDialog = new QFileDialog;
// Darbību saraksts.
QMenu *menu = new QMenu("Darbības"); QMenu *menu = new QMenu("Darbības");
menu->setStyleSheet("font-size: 15px;"); menu->setStyleSheet("font-size: 15px;");
actionOpen = menu->addAction("Atvērt atmiņas bāzi (Ctrl+O)"); actionOpen = menu->addAction("Atvērt atmiņas bāzi (Ctrl+O)");
// Bāzes atvēršana.
QObject::connect( QObject::connect(
actionOpen, actionOpen,
&QAction::triggered, &QAction::triggered,
@@ -931,6 +1091,7 @@ QMainWindow *initMdemListWindow() {
} }
); );
// Parādīt failu pārlūkā.
revealMbase = menu->addAction("Atvērt atmiņas bāzi failu pārlūkā (Ctrl+M)"); revealMbase = menu->addAction("Atvērt atmiņas bāzi failu pārlūkā (Ctrl+M)");
QObject::connect( QObject::connect(
revealMbase, revealMbase,
@@ -949,6 +1110,7 @@ QMainWindow *initMdemListWindow() {
} }
); );
// Konfigurācija.
openSettings = menu->addAction("Iestatījumi (Ctrl+,)"); openSettings = menu->addAction("Iestatījumi (Ctrl+,)");
QObject::connect( QObject::connect(
openSettings, openSettings,
@@ -957,6 +1119,7 @@ QMainWindow *initMdemListWindow() {
settingsWindow->show(); settingsWindow->show();
}); });
// Palīdzība.
actionHelp = menu->addAction("Palīdzība (Ctrl+H)"); actionHelp = menu->addAction("Palīdzība (Ctrl+H)");
QObject::connect( QObject::connect(
actionHelp, actionHelp,
@@ -979,6 +1142,7 @@ QMainWindow *initMdemListWindow() {
QVBoxLayout *vlEditor = new QVBoxLayout; QVBoxLayout *vlEditor = new QVBoxLayout;
wEditor->setLayout(vlEditor); wEditor->setLayout(vlEditor);
// C++ lekseris - iekrāso elementus atbilstoši C++ sintaksei.
QsciLexerCPP *lexer = new QsciLexerCPP; QsciLexerCPP *lexer = new QsciLexerCPP;
editor = new QsciScintilla; editor = new QsciScintilla;
editor->setLexer(lexer); editor->setLexer(lexer);
@@ -1002,6 +1166,7 @@ QMainWindow *initMdemListWindow() {
vlEditor->addWidget(editor); vlEditor->addWidget(editor);
vlEditor->addWidget(editorButtons); vlEditor->addWidget(editorButtons);
// Saglabāšanas funkcija izsauc vienu funkciju redigēšanai vai pievienošanai.
QShortcut* shortcutSave = new QShortcut(QKeySequence("Ctrl+S"), editorWindow); QShortcut* shortcutSave = new QShortcut(QKeySequence("Ctrl+S"), editorWindow);
QObject::connect(shortcutSave, &QShortcut::activated, []() { QObject::connect(shortcutSave, &QShortcut::activated, []() {
if (editor->isVisible()) { if (editor->isVisible()) {
@@ -1073,9 +1238,6 @@ QMainWindow *initMdemListWindow() {
reloadMdem(currentMdemPath); reloadMdem(currentMdemPath);
}); });
QObject::connect(&toolbar->btnSave, &QToolButton::clicked, []() { QObject::connect(&toolbar->btnSave, &QToolButton::clicked, []() {
if (!currentMdemBuffer) {
return;
}
saveMdem(); saveMdem();
}); });
QObject::connect( QObject::connect(