diff options
Diffstat (limited to 'Swift/QtUI/QtChatView.cpp')
-rw-r--r-- | Swift/QtUI/QtChatView.cpp | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/Swift/QtUI/QtChatView.cpp b/Swift/QtUI/QtChatView.cpp index f1a33a1..d399c4f 100644 --- a/Swift/QtUI/QtChatView.cpp +++ b/Swift/QtUI/QtChatView.cpp @@ -86,13 +86,31 @@ void QtChatView::handleKeyPressEvent(QKeyEvent* event) { webView_->keyPressEvent(event); } -void QtChatView::addMessage(boost::shared_ptr<ChatSnippet> snippet) { +void QtChatView::addMessageBottom(boost::shared_ptr<ChatSnippet> snippet) { if (viewReady_) { addToDOM(snippet); } else { /* If this asserts, the previous queuing code was necessary and should be reinstated */ assert(false); } + + if (firstElement_.isNull()) { + firstElement_ = lastElement_; + } +} +void QtChatView::addMessageTop(boost::shared_ptr<ChatSnippet> snippet) { + // save scrollbar maximum value + if (!topMessageAdded_) { + scrollBarMaximum_ = webPage_->mainFrame()->scrollBarMaximum(Qt::Vertical); + } + topMessageAdded_ = true; + + QWebElement newElement = snippetToDOM(snippet); + firstElement_.prependOutside(newElement); + firstElement_ = newElement; + if (lastElement_.isNull()) { + lastElement_ = firstElement_; + } } QWebElement QtChatView::snippetToDOM(boost::shared_ptr<ChatSnippet> snippet) { @@ -241,6 +259,13 @@ void QtChatView::scrollToBottom() { } void QtChatView::handleFrameSizeChanged() { + if (topMessageAdded_) { + // adjust new scrollbar position + int newMaximum = webPage_->mainFrame()->scrollBarMaximum(Qt::Vertical); + webPage_->mainFrame()->setScrollBarValue(Qt::Vertical, newMaximum - scrollBarMaximum_); + topMessageAdded_ = false; + } + if (isAtBottom_) { scrollToBottom(); } @@ -283,6 +308,9 @@ void QtChatView::resizeFont(int fontSizeSteps) { void QtChatView::resetView() { lastElement_ = QWebElement(); + firstElement_ = lastElement_; + topMessageAdded_ = false; + scrollBarMaximum_ = 0; QString pageHTML = theme_->getTemplate(); pageHTML.replace("==bodyBackground==", "background-color:#e3e3e3"); pageHTML.replace(pageHTML.indexOf("%@"), 2, theme_->getBase()); @@ -386,8 +414,17 @@ void QtChatView::setMUCInvitationJoined(QString id) { } void QtChatView::handleScrollRequested(int, int dy, const QRect&) { - int pos = webPage_->mainFrame()->scrollBarValue(Qt::Vertical); - emit scrollRequested(pos - dy); + rememberScrolledToBottom(); + + int pos = webPage_->mainFrame()->scrollBarValue(Qt::Vertical) - dy; + emit scrollRequested(pos); + + if (pos == 0) { + emit scrollReachedTop(); + } + else if (pos == webPage_->mainFrame()->scrollBarMaximum(Qt::Vertical)) { + emit scrollReachedBottom(); + } } int QtChatView::getSnippetPositionByDate(const QDate& date) { |