]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/interaction.cpp
libass: upgrade libass contrib with the latest enhancements from Mplayer.
[vlc] / modules / gui / qt4 / dialogs / interaction.cpp
1 /*****************************************************************************
2  * interaction.cpp : Interaction stuff
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "dialogs/errors.hpp"
29 #include "dialogs/interaction.hpp"
30 #include "main_interface.hpp"
31
32 #include <QLabel>
33 #include <QLineEdit>
34 #include <QPushButton>
35 #include <QProgressBar>
36 #include <QMessageBox>
37 #include <QDialogButtonBox>
38
39 #include <assert.h>
40
41 InteractionDialog::InteractionDialog( intf_thread_t *_p_intf,
42                          interaction_dialog_t *_p_dialog ) : QObject( 0 ),
43                           p_intf( _p_intf), p_dialog( _p_dialog )
44 {
45     QVBoxLayout *layout = NULL;
46     int i_ret = -1;
47     panel = NULL;
48     dialog = NULL;
49
50     if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
51     {
52         i_ret = QMessageBox::critical( NULL, qfu( p_dialog->psz_title ),
53                                        qfu( p_dialog->psz_description ),
54                                        QMessageBox::Ok, 0, 0 );
55     }
56     else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
57     {
58         if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
59             ErrorsDialog::getInstance( p_intf )->addError(
60                  qfu( p_dialog->psz_title ), qfu( p_dialog->psz_description ) );
61         i_ret = 0;
62     }
63     else if( p_dialog->i_flags & DIALOG_WARNING )
64     {
65         if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
66             ErrorsDialog::getInstance( p_intf )->addWarning(
67                 qfu( p_dialog->psz_title ),qfu( p_dialog->psz_description ) );
68         i_ret = 0;
69     }
70     else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
71     {
72         p_dialog->i_status = SENT_DIALOG;
73         i_ret = QMessageBox::question( NULL,
74               qfu( p_dialog->psz_title), qfu( p_dialog->psz_description ),
75               p_dialog->psz_default_button ?
76                     qfu( p_dialog->psz_default_button ) : QString::null,
77               p_dialog->psz_alternate_button ?
78                     qfu( p_dialog->psz_alternate_button ) : QString::null,
79               p_dialog->psz_other_button ?
80                     qfu( p_dialog->psz_other_button ) : QString::null, 0,
81               p_dialog->psz_other_button ? 2 : -1 );
82     }
83     else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
84     {
85         dialog = new QWidget( 0 ); layout = new QVBoxLayout( dialog );
86         layout->setMargin( 2 );
87         panel = new QWidget( 0 );
88         QGridLayout *grid = new QGridLayout;
89
90         description = new QLabel( qfu( p_dialog->psz_description ) );
91         grid->addWidget( description, 0, 0, 1, 2 );
92
93         grid->addWidget( new QLabel( qtr( "Login") ), 1, 0 );
94         loginEdit = new QLineEdit;
95         grid->addWidget( loginEdit, 1, 1 );
96
97         grid->addWidget( new QLabel( qtr("Password") ), 2, 0);
98         passwordEdit = new QLineEdit;
99         passwordEdit->setEchoMode( QLineEdit::Password );
100         grid->addWidget( passwordEdit, 2, 1 );
101
102         panel->setLayout( grid );
103         layout->addWidget( panel );
104     }
105     else if( p_dialog->i_flags & DIALOG_USER_PROGRESS )
106     {
107         dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
108         layout->setMargin( 2 );
109         description = new QLabel( qfu( p_dialog->psz_description ) );
110         layout->addWidget( description );
111
112         progressBar = new QProgressBar;
113         progressBar->setMaximum( 1000 );
114         progressBar->setTextVisible( true );
115         progressBar->setOrientation( Qt::Horizontal );
116         layout->addWidget( progressBar );
117     }
118     else if( p_dialog->i_flags & DIALOG_INTF_PROGRESS )
119     {
120         progressBar = p_intf->p_sys->p_mi->pgBar;
121         progressBar->show();
122         i_ret = 2;
123     }
124     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
125     {
126         dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
127         layout->setMargin( 2 );
128         description = new QLabel( qfu( p_dialog->psz_description ) );
129         layout->addWidget( description );
130
131         inputEdit = new QLineEdit;
132         layout->addWidget( inputEdit );
133     }
134     else
135     {
136         msg_Err( p_intf, "Unknown dialog type %i", p_dialog->i_flags );
137         return;
138     }
139
140     /* We used a message box */
141     if( i_ret != -1 )
142     {
143         if( i_ret == 0 ) Finish( DIALOG_OK_YES );
144         else if ( i_ret == 1 ) Finish( DIALOG_NO );
145         else if ( i_ret == 2 ) return ;
146         else Finish( DIALOG_CANCELLED );
147     }
148     else
149     /* Custom box, finish it */
150     {
151         assert( dialog );
152         /* Start the DialogButtonBox config */
153         QDialogButtonBox *buttonBox = new QDialogButtonBox;
154
155         if( p_dialog->psz_default_button )
156         {
157             defaultButton = new QPushButton;
158             defaultButton->setFocus();
159             defaultButton->setText( qfu( p_dialog->psz_default_button ) );
160             buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
161         }
162         if( p_dialog->psz_alternate_button )
163         {
164             altButton = new QPushButton;
165             altButton->setText( qfu( p_dialog->psz_alternate_button ) );
166             buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
167         }
168         if( p_dialog->psz_other_button )
169         {
170             otherButton = new QPushButton;
171             otherButton->setText( qfu( p_dialog->psz_other_button ) );
172             buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
173         }
174         layout->addWidget( buttonBox );
175         /* End the DialogButtonBox */
176
177         /* CONNECTs */
178         if( p_dialog->psz_default_button )
179             BUTTONACT( defaultButton, defaultB() );
180         if( p_dialog->psz_alternate_button )
181             BUTTONACT( altButton, altB() );
182         if( p_dialog->psz_other_button )
183             BUTTONACT( otherButton, otherB() );
184
185         /* set the layouts and thte title */
186         dialog->setLayout( layout );
187         dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
188     }
189 }
190
191 void InteractionDialog::update()
192 {
193     if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
194         p_dialog->i_flags & DIALOG_INTF_PROGRESS )
195     {
196         assert( progressBar );
197         progressBar->setValue( (int)( p_dialog->val.f_float * 10 ) );
198         description->setText( qfu( p_dialog->psz_description ) );
199     }
200
201     if( ( p_dialog->i_flags & DIALOG_INTF_PROGRESS ) &&
202         ( p_dialog->val.f_float >= 100.0 ) )
203         progressBar->hide();
204     if( ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) &&
205         ( p_dialog->val.f_float >= 100.0 ) )
206         altButton->setText( qtr( "&Close" ) );
207 }
208
209 InteractionDialog::~InteractionDialog()
210 {
211 //    delete panel;
212     delete dialog;
213 }
214
215 void InteractionDialog::defaultB()
216 {
217     Finish( DIALOG_OK_YES );
218 }
219 void InteractionDialog::altB()
220 {
221     Finish( DIALOG_NO );
222 }
223 void InteractionDialog::otherB()
224 {
225     Finish( DIALOG_CANCELLED );
226 }
227
228 void InteractionDialog::Finish( int i_ret )
229 {
230     vlc_object_lock( p_dialog->p_interaction );
231
232     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
233     {
234         p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
235         p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
236     }
237     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
238     {
239         p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
240     }
241     p_dialog->i_status = ANSWERED_DIALOG;
242     p_dialog->i_return = i_ret;
243
244     /* Alert the Dialog_*_Progress that the user had clicked on "cancel" */
245     if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
246         p_dialog->i_flags & DIALOG_INTF_PROGRESS )
247         p_dialog->b_cancelled = true;
248
249     hide();
250     vlc_object_unlock( p_dialog->p_interaction );
251     playlist_Signal( THEPL );
252 }
253