diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-06-01 08:48:42 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-06-01 09:24:28 (GMT) |
commit | 2812bddd81f8a1b804c7460f4e14cd0aa393d129 (patch) | |
tree | d46294f35150c4f0f43deaf2d31fceaf945ae715 /UI/Qt/QtChatView.cpp | |
download | swift-2812bddd81f8a1b804c7460f4e14cd0aa393d129.zip swift-2812bddd81f8a1b804c7460f4e14cd0aa393d129.tar.bz2 |
Import.
Diffstat (limited to 'UI/Qt/QtChatView.cpp')
-rw-r--r-- | UI/Qt/QtChatView.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/UI/Qt/QtChatView.cpp b/UI/Qt/QtChatView.cpp new file mode 100644 index 0000000..b6687d0 --- /dev/null +++ b/UI/Qt/QtChatView.cpp @@ -0,0 +1,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)); +} + +} |