improvements in UI

This commit is contained in:
jorenchik
2024-12-08 15:47:58 +02:00
parent 920e74c8d6
commit 3024ab2ed6
2 changed files with 68 additions and 26 deletions

View File

@@ -231,6 +231,7 @@
<h3>Atmiņas kartīšu saraksts</h3> <h3>Atmiņas kartīšu saraksts</h3>
<ul> <ul>
<li>Ctrl+O - atvērt atmiņas bāzi</li> <li>Ctrl+O - atvērt atmiņas bāzi</li>
<li>Ctrl+O - atvērt atmiņas bāzi failu pārlūkā</li>
<li>Ctrl+H - pamācība</li> <li>Ctrl+H - pamācība</li>
<li>Ctrl+, - uzstādījumi</li> <li>Ctrl+, - uzstādījumi</li>
<li>Ctrl+S - saglabāt</li> <li>Ctrl+S - saglabāt</li>

View File

@@ -31,6 +31,7 @@
#include <QFileDialog> #include <QFileDialog>
#include <QPushButton> #include <QPushButton>
#include <QTextBrowser> #include <QTextBrowser>
#include <QDesktopServices>
#include <Qsci/qscilexer.h> #include <Qsci/qscilexer.h>
#include <Qsci/qsciscintilla.h> #include <Qsci/qsciscintilla.h>
@@ -44,8 +45,8 @@
#include "parser.h" #include "parser.h"
// Atmiņas bāze. // Atmiņas bāze.
QString currentPath = ""; QString currentMbasePath = "";
std::string currentMdem = ""; std::string currentMdemPath = "";
QFileSystemModel *model; QFileSystemModel *model;
QTreeView *mdemList; QTreeView *mdemList;
std::map<std::string, MdemBuffer*> buffers; std::map<std::string, MdemBuffer*> buffers;
@@ -395,7 +396,7 @@ Mdem* makeMdem() {
currentMdemBuffer->questions.erase(currentMdemBuffer->questions.begin() + i); currentMdemBuffer->questions.erase(currentMdemBuffer->questions.begin() + i);
delete mdem->question; delete mdem->question;
mdem->question = nullptr; mdem->question = nullptr;
updateMdemInfo(getFilename(currentMdem), true); updateMdemInfo(getFilename(currentMdemPath), true);
break; break;
} }
} }
@@ -490,8 +491,8 @@ void update(bool isChanged) {
if (pagination->currentPage > -1) { if (pagination->currentPage > -1) {
switchPage(pagination->currentPage); switchPage(pagination->currentPage);
} }
if (currentMdem.length() > 0) { if (currentMdemPath.length() > 0) {
updateMdemInfo(getFilename(currentMdem), isChanged); updateMdemInfo(getFilename(currentMdemPath), isChanged);
} }
} }
@@ -588,7 +589,7 @@ void reloadMdem(std::string path) {
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;
if (currentMdem.compare(path) != 0 && if (currentMdemPath.compare(path) != 0 &&
(!pair.second->isModified || pair.second->error)) { (!pair.second->isModified || pair.second->error)) {
toRemove.push_back(pair.first); toRemove.push_back(pair.first);
} }
@@ -599,8 +600,8 @@ void reloadMdem(std::string path) {
MdemBuffer *buffer; MdemBuffer *buffer;
auto filename = getFilename(path); auto filename = getFilename(path);
if (currentMdem.compare(path) == 0) { if (currentMdemPath.compare(path) == 0) {
currentMdem = path; currentMdemPath = path;
if (buffers.contains(path)) { if (buffers.contains(path)) {
buffers.erase(path); buffers.erase(path);
} }
@@ -608,14 +609,14 @@ void reloadMdem(std::string path) {
buffer = new MdemBuffer; buffer = new MdemBuffer;
buffers[path] = buffer; buffers[path] = buffer;
currentMdemBuffer = buffer; currentMdemBuffer = buffer;
currentMdem = path; currentMdemPath = path;
} else { } else {
buffer = buffers[path]; buffer = buffers[path];
currentMdemBuffer = buffer; currentMdemBuffer = buffer;
makePages(); makePages();
switchPage(0); switchPage(0);
updateMdemInfo(getFilename(filename), buffer->isModified); updateMdemInfo(getFilename(filename), buffer->isModified);
currentMdem = path; currentMdemPath = path;
errorView->box.hide(); errorView->box.hide();
return; return;
} }
@@ -691,7 +692,7 @@ void reloadMdem(std::string path) {
switchPage(0); switchPage(0);
updateMdemInfo(filename, false); updateMdemInfo(filename, false);
} else { } else {
std::cout << std::format("Nevar atvert failu: {}", currentPath.toStdString()) << std::endl; std::cout << std::format("Nevar atvert failu: {}", currentMbasePath.toStdString()) << std::endl;
} }
for (auto mdem: mdems) { for (auto mdem: mdems) {
@@ -710,7 +711,7 @@ void pickDirectory(QString directory) {
) { ) {
QMessageBox::information( QMessageBox::information(
nullptr, nullptr,
"Error", "Kļūda",
"Direktorijs, kas ir norādīts kā noklusētais, neeksistē." "Direktorijs, kas ir norādīts kā noklusētais, neeksistē."
); );
return; return;
@@ -729,7 +730,7 @@ void pickDirectory(QString directory) {
"Atveršanas kļūda", "Atveršanas kļūda",
"Izvēlētajā direktorijā nav iespējams rakstīt." "Izvēlētajā direktorijā nav iespējams rakstīt."
); );
currentPath = ""; currentMbasePath = "";
return; return;
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
@@ -738,11 +739,11 @@ void pickDirectory(QString directory) {
"Atveršanas kļūda", "Atveršanas kļūda",
"Neizdevās pārbaudīt izvēlētā direktorija rakstāmību." "Neizdevās pārbaudīt izvēlētā direktorija rakstāmību."
); );
currentPath = ""; currentMbasePath = "";
return; return;
} }
currentPath = directory; currentMbasePath = directory;
// Atjauno failu sarakstu. // Atjauno failu sarakstu.
if (directory.length() <= 0) { if (directory.length() <= 0) {
@@ -756,7 +757,7 @@ void pickDirectory(QString directory) {
membaseLabel->setText(QString::fromStdString( membaseLabel->setText(QString::fromStdString(
std::format( std::format(
"Atmiņas bāze: {}", "Atmiņas bāze: {}",
getFilename(currentPath.toStdString()) getFilename(currentMbasePath.toStdString())
) )
)); ));
@@ -800,7 +801,7 @@ void setupEditorSave() {
setupMdem(editMdem, res.value.questions[0]); setupMdem(editMdem, res.value.questions[0]);
showBacklabels(editMdem); showBacklabels(editMdem);
editorWindow->hide(); editorWindow->hide();
updateMdemInfo(getFilename(currentMdem), true); updateMdemInfo(getFilename(currentMdemPath), true);
} else { } else {
QMessageBox::information( QMessageBox::information(
nullptr, nullptr,
@@ -824,7 +825,7 @@ void setupEditorSave() {
makePages(); makePages();
switchPage(0); switchPage(0);
editorWindow->hide(); editorWindow->hide();
updateMdemInfo(getFilename(currentMdem), true); updateMdemInfo(getFilename(currentMdemPath), true);
} }
} }
} }
@@ -833,8 +834,20 @@ void setupEditorSave() {
void saveMdem() { void saveMdem() {
start = std::chrono::high_resolution_clock::now(); start = std::chrono::high_resolution_clock::now();
auto filename = getFilename(currentMdem); std::filesystem::path dir(currentMdemPath);
std::ofstream out(currentMdem); auto parentPath = dir.parent_path();
if (!parentPath.empty() && !std::filesystem::exists(parentPath)) {
if (!std::filesystem::create_directories(parentPath)) {
QMessageBox::information(
nullptr,
"Kļūda",
"Neizdevās izveidot direktorijus faila saglabāšanai."
);
}
}
auto filename = getFilename(currentMdemPath);
std::ofstream out(currentMdemPath);
int wrap_width = 80; int wrap_width = 80;
if (settings->contains(SETTING_CHARACTER_WRAP)) { if (settings->contains(SETTING_CHARACTER_WRAP)) {
@@ -850,7 +863,7 @@ void saveMdem() {
wrap_width, wrap_width,
timezoneOffset timezoneOffset
); );
updateMdemInfo(getFilename(currentMdem), false); updateMdemInfo(getFilename(currentMdemPath), false);
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();
@@ -886,13 +899,14 @@ QMainWindow *initMdemListWindow() {
} }
QAction *actionOpen; QAction *actionOpen;
QAction *revealMbase;
QAction *openSettings; QAction *openSettings;
QAction *actionHelp; QAction *actionHelp;
{ // Izvēlne. { // Izvēlne.
QMenuBar *menuBar = new QMenuBar; QMenuBar *menuBar = new QMenuBar;
QFileDialog *fileDialog = new QFileDialog; QFileDialog *fileDialog = new QFileDialog;
QMenu *menu = new QMenu("Fails"); 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)");
QObject::connect( QObject::connect(
@@ -901,6 +915,12 @@ QMainWindow *initMdemListWindow() {
[fileDialog]() { [fileDialog]() {
fileDialog->setDirectory(QDir::homePath()); fileDialog->setDirectory(QDir::homePath());
fileDialog->setFileMode(QFileDialog::FileMode::Directory); fileDialog->setFileMode(QFileDialog::FileMode::Directory);
if (
!currentMbasePath.isEmpty() &&
std::filesystem::exists(currentMbasePath.toStdString())
) {
fileDialog->setDirectory(currentMbasePath);
}
fileDialog->open(); fileDialog->open();
QObject::disconnect(fileDialog, 0, 0, 0); QObject::disconnect(fileDialog, 0, 0, 0);
fileDialog->connect( fileDialog->connect(
@@ -911,6 +931,24 @@ QMainWindow *initMdemListWindow() {
} }
); );
revealMbase = menu->addAction("Atvērt atmiņas bāzi failu pārlūkā (Ctrl+M)");
QObject::connect(
revealMbase,
&QAction::triggered,
[]() {
auto directoryUrl = QUrl::fromLocalFile(
QDir(currentMbasePath).absolutePath()
);
if (!QDesktopServices::openUrl(directoryUrl)) {
QMessageBox::information(
nullptr,
"Kļūda",
"Neizdevās atvērt atmiņas bāzi failu pārlūkā."
);
}
}
);
openSettings = menu->addAction("Iestatījumi (Ctrl+,)"); openSettings = menu->addAction("Iestatījumi (Ctrl+,)");
QObject::connect( QObject::connect(
openSettings, openSettings,
@@ -1032,7 +1070,7 @@ QMainWindow *initMdemListWindow() {
if (!trainWindow->isHidden()) { if (!trainWindow->isHidden()) {
trainWindow->hide(); trainWindow->hide();
} }
reloadMdem(currentMdem); reloadMdem(currentMdemPath);
}); });
QObject::connect(&toolbar->btnSave, &QToolButton::clicked, []() { QObject::connect(&toolbar->btnSave, &QToolButton::clicked, []() {
if (!currentMdemBuffer) { if (!currentMdemBuffer) {
@@ -1109,9 +1147,9 @@ QMainWindow *initMdemListWindow() {
); );
auto header = mdemList->header(); auto header = mdemList->header();
header->setSectionResizeMode(QHeaderView::ResizeToContents); header->setSectionResizeMode(QHeaderView::ResizeToContents);
currentPath = settings->value(SETTING_MEMORYBASE).toString(); currentMbasePath = settings->value(SETTING_MEMORYBASE).toString();
if (currentPath.size() > 0) { if (currentMbasePath.size() > 0) {
pickDirectory(currentPath); pickDirectory(currentMbasePath);
} }
QObject::connect( QObject::connect(
@@ -1260,6 +1298,9 @@ QMainWindow *initMdemListWindow() {
addShortcut("Ctrl+O", [actionOpen]() { addShortcut("Ctrl+O", [actionOpen]() {
actionOpen->trigger(); actionOpen->trigger();
}); });
addShortcut("Ctrl+M", [revealMbase]() {
revealMbase->trigger();
});
addShortcut("Ctrl+H", [actionHelp]() { addShortcut("Ctrl+H", [actionHelp]() {
actionHelp->trigger(); actionHelp->trigger();
}); });