Skip to content

Commit

Permalink
Fix incorrect behaviour when closing a tab other than the current one.
Browse files Browse the repository at this point in the history
Fixes: #304
  • Loading branch information
cameronwhite committed Aug 18, 2020
1 parent 322e917 commit 4fee418
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 17 additions & 15 deletions source/app/powertabeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -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
{
Expand All @@ -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());
Expand Down Expand Up @@ -462,7 +462,7 @@ bool PowerTabEditor::saveFileAs()
path += extension;
}

return saveFile(path);
return saveFile(doc_index, path);
}
else
return false;
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 11 additions & 10 deletions source/app/powertabeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4fee418

Please sign in to comment.