]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/open.cpp
Qt4 - Fix some sizing in open dialog.
[vlc] / modules / gui / qt4 / dialogs / open.cpp
1 /*****************************************************************************
2  * open.cpp : Advanced open dialog
3  *****************************************************************************
4  * Copyright © 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "input_manager.hpp"
28
29 #include "dialogs/open.hpp"
30
31 #include <QTabWidget>
32 #include <QGridLayout>
33 #include <QFileDialog>
34 #include <QRegExp>
35 #include <QMenu>
36
37 OpenDialog *OpenDialog::instance = NULL;
38
39 OpenDialog* OpenDialog::getInstance( QWidget *parent, intf_thread_t *p_intf,
40         int _action_flag, bool modal )
41 {
42     /* Creation */
43     if( !instance )
44         instance = new OpenDialog( parent, p_intf, modal, _action_flag );
45     else
46     {
47         /* Request the instance but change small details:
48            - Button menu
49            - Modality on top of the parent dialog */
50         instance->i_action_flag = _action_flag;
51         instance->setMenuAction();
52         if( modal ) instance->setWindowModality( Qt::WindowModal );
53     }
54     return instance;
55 }
56
57 OpenDialog::OpenDialog( QWidget *parent,
58                         intf_thread_t *_p_intf,
59                         bool modal,
60                         int _action_flag )  :  QVLCDialog( parent, _p_intf )
61 {
62     i_action_flag = _action_flag;
63
64     if( modal ) /* Select mode */
65     {
66         setWindowModality( Qt::WindowModal );
67         i_action_flag = SELECT;
68     }
69
70     /* Basic Creation of the Window */
71     ui.setupUi( this );
72     setWindowTitle( qtr( "Open" ) );
73     resize( 410, 300 );
74
75     /* Tab definition and creation */
76     fileOpenPanel    = new FileOpenPanel( ui.Tab, p_intf );
77     discOpenPanel    = new DiscOpenPanel( ui.Tab, p_intf );
78     netOpenPanel     = new NetOpenPanel( ui.Tab, p_intf );
79     captureOpenPanel = new CaptureOpenPanel( ui.Tab, p_intf );
80
81     /* Insert the tabs */
82     ui.Tab->insertTab( OPEN_FILE_TAB, fileOpenPanel, qtr( "&File" ) );
83     ui.Tab->insertTab( OPEN_DISC_TAB, discOpenPanel, qtr( "&Disc" ) );
84     ui.Tab->insertTab( OPEN_NETWORK_TAB, netOpenPanel, qtr( "&Network" ) );
85     ui.Tab->insertTab( OPEN_CAPTURE_TAB, captureOpenPanel,
86                        qtr( "Capture &Device" ) );
87
88     /* Hide the Slave input widgets */
89     ui.slaveLabel->hide();
90     ui.slaveText->hide();
91     ui.slaveBrowseButton->hide();
92
93     /* Buttons Creation */
94     QSizePolicy buttonSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
95     buttonSizePolicy.setHorizontalStretch( 0 );
96     buttonSizePolicy.setVerticalStretch( 0 );
97
98     /* Play Button */
99     playButton = new QToolButton;
100     playButton->setText( qtr( "&Play" ) );
101     playButton->setSizePolicy( buttonSizePolicy );
102     playButton->setMinimumSize( QSize( 90, 0 ) );
103     playButton->setPopupMode( QToolButton::MenuButtonPopup );
104     playButton->setToolButtonStyle( Qt::ToolButtonTextOnly );
105
106     /* Cancel Button */
107     cancelButton = new QToolButton;
108     cancelButton->setText( qtr( "&Cancel" ) );
109     cancelButton->setSizePolicy( buttonSizePolicy );
110
111     /* Select Button */
112     selectButton = new QToolButton;
113     selectButton->setText( qtr( "Select" ) );
114     selectButton->setSizePolicy( buttonSizePolicy );
115
116     /* Menu for the Play button */
117     QMenu * openButtonMenu = new QMenu( "Open" );
118     openButtonMenu->addAction( qtr( "&Enqueue" ), this, SLOT( enqueue() ),
119                                     QKeySequence( "Alt+E" ) );
120     openButtonMenu->addAction( qtr( "&Play" ), this, SLOT( play() ),
121                                     QKeySequence( "Alt+P" ) );
122     openButtonMenu->addAction( qtr( "&Stream" ), this, SLOT( stream() ) ,
123                                     QKeySequence( "Alt+S" ) );
124     openButtonMenu->addAction( qtr( "&Convert" ), this, SLOT( transcode() ) ,
125                                     QKeySequence( "Alt+C" ) );
126
127     playButton->setMenu( openButtonMenu );
128
129     /* Add the three Buttons */
130     ui.buttonsBox->addButton( playButton, QDialogButtonBox::ActionRole );
131     ui.buttonsBox->addButton( selectButton, QDialogButtonBox::AcceptRole );
132     ui.buttonsBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
133
134     /* At creation time, modify the default buttons */
135     setMenuAction();
136
137     /* Force MRL update on tab change */
138     CONNECT( ui.Tab, currentChanged( int ), this, signalCurrent() );
139
140     CONNECT( fileOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
141     CONNECT( netOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
142     CONNECT( discOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
143     CONNECT( captureOpenPanel, mrlUpdated( QString ), this, updateMRL( QString ) );
144
145     CONNECT( fileOpenPanel, methodChanged( QString ),
146              this, newCachingMethod( QString ) );
147     CONNECT( netOpenPanel, methodChanged( QString ),
148              this, newCachingMethod( QString ) );
149     CONNECT( discOpenPanel, methodChanged( QString ),
150              this, newCachingMethod( QString ) );
151     CONNECT( captureOpenPanel, methodChanged( QString ),
152              this, newCachingMethod( QString ) );
153
154     /* Advanced frame Connects */
155     CONNECT( ui.slaveText, textChanged( QString ), this, updateMRL() );
156     CONNECT( ui.cacheSpinBox, valueChanged( int ), this, updateMRL() );
157     CONNECT( ui.startTimeSpinBox, valueChanged( int ), this, updateMRL() );
158     BUTTONACT( ui.advancedCheckBox , toggleAdvancedPanel() );
159
160     /* Buttons action */
161     BUTTONACT( playButton, selectSlots() );
162     BUTTONACT( selectButton, close() );
163     BUTTONACT( cancelButton, cancel() );
164
165     /* Hide the advancedPanel */
166     if( !config_GetInt( p_intf, "qt-adv-options" ) )
167         ui.advancedFrame->hide();
168     else
169         ui.advancedCheckBox->setChecked( true );
170
171     /* Initialize caching */
172     storedMethod = "";
173     newCachingMethod( "file-caching" );
174 }
175
176 OpenDialog::~OpenDialog()
177 {}
178
179 /* Finish the dialog and decide if you open another one after */
180 void OpenDialog::setMenuAction()
181 {
182     if( i_action_flag == SELECT )
183     {
184         playButton->hide();
185         selectButton->show();
186     }
187     else
188     {
189         switch ( i_action_flag )
190         {
191         case OPEN_AND_STREAM:
192             playButton->setText( qtr( "&Stream" ) );
193             break;
194         case OPEN_AND_SAVE:
195             playButton->setText( qtr( "&Convert / Save" ) );
196             break;
197         case OPEN_AND_ENQUEUE:
198             playButton->setText( qtr( "&Enqueue" ) );
199             break;
200         case OPEN_AND_PLAY:
201         default:
202             playButton->setText( qtr( "&Play" ) );
203         }
204         playButton->show();
205         selectButton->hide();
206     }
207 }
208
209 void OpenDialog::showTab( int i_tab=0 )
210 {
211     ui.Tab->setCurrentIndex( i_tab );
212     show();
213 }
214
215 /* Function called on signal currentChanged triggered */
216 void OpenDialog::signalCurrent()
217 {
218     if( ui.Tab->currentWidget() != NULL )
219         ( dynamic_cast<OpenPanel *>( ui.Tab->currentWidget() ) )->updateMRL();
220 }
221
222 void OpenDialog::toggleAdvancedPanel()
223 {
224     if( ui.advancedFrame->isVisible() )
225     {
226         ui.advancedFrame->hide();
227         //FIXME: Clear Bug here. Qt ?
228         if( size().isValid() )
229             resize( size().width(), size().height() - ui.advancedFrame->height() );
230     }
231     else
232     {
233         ui.advancedFrame->show();
234     }
235 }
236
237 /***********
238  * Actions *
239  ***********/
240 /* If Cancel is pressed or escaped */
241 void OpenDialog::cancel()
242 {
243     /* Clear the panels */
244     for( int i = 0; i < OPEN_TAB_MAX; i++ )
245         dynamic_cast<OpenPanel*>( ui.Tab->widget( i ) )->clear();
246
247     /* Clear the variables */
248     mrl.clear();
249     mainMRL.clear();
250
251     /* If in Select Mode, reject instead of hiding */
252     if( windowModality() != Qt::NonModal ) reject();
253     else hide();
254 }
255
256 /* If EnterKey is pressed */
257 void OpenDialog::close()
258 {
259     if( windowModality() != Qt::NonModal )
260         accept();
261     else
262         selectSlots();
263 }
264
265 /* Play button */
266 void OpenDialog::selectSlots()
267 {
268     switch ( i_action_flag )
269     {
270     case OPEN_AND_STREAM:
271         stream();
272         break;
273     case OPEN_AND_SAVE:
274         transcode();
275         break;
276     case OPEN_AND_ENQUEUE:
277         enqueue();
278         break;
279     case OPEN_AND_PLAY:
280     default:
281         play();
282     }
283 }
284
285 void OpenDialog::play()
286 {
287     finish( false );
288 }
289
290 void OpenDialog::enqueue()
291 {
292     finish( true );
293 }
294
295
296 void OpenDialog::finish( bool b_enqueue = false )
297 {
298     toggleVisible();
299     mrl = ui.advancedLineInput->text();
300
301     if( windowModality() == Qt::NonModal )
302     {
303         QStringList tempMRL = SeparateEntries( mrl );
304         for( size_t i = 0; i < tempMRL.size(); i++ )
305         {
306             bool b_start = !i && !b_enqueue;
307             input_item_t *p_input;
308
309             p_input = input_ItemNew( p_intf, qtu( tempMRL[i] ), NULL );
310
311             /* Insert options */
312             while( i + 1 < tempMRL.size() && tempMRL[i + 1].startsWith( ":" ) )
313             {
314                 i++;
315                 input_ItemAddOption( p_input, qtu( tempMRL[i] ) );
316             }
317
318             /* Switch between enqueuing and starting the item */
319             if( b_start )
320             {
321                 playlist_AddInput( THEPL, p_input,
322                                    PLAYLIST_APPEND | PLAYLIST_GO,
323                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
324             }
325             else
326             {
327                 playlist_AddInput( THEPL, p_input,
328                                    PLAYLIST_APPEND | PLAYLIST_PREPARSE,
329                                    PLAYLIST_END, VLC_TRUE, VLC_FALSE );
330             }
331         }
332     }
333     else
334         accept();
335 }
336
337 void OpenDialog::transcode()
338 {
339     stream( true );
340 }
341
342 void OpenDialog::stream( bool b_transcode_only )
343 {
344     mrl = ui.advancedLineInput->text();
345     toggleVisible();
346     THEDP->streamingDialog( this, mrl, b_transcode_only );
347 }
348
349 /* Update the MRL */
350 void OpenDialog::updateMRL( QString tempMRL )
351 {
352     mainMRL = tempMRL;
353     updateMRL();
354 }
355
356 void OpenDialog::updateMRL() {
357     mrl = mainMRL;
358     if( ui.slaveCheckbox->isChecked() ) {
359         mrl += " :input-slave=" + ui.slaveText->text();
360     }
361     int i_cache = config_GetInt( p_intf, qta( storedMethod ) );
362     if( i_cache != ui.cacheSpinBox->value() ) {
363         mrl += QString( " :%1=%2" ).arg( storedMethod ).
364                                   arg( ui.cacheSpinBox->value() );
365     }
366     if( ui.startTimeSpinBox->value() ) {
367         mrl += " :start-time=" + QString( "%1" ).
368             arg( ui.startTimeSpinBox->value() );
369     }
370     ui.advancedLineInput->setText( mrl );
371 }
372
373 void OpenDialog::newCachingMethod( QString method )
374 {
375     if( method != storedMethod ) {
376         storedMethod = method;
377         int i_value = config_GetInt( p_intf, qta( storedMethod ) );
378         ui.cacheSpinBox->setValue( i_value );
379     }
380 }
381
382 QStringList OpenDialog::SeparateEntries( QString entries )
383 {
384     bool b_quotes_mode = false;
385
386     QStringList entries_array;
387     QString entry;
388
389     int index = 0;
390     while( index < entries.size() )
391     {
392         int delim_pos = entries.indexOf( QRegExp( "\\s+|\"" ), index );
393         if( delim_pos < 0 ) delim_pos = entries.size() - 1;
394         entry += entries.mid( index, delim_pos - index + 1 );
395         index = delim_pos + 1;
396
397         if( entry.isEmpty() ) continue;
398
399         if( !b_quotes_mode && entry.endsWith( "\"" ) )
400         {
401             /* Enters quotes mode */
402             entry.truncate( entry.size() - 1 );
403             b_quotes_mode = true;
404         }
405         else if( b_quotes_mode && entry.endsWith( "\"" ) )
406         {
407             /* Finished the quotes mode */
408             entry.truncate( entry.size() - 1 );
409             b_quotes_mode = false;
410         }
411         else if( !b_quotes_mode && !entry.endsWith( "\"" ) )
412         {
413             /* we found a non-quoted standalone string */
414             if( index < entries.size() ||
415                 entry.endsWith( " " ) || entry.endsWith( "\t" ) ||
416                 entry.endsWith( "\r" ) || entry.endsWith( "\n" ) )
417                 entry.truncate( entry.size() - 1 );
418             if( !entry.isEmpty() ) entries_array.append( entry );
419             entry.clear();
420         }
421         else
422         {;}
423     }
424
425     if( !entry.isEmpty() ) entries_array.append( entry );
426
427     return entries_array;
428 }