]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/interaction.cpp
Merge branch '0.9.0-libass' of git://git.videolan.org/vlc
[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     description = NULL;
47     int i_ret = -1;
48     panel = NULL;
49     dialog = NULL;
50     altButton = NULL;
51
52     if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
53     {
54         i_ret = QMessageBox::critical( NULL, qfu( p_dialog->psz_title ),
55                                        qfu( p_dialog->psz_description ),
56                                        QMessageBox::Ok, 0, 0 );
57     }
58     else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
59     {
60         if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
61             ErrorsDialog::getInstance( p_intf )->addError(
62                  qfu( p_dialog->psz_title ), qfu( p_dialog->psz_description ) );
63         i_ret = 0;
64     }
65     else if( p_dialog->i_flags & DIALOG_WARNING )
66     {
67         if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
68             ErrorsDialog::getInstance( p_intf )->addWarning(
69                 qfu( p_dialog->psz_title ),qfu( p_dialog->psz_description ) );
70         i_ret = 0;
71     }
72     else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
73     {
74         p_dialog->i_status = SENT_DIALOG;
75         i_ret = QMessageBox::question( NULL,
76               qfu( p_dialog->psz_title), qfu( p_dialog->psz_description ),
77               p_dialog->psz_default_button ?
78                     qfu( p_dialog->psz_default_button ) : QString::null,
79               p_dialog->psz_alternate_button ?
80                     qfu( p_dialog->psz_alternate_button ) : QString::null,
81               p_dialog->psz_other_button ?
82                     qfu( p_dialog->psz_other_button ) : QString::null, 0,
83               p_dialog->psz_other_button ? 2 : -1 );
84     }
85     else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
86     {
87         dialog = new QWidget( 0 ); layout = new QVBoxLayout( dialog );
88         layout->setMargin( 2 );
89         panel = new QWidget( 0 );
90         QGridLayout *grid = new QGridLayout;
91
92         description = new QLabel( qfu( p_dialog->psz_description ) );
93         grid->addWidget( description, 0, 0, 1, 2 );
94
95         grid->addWidget( new QLabel( qtr( "Login") ), 1, 0 );
96         loginEdit = new QLineEdit;
97         grid->addWidget( loginEdit, 1, 1 );
98
99         grid->addWidget( new QLabel( qtr("Password") ), 2, 0);
100         passwordEdit = new QLineEdit;
101         passwordEdit->setEchoMode( QLineEdit::Password );
102         grid->addWidget( passwordEdit, 2, 1 );
103
104         panel->setLayout( grid );
105         layout->addWidget( panel );
106     }
107     else if( (p_dialog->i_flags & DIALOG_INTF_PROGRESS ) ||
108              ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) )
109     {
110         dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
111         layout->setMargin( 2 );
112         description = new QLabel( qfu( p_dialog->psz_description ) );
113         layout->addWidget( description );
114
115         progressBar = new QProgressBar;
116         progressBar->setMaximum( 1000 );
117         progressBar->setTextVisible( true );
118         progressBar->setOrientation( Qt::Horizontal );
119         layout->addWidget( progressBar );
120     }
121     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
122     {
123         dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
124         layout->setMargin( 2 );
125         description = new QLabel( qfu( p_dialog->psz_description ) );
126         layout->addWidget( description );
127
128         inputEdit = new QLineEdit;
129         layout->addWidget( inputEdit );
130     }
131     else
132     {
133         msg_Err( p_intf, "Unknown dialog type %i", p_dialog->i_flags );
134         return;
135     }
136
137     /* We used a message box */
138     if( i_ret != -1 )
139     {
140         if( i_ret == 0 ) Finish( DIALOG_OK_YES );
141         else if ( i_ret == 1 ) Finish( DIALOG_NO );
142         else if ( i_ret == 2 ) return ;
143         else Finish( DIALOG_CANCELLED );
144     }
145     else
146     /* Custom box, finish it */
147     {
148         assert( dialog );
149         /* Start the DialogButtonBox config */
150         QDialogButtonBox *buttonBox = new QDialogButtonBox;
151
152         if( p_dialog->psz_default_button )
153         {
154             defaultButton = new QPushButton;
155             defaultButton->setFocus();
156             defaultButton->setText( qfu( p_dialog->psz_default_button ) );
157             buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
158         }
159         if( p_dialog->psz_alternate_button )
160         {
161             altButton = new QPushButton;
162             altButton->setText( qfu( p_dialog->psz_alternate_button ) );
163             buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
164         }
165         if( p_dialog->psz_other_button )
166         {
167             otherButton = new QPushButton;
168             otherButton->setText( qfu( p_dialog->psz_other_button ) );
169             buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
170         }
171         layout->addWidget( buttonBox );
172         /* End the DialogButtonBox */
173
174         /* CONNECTs */
175         if( p_dialog->psz_default_button )
176             BUTTONACT( defaultButton, defaultB() );
177         if( p_dialog->psz_alternate_button )
178             BUTTONACT( altButton, altB() );
179         if( p_dialog->psz_other_button )
180             BUTTONACT( otherButton, otherB() );
181
182         /* set the layouts and thte title */
183         dialog->setLayout( layout );
184         dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
185     }
186 }
187
188 void InteractionDialog::update()
189 {
190     if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
191         p_dialog->i_flags & DIALOG_INTF_PROGRESS )
192     {
193         assert( progressBar );
194         progressBar->setValue( (int)( p_dialog->val.f_float * 10 ) );
195         if( description )
196             description->setText( qfu( p_dialog->psz_description ) );
197     }
198     else return;
199
200     if( ( p_dialog->i_flags & DIALOG_INTF_PROGRESS ) &&
201         ( p_dialog->val.f_float >= 100.0 ) )
202     {
203         progressBar->hide();
204         msg_Dbg( p_intf, "Progress Done" );
205     }
206
207     if( ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) &&
208         ( p_dialog->val.f_float >= 100.0 ) )
209     {
210         assert( altButton );
211         altButton->setText( qtr( "&Close" ) );
212     }
213 }
214
215 InteractionDialog::~InteractionDialog()
216 {
217 //    delete panel;
218     delete dialog;
219 }
220
221 void InteractionDialog::defaultB()
222 {
223     Finish( DIALOG_OK_YES );
224 }
225 void InteractionDialog::altB()
226 {
227     Finish( DIALOG_NO );
228 }
229 void InteractionDialog::otherB()
230 {
231     Finish( DIALOG_CANCELLED );
232 }
233
234 void InteractionDialog::Finish( int i_ret )
235 {
236     vlc_object_lock( p_dialog->p_interaction );
237
238     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
239     {
240         p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
241         p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
242     }
243     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
244     {
245         p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
246     }
247     p_dialog->i_status = ANSWERED_DIALOG;
248     p_dialog->i_return = i_ret;
249
250     /* Alert the Dialog_*_Progress that the user had clicked on "cancel" */
251     if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
252         p_dialog->i_flags & DIALOG_INTF_PROGRESS )
253         p_dialog->b_cancelled = true;
254
255     hide();
256     vlc_object_unlock( p_dialog->p_interaction );
257     playlist_Signal( THEPL );
258 }
259