]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/dialogs/messages.cpp
Qt: Ok is not ok as a button label.
[vlc] / modules / gui / qt4 / dialogs / messages.cpp
index 6f9cfabdc0d8b03dcbdb67cd6b7235b0813cb429..2cd54e7bda14d4713bcf354bd74b415692b59fe2 100644 (file)
@@ -25,9 +25,7 @@
 #endif
 
 #include "dialogs/messages.hpp"
-#include "dialogs_provider.hpp"
 
-#include <QSpacerItem>
 #include <QSpinBox>
 #include <QLabel>
 #include <QTextEdit>
 #include <QTreeWidget>
 #include <QTreeWidgetItem>
 #include <QHeaderView>
+#include <QMutex>
+
+#include <assert.h>
 
 MessagesDialog *MessagesDialog::instance = NULL;
 
+enum {
+    MsgEvent_Type = QEvent::User + MsgEventType + 1,
+};
+
+class MsgEvent : public QEvent
+{
+public:
+    MsgEvent( msg_item_t *msg )
+        : QEvent( (QEvent::Type)MsgEvent_Type ), msg(msg)
+    {
+        msg_Hold( msg );
+    }
+    virtual ~MsgEvent()
+    {
+        msg_Release( msg );
+    }
+
+    msg_item_t *msg;
+};
+
+struct msg_cb_data_t
+{
+    MessagesDialog *self;
+};
+static void MsgCallback( msg_cb_data_t *, msg_item_t *, unsigned );
+
 MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
                : QVLCFrame( _p_intf )
 {
@@ -64,7 +91,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 */
@@ -109,8 +135,21 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
 
     /* General action */
     readSettings( "Messages", QSize( 600, 450 ) );
+
+
+    /* Hook up to LibVLC messaging */
+    cbData = new msg_cb_data_t;
+    cbData->self = this;
+    sub = msg_Subscribe( p_intf->p_libvlc, MsgCallback, cbData );
 }
 
+MessagesDialog::~MessagesDialog()
+{
+    writeSettings( "Messages" );
+    msg_Unsubscribe( sub );
+    delete cbData;
+};
+
 void MessagesDialog::updateTab( int index )
 {
     /* Second tab : modules tree */
@@ -132,71 +171,72 @@ void MessagesDialog::updateTab( int index )
     }
 }
 
-void MessagesDialog::updateLog()
+void MessagesDialog::sinkMessage (msg_item_t *item, unsigned)
 {
-    msg_subscription_t *p_sub = p_intf->p_sys->p_sub;
-    int i_start;
-
-    vlc_mutex_lock( p_sub->p_lock );
-    int i_stop = *p_sub->pi_stop;
-    vlc_mutex_unlock( p_sub->p_lock );
-
-    if( p_sub->i_start != i_stop )
+    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 );
+    if( startPos == endPos && messages->textCursor().selectionEnd() == endPos )
+        endPos = 0;
+    messages->setFontItalic( true );
+    messages->setTextColor( "darkBlue" );
+    messages->insertPlainText( qfu( item->psz_module ) );
+
+    switch (item->i_type)
     {
-        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();
+        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;
+    }
 
-        vlc_mutex_lock( p_sub->p_lock );
-        p_sub->i_start = i_start;
-        vlc_mutex_unlock( p_sub->p_lock );
+    /* 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
+    // If the cursor was at this end, put it at the end,
+    // so we don't need to scroll down.
+    if( endPos == 0 )
+        messages->moveCursor( QTextCursor::End );
+    else
+    {
+        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::customEvent( QEvent *event )
+{
+    MsgEvent *msg = dynamic_cast<MsgEvent*>(event);
+
+    assert( msg );
+    sinkMessage( msg->msg, 0 );
+}
 
 void MessagesDialog::buildTree( QTreeWidgetItem *parentItem,
                                 vlc_object_t *p_obj )
@@ -211,10 +251,10 @@ 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 );
 
@@ -268,3 +308,13 @@ bool MessagesDialog::save()
     }
     return false;
 }
+
+static void MsgCallback( msg_cb_data_t *data, msg_item_t *item, unsigned )
+{
+    int canc = vlc_savecancel();
+
+    QApplication::postEvent( data->self, new MsgEvent( item ) );
+
+    vlc_restorecancel( canc );
+}
+