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