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