File: ScrollKeeper.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (85 lines) | stat: -rw-r--r-- 2,135 bytes parent folder | download
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * ScrollKeeper.cpp
 *
 *  Created on: 30 Oct 2016
 *      Author: rkfg
 */

#include "ScrollKeeper.h"
#ifdef HAVE_WEBKIT
#include <QWebFrame>
#include <QWebView>
#endif
#ifdef HAVE_WEBENGINE
#include <QWebEnginePage>
#endif
#include <QScrollBar>

//#define SCROLL_DEBUG

ScrollKeeper::ScrollKeeper(QWidget* chatView) :
		chatView_(chatView),
		scrollPos_(0),
		scrollToEnd_(false),
		ted_(0)
#ifdef HAVE_WEBKIT
		,mainFrame_(0)
#endif
{
	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
	}
	else {
#ifdef HAVE_WEBKIT
		QWebView* wv = qobject_cast<QWebView*>(chatView);
		if (!wv) {
			return;
		}
		mainFrame_ = wv->page()->mainFrame();
		scrollPos_ = mainFrame_->scrollBarValue(Qt::Vertical);
		if (scrollPos_ == mainFrame_->scrollBarMaximum(Qt::Vertical)) {
			scrollToEnd_ = true;
		}
#ifdef SCROLL_DEBUG
		qDebug() << "QWV Scroll pos:" << scrollPos_ << "to end:" << scrollToEnd_ << "max:"
				<< mainFrame_->scrollBarMaximum(Qt::Vertical) << "min:" << mainFrame_->scrollBarMinimum(Qt::Vertical);
#endif
#endif

#ifdef HAVE_WEBENGINE
  #ifdef __GNUC__
  #warning "ImagePreviewPlugin TODO: add support for webengine"
  #endif
#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_);
	}
#ifdef HAVE_WEBKIT
	if (mainFrame_) {
#ifdef SCROLL_DEBUG
		qDebug() << "QWV restoring scroll pos:" << scrollPos_ << "to end:" << scrollToEnd_ << "max:"
				<< mainFrame_->scrollBarMaximum(Qt::Vertical);
#endif
		mainFrame_->setScrollBarValue(Qt::Vertical,	scrollToEnd_ ? mainFrame_->scrollBarMaximum(Qt::Vertical) : scrollPos_);
	}
#endif
}