@@ -1165,33 +1165,28 @@ void MainWindow::openFileList(const QStringList &fileNames)
11651165
11661166bool MainWindow::checkEditorsBeforeClose (const QVector<ScintillaNext *> &editors)
11671167{
1168- for ( ScintillaNext *editor : editors) {
1169- if (!editor-> isSavedToDisk () ) {
1170- // Switch to it
1171- dockedEditor-> switchToEditor (editor);
1168+ QVector< ScintillaNext *> unsaved;
1169+ for ( auto *e : editors ) {
1170+ if (!e-> isSavedToDisk ()) unsaved. append (e);
1171+ }
11721172
1173- // Ask the user what to do
1174- QString message = tr (" Save file <b>%1</b>?" ).arg (editor->getName ());
1175- auto reply = QMessageBox::question (this , tr (" Save File" ), message, QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
1173+ if (unsaved.isEmpty ()) return true ;
11761174
1177- if (reply == QMessageBox::Cancel) {
1178- // Stop checking and let the caller know
1179- return false ;
1180- }
1181- else if (reply == QMessageBox::Save) {
1182- bool didFileGetSaved = saveFile (editor);
1175+ // Focus the user's attention on the first unsaved file
1176+ dockedEditor->switchToEditor (unsaved.first ());
11831177
1184- // The user might have canceled the save file dialog so just stop now
1185- if (didFileGetSaved == false ) {
1186- // Stop checking and let the caller know
1187- return false ;
1188- }
1189- }
1190- }
1191- }
1178+ // Single point of interaction
1179+ UserSaveAction action = promptForSave (unsaved);
11921180
1193- // Everything is fine
1194- return true ;
1181+ switch (action) {
1182+ case UserSaveAction::DiscardAll:
1183+ return true ;
1184+ case UserSaveAction::SaveAll:
1185+ return saveAllEditors (unsaved);
1186+ case UserSaveAction::Cancel:
1187+ default :
1188+ return false ;
1189+ }
11951190}
11961191
11971192void MainWindow::openFileDialog ()
@@ -1255,30 +1250,11 @@ void MainWindow::closeFile(ScintillaNext *editor)
12551250 return ;
12561251 }
12571252
1258- if (editor-> isSavedToDisk ( )) {
1259- editor-> close () ;
1253+ if (! checkEditorsBeforeClose ({editor} )) {
1254+ return ;
12601255 }
1261- else {
1262- // The user needs be asked what to do about this file, so switch to it
1263- dockedEditor->switchToEditor (editor);
12641256
1265- QString message = tr (" Save file <b>%1</b>?" ).arg (editor->getName ());
1266- auto reply = QMessageBox::question (this , tr (" Save File" ), message, QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
1267-
1268- if (reply == QMessageBox::Cancel) {
1269- return ;
1270- }
1271-
1272- if (reply == QMessageBox::Save) {
1273- bool didFileGetSaved = saveFile (editor);
1274-
1275- // The user might have canceled the save file dialog so just stop now
1276- if (didFileGetSaved == false )
1277- return ;
1278- }
1279-
1280- editor->close ();
1281- }
1257+ editor->close ();
12821258
12831259 // If the last document was closed, figure out what to do next
12841260 if (editorCount () == 0 ) {
@@ -1453,11 +1429,20 @@ bool MainWindow::saveCopyAs(const QString &fileName)
14531429 }
14541430}
14551431
1456- void MainWindow::saveAll ()
1432+ bool MainWindow::saveAll ()
14571433{
1458- for (ScintillaNext *editor : editors ()) {
1459- saveFile (editor);
1434+ return saveAllEditors (editors ());
1435+ }
1436+
1437+ bool MainWindow::saveAllEditors (const QVector<ScintillaNext *> &editors)
1438+ {
1439+ for (ScintillaNext *editor : editors) {
1440+ if (!saveFile (editor)){
1441+ return false ;
1442+ }
14601443 }
1444+
1445+ return true ;
14611446}
14621447
14631448void MainWindow::exportAsFormat (Converter *converter, const QString &filter)
@@ -1961,6 +1946,31 @@ void MainWindow::showEditorZoomLevelIndicator()
19611946 FadingIndicator::showText (currentEditor (), tr (" Zoom: %1%" ).arg (zoomLevel * 10 + 100 ));
19621947}
19631948
1949+ MainWindow::UserSaveAction MainWindow::promptForSave (const QVector<ScintillaNext *> &editors)
1950+ {
1951+ const int count = editors.count ();
1952+ QMessageBox msgBox (this );
1953+ msgBox.setWindowTitle (tr (" Save File" ));
1954+ msgBox.setIcon (QMessageBox::Question);
1955+
1956+ // Using pluralization for the main text
1957+ QString text = (count == 1 )
1958+ ? tr (" Save changes to <b>%1</b>?" ).arg (editors.first ()->getName ())
1959+ : tr (" There are %n files with unsaved changes. Save them?" , " " , count);
1960+ msgBox.setText (text);
1961+
1962+ auto *saveBtn = msgBox.addButton (count > 1 ? tr (" Save All" ) : tr (" Save" ), QMessageBox::AcceptRole);
1963+ auto *discardBtn = msgBox.addButton (count > 1 ? tr (" Discard All" ) : tr (" Discard" ), QMessageBox::DestructiveRole);
1964+ msgBox.addButton (QMessageBox::Cancel);
1965+
1966+ msgBox.setDefaultButton (saveBtn);
1967+ msgBox.exec ();
1968+
1969+ if (msgBox.clickedButton () == saveBtn) return UserSaveAction::SaveAll;
1970+ if (msgBox.clickedButton () == discardBtn) return UserSaveAction::DiscardAll;
1971+ return UserSaveAction::Cancel;
1972+ }
1973+
19641974void MainWindow::saveSettings () const
19651975{
19661976 qInfo (Q_FUNC_INFO);
0 commit comments