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