Separate versions of DoImportMIDI that do and don't assume a project

This commit is contained in:
Paul Licameli 2019-06-24 22:18:23 -04:00
parent ff2cf496cd
commit 8eca219d57
4 changed files with 35 additions and 22 deletions

View File

@ -106,6 +106,9 @@ public:
/// Namespace for functions for File menu
namespace FileActions {
// Import into existing project
bool DoImportMIDI( AudacityProject &project, const FilePath &fileName );
// Import file, creating a project if the first argument is null
AudacityProject *DoImportMIDI(
AudacityProject *pProject, const FilePath &fileName );
}

View File

@ -1303,7 +1303,7 @@ void ProjectFileManager::OpenFile(const FilePath &fileNameArg, bool addtohistory
{
#ifdef USE_MIDI
if (FileNames::IsMidi(fileName))
FileActions::DoImportMIDI( &project, fileName );
FileActions::DoImportMIDI( project, fileName );
else
#endif
Import( fileName );

View File

@ -326,7 +326,7 @@ public:
for (const auto &name : sortednames) {
#ifdef USE_MIDI
if (FileNames::IsMidi(name))
FileActions::DoImportMIDI(mProject, name);
FileActions::DoImportMIDI( *mProject, name);
else
#endif
ProjectFileManager::Get( *mProject ).Import(name);

View File

@ -112,36 +112,46 @@ namespace FileActions {
// exported helper functions
#ifdef USE_MIDI
// Given an existing project, try to import into it, return true on success
bool DoImportMIDI( AudacityProject &project, const FilePath &fileName )
{
auto &tracks = TrackList::Get( project );
auto newTrack = TrackFactory::Get( project ).NewNoteTrack();
if (::ImportMIDI(fileName, newTrack.get())) {
SelectUtilities::SelectNone( project );
auto pTrack = tracks.Add( newTrack );
pTrack->SetSelected(true);
ProjectHistory::Get( project )
.PushState(
wxString::Format(_("Imported MIDI from '%s'"),
fileName),
_("Import MIDI")
);
ProjectWindow::Get( project ).ZoomAfterImport(pTrack);
FileHistory::Global().AddFileToHistory(fileName);
return true;
}
else
return false;
}
// return null on failure; if success, return the given project, or a NEW
// one, if the given was null; create no NEW project if failure
AudacityProject *DoImportMIDI(
AudacityProject *pProject, const FilePath &fileName)
{
auto &tracks = TrackList::Get( *pProject );
AudacityProject *pNewProject {};
if ( !pProject )
pProject = pNewProject = ProjectManager::New();
auto cleanup = finally( [&]
{ if ( pNewProject ) GetProjectFrame( *pNewProject ).Close(true); } );
auto newTrack = TrackFactory::Get( *pProject ).NewNoteTrack();
if (::ImportMIDI(fileName, newTrack.get())) {
SelectUtilities::SelectNone( *pProject );
auto pTrack = tracks.Add( newTrack );
pTrack->SetSelected(true);
ProjectHistory::Get( *pProject )
.PushState(wxString::Format(_("Imported MIDI from '%s'"),
fileName), _("Import MIDI"));
ProjectWindow::Get( *pProject ).ZoomAfterImport(pTrack);
if ( DoImportMIDI( *pProject, fileName ) ) {
pNewProject = nullptr;
FileHistory::Global().AddFileToHistory(fileName);
return pProject;
}
else
@ -512,7 +522,7 @@ void OnImportMIDI(const CommandContext &context)
&window); // Parent
if (!fileName.empty())
DoImportMIDI(&project, fileName);
DoImportMIDI(project, fileName);
}
#endif