root/branches/jackMidi/gui/src/AudioFileBrowser/AudioFileBrowser.cpp @ 822

Revision 822, 8.3 KB (checked in by gabriel@…, 4 years ago)

Merge rev 560:571 from trunk.

Line 
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 *
5 * http://www.hydrogen-music.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY, without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *
21 */
22
23
24#include "AudioFileBrowser.h"
25#include "../HydrogenApp.h"
26#include "InstrumentEditor/InstrumentEditor.h"
27#include "SampleWaveDisplay.h"
28#include "../widgets/Button.h"
29
30#include <hydrogen/data_path.h>
31#include <hydrogen/h2_exception.h>
32#include <hydrogen/Preferences.h>
33#include <hydrogen/sample.h>
34#include <hydrogen/audio_engine.h>
35
36#include <QTreeWidget>
37#include <QMessageBox>
38
39using namespace H2Core;
40using namespace std;
41
42AudioFileBrowser::AudioFileBrowser ( QWidget* pParent )
43                : QDialog ( pParent )
44                , Object ( "AudioFileBrowser" )
45{
46
47        setupUi ( this );
48        INFOLOG ( "INIT" );
49        setWindowTitle ( trUtf8 ( "Audio File Browser" ) );
50        setFixedSize ( width(), height() );
51        installEventFilter( this );
52
53
54        model = new QDirModel();
55        model->setFilter( QDir::AllDirs | QDir::AllEntries | QDir::NoDotAndDotDot );
56        model->setNameFilters( QStringList() << "*.wav" << "*.WAV" << "*.flac"<< "*.FLAC" << "*.aiff" << "*.AIFF"<< "*.au" << "*.AU" );
57        model->setSorting( QDir::DirsFirst |QDir::Name );
58        __index = model->index( QDir::currentPath() );
59       
60        m_pPlayBtn->setEnabled( false );
61        m_pStopBtn->setEnabled( false );
62        openBTN->setEnabled( false );
63
64        tree = new QTreeView( treeView );
65        tree->setModel( model );
66        tree->resize( 799, 310 );
67        tree->header()->resizeSection( 0, 405 );
68        tree->setAlternatingRowColors( true );
69        tree->setRootIndex( model->index( Preferences::getInstance()->__lastsampleDirectory ) );
70
71        pathLineEdit->setText( Preferences::getInstance()->__lastsampleDirectory );
72        m_psamplefilename = "";
73        m_pselectedFile << "false" << "false" << "";
74
75        sEmptySampleFilename = DataPath::get_data_path() + "/emptySample.wav";
76
77        m_pSampleWaveDisplay = new SampleWaveDisplay( waveformview );
78        m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename );
79        m_pSampleWaveDisplay->move( 3, 3 );
80
81        playSamplescheckBox->setChecked( Preferences::getInstance()->__playsamplesonclicking );
82
83        connect( tree, SIGNAL( clicked( const QModelIndex&) ), SLOT( clicked( const QModelIndex& ) ) );
84        connect( pathLineEdit, SIGNAL( returnPressed() ), SLOT( updateModelIndex() ) );
85       
86}
87
88
89
90AudioFileBrowser::~AudioFileBrowser()
91{
92        Sample *pNewSample = Sample::load( sEmptySampleFilename );
93        AudioEngine::get_instance()->get_sampler()->preview_sample( pNewSample, 100 );
94        INFOLOG ( "DESTROY" );
95}
96
97
98void AudioFileBrowser::updateModelIndex()
99{
100        QString toremove = "";
101        QString newpath = pathLineEdit->text();
102
103        if( QDir( newpath ).exists() ){
104                tree->setRootIndex( model->index( newpath ) );
105        }else
106        {
107                toremove = newpath.section( '/', -1 );
108//              QMessageBox::information ( this, "Hydrogen", newpath + toremove);
109                newpath.replace( toremove, "" );
110                tree->setRootIndex( model->index( newpath ) );
111        }
112
113}
114
115void AudioFileBrowser::clicked( const QModelIndex& index )
116{
117
118        QString path = model->filePath( index );
119        pathLineEdit->setText( path );
120        filelineedit->setText( path );
121        m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename );
122
123        updateModelIndex(); //with this you have a navigation like konqueror
124
125        if ( model->isDir( index ) ){
126                m_pPlayBtn->setEnabled( false );
127                return;
128        }
129
130        QString name = path.section( '/', -1 );
131       
132        QString path2 = path;
133        QString onlypath = path;
134        if ( name != "" ){
135                onlypath = path.replace( name, "" );
136        }
137               
138        name = name.left( '.' );
139
140        QString message = "Name: " + name;
141        filelineedit->setText( path2 );
142        pathLineEdit->setText( onlypath );
143
144
145        if      (
146                ( path2.endsWith( ".wav" ) ) ||
147                ( path2.endsWith( ".WAV" ) ) ||
148                ( path2.endsWith( ".au" ) ) ||
149                ( path2.endsWith( ".AU" ) ) ||
150                ( path2.endsWith( ".aiff" ) ) ||
151                ( path2.endsWith( ".AIFF" ) ) ||
152                ( path2.endsWith( ".flac" ) ) ||
153                ( path2.endsWith( ".FLAC" ) )
154                ) {
155
156
157                        Sample *pNewSample = Sample::load( path2 );
158                        if ( pNewSample ) {
159                                m_pNBytesLable->setText( trUtf8( "Size: %1 bytes" ).arg( pNewSample->get_size() / 2 ) );
160                                m_pSamplerateLable->setText( trUtf8( "Samplerate: %1" ).arg( pNewSample->get_sample_rate() ) );
161                                float sec = ( float )( pNewSample->get_n_frames() / (float)pNewSample->get_sample_rate() );
162                                QString qsec = "";
163                                qsec.sprintf( "%2.2f", sec );
164                                m_pLengthLable->setText( trUtf8( "Samplelength: " ) + qsec + trUtf8( " s" ) );
165                               
166                                delete pNewSample;
167                                m_psamplefilename = path2;
168
169                               
170                                if (playSamplescheckBox->isChecked()){
171                                        if ( sec <= 600.00){
172                                                on_m_pPlayBtn_clicked();
173                                        }else
174                                        {
175                                                QMessageBox::information ( this, "Hydrogen", trUtf8( "No clicking audio preview for samples longer than 10 minutes!" )  );
176                                        }
177                                }
178
179                                m_pSampleWaveDisplay->updateDisplay( path2 );
180                                m_pPlayBtn->setEnabled( true );
181                                openBTN->setEnabled( true );
182
183                        }
184               
185                        m_pNameLabel->setText( message );
186                }else{
187                        m_pNameLabel->setText( trUtf8( "Name:"));
188                        m_pNBytesLable->setText( trUtf8( "Size:" ) );
189                        m_pSamplerateLable->setText( trUtf8( "Samplerate:" ) );
190                        m_pLengthLable->setText( trUtf8( "Samplelength:" ) );
191                        m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename );
192                        m_pPlayBtn->setEnabled( false );
193                        m_pStopBtn->setEnabled( false );
194                        openBTN->setEnabled( false );
195                        m_psamplefilename = "";
196                }
197}
198
199
200
201void AudioFileBrowser::on_m_pPlayBtn_clicked()
202{
203
204        if( QFile( m_psamplefilename ).exists() == false )
205                return;
206        m_pStopBtn->setEnabled( true );
207        Sample *pNewSample = Sample::load( m_psamplefilename );
208        if ( pNewSample ){
209                int length = ( ( pNewSample->get_n_frames() / pNewSample->get_sample_rate() + 1) * 100 );
210                AudioEngine::get_instance()->get_sampler()->preview_sample( pNewSample, length );
211        }
212}
213
214
215void AudioFileBrowser::on_m_pStopBtn_clicked()
216{
217        Sample *pNewSample = Sample::load( sEmptySampleFilename );
218        AudioEngine::get_instance()->get_sampler()->preview_sample( pNewSample, 100 );
219        m_pStopBtn->setEnabled( false );
220}
221
222
223
224void AudioFileBrowser::on_cancelBTN_clicked()
225{
226        Preferences::getInstance()->__lastsampleDirectory = pathLineEdit->text();
227        m_pselectedFile << "false" << "false" << "";
228        reject();
229
230}
231
232
233
234void AudioFileBrowser::on_openBTN_clicked()
235{
236        if      (
237                ( ( filelineedit->text().endsWith( ".wav" ) ) ||
238                ( filelineedit->text().endsWith( ".WAV" ) ) ||
239                ( filelineedit->text().endsWith( ".au" ) ) ||
240                ( filelineedit->text().endsWith( ".AU" ) ) ||
241                ( filelineedit->text().endsWith( ".aiff" ) ) ||
242                ( filelineedit->text().endsWith( ".AIFF" ) ) ||
243                ( filelineedit->text().endsWith( ".flac" ) ) ||
244                ( filelineedit->text().endsWith( ".FLAC" ) ) ) &&
245                ( QFile( filelineedit->text() ).exists() == true )
246                ) {             
247                        m_pselectedFile[2] = filelineedit->text();
248                       
249                }else
250                {
251                        m_pselectedFile << "false" << "false" << "";
252                }
253        Preferences::getInstance()->__lastsampleDirectory = pathLineEdit->text();
254        accept();
255}
256
257
258
259void AudioFileBrowser::on_playSamplescheckBox_clicked()
260{
261        Preferences::getInstance()->__playsamplesonclicking = playSamplescheckBox->isChecked();
262}
263
264
265
266QStringList AudioFileBrowser::selectedFile()
267{
268        if ( useNameCheckBox->isChecked() ){
269                 m_pselectedFile[0] = "true";
270        }
271        if ( autoVelCheckBox->isChecked() ){
272                 m_pselectedFile[1] = "true";
273        }
274        return m_pselectedFile;
275}
276
277
278
279void AudioFileBrowser::on_m_pPathHometoolButton_clicked()
280{
281        pathLineEdit->setText( QDir::homePath() );
282        tree->setRootIndex( model->index( QDir::homePath() ) );
283}
284
285
286
287void AudioFileBrowser::on_m_pPathUptoolButton_clicked()
288{
289        QString toremove = "";
290        QString path = pathLineEdit->text();
291
292        if ( path.length() <=1 )
293                return;
294
295        if ( path.endsWith( '/') ) {
296                toremove = path.section( '/', -2 );
297        }else
298        {
299                toremove = path.section( '/', -1 );
300        }
301       
302        QString updir =  path.replace( toremove, "" );
303        pathLineEdit->setText( updir );
304        tree->setRootIndex( model->index( updir ) );
305        tree->collapse( model->index( updir  ) );
306}
Note: See TracBrowser for help on using the browser.