]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/open.cpp
* use an int to select extension filters
[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 Handle selection of more than one subtitles file
124     QStringList files = THEDP->showSimpleOpen( qtr("Open subtitles file"),
125                                                EXT_FILTER_SUBTITLE );
126     ui.subInput->setEditText( files.join(" ") );
127     updateMRL();
128 }
129
130 void FileOpenPanel::updateMRL()
131 {
132     QString mrl = ui.fileInput->currentText();
133
134     if( ui.subCheckBox->isChecked() ) {
135         mrl.append( " :sub-file=" + ui.subInput->currentText() );
136         mrl.append( " :subsdec-align=" + ui.alignSubComboBox->currentText() );
137         mrl.append( " :sub-rel-fontsize=" + ui.sizeSubComboBox->currentText() );
138     }
139     emit mrlUpdated( mrl );
140     emit methodChanged( "file-caching" );
141 }
142
143
144 /* Function called by Open Dialog when clicke on Play/Enqueue */
145 void FileOpenPanel::accept()
146 {
147     ui.fileInput->addItem(ui.fileInput->currentText());
148     if ( ui.fileInput->count() > 8 ) ui.fileInput->removeItem(0);
149 }
150
151
152 /* Function called by Open Dialog when clicked on cancel */
153 void FileOpenPanel::clear()
154 {
155     ui.fileInput->setEditText( "" );
156     ui.subInput->setEditText( "" );
157 }
158
159 bool FileOpenPanel::eventFilter(QObject *object, QEvent *event)
160 {
161     if ( ( object == dialogBox ) && ( event->type() == QEvent::Hide ) )
162     {
163          event->ignore();
164          return true;
165     }
166     // standard event processing
167     else
168         return QObject::eventFilter(object, event);
169 }
170
171 void FileOpenPanel::toggleSubtitleFrame()
172 {
173     if (ui.subFrame->isVisible())
174     {
175         ui.subFrame->hide();
176 //        setMinimumHeight(1);
177         resize( sizeHint());
178     }
179     else
180     {
181         ui.subFrame->show();
182     }
183
184     /* Update the MRL */
185     updateMRL();
186 }
187
188 /**************************************************************************
189  * Disk open
190  **************************************************************************/
191 DiskOpenPanel::DiskOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
192                                 OpenPanel( _parent, _p_intf )
193 {
194     ui.setupUi( this );
195
196     CONNECT( ui.deviceCombo, editTextChanged(QString ), this, updateMRL());
197     BUTTONACT( ui.dvdRadioButton, updateMRL());
198     BUTTONACT( ui.vcdRadioButton, updateMRL());
199     BUTTONACT( ui.audioCDRadioButton, updateMRL());
200
201     CONNECT( ui.titleSpin, valueChanged(int), this, updateMRL());
202     CONNECT( ui.chapterSpin, valueChanged(int), this, updateMRL());
203 }
204
205 DiskOpenPanel::~DiskOpenPanel()
206 {}
207
208 void DiskOpenPanel::clear()
209 {
210     ui.titleSpin->setValue(0);
211     ui.chapterSpin->setValue(0);
212 }
213
214 void DiskOpenPanel::updateMRL()
215 {
216     QString mrl = "";
217     /* DVD */
218     if( ui.dvdRadioButton->isChecked() ) {
219         mrl = "dvd://" + ui.deviceCombo->currentText();
220         emit methodChanged( "dvdnav-caching" );
221
222         if ( ui.titleSpin->value() > 0 ) {
223             mrl += QString("@%1").arg(ui.titleSpin->value());
224             if ( ui.chapterSpin->value() > 0 ) {
225                 mrl+= QString(":%1").arg(ui.chapterSpin->value());
226             }
227         }
228
229     /* VCD */
230     } else if (ui.vcdRadioButton->isChecked() ) {
231         mrl = "vcd://" + ui.deviceCombo->currentText();
232         emit methodChanged( "vcd-caching" );
233
234         if( ui.titleSpin->value() > 0 ) {
235             mrl += QString("@%1").arg(ui.titleSpin->value());
236         }
237
238     /* CDDA */
239     } else {
240         mrl = "cdda://" + ui.deviceCombo->currentText();
241     }
242
243     emit mrlUpdated(mrl);
244 }
245
246
247
248 /**************************************************************************
249  * Net open
250  **************************************************************************/
251 NetOpenPanel::NetOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
252                                 OpenPanel( _parent, _p_intf )
253 {
254     ui.setupUi( this );
255
256     CONNECT( ui.protocolCombo, currentIndexChanged(int),
257              this, updateProtocol(int) );
258     CONNECT( ui.portSpin, valueChanged(int), this, updateMRL());
259     CONNECT( ui.addressText, textChanged(QString), this, updateAddress());
260     CONNECT( ui.timeShift, clicked(), this, updateMRL());
261     CONNECT( ui.ipv6, clicked(), this, updateMRL());
262
263     ui.protocolCombo->addItem("HTTP", QVariant("http"));
264     ui.protocolCombo->addItem("FTP", QVariant("ftp"));
265     ui.protocolCombo->addItem("MMS", QVariant("mms"));
266     ui.protocolCombo->addItem("RTSP", QVariant("rtsp"));
267     ui.protocolCombo->addItem("UDP/RTP (unicast)", QVariant("udp"));
268     ui.protocolCombo->addItem("UDP/RTP (multicast)", QVariant("udp"));
269 }
270
271 NetOpenPanel::~NetOpenPanel()
272 {}
273
274 void NetOpenPanel::clear()
275 {}
276
277 void NetOpenPanel::updateProtocol(int idx) {
278     QString addr = ui.addressText->text();
279     QString proto = ui.protocolCombo->itemData(idx).toString();
280
281     ui.timeShift->setEnabled( idx >= 4);
282     ui.ipv6->setEnabled( idx == 4 );
283     ui.addressText->setEnabled( idx != 4 );
284     ui.portSpin->setEnabled( idx >= 4 );
285
286     /* If we already have a protocol in the address, replace it */
287     if( addr.contains( "://")) {
288         msg_Err( p_intf, "replace");
289         addr.replace(QRegExp("^.*://"), proto + "://");
290         ui.addressText->setText(addr);
291     }
292
293     updateMRL();
294 }
295
296 void NetOpenPanel::updateAddress() {
297     updateMRL();
298 }
299
300 void NetOpenPanel::updateMRL() {
301     QString mrl = "";
302     QString addr = ui.addressText->text();
303     int proto = ui.protocolCombo->currentIndex();
304
305     if( addr.contains( "://") && proto != 4 ) {
306         mrl = addr;
307     } else {
308         switch(proto) {
309         case 0:
310             mrl = "http://" + addr;
311             emit methodChanged("http-caching");
312             break;
313         case 2:
314             mrl = "mms://" + addr;
315             emit methodChanged("mms-caching");
316             break;
317         case 1:
318             mrl = "ftp://" + addr;
319             emit methodChanged("ftp-caching");
320             break;
321         case 3: /* RTSP */
322             mrl = "rtsp://" + addr;
323             emit methodChanged("rtsp-caching");
324             break;
325         case 4:
326             mrl = "udp://@";
327             if( ui.ipv6->isEnabled() && ui.ipv6->isChecked() ) {
328                 mrl += "[::]";
329             }
330             mrl += QString(":%1").arg(ui.portSpin->value());
331             emit methodChanged("udp-caching");
332             break;
333         case 5: /* UDP multicast */
334             mrl = "udp://@";
335             /* Add [] to IPv6 */
336             if ( addr.contains(':') && !addr.contains('[') ) {
337                 mrl += "[" + addr + "]";
338             } else mrl += addr;
339             mrl += QString(":%1").arg(ui.portSpin->value());
340             emit methodChanged("udp-caching");
341         }
342     }
343     if( ui.timeShift->isEnabled() && ui.timeShift->isChecked() ) {
344         mrl += " :access-filter=timeshift";
345     }
346     emit mrlUpdated(mrl);
347 }
348
349 /**************************************************************************
350  * Capture open
351  **************************************************************************/
352 CaptureOpenPanel::CaptureOpenPanel( QWidget *_parent, intf_thread_t *_p_intf ) :
353                                 OpenPanel( _parent, _p_intf )
354 {
355     ui.setupUi( this );
356 }
357
358 CaptureOpenPanel::~CaptureOpenPanel()
359 {}
360
361 void CaptureOpenPanel::clear()
362 {}
363
364 void CaptureOpenPanel::updateMRL()
365 {
366     QString mrl = "";
367     emit mrlUpdated(mrl);
368 }