]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/dialogs/messages.cpp
Fix a cursor selection bug in the Messages logs (see #2163)
[vlc] / modules / gui / qt4 / dialogs / messages.cpp
index 8d56eee6a7ee4bb4687a32bf6ae8c1c0537832c0..dd8d9fe69b3f44988f2d3654419c0accca5e872f 100644 (file)
 #include <QTreeWidget>
 #include <QTreeWidgetItem>
 #include <QHeaderView>
+#include <QMutex>
 
 MessagesDialog *MessagesDialog::instance = NULL;
 
+struct msg_cb_data_t
+{
+    MessagesDialog *self;
+    QMutex          lock; /**< protects MessagesDialog::messages */
+};
+
 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
                : QVLCFrame( _p_intf )
 {
@@ -64,7 +71,6 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
 
     msgLayout->addWidget( messages, 0, 0, 1, 0 );
     mainTab->addTab( msgWidget, qtr( "Messages" ) );
-    ON_TIMEOUT( updateLog() );
 
 
     /* Modules tree */
@@ -81,8 +87,9 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
     /* Buttons and general layout */
     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
     closeButton->setDefault( true );
-    clearUpdateButton = new QPushButton( qtr( "&Clear" ) );
+    clearUpdateButton = new QPushButton( qtr( "C&lear" ) );
     saveLogButton = new QPushButton( qtr( "&Save as..." ) );
+    saveLogButton->setToolTip( qtr( "Saves all the displayed logs to a file" ) );
 
     verbosityBox = new QSpinBox();
     verbosityBox->setRange( 0, 2 );
@@ -95,6 +102,7 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
     mainLayout->addWidget( mainTab, 0, 0, 1, 0 );
     mainLayout->addWidget( verbosityLabel, 1, 0, 1, 1 );
     mainLayout->addWidget( verbosityBox, 1, 1 );
+    mainLayout->setColumnStretch( 2, 10 );
     mainLayout->addWidget( saveLogButton, 1, 3 );
     mainLayout->addWidget( clearUpdateButton, 1, 4 );
     mainLayout->addWidget( closeButton, 1, 5 );
@@ -107,8 +115,21 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
 
     /* General action */
     readSettings( "Messages", QSize( 600, 450 ) );
+
+
+    /* Hook up to LibVLC messaging */
+    cb_data = new msg_cb_data_t;
+    cb_data->self = this;
+    sub = msg_Subscribe (_p_intf->p_libvlc, sinkMessage, cb_data);
 }
 
+MessagesDialog::~MessagesDialog ()
+{
+    writeSettings( "messages" );
+    msg_Unsubscribe (sub);
+    delete cb_data;
+};
+
 void MessagesDialog::updateTab( int index )
 {
     /* Second tab : modules tree */
@@ -130,70 +151,64 @@ void MessagesDialog::updateTab( int index )
     }
 }
 
-void MessagesDialog::updateLog()
+void MessagesDialog::sinkMessage (msg_cb_data_t *data, msg_item_t *item,
+                                  unsigned overruns)
 {
-    msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
-    int i_start;
+    MessagesDialog *self = data->self;
+    QMutexLocker locker (&data->lock);
 
-    vlc_mutex_lock( p_sub->p_lock );
-    int i_stop = *p_sub->pi_stop;
-    vlc_mutex_unlock( p_sub->p_lock );
+    self->sinkMessage (item, overruns);
+}
 
