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