From 989db1dc9b0cb578934c2c9fd3448c9b76a83391 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Fri, 8 Dec 2017 19:26:51 +0100 Subject: [PATCH] Move GUI dependencies out of Song::exportProjectMidi Move all GUI dependencies out of Song::exportProjectMidi and move them into the MainWindow. Show the GUI in the new slot method MainWindow::onExportProjectMidi and delegate to Song::exportProjectMidi once the file name has been determined. The file name is given as a parameter to Song::exportProjectMidi which still contains the business logic of exporting the data. --- include/MainWindow.h | 3 +++ include/Song.h | 2 +- src/core/Song.cpp | 48 +++++++----------------------------------- src/gui/MainWindow.cpp | 43 +++++++++++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 43 deletions(-) diff --git a/include/MainWindow.h b/include/MainWindow.h index 7ba2ac630..bdabd6735 100644 --- a/include/MainWindow.h +++ b/include/MainWindow.h @@ -175,6 +175,9 @@ public slots: void autoSave(); +private slots: + void onExportProjectMidi(); + protected: virtual void closeEvent( QCloseEvent * _ce ); virtual void focusOutEvent( QFocusEvent * _fe ); diff --git a/include/Song.h b/include/Song.h index 5c08426ea..75963cbdd 100644 --- a/include/Song.h +++ b/include/Song.h @@ -298,6 +298,7 @@ public: return m_timeSigModel; } + void exportProjectMidi(QString const & exportFileName) const; public slots: void playSong(); @@ -311,7 +312,6 @@ public slots: void importProject(); void exportProject( bool multiExport = false ); void exportProjectTracks(); - void exportProjectMidi(); void startExport(); void stopExport(); diff --git a/src/core/Song.cpp b/src/core/Song.cpp index f2c1f1dea..67bca57ce 100644 --- a/src/core/Song.cpp +++ b/src/core/Song.cpp @@ -1388,54 +1388,22 @@ void Song::exportProject( bool multiExport ) } -void Song::exportProjectMidi() +void Song::exportProjectMidi(QString const & exportFileName) const { - FileDialog efd( gui->mainWindow() ); + // instantiate midi export plugin + TrackContainer::TrackList const & tracks = this->tracks(); + TrackContainer::TrackList const & tracks_BB = Engine::getBBTrackContainer()->tracks(); - efd.setFileMode( FileDialog::AnyFile ); - - QStringList types; - types << tr("MIDI File (*.mid)"); - efd.setNameFilters( types ); - QString base_filename; - if( !m_fileName.isEmpty() ) + ExportFilter *exf = dynamic_cast (Plugin::instantiate("midiexport", nullptr, nullptr)); + if (exf) { - efd.setDirectory( QFileInfo( m_fileName ).absolutePath() ); - base_filename = QFileInfo( m_fileName ).completeBaseName(); + exf->tryExport(tracks, tracks_BB, getTempo(), m_masterPitchModel.value(), exportFileName); } else { - efd.setDirectory( ConfigManager::inst()->userProjectsDir() ); - base_filename = tr( "untitled" ); + qDebug() << "failed to load midi export filter!"; } - efd.selectFile( base_filename + ".mid" ); - efd.setDefaultSuffix( "mid"); - efd.setWindowTitle( tr( "Select file for project-export..." ) ); - efd.setAcceptMode( FileDialog::AcceptSave ); - - - if( efd.exec() == QDialog::Accepted && !efd.selectedFiles().isEmpty() && !efd.selectedFiles()[0].isEmpty() ) - { - const QString suffix = ".mid"; - - QString export_filename = efd.selectedFiles()[0]; - if (!export_filename.endsWith(suffix)) export_filename += suffix; - - // NOTE start midi export - - // instantiate midi export plugin - TrackContainer::TrackList tracks; - TrackContainer::TrackList tracks_BB; - tracks = Engine::getSong()->tracks(); - tracks_BB = Engine::getBBTrackContainer()->tracks(); - ExportFilter *exf = dynamic_cast (Plugin::instantiate("midiexport", NULL, NULL)); - if (exf==NULL) { - qDebug() << "failed to load midi export filter!"; - return; - } - exf->tryExport(tracks, tracks_BB, getTempo(), m_masterPitchModel.value(), export_filename); - } } diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 18bd49144..4d1f08f47 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -322,8 +322,8 @@ void MainWindow::finalize() project_menu->addAction( embed::getIconPixmap( "midi_file" ), tr( "Export &MIDI..." ), - Engine::getSong(), - SLOT( exportProjectMidi() ), + this, + SLOT( onExportProjectMidi() ), Qt::CTRL + Qt::Key_M ); // Prevent dangling separator at end of menu per https://bugreports.qt.io/browse/QTBUG-40071 @@ -1573,3 +1573,42 @@ void MainWindow::autoSave() } } } + +void MainWindow::onExportProjectMidi() +{ + FileDialog efd( this ); + + efd.setFileMode( FileDialog::AnyFile ); + + QStringList types; + types << tr("MIDI File (*.mid)"); + efd.setNameFilters( types ); + QString base_filename; + QString const & projectFileName = Engine::getSong()->projectFileName(); + if( !projectFileName.isEmpty() ) + { + efd.setDirectory( QFileInfo( projectFileName ).absolutePath() ); + base_filename = QFileInfo( projectFileName ).completeBaseName(); + } + else + { + efd.setDirectory( ConfigManager::inst()->userProjectsDir() ); + base_filename = tr( "untitled" ); + } + efd.selectFile( base_filename + ".mid" ); + efd.setDefaultSuffix( "mid"); + efd.setWindowTitle( tr( "Select file for project-export..." ) ); + + efd.setAcceptMode( FileDialog::AcceptSave ); + + + if( efd.exec() == QDialog::Accepted && !efd.selectedFiles().isEmpty() && !efd.selectedFiles()[0].isEmpty() ) + { + const QString suffix = ".mid"; + + QString export_filename = efd.selectedFiles()[0]; + if (!export_filename.endsWith(suffix)) export_filename += suffix; + + Engine::getSong()->exportProjectMidi(export_filename); + } +}