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