]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/open.cpp
* Use subtitles file extension filter for the "Open subtitles file" dialog.
[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_provider.hpp"
29 #include "util/customwidgets.hpp"
30
31 #include <QFileDialog>
32 #include <QDialogButtonBox>
33 #include <QLineEdit>
34
35 /**************************************************************************
36  * File open
37  **************************************************************************/
38 FileOpenPanel::FileOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
39                                 OpenPanel( _parent, _p_intf )
40 {
41     /* Classic UI Setup */
42     ui.setupUi( this );
43
44     /* Use a QFileDialog and customize it because we don't want to
45        rewrite it all. Be careful to your eyes cause there are a few hacks.
46        Be very careful and test correctly when you modify this. */
47
48     /* Set Filters for file selection */
49     QString fileTypes = "";
50     ADD_FILTER_MEDIA( fileTypes );
51     ADD_FILTER_VIDEO( fileTypes );
52     ADD_FILTER_AUDIO( fileTypes );
53     ADD_FILTER_PLAYLIST( fileTypes );
54     ADD_FILTER_ALL( fileTypes );
55     fileTypes.replace(QString(";*"), QString(" *"));
56
57     // Make this QFileDialog a child of tempWidget from the ui.
58     dialogBox = new QFileDialog( ui.tempWidget, NULL, 
59             qfu( p_intf->p_libvlc->psz_homedir ), fileTypes );
60     dialogBox->setFileMode( QFileDialog::ExistingFiles );
61     /* We don't want to see a grip in the middle of the window, do we? */
62     dialogBox->setSizeGripEnabled( false );
63     dialogBox->setToolTip( qtr( "Select one or multiple files, or a folder" ));
64
65     // Add it to the layout
66     ui.gridLayout->addWidget( dialogBox, 0, 0, 1, 3 );
67
68     // But hide the two OK/Cancel buttons. Enable them for debug.
69 #ifndef WIN32
70     findChild<QDialogButtonBox*>()->hide();
71 #endif
72
73     /* Ugly hacks to get the good Widget */
74     //This lineEdit is the normal line in the fileDialog.
75     lineFileEdit = findChildren<QLineEdit*>()[3];
76     lineFileEdit->hide();
77
78     /* Make a list of QLabel inside the QFileDialog to access the good ones */
79     QList<QLabel *> listLabel = findChildren<QLabel*>();
80
81     /* Hide the FileNames one. Enable it for debug */
82     listLabel[4]->hide();
83     /* Change the text that was uncool in the usual box */
84     listLabel[5]->setText( qtr( "Filter:" ) );
85
86     /* Hacks Continued Catch the close event */
87     dialogBox->installEventFilter( this );
88
89     // Hide the subtitles control by default.
90     ui.subFrame->hide();
91
92
93     BUTTONACT( ui.subBrowseButton, browseFileSub() );
94     BUTTONACT( ui.subCheckBox, toggleSubtitleFrame());
95
96     CONNECT( ui.fileInput, editTextChanged(QString ), this, updateMRL());
97     CONNECT( ui.subInput, editTextChanged(QString ), this, updateMRL());
98     CONNECT( ui.alignSubComboBox, currentIndexChanged(int), this, updateMRL());
99     CONNECT( ui.sizeSubComboBox, currentIndexChanged(int), this, updateMRL());
100     CONNECT( lineFileEdit, textChanged( QString ), this, browseFile());
101 }
102
103 FileOpenPanel::~FileOpenPanel()
104 {}
105
106 QStringList FileOpenPanel::browse(QString help)
107 {
108     return THEDP->showSimpleOpen( help );
109 }
110
111 void FileOpenPanel::browseFile()
112 {
113     QString fileString = "";
114     foreach( QString file, dialogBox->selectedFiles() ) {
115          fileString += "\"" + file + "\" ";
116     }
117     ui.fileInput->setEditText( fileString );
118     updateMRL();
119 }
120
121 void FileOpenPanel::browseFileSub()
122 {
123     // FIXME We shouldn't allow the user to select more than one subtitles file
124     QStringList files = THEDP->showSimpleOpen( qtr("Open subtitles file"),
125                                                false, false, false,
126                                                true, false );
127     ui.subInput->setEditText( files.join(" ") );
128     updateMRL();
129 }
130
131 void FileOpenPanel::updateMRL()
132 {
133     QString mrl = ui.fileInput->currentText();
134
135     if( ui.subCheckBox->isChecked() ) {
136         mrl.append( " :sub-file=" + ui.subInput->currentText() );
137         mrl.append( " :subsdec-align=" + ui.alignSubComboBox->currentText() );
138         mrl.append( " :sub-rel-fontsize=" + ui.sizeSubComboBox->currentText() );
139     }
140     emit mrlUpdated( mrl );
141     emit methodChanged( "file-caching" );
142 }
143
144
145 /* Function called by Open Dialog when clicke on Play/Enqueue */
146 void FileOpenPanel::accept()
147 {
148     ui.fileInput->addItem(ui.fileInput->currentText());
149     if ( ui.fileInput->count() > 8 ) ui.fileInput->removeItem(0);
150 }
151
152
153 /* Function called by Open Dialog when clicked on cancel */
154 void FileOpenPanel::clear()
155 {
156     ui.fileInput->setEditText( "" );
157     ui.subInput->setEditText( "" );
158 }
159
160 bool FileOpenPanel::eventFilter(QObject *object, QEvent *event)
161 {
162     if ( ( object == dialogBox ) && ( event->type() == QEvent::Hide ) )
163     {
164          event->ignore();
165          return true;
166     }
167     // standard event processing
168     else
169         return QObject::eventFilter(object, event);
170 }
171
172 void FileOpenPanel::toggleSubtitleFrame()
173 {
174     if (ui.subFrame->isVisible())
175     {
176         ui.subFrame->hide();
177 //        setMinimumHeight(1);
178         resize( sizeHint());
179     }
180     else
181     {
182         ui.subFrame->show();
183     }
184
185     /* Update the MRL */
186     updateMRL();
187 }
188
189 /**************************************************************************
190  * Disk open
191  **************************************************************************/
192 DiskOpenPanel::DiskOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
193                                 OpenPanel( _parent, _p_intf )
194 {
195     ui.setupUi( this );
196
197     CONNECT( ui.deviceCombo, editTextChanged(QString ), this, updateMRL());
198     BUTTONACT( ui.dvdRadioButton, updateMRL());
199     BUTTONACT( ui.vcdRadioButton, updateMRL());
200     BUTTONACT( ui.audioCDRadioButton, updateMRL());
201
202     CONNECT( ui.titleSpin, valueChanged(int), this, updateMRL());
203     CONNECT( ui.chapterSpin, valueChanged(int), this, updateMRL());
204 }
205
206 DiskOpenPanel::~DiskOpenPanel()
207 {}
208
209 void DiskOpenPanel::clear()
210 {
211     ui.titleSpin->setValue(0);
212     ui.chapterSpin->setValue(0);
213 }
214
215 void DiskOpenPanel::updateMRL()
216 {
217     QString mrl = "";
218     /* DVD */
219     if( ui.dvdRadioButton->isChecked() ) {
220         mrl = "dvd://" + ui.deviceCombo->currentText();
221         emit methodChanged( "dvdnav-caching" );
222
223         if ( ui.titleSpin->value() > 0 ) {
224             mrl += QString("@%1").arg(ui.titleSpin->value());
225             if ( ui.chapterSpin->value() > 0 ) {
226                 mrl+= QString(":%1").arg(ui.chapterSpin->value());
227             }
228         }
229
230     /* VCD */
231     } else if (ui.vcdRadioButton->isChecked() ) {
232         mrl = "vcd://" + ui.deviceCombo->currentText();
233         emit methodChanged( "vcd-caching" );
234
235         if( ui.titleSpin->value() > 0 ) {
236             mrl += QString("@%1").arg(ui.titleSpin->value());
237         }
238
239     /* CDDA */
240     } else {
241         mrl = "cdda://" + ui.deviceCombo->currentText();
242     }
243
244     emit mrlUpdated(mrl);
245 }
246
247
248
249 /**************************************************************************
250  * Net open
251  **************************************************************************/
252 NetOpenPanel::NetOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
253                                 OpenPanel( _parent, _p_intf )
254 {
255     ui.setupUi( this );
256
257     CONNECT( ui.protocolCombo, currentIndexChanged(int),
258              this, updateProtocol(int) );
259     CONNECT( ui.portSpin, valueChanged(int), this, updateMRL());
260     CONNECT( ui.addressText, textChanged(QString), this, updateAddress());
261     CONNECT( ui.timeShift, clicked(), this, updateMRL());
262     CONNECT( ui.ipv6, clicked(), this, updateMRL());
263
264     ui.protocolCombo->addItem("HTTP", QVariant("http"));
265     ui.protocolCombo->addItem("FTP", QVariant("ftp"));
266     ui.protocolCombo->addItem("MMS", QVariant("mms"));
267     ui.protocolCombo->addItem("RTSP", QVariant("rtsp"));
268     ui.protocolCombo->addItem("UDP/RTP (unicast)", QVariant("udp"));
269     ui.protocolCombo->addItem("UDP/RTP (multicast)", QVariant("udp"));
270 }
271
272 NetOpenPanel::~NetOpenPanel()
273 {}
274
275 void NetOpenPanel::clear()
276 {}
277
278 void NetOpenPanel::updateProtocol(int idx) {
279     QString addr = ui.addressText->text();
280     QString proto = ui.protocolCombo->itemData(idx).toString();
281
282     ui.timeShift->setEnabled( idx >= 4);
283     ui.ipv6->setEnabled( idx == 4 );
284     ui.addressText->setEnabled( idx != 4 );
285     ui.portSpin->setEnabled( idx >= 4 );
286
287     /* If we already have a protocol in the address, replace it */
288     if( addr.contains( "://")) {
289         msg_Err( p_intf, "replace");
290         addr.replace(QRegExp("^.*://"), proto + "://");
291         ui.addressText->setText(addr);
292     }
293
294     updateMRL();
295 }
296
297 void NetOpenPanel::updateAddress() {
298     updateMRL();
299 }
300
301 void NetOpenPanel::updateMRL() {
302     QString mrl = "";
303     QString addr = ui.addressText->text();
304     int proto = ui.protocolCombo->currentIndex();
305
306     if( addr.contains( "://") && proto != 4 ) {
307         mrl = addr;
308     } else {
309         switch(proto) {
310         case 0:
311             mrl = "http://" + addr;
312             emit methodChanged("http-caching");
313             break;
314         case 2:
315             mrl = "mms://" + addr;
316             emit methodChanged("mms-caching");
317             break;
318         case 1:
319             mrl = "ftp://" + addr;
320             emit methodChanged("ftp-caching");
321             break;
322         case 3: /* RTSP */
323             mrl = "rtsp://" + addr;
324             emit methodChanged("rtsp-caching");
325             break;
326         case 4:
327             mrl = "udp://@";
328             if( ui.ipv6->isEnabled() && ui.ipv6->isChecked() ) {
329                 mrl += "[::]";
330             }
331             mrl += QString(":%1").arg(ui.portSpin->value());
332             emit methodChanged("udp-caching");
333             break;
334         case 5: /* UDP multicast */
335             mrl = "udp://@";
336             /* Add [] to IPv6 */
337             if ( addr.contains(':') && !addr.contains('[') ) {
338                 mrl += "[" + addr + "]";
339             } else mrl += addr;
340             mrl += QString(":%1").arg(ui.portSpin->value());
341             emit methodChanged("udp-caching");
342         }
343     }
344     if( ui.timeShift->isEnabled() && ui.timeShift->isChecked() ) {
345         mrl += " :access-filter=timeshift";
346     }
347     emit mrlUpdated(mrl);
348 }
349
350 /**************************************************************************
351  * Capture open
352  **************************************************************************/
353 CaptureOpenPanel::CaptureOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
354                                 OpenPanel( _parent, _p_intf )
355 {
356     ui.setupUi( this );
357 }
358
359 CaptureOpenPanel::~CaptureOpenPanel()
360 {}
361
362 void CaptureOpenPanel::clear()
363 {}
364
365 void CaptureOpenPanel::updateMRL()
366 {
367     QString mrl = "";
368     emit mrlUpdated(mrl);
369 }