summaryrefslogtreecommitdiffstats
blob: b6687d000ac27d35572f97cd8fa85c7694a9855e (plain)
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
#include "QtChatView.h"

#include <QtDebug>
#include <QFile>
#include <QVBoxLayout>
#include <QWebView>
#include <QWebFrame>
#include <QKeyEvent>

namespace Swift {

QtChatView::QtChatView(QWidget* parent) : QWidget(parent) {
	setFocusPolicy(Qt::NoFocus);

	QVBoxLayout* mainLayout = new QVBoxLayout(this);
	mainLayout->setSpacing(0);
	mainLayout->setContentsMargins(0,0,0,0);

	webView_ = new QWebView(this);
	webView_->setFocusPolicy(Qt::NoFocus);
	mainLayout->addWidget(webView_);

	webPage_ = new QWebPage(this);
	webPage_->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
	webView_->setPage(webPage_);
	connect(webPage_, SIGNAL(selectionChanged()), SLOT(copySelectionToClipboard()));

	QFile file(":/themes/Default/Template.html");
	bool result = file.open(QIODevice::ReadOnly);
	Q_ASSERT(result);
	Q_UNUSED(result);
	QString pageHTML = file.readAll();
	pageHTML.replace("==bodyBackground==", "background-color:#e3e3e3");
	pageHTML.replace(pageHTML.indexOf("%@"), 2, "qrc:/themes/Default/");
	pageHTML.replace(pageHTML.indexOf("%@"), 2, "Variants/Blue on Green.css");
	pageHTML.replace(pageHTML.indexOf("%@"), 2, "");
	pageHTML.replace(pageHTML.indexOf("%@"), 2, "");
	file.close();
	webPage_->mainFrame()->setHtml(pageHTML);
}

void QtChatView::addMessage(const ChatSnippet& snippet) {
	//bool wasScrolledToBottom = isScrolledToBottom();
	
	QString content = snippet.getContent();
	content.replace("\"", "\\\"");
	content.replace("\n", "\\n");
	if (previousContinuationElementID_.isEmpty() || !snippet.getAppendToPrevious()) {
		webPage_->mainFrame()->evaluateJavaScript("appendMessage(\"" + content + "\");");
	}
	else {
		webPage_->mainFrame()->evaluateJavaScript("appendNextMessage(\"" + content + "\");");
	}

	//qDebug() << webPage_->mainFrame()->toHtml();
	previousContinuationElementID_ = snippet.getContinuationElementID();

	/*if (wasScrolledToBottom) {
		scrollToBottom();
	}*/
}

void QtChatView::copySelectionToClipboard() {
	if (!webPage_->selectedText().isEmpty()) {
		webPage_->triggerAction(QWebPage::Copy);
	}
}

bool QtChatView::isScrolledToBottom() const {
	return webPage_->mainFrame()->scrollBarValue(Qt::Vertical) == webPage_->mainFrame()->scrollBarMaximum(Qt::Vertical);
}

void QtChatView::scrollToBottom() {
	webPage_->mainFrame()->setScrollBarValue(Qt::Vertical, webPage_->mainFrame()->scrollBarMaximum(Qt::Vertical));
}

}