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