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 // Pielāgots lodziņa objekts, kas izveido ziņojuma
// lodziņu ar objektu pirms lietotne tiek aizvērta. // lodziņu ar objektu pirms lietotne tiek aizvērta.
class CloseableMainWindow : public QMainWindow { class CloseableMainWindow : public QMainWindow {
@@ -103,47 +150,33 @@ class CloseableMainWindow : public QMainWindow {
public: public:
explicit CloseableMainWindow(QWidget *parent = nullptr) explicit CloseableMainWindow(QWidget *parent = nullptr)
: QMainWindow(parent) { : QMainWindow(parent) {}
setWindowTitle("Exit Confirmation");
}
protected: protected:
// Apstrādā aizveršanas notikumu. // Apstrādā aizveršanas notikumu.
void closeEvent(QCloseEvent *event) override { void closeEvent(QCloseEvent *event) override {
QMessageBox msgBox; auto answer = showPrompt(
"Darba beigšana",
// Pievieno tekstu un pogas. "Vai tiešām beigt darbu?",
msgBox.setWindowTitle("Darba beigšana"); "Saglabāt un beigt",
msgBox.setText("Vai tiešām beigt darbu?"); "Beigt bez saglabāšanas",
auto *saveButton = msgBox.addButton( "Atcelt"
"Saglabāt un beigt", );
QMessageBox::AcceptRole switch (answer) {
); case YES: {
auto *exitButton = msgBox.addButton( for (auto pair: buffers) {
"Beigt bez saglabāšanas", saveMdem(pair.second, pair.first);
QMessageBox::DestructiveRole }
); QApplication::exit();
auto *cancelButton = msgBox.addButton( } break;
"Atcelt", case NO: {
QMessageBox::RejectRole QApplication::exit();
); } break;
msgBox.exec(); case CANCEL: {
event->ignore();
// Apstrādā izvēli. } break;
if (msgBox.clickedButton() == saveButton) { }
for (auto pair: buffers) {
saveMdem(pair.second, pair.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();
}
} }
}; };
@@ -1124,7 +1157,27 @@ QMainWindow *initMdemListWindow() {
fileDialog->connect( fileDialog->connect(
fileDialog, fileDialog,
&QFileDialog::fileSelected, &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);
}
); );
} }
); );