diff --git a/src/qtapp/mdemList.cpp b/src/qtapp/mdemList.cpp index 9d7020b..870263d 100644 --- a/src/qtapp/mdemList.cpp +++ b/src/qtapp/mdemList.cpp @@ -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,47 +150,33 @@ 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( - "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 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(); - } + auto answer = showPrompt( + "Darba beigšana", + "Vai tiešām beigt darbu?", + "Saglabāt un beigt", + "Beigt bez saglabāšanas", + "Atcelt" + ); + switch (answer) { + case YES: { + for (auto pair: buffers) { + saveMdem(pair.second, pair.first); + } + QApplication::exit(); + } break; + case NO: { + QApplication::exit(); + } 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); + } ); } );