]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/open.cpp
* qt open dialog: double click plays the selected file
[vlc] / modules / gui / qt4 / components / open.cpp
1 /*****************************************************************************
2  * open.cpp : Panels for the open dialogs
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25
26 #include "qt4.hpp"
27 #include "components/open.hpp"
28 #include "dialogs/open.hpp"
29 #include "dialogs_provider.hpp"
30 #include "util/customwidgets.hpp"
31
32 #include <QFileDialog>
33 #include <QDialogButtonBox>
34 #include <QLineEdit>
35
36 /**************************************************************************
37  * File open
38  **************************************************************************/
39 FileOpenPanel::FileOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
40                                 OpenPanel( _parent, _p_intf )
41 {
42     /* Classic UI Setup */
43     ui.setupUi( this );
44
45     /* Use a QFileDialog and customize it because we don't want to
46        rewrite it all. Be careful to your eyes cause there are a few hacks.
47        Be very careful and test correctly when you modify this. */
48
49     /* Set Filters for file selection */
50     QString fileTypes = "";
51     ADD_FILTER_MEDIA( fileTypes );
52     ADD_FILTER_VIDEO( fileTypes );
53     ADD_FILTER_AUDIO( fileTypes );
54     ADD_FILTER_PLAYLIST( fileTypes );
55     ADD_FILTER_ALL( fileTypes );
56     fileTypes.replace(QString(";*"), QString(" *"));
57
58     // Make this QFileDialog a child of tempWidget from the ui.
59     dialogBox = new FileOpenBox( ui.tempWidget, NULL,
60             qfu( p_intf->p_libvlc->psz_homedir ), fileTypes );
61     dialogBox->setFileMode( QFileDialog::ExistingFiles );
62     /* We don't want to see a grip in the middle of the window, do we? */
63     dialogBox->setSizeGripEnabled( false );
64     dialogBox->setToolTip( qtr( "Select one or multiple files, or a folder" ));
65
66     // Add it to the layout
67     ui.gridLayout->addWidget( dialogBox, 0, 0, 1, 3 );
68
69     // But hide the two OK/Cancel buttons. Enable them for debug.
70 #ifndef WIN32
71     findChild<QDialogButtonBox*>()->hide();
72 #endif
73
74     /* Ugly hacks to get the good Widget */
75     //This lineEdit is the normal line in the fileDialog.
76     lineFileEdit = findChildren<QLineEdit*>()[3];
77     lineFileEdit->hide();
78
79     /* Make a list of QLabel inside the QFileDialog to access the good ones */
80     QList<QLabel *> listLabel = findChildren<QLabel*>();
81
82     /* Hide the FileNames one. Enable it for debug */
83     listLabel[4]->hide();
84     /* Change the text that was uncool in the usual box */
85     listLabel[5]->setText( qtr( "Filter:" ) );
86
87     // Hide the subtitles control by default.
88     ui.subFrame->hide();
89
90     /* Build the subs size combo box */
91     module_config_t *p_item =
92         config_FindConfig( VLC_OBJECT(p_intf), "freetype-rel-fontsize" );
93     if( p_item )
94     {
95         for( int i_index = 0; i_index < p_item->i_list; i_index++ )
96         {
97             ui.sizeSubComboBox->addItem(
98                 qfu( p_item->ppsz_list_text[i_index] ),
99                 QVariant( p_item->pi_list[i_index] ) );
100             if( p_item->value.i == p_item->pi_list[i_index] )
101             {
102                 ui.sizeSubComboBox->setCurrentIndex( i_index );
103             }
104         }
105     }
106
107     /* Build the subs align combo box */
108     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-align" );
109     if( p_item )
110     {
111         for( int i_index = 0; i_index < p_item->i_list; i_index++ )
112         {
113             ui.alignSubComboBox->addItem(
114                 qfu( p_item->ppsz_list_text[i_index] ),
115                 QVariant( p_item->pi_list[i_index] ) );
116             if( p_item->value.i == p_item->pi_list[i_index] )
117             {
118                 ui.alignSubComboBox->setCurrentIndex( i_index );
119             }
120         }
121     }
122
123     BUTTONACT( ui.subBrowseButton, browseFileSub() );
124     BUTTONACT( ui.subCheckBox, toggleSubtitleFrame());
125
126     CONNECT( ui.fileInput, editTextChanged(QString ), this, updateMRL());
127     CONNECT( ui.subInput, editTextChanged(QString ), this, updateMRL());
128     CONNECT( ui.alignSubComboBox, currentIndexChanged(int), this, updateMRL());
129     CONNECT( ui.sizeSubComboBox, currentIndexChanged(int), this, updateMRL());
130     CONNECT( lineFileEdit, textChanged( QString ), this, browseFile());
131 }
132
133 FileOpenPanel::~FileOpenPanel()
134 {}
135
136 QStringList FileOpenPanel::browse(QString help)
137 {
138     return THEDP->showSimpleOpen( help );
139 }
140
141 void FileOpenPanel::browseFile()
142 {
143     QString fileString = "";
144     foreach( QString file, dialogBox->selectedFiles() ) {
145          fileString += "\"" + file + "\" ";
146     }
147     ui.fileInput->setEditText( fileString );
148     updateMRL();
149 }
150
151 void FileOpenPanel::browseFileSub()
152 {
153     // FIXME Handle selection of more than one subtitles file
154     QStringList files = THEDP->showSimpleOpen( qtr("Open subtitles file"),
155                             EXT_FILTER_SUBTITLE,
156                             dialogBox->directory().absolutePath() );
157     if( files.isEmpty() ) return;
158     ui.subInput->setEditText( files.join(" ") );
159     updateMRL();
160 }
161
162 void FileOpenPanel::updateMRL()
163 {
164     QString mrl = ui.fileInput->currentText();
165
166     if( ui.subCheckBox->isChecked() ) {
167         mrl.append( " :sub-file=" + ui.subInput->currentText() );
168         int align = ui.alignSubComboBox->itemData( ui.alignSubComboBox->currentIndex() ).toInt();
169         mrl.append( " :subsdec-align=" + QString().setNum( align ) );
170         int size = ui.sizeSubComboBox->itemData( ui.sizeSubComboBox->currentIndex() ).toInt();
171         mrl.append( " :freetype-rel-fontsize=" + QString().setNum( size ) );
172     }
173     emit mrlUpdated( mrl );
174     emit methodChanged( "file-caching" );
175 }
176
177
178 /* Function called by Open Dialog when clicke on Play/Enqueue */
179 void FileOpenPanel::accept()
180 {
181     ui.fileInput->addItem(ui.fileInput->currentText());
182     if ( ui.fileInput->count() > 8 ) ui.fileInput->removeItem(0);
183 }
184
185 void FileOpenBox::accept()
186 {
187     OpenDialog::getInstance( NULL, NULL )->play();
188 }
189
190 /* Function called by Open Dialog when clicked on cancel */
191 void FileOpenPanel::clear()
192 {
193     ui.fileInput->setEditText( "" );
194     ui.subInput->setEditText( "" );
195 }
196
197 void FileOpenPanel::toggleSubtitleFrame()
198 {
199     if (ui.subFrame->isVisible())
200     {
201         ui.subFrame->hide();
202 //        setMinimumHeight(1);
203         resize( sizeHint());
204     }
205     else
206     {
207         ui.subFrame->show();
208     }
209
210     /* Update the MRL */
211     updateMRL();
212 }
213
214 /**************************************************************************
215  * Disk open
216  **************************************************************************/
217 DiskOpenPanel::DiskOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
218                                 OpenPanel( _parent, _p_intf )
219 {
220     ui.setupUi( this );
221
222     CONNECT( ui.deviceCombo, editTextChanged(QString ), this, updateMRL());
223     BUTTONACT( ui.dvdRadioButton, updateMRL());
224     BUTTONACT( ui.vcdRadioButton, updateMRL());
225     BUTTONACT( ui.audioCDRadioButton, updateMRL());
226
227     CONNECT( ui.titleSpin, valueChanged(int), this, updateMRL());
228     CONNECT( ui.chapterSpin, valueChanged(int), this, updateMRL());
229 }
230
231 DiskOpenPanel::~DiskOpenPanel()
232 {}
233
234 void DiskOpenPanel::clear()
235 {
236     ui.titleSpin->setValue(0);
237     ui.chapterSpin->setValue(0);
238 }
239
240 void DiskOpenPanel::updateMRL()
241 {
242     QString mrl = "";
243     /* DVD */
244     if( ui.dvdRadioButton->isChecked() ) {
245         mrl = "dvd://" + ui.deviceCombo->currentText();
246         emit methodChanged( "dvdnav-caching" );
247
248         if ( ui.titleSpin->value() > 0 ) {
249             mrl += QString("@%1").arg(ui.titleSpin->value());
250             if ( ui.chapterSpin->value() > 0 ) {
251                 mrl+= QString(":%1").arg(ui.chapterSpin->value());
252             }
253         }
254
255     /* VCD */
256     } else if (ui.vcdRadioButton->isChecked() ) {
257         mrl = "vcd://" + ui.deviceCombo->currentText();
258         emit methodChanged( "vcd-caching" );
259
260         if( ui.titleSpin->value() > 0 ) {
261             mrl += QString("@%1").arg(ui.titleSpin->value());
262         }
263
264     /* CDDA */
265     } else {
266         mrl = "cdda://" + ui.deviceCombo->currentText();
267     }
268
269     emit mrlUpdated(mrl);
270 }
271
272
273
274 /**************************************************************************
275  * Net open
276  **************************************************************************/
277 NetOpenPanel::NetOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
278                                 OpenPanel( _parent, _p_intf )
279 {
280     ui.setupUi( this );
281
282     CONNECT( ui.protocolCombo, currentIndexChanged(int),
283              this, updateProtocol(int) );
284     CONNECT( ui.portSpin, valueChanged(int), this, updateMRL());
285     CONNECT( ui.addressText, textChanged(QString), this, updateAddress());
286     CONNECT( ui.timeShift, clicked(), this, updateMRL());
287     CONNECT( ui.ipv6, clicked(), this, updateMRL());
288
289     ui.protocolCombo->addItem("HTTP", QVariant("http"));
290     ui.protocolCombo->addItem("FTP", QVariant("ftp"));
291     ui.protocolCombo->addItem("MMS", QVariant("mms"));
292     ui.protocolCombo->addItem("RTSP", QVariant("rtsp"));
293     ui.protocolCombo->addItem("UDP/RTP (unicast)", QVariant("udp"));
294     ui.protocolCombo->addItem("UDP/RTP (multicast)", QVariant("udp"));
295 }
296
297 NetOpenPanel::~NetOpenPanel()
298 {}
299
300 void NetOpenPanel::clear()
301 {}
302
303 void NetOpenPanel::updateProtocol(int idx) {
304     QString addr = ui.addressText->text();
305     QString proto = ui.protocolCombo->itemData(idx).toString();
306
307     ui.timeShift->setEnabled( idx >= 4);
308     ui.ipv6->setEnabled( idx == 4 );
309     ui.addressText->setEnabled( idx != 4 );
310     ui.portSpin->setEnabled( idx >= 4 );
311
312     /* If we already have a protocol in the address, replace it */
313     if( addr.contains( "://")) {
314         msg_Err( p_intf, "replace");
315         addr.replace(QRegExp("^.*://"), proto + "://");
316         ui.addressText->setText(addr);
317     }
318
319     updateMRL();
320 }
321
322 void NetOpenPanel::updateAddress() {
323     updateMRL();
324 }
325
326 void NetOpenPanel::updateMRL() {
327     QString mrl = "";
328     QString addr = ui.addressText->text();
329     int proto = ui.protocolCombo->currentIndex();
330
331     if( addr.contains( "://") && proto != 4 ) {
332         mrl = addr;
333     } else {
334         switch(proto) {
335         case 0:
336             mrl = "http://" + addr;
337             emit methodChanged("http-caching");
338             break;
339         case 2:
340             mrl = "mms://" + addr;
341             emit methodChanged("mms-caching");
342             break;
343         case 1:
344             mrl = "ftp://" + addr;
345             emit methodChanged("ftp-caching");
346             break;
347         case 3: /* RTSP */
348             mrl = "rtsp://" + addr;
349             emit methodChanged("rtsp-caching");
350             break;
351         case 4:
352             mrl = "udp://@";
353             if( ui.ipv6->isEnabled() && ui.ipv6->isChecked() ) {
354                 mrl += "[::]";
355             }
356             mrl += QString(":%1").arg(ui.portSpin->value());
357             emit methodChanged("udp-caching");
358             break;
359         case 5: /* UDP multicast */
360             mrl = "udp://@";
361             /* Add [] to IPv6 */
362             if ( addr.contains(':') && !addr.contains('[') ) {
363                 mrl += "[" + addr + "]";
364             } else mrl += addr;
365             mrl += QString(":%1").arg(ui.portSpin->value());
366             emit methodChanged("udp-caching");
367         }
368     }
369     if( ui.timeShift->isEnabled() && ui.timeShift->isChecked() ) {
370         mrl += " :access-filter=timeshift";
371     }
372     emit mrlUpdated(mrl);
373 }
374
375 /**************************************************************************
376  * Capture open
377  **************************************************************************/
378 CaptureOpenPanel::CaptureOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
379                                 OpenPanel( _parent, _p_intf )
380 {
381     ui.setupUi( this );
382 }
383
384 CaptureOpenPanel::~CaptureOpenPanel()
385 {}
386
387 void CaptureOpenPanel::clear()
388 {}
389
390 void CaptureOpenPanel::updateMRL()
391 {
392     QString mrl = "";
393     emit mrlUpdated(mrl);
394 }