]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
[Qt] Remove QTimer wher we can.
[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( "C&lear" ) );
85     saveLogButton = new QPushButton( qtr( "&Save as..." ) );
86     saveLogButton->setToolTip( qtr( "Saves 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 #if 0
138     msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
139     int i_start;
140
141     vlc_mutex_lock( p_sub->p_lock );
142     int i_stop = *p_sub->pi_stop;
143     vlc_mutex_unlock( p_sub->p_lock );
144
145     if( p_sub->i_start != i_stop )
146     {
147         messages->textCursor().movePosition( QTextCursor::End );
148
149         for( i_start = p_sub->i_start;
150                 i_start != i_stop;
151                 i_start = (i_start+1) % VLC_MSG_QSIZE )
152         {
153             if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
154                 p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
155                 p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
156                     verbosityBox->value() >= 1 ||
157                 p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
158                     verbosityBox->value() >= 2 )
159             {
160                 messages->setFontItalic( true );
161                 messages->setTextColor( "darkBlue" );
162                 messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
163             }
164             else
165                 continue;
166
167             switch( p_sub->p_msg[i_start].i_type )
168             {
169                 case VLC_MSG_INFO:
170                     messages->setTextColor( "blue" );
171                     messages->insertPlainText( " info: " );
172                     break;
173                 case VLC_MSG_ERR:
174                     messages->setTextColor( "red" );
175                     messages->insertPlainText( " error: " );
176                     break;
177                 case VLC_MSG_WARN:
178                     messages->setTextColor( "green" );
179                     messages->insertPlainText( " warning: " );
180                     break;
181                 case VLC_MSG_DBG:
182                 default:
183                     messages->setTextColor( "grey" );
184                     messages->insertPlainText( " debug: " );
185                     break;
186             }
187
188             /* Add message Regular black Font */
189             messages->setFontItalic( false );
190             messages->setTextColor( "black" );
191             messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
192             messages->insertPlainText( "\n" );
193         }
194         messages->ensureCursorVisible();
195
196         vlc_mutex_lock( p_sub->p_lock );
197         p_sub->i_start = i_start;
198         vlc_mutex_unlock( p_sub->p_lock );
199     }
200 #endif
201 }
202
203 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
204                                 vlc_object_t *p_obj )
205 {
206     QTreeWidgetItem *item;
207
208     if( parentItem )
209         item = new QTreeWidgetItem( parentItem );
210     else
211         item = new QTreeWidgetItem( modulesTree );
212
213     if( p_obj->psz_object_name )
214         item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
215                        qfu( p_obj->psz_object_name ) + "\" (" +
216                        QString::number((uintptr_t)p_obj) + ")" );
217     else
218         item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
219                        QString::number((uintptr_t)p_obj) + ")" );
220
221     item->setExpanded( true );
222
223     vlc_list_t *l = vlc_list_children( p_obj );
224     for( int i=0; i < l->i_count; i++ )
225         buildTree( item, l->p_values[i].p_object );
226     vlc_list_release( l );
227 }
228
229 void MessagesDialog::clearOrUpdate()
230 {
231     if( mainTab->currentIndex() )
232         updateTree();
233     else
234         clear();
235 }
236
237 void MessagesDialog::updateTree()
238 {
239     modulesTree->clear();
240     buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
241 }
242
243 void MessagesDialog::clear()
244 {
245     messages->clear();
246 }
247
248 bool MessagesDialog::save()
249 {
250     QString saveLogFileName = QFileDialog::getSaveFileName(
251             this, qtr( "Save log file as..." ),
252             qfu( config_GetHomeDir() ),
253             qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
254
255     if( !saveLogFileName.isNull() )
256     {
257         QFile file( saveLogFileName );
258         if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
259             QMessageBox::warning( this, qtr( "Application" ),
260                     qtr( "Cannot write to file %1:\n%2." )
261                     .arg( saveLogFileName )
262                     .arg( file.errorString() ) );
263             return false;
264         }
265
266         QTextStream out( &file );
267         out << messages->toPlainText() << "\n";
268
269         return true;
270     }
271     return false;
272 }