-    if( p_sub->i_start != i_stop )
-    {
-        messages->textCursor().movePosition( QTextCursor::End );
-
-        for( i_start = p_sub->i_start;
-                i_start != i_stop;
-                i_start = (i_start+1) % VLC_MSG_QSIZE )
-        {
-            if( p_sub->p_msg[i_start].i_type == VLC_MSG_INFO ||
-                p_sub->p_msg[i_start].i_type == VLC_MSG_ERR ||
-                p_sub->p_msg[i_start].i_type == VLC_MSG_WARN &&
-                    verbosityBox->value() >= 1 ||
-                p_sub->p_msg[i_start].i_type == VLC_MSG_DBG &&
-                    verbosityBox->value() >= 2 )
-            {
-                messages->setFontItalic( true );
-                messages->setTextColor( "darkBlue" );
-                messages->insertPlainText( qfu( p_sub->p_msg[i_start].psz_module ) );
-            }
-            else
-                continue;
-
-            switch( p_sub->p_msg[i_start].i_type )
-            {
-                case VLC_MSG_INFO:
-                    messages->setTextColor( "blue" );
-                    messages->insertPlainText( " info: " );
-                    break;
-                case VLC_MSG_ERR:
-                    messages->setTextColor( "red" );
-                    messages->insertPlainText( " error: " );
-                    break;
-                case VLC_MSG_WARN:
-                    messages->setTextColor( "green" );
-                    messages->insertPlainText( " warning: " );
-                    break;
-                case VLC_MSG_DBG:
-                default:
-                    messages->setTextColor( "grey" );
-                    messages->insertPlainText( " debug: " );
-                    break;
-            }
-
-            /* Add message Regular black Font */
-            messages->setFontItalic( false );
-            messages->setTextColor( "black" );
-            messages->insertPlainText( qfu(p_sub->p_msg[i_start].psz_msg) );
-            messages->insertPlainText( "\n" );
-        }
-        messages->ensureCursorVisible();
+void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
+{
+    if ((item->i_type == VLC_MSG_WARN && verbosityBox->value() < 1)
+     || (item->i_type == VLC_MSG_DBG && verbosityBox->value() < 2 ))
+        return;
+
+    // Saving cursor selection
+    int startPos = messages->textCursor().selectionStart();
+    int endPos = messages->textCursor().selectionEnd();
+
+    messages->moveCursor( QTextCursor::End );
+    messages->setFontItalic( true );
+    messages->setTextColor( "darkBlue" );
+    messages->insertPlainText( qfu( item->psz_module ) );
 
-        vlc_mutex_lock( p_sub->p_lock );
-        p_sub->i_start = i_start;
-        vlc_mutex_unlock( p_sub->p_lock );
+    switch (item->i_type)
+    {
+        case VLC_MSG_INFO:
+            messages->setTextColor( "blue" );
+            messages->insertPlainText( " info: " );
+            break;
+        case VLC_MSG_ERR:
+            messages->setTextColor( "red" );
+            messages->insertPlainText( " error: " );
+            break;
+        case VLC_MSG_WARN:
+            messages->setTextColor( "green" );
+            messages->insertPlainText( " warning: " );
+            break;
+        case VLC_MSG_DBG:
+        default:
+            messages->setTextColor( "grey" );
+            messages->insertPlainText( " debug: " );
+            break;
     }
+
+    /* Add message Regular black Font */
+    messages->setFontItalic( false );
+    messages->setTextColor( "black" );
+    messages->insertPlainText( qfu(item->psz_msg) );
+    messages->insertPlainText( "\n" );
+    messages->ensureCursorVisible();
+
+    // Restoring saved cursor selection
+    QTextCursor cur = messages->textCursor();
+    cur.movePosition( QTextCursor::Start );
+    cur.movePosition( QTextCursor::NextCharacter, QTextCursor::MoveAnchor, startPos );
+    cur.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, endPos - startPos );
+    messages->setTextCursor( cur );
 }
 
 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
@@ -209,18 +224,17 @@ void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
     if( p_obj->psz_object_name )
         item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" +
                        qfu( p_obj->psz_object_name ) + "\" (" +
-                       QString::number(p_obj->i_object_id) + ")" );
+                       QString::number((uintptr_t)p_obj) + ")" );
     else
         item->setText( 0, qfu( p_obj->psz_object_type ) + " (" +
-                       QString::number(p_obj->i_object_id) + ")" );
+                       QString::number((uintptr_t)p_obj) + ")" );
 
     item->setExpanded( true );
 
     vlc_list_t *l = vlc_list_children( p_obj );
-    vlc_object_release( p_obj );
-
     for( int i=0; i < l->i_count; i++ )
         buildTree( item, l->p_values[i].p_object );
+    vlc_list_release( l );
 }
 
 void MessagesDialog::clearOrUpdate()
@@ -234,20 +248,19 @@ void MessagesDialog::clearOrUpdate()
 void MessagesDialog::updateTree()
 {
     modulesTree->clear();
-
-    vlc_object_yield( p_intf->p_libvlc );
     buildTree( NULL, VLC_OBJECT( p_intf->p_libvlc ) );
 }
 
 void MessagesDialog::clear()
 {
+    QMutexLocker locker (&cb_data->lock);
     messages->clear();
 }
 
 bool MessagesDialog::save()
 {
     QString saveLogFileName = QFileDialog::getSaveFileName(
-            this, qtr( "Choose a filename to save the logs under..." ),
+            this, qtr( "Save log file as..." ),
             qfu( config_GetHomeDir() ),
             qtr( "Texts / Logs (*.log *.txt);; All (*.*) ") );
 
@@ -256,13 +269,14 @@ bool MessagesDialog::save()
         QFile file( saveLogFileName );
         if ( !file.open( QFile::WriteOnly | QFile::Text ) ) {
             QMessageBox::warning( this, qtr( "Application" ),
-                    qtr( "Cannot write file %1:\n%2." )
+                    qtr( "Cannot write to file %1:\n%2." )
                     .arg( saveLogFileName )
                     .arg( file.errorString() ) );
             return false;
         }
 
         QTextStream out( &file );
+        QMutexLocker locker (&cb_data->lock);
         out << messages->toPlainText() << "\n";
 
         return true;