File: ScrollKeeper.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (39 lines) | stat: -rw-r--r-- 1,069 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * ScrollKeeper.cpp
 *
 *  Created on: 30 Oct 2016
 *      Author: rkfg
 */

#include "ScrollKeeper.h"
#include <QScrollBar>

//#define SCROLL_DEBUG

ScrollKeeper::ScrollKeeper(QWidget *chatView) : chatView_(chatView), scrollPos_(0), scrollToEnd_(false), ted_(nullptr)
{
    Q_UNUSED(chatView_)

    ted_ = qobject_cast<QTextEdit *>(chatView);
    if (ted_) {
        scrollPos_ = ted_->verticalScrollBar()->value();
        if (scrollPos_ == ted_->verticalScrollBar()->maximum()) {
            scrollToEnd_ = true;
        }
#ifdef SCROLL_DEBUG
        qDebug() << "QTED Scroll pos:" << scrollPos_ << "to end:" << scrollToEnd_
                 << "max:" << ted_->verticalScrollBar()->maximum();
#endif
    }
}

ScrollKeeper::~ScrollKeeper()
{
    if (ted_) {
#ifdef SCROLL_DEBUG
        qDebug() << "QTED restoring scroll pos:" << scrollPos_ << "to end:" << scrollToEnd_
                 << "max:" << ted_->verticalScrollBar()->maximum();
#endif
        ted_->verticalScrollBar()->setValue(scrollToEnd_ ? ted_->verticalScrollBar()->maximum() : scrollPos_);
    }
}