hydrogen 1.2.6
ExportMidiDialog.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#include <QLabel>
24
25#include "ExportMidiDialog.h"
26#include "CommonStrings.h"
27#include "HydrogenApp.h"
28#include "Widgets/FileDialog.h"
29
30#include <core/Basics/Song.h>
31#include <core/Hydrogen.h>
33#include <core/SMF/SMF.h>
34
35using namespace H2Core;
36
38
39// Here we are going to store export filename
41
43 : QDialog( parent )
44 , Object()
45 , m_pHydrogen( Hydrogen::get_instance() )
46 , m_pPreferences( Preferences::get_instance() )
47 , m_bFileSelected( false )
48 , m_sExtension( ".mid" )
49{
50 setupUi( this );
51 setModal( true );
52 setWindowTitle( tr( "Export midi" ) );
53
54 exportTypeCombo->addItem( tr("SMF1 single: export all instruments to a single track") );
55 exportTypeCombo->addItem( tr("SMF1 multi: export each instrument to separate track") );
56 exportTypeCombo->addItem( tr("SMF0: export all events to one track") );
57
59
60 adjustSize();
61}
62
66
68{
69 m_pPreferences->setMidiExportMode( exportTypeCombo->currentIndex() );
70
71 // extracting dirname from export box
72 QString sFilename = exportNameTxt->text();
73 QFileInfo info( sFilename );
74 QDir dir = info.absoluteDir();
75 if ( !dir.exists() ) {
76 // very strange if it happens but better to check for it anyway
77 return;
78 }
79
80 sLastFilename = info.fileName();
82}
83
85{
86 QString sDefaultFilename = m_pHydrogen->getSong()->getFilename();
87
88 if( sDefaultFilename.isEmpty() ){
89 sDefaultFilename = m_pHydrogen->getSong()->getName();
90 } else {
91 // extracting filename from full path
92 QFileInfo qDefaultFile( sDefaultFilename );
93 sDefaultFilename = qDefaultFile.fileName();
94 }
95
96 sDefaultFilename.replace( '*', "_" );
97 sDefaultFilename.replace( Filesystem::songs_ext, "" );
98 sDefaultFilename += m_sExtension;
99 return sDefaultFilename;
100}
101
103{
104 // loading previous directory and filling filename text field
105 // loading default filename on a first run
106 if( sLastFilename.isEmpty() ) {
108 }
109
110 QString sDirPath = m_pPreferences->getLastExportMidiDirectory();
111 QDir qd = QDir( sDirPath );
112
113 // joining filepath with dirname
114 QString sFullPath = qd.absoluteFilePath( sLastFilename );
115 exportNameTxt->setText( sFullPath );
116
117 // loading rest of the options
118 exportTypeCombo->setCurrentIndex( m_pPreferences->getMidiExportMode() );
119}
120
121
123{
125 if ( ! Filesystem::dir_writable( sPath, false ) ){
127 }
128
129 FileDialog fd( this );
130
131 fd.setFileMode( QFileDialog::AnyFile );
132 fd.setNameFilter( tr("Midi file (*%1)").arg( m_sExtension ) );
133 fd.setDirectory( sPath );
134 fd.setWindowTitle( tr( "Export MIDI file" ) );
135 fd.setAcceptMode( QFileDialog::AcceptSave );
136
137 QString sDefaultFilename = exportNameTxt->text();
138 fd.selectFile( sDefaultFilename );
139
140 QString sFilename;
141 if ( fd.exec() == QDialog::Accepted ) {
142 m_bFileSelected = true;
143 sFilename = fd.selectedFiles().first();
144 }
145
146 if ( sFilename.isEmpty() ) {
147 return;
148 }
149
150 if ( sFilename.endsWith( m_sExtension ) == false ) {
151 sFilename += m_sExtension;
152 }
153
154 exportNameTxt->setText( sFilename );
155}
156
158{
159 // check if directory exists otherwise error
160 QString filename = exportNameTxt->text();
161 QFileInfo file( filename );
162 QDir dir = file.dir();
163 if( !dir.exists() ) {
164 QMessageBox::warning(
165 this, "Hydrogen",
166 tr( "Directory %1 does not exist").arg( dir.absolutePath() ),
167 QMessageBox::Ok
168 );
169 return false;
170 }
171
172 return true;
173}
174
176{
177 if ( !validateUserInput() ) {
178 return;
179 }
180
181 auto pCommonStrings = HydrogenApp::get_instance()->getCommonStrings();
182
184
185 std::shared_ptr<Song> pSong = m_pHydrogen->getSong();
186
187 QString sFilename = exportNameTxt->text();
188 QFileInfo qFile( sFilename );
189
190 if ( ! Filesystem::dir_writable( qFile.absoluteDir().absolutePath(), false ) ) {
191 QMessageBox::warning( this, "Hydrogen",
192 pCommonStrings->getFileDialogMissingWritePermissions(),
193 QMessageBox::Ok );
194 return;
195 }
196
197 if ( qFile.exists() == true && m_bFileSelected == false ) {
198 int res = QMessageBox::information( this, "Hydrogen", tr( "The file %1 exists. \nOverwrite the existing file?").arg(sFilename), QMessageBox::Yes | QMessageBox::No );
199 if ( res == QMessageBox::No ) {
200 return;
201 }
202 }
203
204 // choosing writer
205 SMFWriter *pSmfWriter = nullptr;
206 if( exportTypeCombo->currentIndex() == EXPORT_SMF1_SINGLE ){
207 pSmfWriter = new SMF1WriterSingle();
208 } else if ( exportTypeCombo->currentIndex() == EXPORT_SMF1_MULTI ){
209 pSmfWriter = new SMF1WriterMulti();
210 } else if ( exportTypeCombo->currentIndex() == EXPORT_SMF0 ){
211 pSmfWriter = new SMF0Writer();
212 }
213
214 assert( pSmfWriter );
215
216 pSmfWriter->save( sFilename, pSong );
217
218 delete pSmfWriter;
219 accept();
220}
221
223{
224 accept();
225}
226
228{
229 QString filename = exportNameTxt->text();
230 if ( !filename.isEmpty() ) {
231 okBtn->setEnabled( true );
232 }
233 else {
234 okBtn->setEnabled( false );
235 }
236}
@ EXPORT_SMF0
@ EXPORT_SMF1_SINGLE
@ EXPORT_SMF1_MULTI
static QString sLastFilename
H2Core::Preferences * m_pPreferences
QString createDefaultFilename()
void on_exportNameTxt_textChanged(const QString &text)
void restoreSettingsFromPreferences()
ExportMidiDialog(QWidget *parent)
H2Core::Hydrogen * m_pHydrogen
Custom file dialog checking whether the user has write access to the selected folder before allowing ...
Definition FileDialog.h:34
static bool dir_writable(const QString &path, bool silent=false)
returns true if the given path is a writable regular directory
static QString usr_data_path()
returns user data path
static const QString songs_ext
Definition Filesystem.h:117
Hydrogen Audio Engine.
Definition Hydrogen.h:54
Manager for User Preferences File (singleton)
Definition Preferences.h:79
void setLastExportMidiDirectory(QString sPath)
static Preferences * get_instance()
Returns a pointer to the current Preferences singleton stored in __instance.
QString getLastExportMidiDirectory() const
void save(const QString &sFilename, std::shared_ptr< Song > pSong)
Definition SMF.cpp:224
static HydrogenApp * get_instance()
Returns the instance of HydrogenApp class.
std::shared_ptr< CommonStrings > getCommonStrings()