root/branches/audiofilebrowser/gui/src/AudioFileBrowser/AudioFileBrowser.cpp @ 522

Revision 522, 7.6 KB (checked in by wolke, 5 years ago)

add namefilters for wav, flac, aiff, au files

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        openBTN->setEnabled( false );
62
63        tree = new QTreeView( treeView );
64        tree->setModel( model );
65        tree->resize( 799, 310 );
66        tree->header()->resizeSection( 0, 405 );
67        tree->setAlternatingRowColors( true );
68        tree->setRootIndex( model->index( Preferences::getInstance()->__lastsampleDirectory ) );
69
70        pathLineEdit->setText( Preferences::getInstance()->__lastsampleDirectory );
71        m_psamplefilename = "";
72        m_pselectedFile = "";
73
74        sEmptySampleFilename = DataPath::get_data_path() + "/emptySample.wav";
75
76        m_pSampleWaveDisplay = new SampleWaveDisplay( waveformview );
77        m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename );
78        m_pSampleWaveDisplay->move( 3, 3 );
79
80        playSamplescheckBox->setChecked( Preferences::getInstance()->__playsamplesonclicking );
81
82        connect( tree, SIGNAL( clicked( const QModelIndex&) ), SLOT( clicked( const QModelIndex& ) ) );
83        connect( pathLineEdit, SIGNAL( returnPressed() ), SLOT( updateModelIndex() ) );
84       
85}
86
87
88
89AudioFileBrowser::~AudioFileBrowser()
90{
91        INFOLOG ( "DESTROY" );
92}
93
94
95void AudioFileBrowser::updateModelIndex()
96{
97        QString toremove = "";
98        QString newpath = pathLineEdit->text();
99
100        if( QDir( newpath ).exists() ){
101                tree->setRootIndex( model->index( newpath ) );
102        }else
103        {
104                toremove = newpath.section( '/', -1 );
105//              QMessageBox::information ( this, "Hydrogen", newpath + toremove);
106                newpath.replace( toremove, "" );
107                tree->setRootIndex( model->index( newpath ) );
108        }
109
110}
111
112void AudioFileBrowser::clicked( const QModelIndex& index )
113{
114
115        QString path = model->filePath( index );
116        pathLineEdit->setText( path );
117        filelineedit->setText( path );
118        m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename );
119
120        updateModelIndex(); //with this you have a navigation like konqueror
121
122        if ( model->isDir( index ) )
123                 return;
124
125        QString name = path.section( '/', -1 );
126       
127        QString path2 = path;
128        QString onlypath = path;
129        if ( name != "" ){
130                onlypath = path.replace( name, "" );
131        }
132               
133        name = name.left( '.' );
134
135        QString message = "Name: " + name;
136        filelineedit->setText( path2 );
137        pathLineEdit->setText( onlypath );
138
139
140        if      (
141                ( path2.endsWith( ".wav" ) ) ||
142                ( path2.endsWith( ".WAV" ) ) ||
143                ( path2.endsWith( ".au" ) ) ||
144                ( path2.endsWith( ".AU" ) ) ||
145                ( path2.endsWith( ".aiff" ) ) ||
146                ( path2.endsWith( ".AIFF" ) ) ||
147                ( path2.endsWith( ".flac" ) ) ||
148                ( path2.endsWith( ".FLAC" ) )
149                ) {
150
151
152                        Sample *pNewSample = Sample::load( path2 );
153                        if ( pNewSample ) {
154                                m_pNBytesLable->setText( trUtf8( "Size: %1 bytes" ).arg( pNewSample->get_size() / 2 ) );
155                                m_pSamplerateLable->setText( trUtf8( "Samplerate: %1" ).arg( pNewSample->get_sample_rate() ) );
156                                float sec = ( float )( pNewSample->get_n_frames() / (float)pNewSample->get_sample_rate() );
157                                QString qsec = "";
158                                qsec.sprintf( "%2.2f", sec );
159                                m_pLengthLable->setText( trUtf8( "Samplelength: " ) + qsec + trUtf8( " s" ) );
160                               
161                                delete pNewSample;
162                                m_psamplefilename = path2;
163
164                               
165                                if (playSamplescheckBox->isChecked()){
166                                        if ( sec <= 600.00){
167                                                on_m_pPlayBtn_clicked();
168                                        }else
169                                        {
170                                                QMessageBox::information ( this, "Hydrogen", trUtf8( "No clicking audio preview for samples longer than 10 minutes!" )  );
171                                        }
172                                }
173
174                                m_pSampleWaveDisplay->updateDisplay( path2 );
175                                m_pPlayBtn->setEnabled( true );
176                                openBTN->setEnabled( true );
177
178                        }
179               
180                        m_pNameLabel->setText( message );
181                }else{
182                        m_pNameLabel->setText( trUtf8( "Name:"));
183                        m_pNBytesLable->setText( trUtf8( "Size:" ) );
184                        m_pSamplerateLable->setText( trUtf8( "Samplerate:" ) );
185                        m_pLengthLable->setText( trUtf8( "Samplelength:" ) );
186                        m_pSampleWaveDisplay->updateDisplay( sEmptySampleFilename );
187                        m_pPlayBtn->setEnabled( false );
188                        openBTN->setEnabled( false );
189                        m_psamplefilename = "";
190                }
191}
192
193
194
195void AudioFileBrowser::on_m_pPlayBtn_clicked()
196{
197
198        if( QFile( m_psamplefilename ).exists() == false )
199                return;
200        Sample *pNewSample = Sample::load( m_psamplefilename );
201        if ( pNewSample ){
202                int length = ( ( pNewSample->get_n_frames() / pNewSample->get_sample_rate() + 1) * 100 );
203                AudioEngine::get_instance()->get_sampler()->preview_sample( pNewSample, length );
204        }
205}
206
207
208
209
210void AudioFileBrowser::on_cancelBTN_clicked()
211{
212        Preferences::getInstance()->__lastsampleDirectory = pathLineEdit->text();
213        m_pselectedFile = "";
214        reject();
215}
216
217
218
219void AudioFileBrowser::on_openBTN_clicked()
220{
221        if      (
222                ( ( filelineedit->text().endsWith( ".wav" ) ) ||
223                ( filelineedit->text().endsWith( ".WAV" ) ) ||
224                ( filelineedit->text().endsWith( ".au" ) ) ||
225                ( filelineedit->text().endsWith( ".AU" ) ) ||
226                ( filelineedit->text().endsWith( ".aiff" ) ) ||
227                ( filelineedit->text().endsWith( ".AIFF" ) ) ||
228                ( filelineedit->text().endsWith( ".flac" ) ) ||
229                ( filelineedit->text().endsWith( ".FLAC" ) ) ) &&
230                ( QFile( filelineedit->text() ).exists() == true )
231                ) {             
232                        m_pselectedFile = filelineedit->text();
233                       
234                }else
235                {
236                        m_pselectedFile = "";
237                }
238        Preferences::getInstance()->__lastsampleDirectory = pathLineEdit->text();
239        accept();
240}
241
242
243
244void AudioFileBrowser::on_playSamplescheckBox_clicked()
245{
246        Preferences::getInstance()->__playsamplesonclicking = playSamplescheckBox->isChecked();
247}
248
249
250
251QString AudioFileBrowser::selectedFile()
252{
253        if ( useNameCheckBox->isChecked() ){
254                 m_pselectedFile.append( "___trUe" );
255        }
256        return m_pselectedFile;
257}
258
259
260
261void AudioFileBrowser::on_m_pPathHometoolButton_clicked()
262{
263        pathLineEdit->setText( QDir::homePath() );
264        tree->setRootIndex( model->index( QDir::homePath() ) );
265}
266
267
268
269void AudioFileBrowser::on_m_pPathUptoolButton_clicked()
270{
271        QString toremove = "";
272        QString path = pathLineEdit->text();
273
274        if ( path.length() <=1 )
275                return;
276
277        if ( path.endsWith( '/') ) {
278                toremove = path.section( '/', -2 );
279        }else
280        {
281                toremove = path.section( '/', -1 );
282        }
283       
284        QString updir =  path.replace( toremove, "" );
285        pathLineEdit->setText( updir );
286        tree->setRootIndex( model->index( updir ) );
287        tree->collapse( model->index( updir  ) );
288}
Note: See TracBrowser for help on using the browser.