]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
FIx annoying button behaviour (resize when tab changing).
[vlc] / modules / gui / qt4 / dialogs / messages.cpp
1 /*****************************************************************************
2  * Messages.cpp : Information about an item
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb (at) 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/messages.hpp"
28 #include "dialogs_provider.hpp"
29
30 #include <QSpacerItem>
31 #include <QSpinBox>
32 #include <QLabel>
33 #include <QTextEdit>
34 #include <QTextCursor>
35 #include <QFileDialog>
36 #include <QTextStream>
37 #include <QMessageBox>
38 #include <QTabWidget>
39 #include <QTreeWidget>
40 #include <QTreeWidgetItem>
41 #include <QHeaderView>
42
43 MessagesDialog *MessagesDialog::instance = NULL;
44
45 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
46                : QVLCFrame( _p_intf )
47 {
48     setWindowTitle( qtr( "Messages" ) );
49
50     /* General widgets */
51     QGridLayout *mainLayout = new QGridLayout( this );
52     mainTab = new QTabWidget( this );
53     mainTab->setTabPosition( QTabWidget::North );
54
55
56     /* Messages */
57     QWidget     *msgWidget = new QWidget;
58     QGridLayout *msgLayout = new QGridLayout( msgWidget );
59
60     messages = new QTextEdit();
61     messages->setReadOnly( true );
62     messages->setGeometry( 0, 0, 440, 600 );
63     messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
64
65     msgLayout->addWidget( messages, 0, 0, 1, 0 );
66     mainTab->addTab( msgWidget, qtr( "Messages" ) );
67     ON_TIMEOUT( updateLog() );
68
69
70     /* Modules tree */
71     QWidget     *treeWidget = new QWidget;
72     QGridLayout *treeLayout = new QGridLayout( treeWidget );
73
74     modulesTree = new QTreeWidget();
75     modulesTree->header()->hide();
76
77     treeLayout->addWidget( modulesTree, 0, 0, 1, 0 );
78     mainTab->addTab( treeWidget, qtr( "Modules tree" ) );
79
80
81     /* Buttons and general layout */
82     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
83     closeButton->setDefault( true );
84     clearUpdateButton = new QPushButton( qtr( "&Clear" ) );
85     saveLogButton = new QPushButton( qtr( "&Save as..." ) );
86     saveLogButton->setToolTip( qtr( "Save all the displayed logs to a file" ) );
87
88     verbosityBox = new QSpinBox();
89     verbosityBox->setRange( 0, 2 );
90     verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
91     verbosityBox->setWrapping( true );
92     verbosityBox->setMaximumWidth( 50 );
93
94     verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
95
96     mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
97     mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
98     mainLayout->addWidget( verbosityBox, 1, 1 );
99     mainLayout->setColumnStretch( 2, 10 );
100     mainLayout->addWidget( saveLogButton, 1, 3 );
101     mainLayout->addWidget( clearUpdateButton, 1, 4 );
102     mainLayout->addWidget( closeButton, 1, 5 );
103
104     BUTTONACT( closeButton, hide() );
105     BUTTONACT( clearUpdateButton, clearOrUpdate() );
106     BUTTONACT( saveLogButton, save() );
107     CONNECT( mainTab, currentChanged( int ),
108              this, updateTab( int ) );
109
110     /* General action */
111     readSettings( "Messages", QSize( 600, 450 ) );
112 }
113
114 void MessagesDialog::updateTab( int index )
115 {
116     /* Second tab : modules tree */
117     if( index == 1 )
118     {
119         verbosityLabel->hide();
120         verbosityBox->hide();
121         clearUpdateButton->setText( qtr( "&Update" ) );
122         saveLogButton->hide();
123         updateTree();
124     }
125     /* First tab : messages */
126     else
127     {
128         verbosityLabel->show();
129         verbosityBox->show();
130         clearUpdateButton->setText( qtr( "&Clear" ) );
131         saveLogButton->show();
132     }
133 }
134
135 void MessagesDialog::updateLog()
136 {
137     msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
138     int i_start;
139
140     vlc_mutex_lock( p_sub->p_lock );
141     int i_stop = *p_sub->pi_stop;
142     vlc_mutex_unlock( p_sub->p_lock );
143
144     if( p_sub->i_start != i_stop )
145     {
146         messages->textCursor().movePosition( QTextCursor::End );
147
148         for( i_start = p_sub->i_start;
149                 i_start != i_stop;
150                 i_start = (i_start+1) % VLC_MSG_QSIZE )
151         {
152             if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
153                 p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
154                 p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
155                     verbosityBox->value() >= 1 ||
156                 p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
157                     verbosityBox->value() >= 2 )
158             {
159                 messages->setFontItalic( true );
160                 messages->setTextColor( "darkBlue" );
161                 messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
162             }
163             else
164                 continue;
165
166             switch( p_sub->p_msg[i_start].i_type )
167             {
168                 case VLC_MSG_INFO:
169                     messages->setTextColor( "blue" );
170                     messages->insertPlainText( " info: " );
171                     break;
172                 case VLC_MSG_ERR:
173                     messages->setTextColor( "red" );
174                     messages->insertPlainText( " error: " );
175                     break;
176                 case VLC_MSG_WARN:
177                     messages->setTextColor( "green" );
178                     messages->insertPlainText( " warning: " );
179                     break;
180                 case VLC_MSG_DBG:
181                 default:
182                     messages->setTextColor( "grey" );
183                     messages->insertPlainText( " debug: " );
184                     break;
185             }
186
187             /* Add message Regular black Font */
188             messages->setFontItalic( false );
189             messages->setTextColor( "black" );
190             messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
191             messages->insertPlainText( "\n" );
192         }
193         messages->ensureCursorVisible();
194
195         vlc_mutex_lock( p_sub->p_lock );
196         p_sub->i_start = i_start;
197         vlc_mutex_unlock( p_sub->p_lock );
198     }
199 }
200
201 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
202                                 vlc_object_t *p_obj )
203 {
204     QTreeWidgetItem *item;
205
206     if( parentItem )
207         item = new QTreeWidgetItem( parentItem );
208     else
209         item = new QTreeWidgetItem( modulesTree );
210
211     if( p_obj->psz_object_name )
212         item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
213                        qfu( p_obj->psz_object_name ) + "\" (" +
214                        QString::number(p_obj->i_object_id) + ")" );
215     else
216         item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
217                        QString::number(p_obj->i_object_id) + ")" );
218
219     item->setExpanded( true );
220
221     vlc_list_t *l = vlc_list_children( p_obj );
222     for( int i=0; i < l->i_count; i++ )
223         buildTree( item, l->p_values[i].p_object );
224     vlc_list_release( l );
225 }
226
227 void MessagesDialog::clearOrUpdate()
228 {
229     if( mainTab->currentIndex() )
230         updateTree();
231     else
232         clear();
233 }
234
235 void MessagesDialog::updateTree()
236 {
237     modulesTree->clear();
238     buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
239 }
240
241 void MessagesDialog::clear()
242 {
243     messages->clear();
244 }
245
246 bool MessagesDialog::save()
247 {
248     QString saveLogFileName = QFileDialog::getSaveFileName(
249             this, qtr( "Select a name for the logs file" ),
250             qfu( config_GetHomeDir() ),
251             qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
252
253     if( !saveLogFileName.isNull() )
254     {
255         QFile file( saveLogFileName );
256         if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
257             QMessageBox::warning( this, qtr( "Application" ),
258                     qtr( "Cannot write file %1:\n%2." )
259                     .arg( saveLogFileName )
260                     .arg( file.errorString() ) );
261             return false;
262         }
263
264         QTextStream out( &file );
265         out << messages->toPlainText() << "\n";
266
267         return true;
268     }
269     return false;
270 }