many UI improvement

This commit is contained in:
jorenchik
2024-12-16 20:22:44 +02:00
parent f5d442ea85
commit dd1be66181
2 changed files with 109 additions and 17 deletions

View File

@@ -1,4 +1,6 @@
#include <cstdio>
#include <qfilesystemmodel.h>
#include <qnamespace.h>
#include <time.h>
#include <fstream>
#include <iostream>
@@ -72,6 +74,28 @@ QLabel *mdemLabel;
QLabel *lastPracticeLabel;
QMainWindow *trainWindow;
/*
* Pielāgots modelis, kas parāda * indikatoru atbilstoši buferu sarakstam.
* */
class IndicatorFileSystemModel : public QFileSystemModel {
Q_OBJECT
public:
explicit IndicatorFileSystemModel(QObject *parent = nullptr) : QFileSystemModel(parent) {}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
auto path = filePath(index);
if (role == Qt::DisplayRole) {
QString originalName = QFileSystemModel::data(index, role).toString();
if (buffers.contains(path.toStdString()) && buffers[path.toStdString()]->isModified) {
return QString("%1 *").arg(originalName);
} else {
return QString("%1").arg(originalName);
}
}
return QFileSystemModel::data(index, role);
}
};
// Pielāgots lodziņa objekts, kas izveido ziņojuma
// lodziņu ar objektu pirms lietotne tiek aizvērta.
class CloseableMainWindow : public QMainWindow {
@@ -107,8 +131,8 @@ protected:
// Apstrādā izvēli.
if (msgBox.clickedButton() == saveButton) {
for (auto buffer: buffers) {
saveMdem(buffer.second, buffer.first);
for (auto pair: buffers) {
saveMdem(pair.second, pair.first);
}
QApplication::exit();
} else if (msgBox.clickedButton() == exitButton) {
@@ -122,6 +146,7 @@ protected:
}
}
};
#include "mdemList.moc"
/*
@@ -360,9 +385,20 @@ std::string getFilename(std::string path) {
* Atjauno atmiņas kartīšu faila augšējo informāciju.
* */
void updateMdemInfo(std::string filename, bool isChanged) {
// Uzstāda indikatoru.
if (currentMdemBuffer) {
bool previousValue = currentMdemBuffer->isModified;
currentMdemBuffer->isModified = isChanged;
if (previousValue != isChanged) {
model->dataChanged(
model->index(0, 0),
model->index(model->rowCount() - 1, model->columnCount() - 1),
{Qt::DisplayRole}
);
}
}
// Uzstāda informāciju.
if (filename.length() > 0) {
std::stringstream ss;
ss << std::format("Atmiņas kartīšu fails: {}", filename);
@@ -424,6 +460,7 @@ Mdem* makeMdem() {
&mdem->editButton,
&QToolButton::clicked,
[mdem]() {
trainWindow->close();
editMdem = mdem;
if (mdem->question) {
int wrap_width = 80;
@@ -448,6 +485,7 @@ Mdem* makeMdem() {
&mdem->deleteButton,
&QToolButton::clicked,
[mdem]() {
trainWindow->close();
if (mdem->question) {
Question* deleted = nullptr;
for (size_t i = 0; i < currentMdemBuffer->questions.size(); ++i) {
@@ -661,26 +699,26 @@ void reloadMdem(std::string path) {
buffers.erase(key);
}
MdemBuffer *buffer;
auto filename = getFilename(path);
if (path == "") {
// Atiesta atmiņas kartīšu failus.
currentMdemBuffer = nullptr;
currentMdemPath = path;
} else if (currentMdemPath == path) {
// Pārlādē tagadējo failu - izdzēš esošo bufferi.
if (buffers.contains(path)) {
buffers.erase(path);
}
// Pārlādē tagadējo failu - nonullē esošo bufferi.
for (auto question: currentMdemBuffer->questions) {
delete question;
}
currentMdemBuffer->questions.clear();
} else if (!buffers.contains(path)) {
// Izveido bufferi, ja tāds neeksistē.
buffer = new MdemBuffer;
auto *buffer = new MdemBuffer;
buffers[path] = buffer;
currentMdemBuffer = buffer;
currentMdemPath = path;
} else {
// Bufferis eksistē, ielādē to no atmiņas.
buffer = buffers[path];
auto *buffer = buffers[path];
currentMdemBuffer = buffer;
currentMdemPath = path;
makePages();
@@ -705,7 +743,7 @@ void reloadMdem(std::string path) {
currentMdemBuffer->questions.clear();
}
// TODO comment
// Transpilē faila saturu un izveido kartītes.
if (currentMdemBuffer) {
if (file) {
// Ielādē faila saturu buferī.
@@ -973,6 +1011,7 @@ void setupEditorSave() {
* */
void saveMdem() {
saveMdem(currentMdemBuffer, currentMdemPath);
updateMdemInfo(getFilename(currentMdemPath), false);
}
/*
@@ -1017,8 +1056,6 @@ void saveMdem(MdemBuffer* buffer, std::string path) {
wrap_width,
timezoneOffset
);
// Indikators - kartīšu faila saturs nav mainīts pēc pēdējās saglabāšanas.
updateMdemInfo(getFilename(path), false);
end = std::chrono::high_resolution_clock::now();
showTimes = settings->value(SETTING_SHOW_TIMES).toBool();
@@ -1057,6 +1094,7 @@ QMainWindow *initMdemListWindow() {
QAction *actionOpen;
QAction *revealMbase;
QAction *saveAll;
QAction *openSettings;
QAction *actionHelp;
{ // Izvēlne.
@@ -1066,9 +1104,9 @@ QMainWindow *initMdemListWindow() {
// Darbību saraksts.
QMenu *menu = new QMenu("Darbības");
menu->setStyleSheet("font-size: 15px;");
actionOpen = menu->addAction("Atvērt atmiņas bāzi (Ctrl+O)");
// Bāzes atvēršana.
actionOpen = menu->addAction("Atvērt atmiņas bāzi (Ctrl+O)");
QObject::connect(
actionOpen,
&QAction::triggered,
@@ -1110,6 +1148,25 @@ QMainWindow *initMdemListWindow() {
}
);
// Saglabāt visus failus.
saveAll = menu->addAction("Saglabāt atmiņas bāzi (Ctrl+D)");
QObject::connect(
saveAll,
&QAction::triggered,
[]() {
for (auto pair: buffers) {
saveMdem(pair.second, pair.first);
pair.second->isModified = false;
}
model->dataChanged(
model->index(0, 0),
model->index(model->rowCount() - 1, model->columnCount() - 1),
{Qt::DisplayRole}
);
updateMdemInfo(getFilename(currentMdemPath), false);
}
);
// Konfigurācija.
openSettings = menu->addAction("Iestatījumi (Ctrl+,)");
QObject::connect(
@@ -1224,6 +1281,7 @@ QMainWindow *initMdemListWindow() {
if (!currentMdemBuffer) {
return;
}
trainWindow->close();
editMdem = nullptr;
editorWindow->show();
editor->setText("");
@@ -1239,6 +1297,7 @@ QMainWindow *initMdemListWindow() {
});
QObject::connect(&toolbar->btnSave, &QToolButton::clicked, []() {
saveMdem();
updateMdemInfo(getFilename(currentMdemPath), false);
});
QObject::connect(
&toolbar->btnPractice,
@@ -1295,7 +1354,7 @@ QMainWindow *initMdemListWindow() {
QVBoxLayout *leftLayout = new QVBoxLayout();
leftWidget->setLayout(leftLayout);
model = new QFileSystemModel();
model = new IndicatorFileSystemModel();
mdemList = new QTreeView();
QStringList filters;
@@ -1319,9 +1378,13 @@ QMainWindow *initMdemListWindow() {
&QTreeView::doubleClicked,
[](const QModelIndex &index) {
auto fileInfo = model->fileInfo(index);
auto path = fileInfo.filePath().toStdString();
if (currentMdemPath == path) {
return;
}
if (!fileInfo.isDir()) {
trainWindow->close();
reloadMdem(fileInfo.filePath().toStdString());
reloadMdem(path);
}
}
);
@@ -1463,6 +1526,9 @@ QMainWindow *initMdemListWindow() {
addShortcut("Ctrl+M", [revealMbase]() {
revealMbase->trigger();
});
addShortcut("Ctrl+D", [saveAll]() {
saveAll->trigger();
});
addShortcut("Ctrl+H", [actionHelp]() {
actionHelp->trigger();
});

View File

@@ -1,3 +1,5 @@
#include <charconv>
#include <qlabel.h>
#include <random>
#include <algorithm>
#include <cstdint>
@@ -211,6 +213,7 @@ QVBoxLayout *vTrainWidget;
CustomItemDelegate *itemDelegate;
// Jautājums.
QLabel *intervalLabel;
QWidget *questionBox;
QVBoxLayout *vQuestionBox;
QLabel *lQuestionText;
@@ -710,6 +713,22 @@ time_t getTime() {
).count();
}
/*
* Uzstāda intervāla skaitļa tekstu.
* Ja ir padots -1, intervāla tekstu paslēp.
*/
void setInterval(double interval) {
if (interval == -1.0) {
intervalLabel->setText("");
} else {
intervalLabel->setText(
QString::fromStdString(
std::format("Intervāls: {} stundas", interval)
)
);
}
}
/*
* Uzstāda nākamo jautajumu atbilstoši algoritmam.
*/
@@ -717,6 +736,7 @@ void setupNextQuestion() {
if (practiceBuffer->questions.size() <= 0) {
return;
}
setInterval(-1.0);
switch (practiceAlgoritm) {
case PRIMARY: {
// Uzstāda nākamo pēc indeksa.
@@ -814,6 +834,7 @@ void setCooldownHours(double cooldown) {
practiceBuffer->trainedAt = time;
auto question = practiceBuffer->questions[currentQuestionIndex];
question->cooldown = cooldown;
setInterval(cooldown);
update(true);
}
@@ -856,6 +877,12 @@ QMainWindow *initTrainWindow() {
topButtons->setStyleSheet(buttonStyle);
topButtons->setLayout(hTopButtons);
// Intervāla teksts.
intervalLabel = new QLabel;
hTopButtons->addWidget(intervalLabel);
intervalLabel->setStyleSheet("font-size: 20px");
setInterval(-1.0);
// Starplika, lai poga parādās labā pusē.
auto topLeftSpacer = new QSpacerItem(
50,
@@ -1071,8 +1098,7 @@ QMainWindow *initTrainWindow() {
[]() {
if (btnCheck->isVisible()) {
btnCheck->click();
}
if (btnShowAnswer->isVisible()) {
} else if (btnShowAnswer->isVisible()) {
btnShowAnswer->click();
}
}