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