]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/messages.cpp
Qt: kill many warnings.
[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
29 #include <QSpinBox>
30 #include <QLabel>
31 #include <QTextEdit>
32 #include <QTextCursor>
33 #include <QFileDialog>
34 #include <QTextStream>
35 #include <QMessageBox>
36 #include <QTabWidget>
37 #include <QTreeWidget>
38 #include <QTreeWidgetItem>
39 #include <QHeaderView>
40 #include <QMutex>
41
42 #include <assert.h>
43
44 MessagesDialog *MessagesDialog::instance = NULL;
45
46 enum {
47     MsgEvent_Type = QEvent::User + MsgEventType + 1,
48 };
49
50 class MsgEvent : public QEvent
51 {
52 public:
53     MsgEvent( msg_item_t *msg )
54         : QEvent( (QEvent::Type)MsgEvent_Type ), msg(msg)
55     {
56         msg_Hold( msg );
57     }
58     virtual ~MsgEvent()
59     {
60         msg_Release( msg );
61     }
62
63     msg_item_t *msg;
64 };
65
66 struct msg_cb_data_t
67 {
68     MessagesDialog *self;
69 };
70 static void MsgCallback( msg_cb_data_t *, msg_item_t *, unsigned );
71
72 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
73                : QVLCFrame( _p_intf )
74 {
75     setWindowTitle( qtr( "Messages" ) );
76
77     /* General widgets */
78     QGridLayout *mainLayout = new QGridLayout( this );
79     mainTab = new QTabWidget( this );
80     mainTab->setTabPosition( QTabWidget::North );
81
82
83     /* Messages */
84     QWidget     *msgWidget = new QWidget;
85     QGridLayout *msgLayout = new QGridLayout( msgWidget );
86
87     messages = new QTextEdit();
88     messages->setReadOnly( true );
89     messages->setGeometry( 0, 0, 440, 600 );
90     messages->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
91
92     msgLayout->addWidget( messages, 0, 0, 1, 0 );
93     mainTab->addTab( msgWidget, qtr( "Messages" ) );
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     /* Buttons and general layout */
108     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
109     closeButton->setDefault( true );
110     clearUpdateButton = new QPushButton( qtr( "C&lear" ) );
111     saveLogButton = new QPushButton( qtr( "&Save as..." ) );
112     saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
113
114     verbosityBox = new QSpinBox();
115     verbosityBox->setRange( 0, 2 );
116     verbosityBox->setValue( config_GetInt( p_intf, "verbose" ) );
117     verbosityBox->setWrapping( true );
118     verbosityBox->setMaximumWidth( 50 );
119
120     verbosityLabel = new QLabel( qtr( "Verbosity Level" ) );
121
122     mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
123     mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
124     mainLayout->addWidget( verbosityBox, 1, 1 );
125     mainLayout->setColumnStretch( 2, 10 );
126     mainLayout->addWidget( saveLogButton, 1, 3 );
127     mainLayout->addWidget( clearUpdateButton, 1, 4 );
128     mainLayout->addWidget( closeButton, 1, 5 );
129
130     BUTTONACT( closeButton, hide() );
131     BUTTONACT( clearUpdateButton, clearOrUpdate() );
132     BUTTONACT( saveLogButton, save() );
133     CONNECT( mainTab, currentChanged( int ),
134              this, updateTab( int ) );
135
136     /* General action */
137     readSettings( "Messages", QSize( 600, 450 ) );
138
139
140     /* Hook up to LibVLC messaging */
141     cbData = new msg_cb_data_t;
142     cbData->self = this;
143     sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
144 }
145
146 MessagesDialog::~MessagesDialog()
147 {
148     writeSettings( "Messages" );
149     msg_Unsubscribe( sub );
150     delete cbData;
151 };
152
153 void MessagesDialog::updateTab( int index )
154 {
155     /* Second tab : modules tree */
156     if( index == 1 )
157     {
158         verbosityLabel->hide();
159         verbosityBox->hide();
160         clearUpdateButton->setText( qtr( "&Update" ) );
161         saveLogButton->hide();
162         updateTree();
163     }
164     /* First tab : messages */
165     else
166     {
167         verbosityLabel->show();
168         verbosityBox->show();
169         clearUpdateButton->setText( qtr( "&Clear" ) );
170         saveLogButton->show();
171     }
172 }
173
174 void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
175 {
176     if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
177      || (item->i_type == VLC_MSG_DBG && verbosityBox->value() < 2 ))
178         return;
179
180     // Saving cursor selection
181     int startPos = messages->textCursor().selectionStart();
182     int endPos = messages->textCursor().selectionEnd();
183
184     messages->moveCursor( QTextCursor::End );
185     if( startPos == endPos && messages->textCursor().selectionEnd() == endPos )
186         endPos = 0;
187     messages->setFontItalic( true );
188     messages->setTextColor( "darkBlue" );
189     messages->insertPlainText( qfu( item->psz_module ) );
190
191     switch (item->i_type)
192     {
193         case VLC_MSG_INFO:
194             messages->setTextColor( "blue" );
195             messages->insertPlainText( " info: " );
196             break;
197         case VLC_MSG_ERR:
198             messages->setTextColor( "red" );
199             messages->insertPlainText( " error: " );
200             break;
201         case VLC_MSG_WARN:
202             messages->setTextColor( "green" );
203             messages->insertPlainText( " warning: " );
204             break;
205         case VLC_MSG_DBG:
206         default:
207             messages->setTextColor( "grey" );
208             messages->insertPlainText( " debug: " );
209             break;
210     }
211
212     /* Add message Regular black Font */
213     messages->setFontItalic( false );
214     messages->setTextColor( "black" );
215     messages->insertPlainText( qfu(item->psz_msg) );
216     messages->insertPlainText( "\n" );
217     messages->ensureCursorVisible();
218
219     // Restoring saved cursor selection
220     // If the cursor was at this end, put it at the end,
221     // so we don't need to scroll down.
222     if( endPos == 0 )
223         messages->moveCursor( QTextCursor::End );
224     else
225     {
226         QTextCursor cur = messages->textCursor();
227         cur.movePosition( QTextCursor::Start );
228         cur.movePosition( QTextCursor::NextCharacter, QTextCursor::MoveAnchor, startPos );
229         cur.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endPos - startPos );
230         messages->setTextCursor( cur );
231     }
232 }
233 void MessagesDialog::customEvent( QEvent *event )
234 {
235     MsgEvent *msg = dynamic_cast<MsgEvent*>(event);
236
237     assert( msg );
238     sinkMessage( msg->msg, 0 );
239 }
240
241 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
242                                 vlc_object_t *p_obj )
243 {
244     QTreeWidgetItem *item;
245
246     if( parentItem )
247         item = new QTreeWidgetItem( parentItem );
248     else
249         item = new QTreeWidgetItem( modulesTree );
250
251     if( p_obj->psz_object_name )
252         item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
253                        qfu( p_obj->psz_object_name ) + "\" (" +
254                        QString::number((uintptr_t)p_obj) + ")" );
255     else
256         item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
257                        QString::number((uintptr_t)p_obj) + ")" );
258
259     item->setExpanded( true );
260
261     vlc_list_t *l = vlc_list_children( p_obj );
262     for( int i=0; i < l->i_count; i++ )
263         buildTree( item, l->p_values[i].p_object );
264     vlc_list_release( l );
265 }
266
267 void MessagesDialog::clearOrUpdate()
268 {
269     if( mainTab->currentIndex() )
270         updateTree();
271     else
272         clear();
273 }
274
275 void MessagesDialog::updateTree()
276 {
277     modulesTree->clear();
278     buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
279 }
280
281 void MessagesDialog::clear()
282 {
283     messages->clear();
284 }
285
286 bool MessagesDialog::save()
287 {
288     QString saveLogFileName = QFileDialog::getSaveFileName(
289             this, qtr( "Save log file as..." ),
290             qfu( config_GetHomeDir() ),
291             qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
292
293     if( !saveLogFileName.isNull() )
294     {
295         QFile file( saveLogFileName );
296         if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
297             QMessageBox::warning( this, qtr( "Application" ),
298                     qtr( "Cannot write to file %1:\n%2." )
299                     .arg( saveLogFileName )
300                     .arg( file.errorString() ) );
301             return false;
302         }
303
304         QTextStream out( &file );
305         out << messages->toPlainText() << "\n";
306
307         return true;
308     }
309     return false;
310 }
311
312 static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
313 {
314     int canc = vlc_savecancel();
315
316     QApplication::postEvent( data->self, new MsgEvent( item ) );
317
318     vlc_restorecancel( canc );
319 }
320