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