Allow navigating backwards chat widget log while new lines are being added
authorunc0rr
Sun, 27 Jan 2013 21:46:25 +0400
changeset 8451 8c7da08df048
parent 8449 2816230a107e
child 8452 170afc3ac39f
Allow navigating backwards chat widget log while new lines are being added
QTfrontend/hwform.cpp
QTfrontend/ui/widget/chatwidget.cpp
QTfrontend/ui/widget/chatwidget.h
--- a/QTfrontend/hwform.cpp	Sun Jan 27 02:09:02 2013 +0100
+++ b/QTfrontend/hwform.cpp	Sun Jan 27 21:46:25 2013 +0400
@@ -1278,7 +1278,7 @@
     connect(hwnet, SIGNAL(chatStringLobby(const QString&)),
             ui.pageRoomsList->chatWidget, SLOT(onChatString(const QString&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(chatStringLobby(const QString&, const QString&)),
-            ui.pageRoomsList->chatWidget, SLOT(onChatString(const QString&, const QString&)));
+            ui.pageRoomsList->chatWidget, SLOT(onChatString(const QString&, const QString&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(chatStringFromMeLobby(const QString&)),
             ui.pageRoomsList->chatWidget, SLOT(onChatString(const QString&)), Qt::QueuedConnection);
 
--- a/QTfrontend/ui/widget/chatwidget.cpp	Sun Jan 27 02:09:02 2013 +0100
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Sun Jan 27 21:46:25 2013 +0400
@@ -31,6 +31,7 @@
 #include <QModelIndexList>
 #include <QSortFilterProxyModel>
 #include <QMenu>
+#include <QScrollBar>
 
 #include "DataManager.h"
 #include "hwconsts.h"
@@ -156,24 +157,18 @@
 void HWChatWidget::displayError(const QString & message)
 {
     addLine("msg_Error", " !!! " + message);
-    // scroll to the end
-    chatText->moveCursor(QTextCursor::End);
 }
 
 
 void HWChatWidget::displayNotice(const QString & message)
 {
     addLine("msg_Notice", " *** " + message);
-    // scroll to the end
-    chatText->moveCursor(QTextCursor::End);
 }
 
 
 void HWChatWidget::displayWarning(const QString & message)
 {
     addLine("msg_Warning", " *!* " + message);
-    // scroll to the end
-    chatText->moveCursor(QTextCursor::End);
 }
 
 
@@ -451,6 +446,8 @@
     if (s_displayNone->contains(cssClass))
         return; // the css forbids us to display this line
 
+    beforeContentAdd();
+
     if (chatStrings.size() > 250)
         chatStrings.removeFirst();
 
@@ -473,11 +470,13 @@
 
     chatText->setHtml("<html><body>"+chatStrings.join("<br>")+"</body></html>");
 
-    chatText->moveCursor(QTextCursor::End);
+    afterContentAdd();
 }
 
 void HWChatWidget::onServerMessage(const QString& str)
 {
+    beforeContentAdd();
+
     if (chatStrings.size() > 250)
         chatStrings.removeFirst();
 
@@ -485,7 +484,7 @@
 
     chatText->setHtml("<html><body>"+chatStrings.join("<br>")+"</body></html>");
 
-    chatText->moveCursor(QTextCursor::End);
+    afterContentAdd();
 }
 
 
@@ -901,3 +900,21 @@
 
     m_nicksMenu->popup(chatNicks->mapToGlobal(pos));
 }
+
+void HWChatWidget::beforeContentAdd()
+{
+    m_scrollBarPos = chatText->verticalScrollBar()->value();
+    m_scrollToBottom = m_scrollBarPos == chatText->verticalScrollBar()->maximum();
+}
+
+void HWChatWidget::afterContentAdd()
+{
+    if(m_scrollToBottom)
+    {
+        chatText->verticalScrollBar()->setValue(chatText->verticalScrollBar()->maximum());
+        chatText->moveCursor(QTextCursor::End);
+    } else
+    {
+        chatText->verticalScrollBar()->setValue(m_scrollBarPos);
+    }
+}
--- a/QTfrontend/ui/widget/chatwidget.h	Sun Jan 27 02:09:02 2013 +0100
+++ b/QTfrontend/ui/widget/chatwidget.h	Sun Jan 27 21:46:25 2013 +0400
@@ -83,6 +83,8 @@
         void discardStyleSheet();
         void saveStyleSheet();
         QString linkedNick(const QString & nickname);
+        void beforeContentAdd();
+        void afterContentAdd();
 
     public slots:
         void onChatString(const QString& str);
@@ -124,6 +126,8 @@
         QList<QRegExp> m_highlights; ///< regular expressions used for highlighting
         bool notify;
         bool m_autoKickEnabled;
+        bool m_scrollToBottom;
+        int m_scrollBarPos;
 
     private slots:
         void returnPressed();