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