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