From 4fee4181a82743456d052fe5ad72c6ebb61cd6f2 Mon Sep 17 00:00:00 2001 From: Cameron White Date: Tue, 18 Aug 2020 18:08:53 -0400 Subject: [PATCH] Fix incorrect behaviour when closing a tab other than the current one. Fixes: #304 --- CHANGELOG.md | 1 + source/app/powertabeditor.cpp | 32 +++++++++++++++++--------------- source/app/powertabeditor.h | 21 +++++++++++---------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc54809c8..dbbd543b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Thanks to the following contributors who worked on this release: ### Changed ### Fixed +- Closing a tab that has modifications and isn't the active tab now works correctly (#304). ## [Alpha 13] - 2020-08-11 diff --git a/source/app/powertabeditor.cpp b/source/app/powertabeditor.cpp index 10b0f7481..88245dfb9 100644 --- a/source/app/powertabeditor.cpp +++ b/source/app/powertabeditor.cpp @@ -327,7 +327,7 @@ void PowerTabEditor::switchTab(int index) bool PowerTabEditor::closeTab(int index) { // Prompt to save modified documents. - if (isWindowModified()) + if (!myUndoManager->stacks()[index]->isClean()) { QMessageBox msg(this); msg.setWindowTitle(tr("Close Document")); @@ -340,7 +340,7 @@ bool PowerTabEditor::closeTab(int index) const int ret = msg.exec(); if (ret == QMessageBox::Save) { - if (!saveFile()) + if (!saveFile(index)) return false; } else if (ret == QMessageBox::Cancel) @@ -371,18 +371,18 @@ bool PowerTabEditor::closeCurrentTab() return closeTab(myDocumentManager->getCurrentDocumentIndex()); } -bool PowerTabEditor::saveFile() +bool PowerTabEditor::saveFile(int doc_index) { - const Document &doc = myDocumentManager->getCurrentDocument(); + Document &doc = myDocumentManager->getDocument(doc_index); if (!doc.hasFilename()) - return saveFileAs(); + return saveFileAs(doc_index); const QString filename = Paths::toQString(doc.getFilename()); - return QFileInfo(filename).suffix() == "pt2" ? saveFile(filename) - : saveFileAs(); + return QFileInfo(filename).suffix() == "pt2" ? saveFile(doc_index, filename) + : saveFileAs(doc_index); } -bool PowerTabEditor::saveFile(QString path) +bool PowerTabEditor::saveFile(int doc_index, QString path) { QFileInfo info(path); QString extension = info.suffix(); @@ -398,7 +398,7 @@ bool PowerTabEditor::saveFile(QString path) } auto path_str = Paths::fromQString(path); - Document &doc = myDocumentManager->getCurrentDocument(); + Document &doc = myDocumentManager->getDocument(doc_index); try { @@ -420,21 +420,21 @@ bool PowerTabEditor::saveFile(QString path) // Update window title and tab bar. updateWindowTitle(); const QString filename = info.fileName(); - myTabWidget->setTabText(myTabWidget->currentIndex(), filename); - myTabWidget->setTabToolTip(myTabWidget->currentIndex(), filename); + myTabWidget->setTabText(doc_index, filename); + myTabWidget->setTabToolTip(doc_index, filename); // Add to the recent files list and update the last used directory. myRecentFiles->add(path); setPreviousDirectory(path); // Mark the file as being in an unmodified state. - myUndoManager->setClean(); + myUndoManager->stacks()[doc_index]->setClean(); } return true; } -bool PowerTabEditor::saveFileAs() +bool PowerTabEditor::saveFileAs(int doc_index) { const QString filter = QString::fromStdString(myFileFormatManager->exportFileFilter()); @@ -462,7 +462,7 @@ bool PowerTabEditor::saveFileAs() path += extension; } - return saveFile(path); + return saveFile(doc_index, path); } else return false; @@ -1898,7 +1898,9 @@ void PowerTabEditor::createCommands() mySaveCommand = new Command(tr("Save"), "File.Save", QKeySequence::Save, this); - connect(mySaveCommand, &QAction::triggered, this, [this]() { saveFile(); }); + connect(mySaveCommand, &QAction::triggered, this, [this]() { + saveFile(myDocumentManager->getCurrentDocumentIndex()); + }); mySaveAsCommand = new Command(tr("Save As..."), "File.SaveAs", QKeySequence::SaveAs, this); diff --git a/source/app/powertabeditor.h b/source/app/powertabeditor.h index 99b29f4c7..7ab2cfea4 100644 --- a/source/app/powertabeditor.h +++ b/source/app/powertabeditor.h @@ -75,14 +75,6 @@ private slots: /// @return True if the document was closed successfully. bool closeCurrentTab(); - /// Saves the current document. - /// @return True if the file was successfully saved. - bool saveFile(); - - /// Saves the current document to a new filename. - /// @return True if the file was successfully saved. - bool saveFileAs(); - /// Prints the current document. void printDocument(); @@ -372,9 +364,18 @@ private slots: /// open. void openFilesInteractive(); - /// Saves the current document to the specified path. + /// Saves the document, either to its current path or launching the Save As + /// dialog. + /// @return True if the file was successfully saved. + bool saveFile(int doc_index); + + /// Saves the document to the specified path. + /// @return True if the file was successfully saved. + bool saveFile(int doc_index, QString path); + + /// Saves the document to a new filename. /// @return True if the file was successfully saved. - bool saveFile(QString path); + bool saveFileAs(int doc_index); /// Adds or removes a rest at the current location. void editRest(Position::DurationType duration);