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