prompts to save or cancel on mbase change

This commit is contained in:
jorenchik
2024-12-25 11:00:46 +02:00
parent 6ee66399cb
commit 0448e51c63

View File

@@ -96,6 +96,53 @@ public:
}
};
/*
* Jautājuma lodziņa atbildes.
*/
enum PromptChoice {
YES, NO, CANCEL
};
/*
* Parāda jautājuma lodziņu ar pielāgojamiem tekstiem.
* Iespējamās atbildes - Jā, Nē, Atcelt.
*/
PromptChoice showPrompt(
QString windowTitle,
QString text,
QString yesText,
QString noText,
QString cancelText
) {
QMessageBox msgBox;
// Pievieno tekstu un pogas.
msgBox.setWindowTitle(windowTitle);
msgBox.setText(text);
auto *yesButton = msgBox.addButton(
yesText,
QMessageBox::AcceptRole
);
auto *noButton = msgBox.addButton(
noText,
QMessageBox::DestructiveRole
);
auto *cancelButton = msgBox.addButton(
cancelText,
QMessageBox::RejectRole
);
msgBox.exec();
// Apstrādā izvēli.
if (msgBox.clickedButton() == yesButton) {
return YES;
} else if (msgBox.clickedButton() == noButton) {
return NO;
} else {
return CANCEL;
}
}
// Pielāgots lodziņa objekts, kas izveido ziņojuma
// lodziņu ar objektu pirms lietotne tiek aizvērta.
class CloseableMainWindow : public QMainWindow {
@@ -103,46 +150,32 @@ class CloseableMainWindow : public QMainWindow {
public:
explicit CloseableMainWindow(QWidget *parent = nullptr)
: QMainWindow(parent) {
setWindowTitle("Exit Confirmation");
}
: QMainWindow(parent) {}
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(
auto answer = showPrompt(
"Darba beigšana",
"Vai tiešām beigt darbu?",
"Saglabāt un beigt",
QMessageBox::AcceptRole
);
auto *exitButton = msgBox.addButton(
"Beigt bez saglabāšanas",
QMessageBox::DestructiveRole
"Atcelt"
);
auto *cancelButton = msgBox.addButton(
"Atcelt",
QMessageBox::RejectRole
);
msgBox.exec();
// Apstrādā izvēli.
if (msgBox.clickedButton() == saveButton) {
switch (answer) {
case YES: {
for (auto pair: buffers) {
saveMdem(pair.second, pair.first);
}
QApplication::exit();
} else if (msgBox.clickedButton() == exitButton) {
} break;
case NO: {
QApplication::exit();
} else if (msgBox.clickedButton() == cancelButton) {
msgBox.close();
event->ignore();
} else {
// Ja izvēle nav izdarīta - izvēli atceļ.
} break;
case CANCEL: {
event->ignore();
} break;
}
}
};
@@ -1124,7 +1157,27 @@ QMainWindow *initMdemListWindow() {
fileDialog->connect(
fileDialog,
&QFileDialog::fileSelected,
pickDirectory
[](QString directory) {
auto answer = showPrompt(
"Atmiņas bāzes maiņa",
"Vai saglabāt atmiņas bāzi pirms to mainīt?",
"Saglabāt un mainīt",
"Mainīt bez saglabāšanas",
"Nemainīt atmiņas bāzi"
);
switch (answer) {
case YES: {
for (auto pair: buffers) {
saveMdem(pair.second, pair.first);
pair.second->isModified = false;
}
} break;
case CANCEL: {
return;
} break;
}
pickDirectory(directory);
}
);
}
);