]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/interaction.cpp
Various strings change and capitalisation changes to match the guidelines.
[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 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include "dialogs/errors.hpp"
28 #include "dialogs/interaction.hpp"
29 #include "main_interface.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     int i_ret = -1;
46     panel = NULL;
47     dialog = NULL;
48
49     if( p_dialog->i_flags & DIALOG_BLOCKING_ERROR )
50     {
51         i_ret = QMessageBox::critical( NULL, qfu( p_dialog->psz_title ),
52                                        qfu( p_dialog->psz_description ),
53                                        QMessageBox::Ok, 0, 0 );
54     }
55     else if( p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
56     {
57         if( config_GetInt( p_intf, "qt-error-dialogs" ) != 0 )
58             ErrorsDialog::getInstance( p_intf )->addError(
59                  qfu( p_dialog->psz_title ), qfu( p_dialog->psz_description ) );
60         i_ret = 0;
61         //  QApplication::style()->standardPixmap(QStyle::SP_MessageBoxCritical)
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     }
123     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
124     {
125         dialog = new QWidget( 0 );layout = new QVBoxLayout( dialog );
126         layout->setMargin( 2 );
127         description = new QLabel( qfu( p_dialog->psz_description ) );
128         layout->addWidget( description );
129
130         inputEdit = new QLineEdit;
131         layout->addWidget( inputEdit );
132     }
133     else
134         msg_Err( p_intf, "unknown dialog type %i", p_dialog->i_flags );
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 Finish( DIALOG_CANCELLED );
142     }
143     else
144     /* Custom box, finish it */
145     {
146         QDialogButtonBox *buttonBox = new QDialogButtonBox;
147
148         if( p_dialog->psz_default_button )
149         {
150             defaultButton = new QPushButton;
151             defaultButton->setFocus();
152             defaultButton->setText( qfu( p_dialog->psz_default_button ) );
153             buttonBox->addButton( defaultButton, QDialogButtonBox::AcceptRole );
154         }
155         if( p_dialog->psz_alternate_button )
156         {
157             altButton = new QPushButton;
158             altButton->setText( qfu( p_dialog->psz_alternate_button ) );
159             buttonBox->addButton( altButton, QDialogButtonBox::RejectRole );
160         }
161         if( p_dialog->psz_other_button )
162         {
163             otherButton = new QPushButton;
164             otherButton->setText( qfu( p_dialog->psz_other_button ) );
165             buttonBox->addButton( otherButton, QDialogButtonBox::ActionRole );
166         }
167         layout->addWidget( buttonBox );
168         if( p_dialog->psz_default_button )
169             BUTTONACT( defaultButton, defaultB() );
170         if( p_dialog->psz_alternate_button )
171             BUTTONACT( altButton, altB() );
172         if( p_dialog->psz_other_button )
173             BUTTONACT( otherButton, otherB() );
174         dialog->setLayout( layout );
175         dialog->setWindowTitle( qfu( p_dialog->psz_title ) );
176     }
177 }
178
179 void InteractionDialog::update()
180 {
181     if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
182         p_dialog->i_flags & DIALOG_INTF_PROGRESS )
183     {
184         assert( progressBar );
185         progressBar->setValue( (int)( p_dialog->val.f_float * 10 ) );
186         description->setText( qfu( p_dialog->psz_description ) );
187     }
188
189     if( ( p_dialog->i_flags & DIALOG_INTF_PROGRESS ) &&
190         ( p_dialog->val.f_float >= 100.0 ) )
191         progressBar->hide();
192     if( ( p_dialog->i_flags & DIALOG_USER_PROGRESS ) &&
193         ( p_dialog->val.f_float >= 100.0 ) )
194         altButton->setText( qtr( "&Close" ) );
195 }
196
197 InteractionDialog::~InteractionDialog()
198 {
199 //    delete panel;
200     delete dialog;
201 }
202
203 void InteractionDialog::defaultB()
204 {
205     Finish( DIALOG_OK_YES );
206 }
207 void InteractionDialog::altB()
208 {
209     Finish( DIALOG_NO );
210 }
211 void InteractionDialog::otherB()
212 {
213     Finish( DIALOG_CANCELLED );
214 }
215
216 void InteractionDialog::Finish( int i_ret )
217 {
218     vlc_object_lock( p_dialog->p_interaction );
219
220     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
221     {
222         p_dialog->psz_returned[0] = strdup( qtu( loginEdit->text() ) );
223         p_dialog->psz_returned[1] = strdup( qtu( passwordEdit->text() ) );
224     }
225     else if( p_dialog->i_flags & DIALOG_PSZ_INPUT_OK_CANCEL )
226     {
227         p_dialog->psz_returned[0] = strdup( qtu( inputEdit->text() ) );
228     }
229     p_dialog->i_status = ANSWERED_DIALOG;
230     p_dialog->i_return = i_ret;
231
232     /* Alert the Dialog_*_Progress that the user had clicked on "cancel" */
233     if( p_dialog->i_flags & DIALOG_USER_PROGRESS ||
234         p_dialog->i_flags & DIALOG_INTF_PROGRESS )
235         p_dialog->b_cancelled = true;
236
237     hide();
238     vlc_object_unlock( p_dialog->p_interaction );
239     playlist_Signal( THEPL );
240 }
241